-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbgUtils.m
298 lines (227 loc) · 6.83 KB
/
DbgUtils.m
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//----------------------------------------------------------------------------------------
// DbgUtils.m - Cocoa debugging and error handling
//
// Copyright © Chris, 2003 - 2010. All rights reserved.
//----------------------------------------------------------------------------------------
#import "NSString+MyAdditions.h"
#import "SvnInterface.h"
#if qDebug
//----------------------------------------------------------------------------------------
static UInt32
OSX ()
{
static SInt32 response;
if (response == 0 && Gestalt(gestaltSystemVersion, &response) != noErr)
response = 1;
// fprintf(stderr, "response=0x%lX\n", response);
return response;
}
//----------------------------------------------------------------------------------------
static const char*
Prefix ()
{
static const char* prefix = NULL;
if (prefix == NULL)
prefix = (OSX() >= 0x1050) ? "" : kAppName ": ";
return prefix;
}
//----------------------------------------------------------------------------------------
static FILE*
OS ()
{
static FILE* file = NULL;
if (file == NULL)
{
if (OSX() >= 0x1050)
{
// file = fopen("/tmp/" kAppName "-dbg.log", "a");
if (file != NULL)
fprintf(file, "\n------------------------------------------------------------\n");
}
if (file == NULL)
file = stderr;
setlinebuf(file);
}
return file;
}
//----------------------------------------------------------------------------------------
ConstCStr
LeafName (ConstCStr file)
{
if (file != NULL)
{
ConstCStr s;
char ch;
for (s = file; (ch = *s++) != 0; )
if (ch == '/')
file = s;
}
return file;
}
//----------------------------------------------------------------------------------------
ConstCStr
Demangle (ConstCStr func)
{
if (func != NULL)
{
static char buf[256];
ConstCStr s;
char ch;
for (s = func; (ch = *s++) != 0; )
if (ch == ' ')
{
int i = 0;
while (i < sizeof(buf) - 1)
{
ch = *s++;
if (ch == ']' || ch == 0)
break;
buf[i++] = ch;
}
buf[i] = 0;
func = buf;
break;
}
}
return func;
}
//----------------------------------------------------------------------------------------
OSStatus
DbgWarnIf (ConstCStr file, int line, ConstCStr func, OSStatus err)
{
if (err != noErr)
{
DbgLogF(file, line, func, "WARNING err=%s=%d", GetMacOSStatusErrorString(err), err);
}
return err;
}
//----------------------------------------------------------------------------------------
OSStatus
DbgWarnIfNot (ConstCStr file, int line, ConstCStr func, OSStatus err, OSStatus exclude)
{
if (err != noErr && err != exclude)
{
DbgLogF(file, line, func, "WARNING err=%s=%d", GetMacOSStatusErrorString(err), err);
}
return err;
}
//----------------------------------------------------------------------------------------
void
DbgAssert (ConstCStr file, int line, ConstCStr func, ConstCStr expr)
{
DbgLogF(file, line, func, "ASSERT %s", expr);
int n = *(int*) 0; (void) n;
exit(1);
}
//----------------------------------------------------------------------------------------
// return "[<file>:<line>] <func>: "
static ConstCStr
DbgFLF (ConstCStr file, int line, ConstCStr func)
{
static char buf[250];
snprintf(buf, sizeof(buf), "%s[%s:%d] %s: ",
Prefix(), LeafName(file), line, Demangle(func));
return buf;
}
//----------------------------------------------------------------------------------------
// Log "[<file>:<line>] <func>: <msg>..."
void
DbgLogF (ConstCStr file, int line, ConstCStr func, ConstCStr fmt, ...)
{
va_list ap; va_start(ap, fmt);
NSString* s = [[NSString alloc] initWithFormat: UTF8(fmt) arguments: ap];
va_end(ap);
char buf[2048];
if (!ToUTF8(s, buf, sizeof(buf)))
buf[0] = 0;
[s release];
fprintf(OS(), "%s[%s:%d] %s: %s\n", Prefix(), LeafName(file), line, Demangle(func), buf);
}
//----------------------------------------------------------------------------------------
// Log "[<file>:<line>] <func>: <msg>"
void
DbgLog (ConstCStr file, int line, ConstCStr func, ConstCStr msg)
{
// fprintf(OS(), "%s[%s:%d] %s: %s\n", Prefix(), LeafName(file), line, Demangle(func), msg);
DbgLogF(file, line, func, "%s", msg);
}
//----------------------------------------------------------------------------------------
// Log "<msg>..."
void
DbgLogF2 (ConstCStr fmt, ...)
{
va_list ap; va_start(ap, fmt);
NSString* s = [[NSString alloc] initWithFormat: UTF8(fmt) arguments: ap];
va_end(ap);
char buf[2048];
if (!ToUTF8(s, buf, sizeof(buf)))
buf[0] = 0;
[s release];
fprintf(OS(), "%s\n", buf);
}
//----------------------------------------------------------------------------------------
static char*
SvnErrorToString (SvnError err, char buf[], size_t bufSize, ConstCStr prefix)
{
snprintf(buf, bufSize, "%s{[%s:%ld] err=%d %s}",
prefix, err->file, err->line, err->apr_err, err->message);
return buf;
}
//----------------------------------------------------------------------------------------
void
DbgSvnPrint (SvnError err)
{
if (err)
{
char buf[512];
fprintf(OS(), "%s\n", SvnErrorToString(err, buf, sizeof(buf), " SvnError="));
}
}
//----------------------------------------------------------------------------------------
void
DbgSvnThrowIf (SvnError err, ConstCStr file, int line, ConstCStr func)
{
if (err)
{
char buf[512];
DbgLog(file, line, func, SvnErrorToString(err, buf, sizeof(buf), "THROW SvnError="));
SvnDoThrow(err);
}
}
//----------------------------------------------------------------------------------------
void
DbgSvnReportCatch (SvnException* ex, ConstCStr file, int line, ConstCStr func)
{
char buf[512];
DbgLog(file, line, func, SvnErrorToString([ex error], buf, sizeof(buf), "CAUGHT SvnError="));
}
//----------------------------------------------------------------------------------------
void
DbgReportCatch (ConstCStr file, int line, ConstCStr func, NSObject* err)
{
char buf[2048];
ConstCStr msg = "CAUGHT ???";
if ([[NSString stringWithFormat: @"CAUGHT %@", err]
getCString: buf maxLength: sizeof(buf) encoding: NSUTF8StringEncoding])
msg = buf;
DbgLog(file, line, func, msg);
}
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@interface NSObject (Debug)
- (void) doesNotRecognizeSelector: (SEL) aSelector;
@end // NSObject (Debug)
//----------------------------------------------------------------------------------------
@implementation NSObject (Debug)
- (void) doesNotRecognizeSelector: (SEL) aSelector
{
dprintf_("UNREGOCNISED SELECTOR: [%@:0x%X %s]",
[self className], self, sel_getName(aSelector));
int n = *(int*) 0; (void) n;
}
@end // NSObject (Debug)
//----------------------------------------------------------------------------------------
#endif // qDebug
//----------------------------------------------------------------------------------------
// End of DbgUtils.m