-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathDebugHelper.cpp
More file actions
185 lines (165 loc) · 4.51 KB
/
DebugHelper.cpp
File metadata and controls
185 lines (165 loc) · 4.51 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
#include "StdAfx.h"
#include "DebugHelper.h"
namespace Debug
{
static const int TARGETS_COUNT = 5;
bool targets[TARGETS_COUNT];
// ******************************************************************
// CustomCPLErrorHandler
// ******************************************************************
void CPL_STDCALL CustomCPLErrorHandler(const CPLErr error, int errorCode, const char* message)
{
if (m_globalSettings.suppressGdalErrors && !targets[DebugForceGdal]) return;
CString s = message;
switch (error)
{
case CE_Debug:
s = "GDAL DEBUG: " + s;
break;
case CE_Warning:
s = "GDAL WARNING: " + s;
break;
case CE_Failure:
s = "GDAL FAILURE: " + s;
break;
case CE_Fatal:
s = "GDAL FATAL: " + s;
break;
default:
s = "GDAL: " + s;
}
if (m_globalSettings.suppressGdalErrors)
s = "SUPPRESSED " + s;
if (m_globalSettings.callback && !targets[DebugForceGdal])
{
const CComBSTR bstr(s);
m_globalSettings.callback->Error(m_globalSettings.gdalBstr, bstr);
}
else
{
if (Debug::IsDebugMode())
Debug::WriteError(s);
}
}
bool LogTiles() { return targets[DebugTiles]; }
void Init() {
targets[DebugAny] = true;
targets[DebugOgrLoading] = false;
targets[DebugPanning] = false;
targets[DebugTiles] = false;
targets[DebugForceGdal] = false;
CPLSetErrorHandler(CustomCPLErrorHandler);
}
bool CheckDebugTarget(DebugTarget target)
{
return targets[target];
}
CString GetDebugTargetName(DebugTarget target)
{
switch (target)
{
case DebugAny: return "";
case DebugOgrLoading: return "OGR: ";
case DebugPanning: return "PANNING: ";
case DebugTiles: return "TILES: ";
}
return "";
}
void PrintOutput(DebugTarget target, CString& msg, bool error = false)
{
CString s;
if (target == DebugAny)
s = (error ? "OCX ERROR: " : "OCX: ");
else
s = GetDebugTargetName(target);
s += msg + "\n";
OutputDebugStringA(s);
}
// ******************************************************************
// Format
// ******************************************************************
CString Format(char* format, ...)
{
#ifndef RELEASE_MODE
TCHAR buffer[1024];
va_list args;
va_start(args, format);
int len = vsnprintf(buffer, _countof(buffer), format, args);
va_end(args);
CString s = buffer;
return s;
#else
return "";
#endif
}
// ******************************************************************
// WriteWithTime
// ******************************************************************
void WriteWithTime(CString msg, DebugTarget target)
{
#ifndef RELEASE_MODE
SYSTEMTIME time;
GetLocalTime(&time);
CString sTime;
sTime.Format("%02d:%02d:%02d.%-3d: ", time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
msg = sTime + msg;
PrintOutput(target, msg);
#endif
}
// ******************************************************************
// WriteWithThreadId
// ******************************************************************
void WriteWithThreadId(CString msg, const DebugTarget target)
{
#ifndef RELEASE_MODE
if (!CheckDebugTarget(target)) return;
CString s;
s.Format("Id=%d: ", GetCurrentThreadId());
msg = s + msg;
WriteWithTime(msg, target);
#endif
}
// ******************************************************************
// WriteLine
// ******************************************************************
void WriteLine(CString format, ...)
{
#ifndef RELEASE_MODE
if (!CheckDebugTarget(DebugAny)) return;
TCHAR buffer[1024];
va_list args;
va_start(args, format);
vsnprintf(buffer, _countof(buffer), format, args);
va_end(args);
format = buffer;
PrintOutput(DebugAny, format);
#endif
}
// ******************************************************************
// WriteError
// ******************************************************************
void WriteError(CString format, ...)
{
#ifndef RELEASE_MODE
if (!CheckDebugTarget(DebugAny)) return;
TCHAR buffer[1024];
va_list args;
va_start(args, format);
int len = vsnprintf(buffer, _countof(buffer), format, args);
va_end(args);
format = buffer;
PrintOutput(DebugAny, format, true);
#endif
}
// ***************************************************
// IsDebugMode
// ***************************************************
bool IsDebugMode()
{
bool debug = true;
#ifdef RELEASE_MODE
debug = false;
#endif
return debug;
}
}