-
Notifications
You must be signed in to change notification settings - Fork 1
/
djltrace.hxx
339 lines (281 loc) · 9.86 KB
/
djltrace.hxx
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#pragma once
// In one source file, declare the CDJLTrace named tracer like this:
// CDJLTrace tracer;
// When the app starts, enable tracing like this:
// tracer.Enable( true );
// By default the tracing file is placed in %temp%\tracer.txt
// Arguments to Trace() are just like printf. e.g.:
// tracer.Trace( "what to log with an integer argument %d and a wide string %ws\n", 10, pwcHello );
//
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <sys/types.h>
#include <mutex>
#include <memory>
#include <vector>
#include <cstring>
#include <djl_os.hxx>
#if !defined(_WIN32) && !defined(WATCOM)
#include <sys/unistd.h>
#ifdef __APPLE__
#include <unistd.h>
#endif
#endif
using namespace std;
class CDJLTrace
{
private:
FILE * fp;
#ifndef WATCOM
std::mutex mtx;
#endif
bool quiet; // no pid
bool flush; // flush after each write
static char * appendHexNibble( char * p, uint8_t val )
{
*p++ = ( val <= 9 ) ? val + '0' : val - 10 + 'a';
return p;
} //appendHexNibble
static char * appendHexByte( char * p, uint8_t val )
{
p = appendHexNibble( p, ( val >> 4 ) & 0xf );
p = appendHexNibble( p, val & 0xf );
return p;
} //appendBexByte
static char * appendHexWord( char * p, uint16_t val )
{
p = appendHexByte( p, ( val >> 8 ) & 0xff );
p = appendHexByte( p, val & 0xff );
return p;
} //appendHexWord
void ShowBinaryData( uint8_t * pData, uint32_t length, uint32_t indent, bool trace )
{
int64_t offset = 0;
int64_t beyond = length;
const int64_t bytesPerRow = 32;
uint8_t buf[ bytesPerRow ];
char acLine[ 200 ];
while ( offset < beyond )
{
char * pline = acLine;
for ( uint32_t i = 0; i < indent; i++ )
*pline++ = ' ';
pline = appendHexWord( pline, (uint16_t) offset );
*pline++ = ' ';
*pline++ = ' ';
int64_t end_of_row = offset + bytesPerRow;
int64_t cap = ( end_of_row > beyond ) ? beyond : end_of_row;
int64_t toread = ( ( offset + bytesPerRow ) > beyond ) ? ( length % bytesPerRow ) : bytesPerRow;
memcpy( buf, pData + offset, toread );
uint64_t extraSpace = 2;
for ( int64_t o = offset; o < cap; o++ )
{
pline = appendHexByte( pline, buf[ o - offset ] );
*pline++ = ' ';
if ( ( bytesPerRow > 16 ) && ( o == ( offset + 15 ) ) )
{
*pline++ = ':';
*pline++ = ' ';
extraSpace = 0;
}
}
uint64_t spaceNeeded = extraSpace + ( ( bytesPerRow - ( cap - offset ) ) * 3 );
for ( uint64_t sp = 0; sp < ( 1 + spaceNeeded ); sp++ )
*pline++ = ' ';
for ( int64_t o = offset; o < cap; o++ )
{
char ch = buf[ o - offset ];
if ( (int8_t) ch < ' ' || 127 == ch )
ch = '.';
*pline++ = ch;
}
offset += bytesPerRow;
*pline = 0;
if ( trace )
TraceQuiet( "%s\n", acLine );
else
printf( "%s\n", acLine );
}
} //ShowBinaryData
public:
CDJLTrace() : fp( NULL ), quiet( false ), flush( true ) {}
bool Enable( bool enable, const wchar_t * pcLogFile = NULL, bool destroyContents = false )
{
if ( 0 != pcLogFile )
{
size_t len = wcslen( pcLogFile );
vector<char> narrow( 1 + len );
wcstombs( narrow.data(), pcLogFile, 1 + len );
return Enable( enable, narrow.data(), destroyContents );
}
return Enable( enable, (const char *) 0, destroyContents );
} // Enable
bool Enable( bool enable, const char * pcLogFile = NULL, bool destroyContents = false )
{
Shutdown();
if ( enable )
{
const char * mode = destroyContents ? "w+t" : "a+t";
if ( NULL == pcLogFile )
{
const char * tracefile = "tracer.txt";
size_t len = strlen( tracefile );
vector<char> tempPath( 1 + len );
tempPath[0] = 0;
const char * ptmp = getenv( "TEMP" );
if ( ptmp )
{
tempPath.resize( 1 + len + strlen( ptmp ) );
strcpy( tempPath.data(), ptmp );
strcat( tempPath.data(), "/" );
}
strcat( tempPath.data(), tracefile );
fp = fopen( tempPath.data(), mode );
}
else
{
#ifdef WATCOM // workaround for WATCOM, which doesn't delete the file with "w+t" in spite of its documentation claiming otherwise
if ( !strcmp( mode, "w+t" ) )
remove( pcLogFile );
#endif
fp = fopen( pcLogFile, mode );
}
}
return ( NULL != fp );
} //Enable
void Shutdown()
{
if ( NULL != fp )
{
fflush( fp );
fclose( fp );
fp = NULL;
}
} //Shutdown
~CDJLTrace()
{
Shutdown();
} //~CDJLTrace
bool IsEnabled() { return ( 0 != fp ); }
void SetQuiet( bool q ) { quiet = q; }
void SetFlushEachTrace( bool f ) { flush = f; }
void Flush() { if ( 0 != fp ) fflush( fp ); }
void Trace( const char * format, ... )
{
if ( NULL != fp )
{
#ifndef WATCOM
lock_guard<mutex> lock( mtx );
#endif
va_list args;
va_start( args, format );
if ( !quiet )
fprintf( fp, "PID %6u -- ",
#ifdef _WIN32
(unsigned) _getpid() );
#else
getpid() );
#endif
vfprintf( fp, format, args );
va_end( args );
if ( flush )
fflush( fp );
}
} //Trace
void TraceVA( const char * format, va_list args )
{
if ( NULL != fp )
{
fprintf( fp, format, args );
if ( flush )
fflush( fp );
}
} //TraceVA
// Don't prepend the PID to the trace
void TraceQuiet( const char * format, ... )
{
if ( NULL != fp )
{
#ifndef WATCOM
lock_guard<mutex> lock( mtx );
#endif
va_list args;
va_start( args, format );
vfprintf( fp, format, args );
va_end( args );
if ( flush )
fflush( fp );
}
} //TraceQuiet
void TraceDebug( bool condition, const char * format, ... )
{
#ifdef DEBUG
if ( NULL != fp && condition )
{
#ifndef WATCOM
lock_guard<mutex> lock( mtx );
#endif
va_list args;
va_start( args, format );
if ( !quiet )
fprintf( fp, "PID %6u -- ",
#ifdef _WIN32
_getpid() );
#else
getpid() );
#endif
vfprintf( fp, format, args );
va_end( args );
if ( flush )
fflush( fp );
}
#else
#if !defined( WATCOM ) && !defined( __APPLE__ )
condition; // unused
format; // unused
#endif
#endif
} //TraceDebug
void TraceBinaryData( uint8_t * pData, uint32_t length, uint32_t indent )
{
if ( NULL != fp )
ShowBinaryData( pData, length, indent, true );
} //TraceBinaryData
void PrintBinaryData( uint8_t * pData, uint32_t length, uint32_t indent )
{
ShowBinaryData( pData, length, indent, false );
} //PrintBinaryData
static char * RenderNumberWithCommas( long long n, char * pc )
{
char actmp[ 32 ];
long long orig = n;
if ( 0 == n )
{
strcpy( pc, "0" );
return pc;
}
else if ( n < 0 )
n = -n;
pc[ 0 ] = 0;
while ( 0 != n )
{
strcpy( actmp, pc );
if ( n >= 1000 )
snprintf( pc, 5, ",%03lld", n % 1000 );
else
snprintf( pc, 4, "%lld", n );
strcat( pc, actmp );
n /= 1000;
}
if ( orig < 0 )
{
strcpy( actmp, pc );
strcpy( pc, "-" );
strcat( pc, actmp );
}
return pc;
} //RenderNumberWithCommas
}; //CDJLTrace
extern CDJLTrace tracer;