-
Notifications
You must be signed in to change notification settings - Fork 27
/
bunyan.c
564 lines (494 loc) · 12 KB
/
bunyan.c
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2019, Joyent Inc
* Author: Alex Wilson <alex.wilson@joyent.com>
*/
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <limits.h>
#include <fcntl.h>
#include <inttypes.h>
#include "debug.h"
#include "bunyan.h"
#include "errf.h"
#include "utils.h"
#include "openssh/sshkey.h"
static const char *bunyan_name = NULL;
/*
* This bunyan code was taken from the humboldt repo, where it used to run in
* a multithreaded context and used thread-locals here for bunyan_buf etc.
*
* Unfortunately, pivy would like to be portable to platforms that don't support
* thread-local annotations on variables (looking at you OpenBSD), so we've
* removed those annotations here (and the mutexes) but left the remainder of
* the code as-is.
*
* As a result this version of bunyan.c is completely unsafe to use in the
* face of multi-threading. This is fine within the pivy repo where all we have
* is a couple of single-threaded commandline tools, but if you re-use this
* code elsewhere, beware!
*/
/*
* This is called "bunyan.c" because it used to produce bunyan-compatible JSON
* log lines as output. Now it just produces text loosely modelled after the
* bunyan cmdline tool's output. C'est la vie in the land of wanting to be
* portable to lots of other operating systems.
*/
static void
bunyan_default_printer(enum bunyan_log_level lvl, const char *msg)
{
fprintf(stderr, "%s", msg);
}
static bunyan_printer_t bunyan_printer = bunyan_default_printer;
static char *bunyan_buf = NULL;
static size_t bunyan_buf_sz = 0;
static enum bunyan_log_level bunyan_min_level = BNY_WARN;
static boolean_t bunyan_omit_timestamp = B_FALSE;
struct bunyan_var {
struct bunyan_var *bv_next;
const char *bv_name;
enum bunyan_arg_type bv_type;
union {
const char *bvv_string;
int bvv_int;
uint bvv_uint;
uint64_t bvv_uint64;
size_t bvv_size_t;
struct {
const uint8_t *bvvbh_data;
size_t bvvbh_len;
} bvv_bin_hex;
errf_t *bvv_erf;
} bv_value;
};
struct bunyan_frame {
const char *bf_func;
struct bunyan_frame *bf_next;
struct bunyan_var *bf_vars;
struct bunyan_var *bf_lastvar;
};
struct bunyan_stack {
struct bunyan_stack *bs_next;
struct bunyan_frame *bs_top;
};
static struct bunyan_stack *thstack;
static struct bunyan_stack *bunyan_stacks;
void
bunyan_set_level(enum bunyan_log_level level)
{
bunyan_min_level = level;
}
void
bunyan_set_printer(bunyan_printer_t printer, boolean_t omit_timestamp)
{
bunyan_printer = printer;
bunyan_omit_timestamp = omit_timestamp;
}
enum bunyan_log_level
bunyan_get_level(void)
{
return (bunyan_min_level);
}
static void
printf_buf(const char *fmt, ...)
{
size_t orig, avail;
int wrote;
char *nbuf;
va_list ap, ap2;
if (bunyan_buf_sz == 0) {
bunyan_buf_sz = 1024;
bunyan_buf = calloc(bunyan_buf_sz, 1);
VERIFY(bunyan_buf != NULL);
}
va_start(ap, fmt);
/* Make a backup copy of the args so we can try again if we resize. */
va_copy(ap2, ap);
orig = strlen(bunyan_buf);
avail = bunyan_buf_sz - orig;
again:
wrote = vsnprintf(bunyan_buf + orig, avail, fmt, ap);
VERIFY(wrote >= 0);
if (wrote >= avail) {
while (bunyan_buf_sz < orig + wrote)
bunyan_buf_sz *= 2;
nbuf = calloc(bunyan_buf_sz, 1);
VERIFY(nbuf != NULL);
bcopy(bunyan_buf, nbuf, orig);
nbuf[orig] = 0;
free(bunyan_buf);
bunyan_buf = nbuf;
avail = bunyan_buf_sz - orig;
va_end(ap);
va_copy(ap, ap2);
goto again;
}
va_end(ap);
va_end(ap2);
}
static void
reset_buf(void)
{
if (bunyan_buf_sz > 0)
bunyan_buf[0] = 0;
}
#if defined(__linux__)
static boolean_t
bunyan_detect_journald(void)
{
char *envp;
/*
* If journald is providing logging for this process, it will set
* JOURNAL_STREAM to <dev>:<inode>, corresponding to the device and
* inode of the socket attached to stderr and stdout.
*/
if ((envp = getenv("JOURNAL_STREAM")) != NULL) {
char *p;
unsigned long device, inode;
struct stat info;
errno = 0;
device = strtoul(envp, &p, 10);
if (errno != 0 || *p != ':')
return (B_FALSE);
errno = 0;
inode = strtoul(++p, &p, 10);
if (errno != 0 || *p != '\0')
return (B_FALSE);
if (fstat(STDERR_FILENO, &info) == 0) {
if (info.st_dev == device &&
info.st_ino == inode) {
return (B_TRUE);
}
}
}
return (B_FALSE);
}
#endif /* defined (__linux__) */
void
bunyan_init(void)
{
#if defined(__linux__)
/*
* When logging to journald, generating our own timestamps is
* unecessary as the journal has its own native ones.
*/
if (bunyan_detect_journald()) {
bunyan_omit_timestamp = B_TRUE;
}
#endif
}
void
bunyan_set_name(const char *name)
{
bunyan_name = name;
}
#define MAX_TS_LEN 128
void
bunyan_timestamp(char *buffer, size_t len)
{
struct timespec ts;
struct tm *info;
int w;
VERIFY0(clock_gettime(CLOCK_REALTIME, &ts));
info = gmtime(&ts.tv_sec);
VERIFY(info != NULL);
w = snprintf(buffer, len, "%04d-%02d-%02dT%02d:%02d:%02d.%03ldZ",
info->tm_year + 1900, info->tm_mon + 1, info->tm_mday,
info->tm_hour, info->tm_min, info->tm_sec, ts.tv_nsec / 1000000);
VERIFY(w < MAX_TS_LEN);
}
static void
bunyan_add_vars_p(struct bunyan_frame *frame, va_list ap)
{
struct bunyan_var *var = frame->bf_lastvar;
const char *propname;
while (1) {
propname = va_arg(ap, const char *);
if (propname == NULL)
break;
if (var == NULL) {
frame->bf_vars = (var =
calloc(1, sizeof (struct bunyan_var)));
VERIFY(var != NULL);
} else {
var->bv_next = calloc(1, sizeof (struct bunyan_var));
var = var->bv_next;
VERIFY(var != NULL);
}
var->bv_name = propname;
var->bv_type = va_arg(ap, enum bunyan_arg_type);
switch (var->bv_type) {
case BNY_STRING:
var->bv_value.bvv_string = va_arg(ap, const char *);
break;
case BNY_INT:
var->bv_value.bvv_int = va_arg(ap, int);
break;
case BNY_UINT:
var->bv_value.bvv_uint = va_arg(ap, uint);
break;
case BNY_UINT64:
var->bv_value.bvv_uint64 = va_arg(ap, uint64_t);
break;
case BNY_SIZE_T:
var->bv_value.bvv_size_t = va_arg(ap, size_t);
break;
case BNY_BIN_HEX:
var->bv_value.bvv_bin_hex.bvvbh_data =
va_arg(ap, const uint8_t *);
var->bv_value.bvv_bin_hex.bvvbh_len =
va_arg(ap, size_t);
break;
case BNY_ERF:
var->bv_value.bvv_erf = va_arg(ap, errf_t *);
break;
default:
abort();
}
}
frame->bf_lastvar = var;
}
void
bunyan_add_vars(struct bunyan_frame *frame, ...)
{
va_list ap;
va_start(ap, frame);
bunyan_add_vars_p(frame, ap);
va_end(ap);
}
struct bunyan_frame *
_bunyan_push(const char *func, ...)
{
va_list ap;
struct bunyan_frame *frame;
frame = calloc(1, sizeof (struct bunyan_frame));
VERIFY(frame != NULL);
frame->bf_func = func;
va_start(ap, func);
bunyan_add_vars_p(frame, ap);
va_end(ap);
if (thstack == NULL) {
thstack = calloc(1, sizeof (struct bunyan_stack));
VERIFY(thstack != NULL);
thstack->bs_next = bunyan_stacks;
bunyan_stacks = thstack;
}
frame->bf_next = thstack->bs_top;
thstack->bs_top = frame;
return (frame);
}
void
bunyan_pop(struct bunyan_frame *frame)
{
struct bunyan_var *var, *nvar;
VERIFY(frame != NULL);
VERIFY(thstack != NULL);
VERIFY(thstack->bs_top == frame);
thstack->bs_top = frame->bf_next;
for (var = frame->bf_vars; var != NULL; var = nvar) {
nvar = var->bv_next;
free(var);
}
free(frame);
}
static void
print_frame(struct bunyan_frame *frame, uint *pn, struct bunyan_var **evars)
{
struct bunyan_var *var;
uint n = *pn;
struct bunyan_var *evar;
char *wstrval;
for (var = frame->bf_vars; var != NULL; var = var->bv_next, ++n) {
if (n == 0) {
printf_buf(": ");
} else {
printf_buf(", ");
}
switch (var->bv_type) {
case BNY_STRING:
printf_buf("%s = \"%s\"", var->bv_name,
var->bv_value.bvv_string);
break;
case BNY_INT:
printf_buf("%s = %d", var->bv_name,
var->bv_value.bvv_int);
break;
case BNY_UINT:
printf_buf("%s = 0x%x", var->bv_name,
var->bv_value.bvv_uint);
break;
case BNY_UINT64:
printf_buf("%s = 0x%" PRIx64, var->bv_name,
var->bv_value.bvv_uint64);
break;
case BNY_SIZE_T:
printf_buf("%s = %zu", var->bv_name,
var->bv_value.bvv_size_t);
break;
case BNY_BIN_HEX:
wstrval = buf_to_hex(
var->bv_value.bvv_bin_hex.bvvbh_data,
var->bv_value.bvv_bin_hex.bvvbh_len, 1);
printf_buf("%s = << %s >>", var->bv_name, wstrval);
free(wstrval);
break;
case BNY_ERF:
evar = calloc(1, sizeof (struct bunyan_var));
VERIFY(evar != NULL);
bcopy(var, evar, sizeof (struct bunyan_var));
evar->bv_next = *evars;
*evars = evar;
printf_buf("%s = %s...", var->bv_name,
errf_name(var->bv_value.bvv_erf));
break;
default:
abort();
}
}
*pn = n;
}
void
bunyan_log(enum bunyan_log_level level, const char *msg, ...)
{
va_list ap;
const char *propname;
errf_t *err = NULL;
enum bunyan_arg_type typ;
uint n = 0;
struct bunyan_frame *frame;
struct bunyan_var *evars = NULL, *evar, *nevar;
reset_buf();
if (!bunyan_omit_timestamp) {
char time[MAX_TS_LEN];
bunyan_timestamp(time, sizeof (time));
printf_buf("[%s] ", time);
}
if (bunyan_printer == bunyan_default_printer) {
switch (level) {
case BNY_TRACE:
printf_buf("TRACE: ");
break;
case BNY_DEBUG:
printf_buf("DEBUG: ");
break;
case BNY_INFO:
printf_buf("INFO: ");
break;
case BNY_WARN:
printf_buf("WARN: ");
break;
case BNY_ERROR:
printf_buf("ERROR: ");
break;
case BNY_FATAL:
printf_buf("FATAL: ");
break;
}
}
printf_buf("%s", msg);
if (thstack != NULL) {
frame = thstack->bs_top;
for (; frame != NULL; frame = frame->bf_next) {
print_frame(frame, &n, &evars);
}
}
va_start(ap, msg);
while (1) {
const char *strval;
char *wstrval;
const uint8_t *binval;
int intval;
uint uintval;
uint64_t uint64val;
size_t szval;
struct sshkey *pubk;
propname = va_arg(ap, const char *);
if (propname == NULL)
break;
if (n == 0) {
printf_buf(": ");
} else {
printf_buf(", ");
}
++n;
typ = va_arg(ap, enum bunyan_arg_type);
switch (typ) {
case BNY_STRING:
strval = va_arg(ap, const char *);
printf_buf("%s = \"%s\"", propname, strval);
break;
case BNY_INT:
intval = va_arg(ap, int);
printf_buf("%s = %d", propname, intval);
break;
case BNY_UINT:
uintval = va_arg(ap, uint);
printf_buf("%s = 0x%x", propname, uintval);
break;
case BNY_UINT64:
uint64val = va_arg(ap, uint64_t);
printf_buf("%s = 0x%" PRIx64, propname, uint64val);
break;
case BNY_SIZE_T:
szval = va_arg(ap, size_t);
printf_buf("%s = %zu", propname, szval);
break;
case BNY_BIN_HEX:
binval = va_arg(ap, const uint8_t *);
szval = va_arg(ap, size_t);
wstrval = buf_to_hex(binval, szval, 1);
printf_buf("%s = << %s >>", propname, wstrval);
free(wstrval);
break;
case BNY_ERF:
err = va_arg(ap, errf_t *);
printf_buf("%s = %s...", propname, errf_name(err));
evar = calloc(1, sizeof (struct bunyan_var));
VERIFY(evar != NULL);
evar->bv_name = propname;
evar->bv_value.bvv_erf = err;
evar->bv_next = evars;
evars = evar;
break;
case BNY_SSHKEY:
pubk = va_arg(ap, struct sshkey *);
wstrval = sshkey_fingerprint(pubk, SSH_DIGEST_SHA256,
SSH_FP_BASE64);
printf_buf("%s = %s key (%u bits): %s", propname,
sshkey_type(pubk), sshkey_size(pubk), wstrval);
free(wstrval);
break;
default:
abort();
}
}
va_end(ap);
printf_buf("\n");
for (evar = evars; evar != NULL; evar = nevar) {
const char *prefix = "";
nevar = evar->bv_next;
printf_buf("\t%s = ", evar->bv_name);
err = evar->bv_value.bvv_erf;
for (; err != NULL; err = errf_cause(err)) {
printf_buf("%s%s: %s\n\t in %s() at %s:%d\n", prefix,
errf_name(err), errf_message(err),
errf_function(err), errf_file(err), errf_line(err));
prefix = "\t Caused by ";
}
free(evar);
}
if (level < bunyan_min_level) {
return;
}
(*bunyan_printer)(level, bunyan_buf);
}