forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.cpp
More file actions
432 lines (368 loc) · 8.32 KB
/
logger.cpp
File metadata and controls
432 lines (368 loc) · 8.32 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* @file logger.cpp
*
* Functions to do logging.
*
* If a log statement ends in a newline, the current log is ended.
* When the log severity changes, an implicit newline is inserted.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "logger.h"
#include <cstdio>
#include <deque>
#include <stdarg.h>
#include "unc_ctype.h"
#include "log_levels.h"
struct log_fcn_info
{
log_fcn_info(const char *name, int line) : name(name), line(line)
{
}
const char *name;
int line;
};
static std::deque<log_fcn_info> g_fq;
/** Private log structure */
struct log_buf
{
log_buf() : log_file(0), sev(LSYS), in_log(0), buf_len(0), show_hdr(false)
{
}
FILE *log_file;
log_sev_t sev;
int in_log;
char buf[256];
int buf_len;
log_mask_t mask;
bool show_hdr;
};
static struct log_buf g_log;
/**
* Initializes the log subsystem - call this first.
* This function sets the log stream and enables the top 3 sevs (0-2).
*
* @param log_file NULL for stderr or the FILE stream for logs.
*/
void log_init(FILE *log_file)
{
/* set the top 3 severities */
logmask_set_all(g_log.mask, false);
log_set_sev(LSYS, true);
log_set_sev(LERR, true);
log_set_sev(LWARN, true);
g_log.log_file = (log_file != NULL) ? log_file : stderr;
}
/**
* Show or hide the severity prefix "<1>"
*
* @param true=show false=hide
*/
void log_show_sev(bool show)
{
g_log.show_hdr = show;
}
/**
* Returns whether a log severity is active.
*
* @param sev The severity
* @return true/false
*/
bool log_sev_on(log_sev_t sev)
{
return(logmask_test(g_log.mask, sev));
}
/**
* Sets a log sev on or off
*
* @param sev The severity
* @return true/false
*/
void log_set_sev(log_sev_t sev, bool value)
{
logmask_set_sev(g_log.mask, sev, value);
}
/**
* Sets the log mask
*
* @param mask The mask to copy
*/
void log_set_mask(const log_mask_t& mask)
{
g_log.mask = mask;
}
/**
* Gets the log mask
*
* @param mask Where to copy the mask
*/
void log_get_mask(log_mask_t& mask)
{
mask = g_log.mask;
}
/**
* Flushes the cached log text to the stream
*
* @param force_nl Append NL if not present
*/
static void log_flush(bool force_nl)
{
if (g_log.buf_len > 0)
{
if (force_nl && (g_log.buf[g_log.buf_len - 1] != '\n'))
{
g_log.buf[g_log.buf_len++] = '\n';
g_log.buf[g_log.buf_len] = 0;
}
if (fwrite(g_log.buf, g_log.buf_len, 1, g_log.log_file) != 1)
{
/* maybe we should log something to complain... =) */
}
g_log.buf_len = 0;
}
}
/**
* Starts the log statement by flushing if needed and printing the header
*
* @param sev The log severity
* @return The number of bytes available
*/
static size_t log_start(log_sev_t sev)
{
if (sev != g_log.sev)
{
if (g_log.buf_len > 0)
{
log_flush(true);
}
g_log.sev = sev;
g_log.in_log = false;
}
/* If not in a log, the buffer is empty. Add the header, if enabled. */
if (!g_log.in_log && g_log.show_hdr)
{
g_log.buf_len = snprintf(g_log.buf, sizeof(g_log.buf), "<%d>", sev);
}
int cap = ((int)sizeof(g_log.buf) - 2) - g_log.buf_len;
return((cap > 0) ? (size_t)cap : 0);
}
/**
* Cleans up after a log statement by detecting whether the log is done,
* (it ends in a newline) and possibly flushing the log.
*/
static void log_end(void)
{
g_log.in_log = (g_log.buf[g_log.buf_len - 1] != '\n');
if (!g_log.in_log || (g_log.buf_len > (int)(sizeof(g_log.buf) / 2)))
{
log_flush(false);
}
}
/**
* Logs a string of known length
*
* @param sev The severity
* @param str The pointer to the string
* @param len The length of the string from strlen(str)
*/
void log_str(log_sev_t sev, const char *str, int len)
{
if ((str == NULL) || (len <= 0) || !log_sev_on(sev))
{
return;
}
size_t cap = log_start(sev);
if (cap > 0)
{
if (len > (int)cap)
{
len = cap;
}
memcpy(&g_log.buf[g_log.buf_len], str, len);
g_log.buf_len += len;
g_log.buf[g_log.buf_len] = 0;
}
log_end();
}
/**
* Logs a formatted string -- similiar to printf()
*
* @param sev The severity
* @param fmt The format string
* @param ... Additional arguments
*/
void log_fmt(log_sev_t sev, const char *fmt, ...)
{
va_list args;
int len;
size_t cap;
if ((fmt == NULL) || !log_sev_on(sev))
{
return;
}
/* Some implementation of vsnprintf() return the number of characters
* that would have been stored if the buffer was large enough instead of
* the number of characters actually stored.
*/
cap = log_start(sev);
/* Add on the variable log parameters to the log string */
va_start(args, fmt);
len = vsnprintf(&g_log.buf[g_log.buf_len], cap, fmt, args);
va_end(args);
if (len > 0)
{
if (len > (int)cap)
{
len = cap;
}
g_log.buf_len += len;
g_log.buf[g_log.buf_len] = 0;
}
log_end();
}
/**
* Dumps hex characters inline, no newlines inserted
*
* @param sev The severity
* @param data The data to log
* @param len The number of bytes to log
*/
void log_hex(log_sev_t sev, const void *vdata, int len)
{
const UINT8 *dat = (const UINT8 *)vdata;
int idx;
char buf[80];
if ((vdata == NULL) || !log_sev_on(sev))
{
return;
}
idx = 0;
while (len-- > 0)
{
buf[idx++] = to_hex_char(*dat >> 4);
buf[idx++] = to_hex_char(*dat);
dat++;
if (idx >= (int)(sizeof(buf) - 3))
{
buf[idx] = 0;
log_str(sev, buf, idx);
idx = 0;
}
}
if (idx > 0)
{
buf[idx] = 0;
log_str(sev, buf, idx);
}
}
/**
* Logs a block of data in a pretty hex format
* Numbers on the left, characters on the right, just like I like it.
*
* @param sev The severity
* @param data The data to log
* @param len The number of bytes to log
*/
void log_hex_blk(log_sev_t sev, const void *data, int len)
{
static char buf[80] = "nnn | XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | cccccccccccccccc\n";
const UINT8 *dat = (const UINT8 *)data;
int idx;
int count;
int str_idx = 0;
int chr_idx = 0;
int tmp;
int total;
if ((data == NULL) || (len <= 0) || !log_sev_on(sev))
{
return;
}
/*
* Dump the specified number of bytes in hex, 16 byte per line by
* creating a string and then calling log_str()
*/
/* Loop through the data of the current iov */
count = 0;
total = 0;
for (idx = 0; idx < len; idx++)
{
if (count == 0)
{
str_idx = 6;
chr_idx = 56;
buf[0] = to_hex_char(total >> 12);
buf[1] = to_hex_char(total >> 8);
buf[2] = to_hex_char(total >> 4);
}
tmp = dat[idx];
buf[str_idx] = to_hex_char(tmp >> 4);
buf[str_idx + 1] = to_hex_char(tmp);
str_idx += 3;
buf[chr_idx++] = unc_isprint(tmp) ? tmp : '.';
total++;
count++;
if (count >= 16)
{
count = 0;
log_str(sev, buf, 73);
}
}
/*
** Print partial line if any
*/
if (count != 0)
{
/* Clear out any junk */
while (count < 16)
{
buf[str_idx] = ' '; /* MSB hex */
buf[str_idx + 1] = ' '; /* LSB hex */
str_idx += 3;
buf[chr_idx++] = ' ';
count++;
}
log_str(sev, buf, 73);
}
}
log_func::log_func(const char *name, int line)
{
g_fq.push_back(log_fcn_info(name, line));
}
log_func::~log_func()
{
g_fq.pop_back();
}
void log_func_call(int line)
{
/* REVISIT: pass the __func__ and verify it matches the top entry? */
if (!g_fq.empty())
{
g_fq.back().line = line;
}
}
void log_func_stack(log_sev_t sev, const char *prefix, const char *suffix, int skip_cnt)
{
if (prefix)
{
LOG_FMT(sev, "%s", prefix);
}
if (skip_cnt < 0)
{
skip_cnt = 0;
}
#ifdef DEBUG
const char *sep = "";
for (int idx = (int)g_fq.size() - (skip_cnt + 1); idx >= 0; idx--)
{
LOG_FMT(sev, "%s %s:%d", sep, g_fq[idx].name, g_fq[idx].line);
sep = ",";
}
#else
LOG_FMT(sev, "-DEBUG NOT SET-");
#endif
if (suffix)
{
LOG_FMT(sev, "%s", suffix);
}
}