-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetsErrHandler.cpp
More file actions
200 lines (174 loc) · 4.97 KB
/
AssetsErrHandler.cpp
File metadata and controls
200 lines (174 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
Copyright (c) 2024-2025 Toksisitee. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, refer to license.md or https://opensource.org/licenses/MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include "spdlog/spdlog.h"
#include "Editor.h"
#include "AssetsErrHandler.h"
Assets::CErrHandler g_ErrHandler;
namespace Assets
{
CErrHandler::CErrHandler()
{
m_szErrBuffer = (char*)malloc( m_kErrBufferSize );
m_szTypeBuffer = (char*)malloc( m_kTypeBufferSize );
m_FileType = FileType::None;
m_Err = Result::OK;
}
CErrHandler::~CErrHandler()
{
SAFE_FREE( m_szErrBuffer );
SAFE_FREE( m_szTypeBuffer );
}
void CErrHandler::LogFmt( const char* pszFmt, ... )
{
char* pszBuffer = (char*)malloc( m_kErrBufferSize );
assert( pszBuffer && "CErrHandler: Failed to allocate memory" );
if ( pszBuffer ) {
va_list argptr;
va_start( argptr, pszFmt );
vsprintf_s( pszBuffer, m_kErrBufferSize, pszFmt, argptr );
va_end( argptr );
Log( pszBuffer );
free( pszBuffer );
}
}
void CErrHandler::LogFmt( Log::Level eLevel, const char* pszFmt, ... )
{
char* pszBuffer = (char*)malloc( m_kErrBufferSize );
assert( pszBuffer && "CErrHandler: Failed to allocate memory" );
if ( pszBuffer ) {
va_list argptr;
va_start( argptr, pszFmt );
vsprintf_s( pszBuffer, m_kErrBufferSize, pszFmt, argptr );
va_end( argptr );
EmitLog( pszBuffer, eLevel );
free( pszBuffer );
}
}
void CErrHandler::Log( const char* pszMsg )
{
assert( m_FileType != FileType::None && "CErrHandler: File type not set" );
sprintf_s( m_szErrBuffer, m_kErrBufferSize, "[%s]: %s", GetLastFileTypeSz(), pszMsg );
EmitLogBuffer();
}
void CErrHandler::Log( Log::Level eLevel, const char* pszMsg )
{
EmitLog( pszMsg, eLevel );
}
void CErrHandler::EmitLogBuffer()
{
switch ( m_Err ) {
case Assets::Result::FAIL:
case Assets::Result::FAIL_PARSE:
case Assets::Result::FAIL_LOAD:
spdlog::critical( m_szErrBuffer );
break;
case Assets::Result::FAIL_EXPORT:
spdlog::error( m_szErrBuffer );
break;
case Assets::Result::OK:
default:
spdlog::info( m_szErrBuffer );
break;
}
}
void CErrHandler::EmitLog( const char* pszMsg, Log::Level eLevel )
{
switch ( eLevel ) {
case Log::Level::TRC:
spdlog::trace( pszMsg );
break;
case Log::Level::DBG:
spdlog::debug( pszMsg );
break;
case Log::Level::WRN:
spdlog::warn( pszMsg );
break;
case Log::Level::ERR:
spdlog::error( pszMsg );
break;
case Log::Level::CRT:
spdlog::critical( pszMsg );
break;
case Log::Level::INF:
default:
spdlog::info( pszMsg );
break;
}
}
Result CErrHandler::HandleResult( Assets::Result eResult, std::optional<std::string> sErrorMsg )
{
assert( m_FileType != FileType::None && "CErrHandler: File type not set" );
SetError( eResult );
char szErrCode[128];
switch ( m_Err ) {
case Assets::Result::OK:
sprintf_s( szErrCode, sizeof( szErrCode ), "OK." );
break;
case Assets::Result::OK_LOAD:
sprintf_s( szErrCode, sizeof( szErrCode ), "Successfully loaded file." );
break;
case Assets::Result::OK_EXPORT:
sprintf_s( szErrCode, sizeof( szErrCode ), "Successfully exported file." );
break;
case Assets::Result::OK_PARSE:
sprintf_s( szErrCode, sizeof( szErrCode ), "Successfully parsed file.." );
break;
case Assets::Result::FAIL_LOAD:
sprintf_s( szErrCode, sizeof( szErrCode ), "Failed to load file." );
break;
case Assets::Result::FAIL_EXPORT:
sprintf_s( szErrCode, sizeof( szErrCode ), "Failed to export file." );
break;
case Assets::Result::FAIL_PARSE:
sprintf_s( szErrCode, sizeof( szErrCode ), "Failed to parse file." );
break;
case Assets::Result::OK_GENERATE:
sprintf_s( szErrCode, sizeof( szErrCode ), "Generate OK." );
break;
case Assets::Result::FAIL_GENERATE:
sprintf_s( szErrCode, sizeof( szErrCode ), "Generate FAIL." );
break;
default:
sprintf_s( szErrCode, sizeof( szErrCode ), "Unknown error code." );
break;
}
if ( sErrorMsg.has_value() ) {
sprintf_s( m_szErrBuffer, m_kErrBufferSize, "[%s]: %s", GetLastFileTypeSz(), sErrorMsg.value().c_str() );
}
else {
sprintf_s( m_szErrBuffer, m_kErrBufferSize, "[%s]: %s", GetLastFileTypeSz(), szErrCode );
}
EmitLogBuffer();
return eResult;
}
const char* CErrHandler::GetLastFileTypeSz()
{
return Assets::GetFileTypeSz( m_FileType );
}
const char* CErrHandler::GetLastErrorSz()
{
return m_szErrBuffer;
};
FileType CErrHandler::GetFileType()
{
return m_FileType;
}
void CErrHandler::SetFileTypeSz( const char* pszFileType )
{
sprintf_s( m_szTypeBuffer, m_kTypeBufferSize, pszFileType );
}
void CErrHandler::SetFileType( Assets::FileType eType )
{
m_FileType = eType;
}
void CErrHandler::SetError( Assets::Result eResult )
{
m_Err = eResult;
}
}