-
Notifications
You must be signed in to change notification settings - Fork 30
/
logfmtjson.c
258 lines (224 loc) · 4.94 KB
/
logfmtjson.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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <daniel@roe.ch>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
/*
* JSON Lines and JSON Seq log format drivers
*/
#include "logfmtjson.h"
#include "logutl.h"
#include "sys.h"
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
static char *opteol, *optsp;
static bool indent_used[LOGFMT_INDENT_MAX+1] = {0};
static char indent[2*LOGFMT_INDENT_MAX+1] = {0};
static size_t indent_level = 0;
static int
logfmtjson_init(config_t *cfg) {
if (cfg->logoneline) {
opteol = "";
optsp = "";
} else {
opteol = "\n";
optsp = " ";
}
return 0;
}
static void
logfmtjson_indent_inc(void) {
indent_level++;
assert(indent_level <= LOGFMT_INDENT_MAX);
indent_used[indent_level] = false;
if (opteol[0] == '\0')
return;
indent[indent_level * 2 - 2] = ' ';
indent[indent_level * 2 - 1] = ' ';
indent[indent_level * 2] = '\0';
}
static void
logfmtjson_indent_dec(void) {
assert(indent_level > 0);
indent_level--;
if (opteol[0] == '\0')
return;
indent[indent_level * 2] = '\0';
}
static void
logfmtjson_record_begin_jsonlines(UNUSED FILE *f) {
return;
}
static void
logfmtjson_record_begin_jsonseq(FILE *f) {
fprintf(f, "\x1E");
}
static void
logfmtjson_record_end(FILE *f) {
fprintf(f, "\n");
}
static void
logfmtjson_dict_begin(FILE *f) {
fprintf(f, "{");
logfmtjson_indent_inc();
}
static void
logfmtjson_dict_end(FILE *f) {
logfmtjson_indent_dec();
fprintf(f, "%s%s}", opteol, indent);
}
static void
logfmtjson_dict_item(FILE *f, const char *label) {
bool first = !indent_used[indent_level];
if (first)
indent_used[indent_level] = true;
fprintf(f, "%s%s%s\"%s\":%s",
first ? "" : ",", opteol, indent, label, optsp);
}
static void
logfmtjson_list_begin(FILE *f) {
fprintf(f, "[");
logfmtjson_indent_inc();
}
static void
logfmtjson_list_end(FILE *f) {
logfmtjson_indent_dec();
fprintf(f, "%s%s]", opteol, indent);
}
static void
logfmtjson_list_item(FILE *f, UNUSED const char *label) {
bool first = !indent_used[indent_level];
if (first)
indent_used[indent_level] = true;
fprintf(f, "%s%s%s", first ? "" : ",", opteol, indent);
}
static void
logfmtjson_value_null(FILE *f) {
fprintf(f, "null");
}
static void
logfmtjson_value_bool(FILE *f, bool value) {
fprintf(f, value ? "true" : "false");
}
static void
logfmtjson_value_int(FILE *f, int64_t value) {
fprintf(f, "%"PRId64, value);
}
static void
logfmtjson_value_uint(FILE *f, uint64_t value) {
fprintf(f, "%"PRIu64, value);
}
static void
logfmtjson_value_uint_oct(FILE *f, uint64_t value) {
fprintf(f, "\"0%"PRIo64"\"", value);
}
static void
logfmtjson_value_timespec(FILE *f, struct timespec *tv) {
assert(tv->tv_sec > 0);
fprintf(f, "\"");
logutl_fwrite_timespec(f, tv);
fprintf(f, "\"");
}
static void
logfmtjson_value_ttydev(FILE *f, dev_t dev) {
fprintf(f, "\"/dev/%s\"", sys_ttydevname(dev));
}
static void
logfmtjson_value_buf_hex(FILE *f, const unsigned char *buf, size_t sz) {
fprintf(f, "\"");
logutl_fwrite_hex(f, buf, sz);
fprintf(f, "\"");
}
static void
logfmtjson_value_string(FILE *f, const char *s) {
const unsigned char *p = (const unsigned char *)s;
size_t sz;
fputc('"', f);
while (*p != '\0') {
sz = 0;
while (p[sz] >= 0x20 && p[sz] != '"' && p[sz] != '\\')
sz++;
if (sz > 0) {
fwrite(p, sz, 1, f);
p = p + sz;
}
for (;;) {
if (*p == '"' || *p == '\\') {
fputc('\\', f);
fputc(*p, f);
p++;
} else if (*p == '\b') {
fprintf(f, "\\b");
p++;
} else if (*p == '\f') {
fprintf(f, "\\f");
p++;
} else if (*p == '\n') {
fprintf(f, "\\n");
p++;
} else if (*p == '\r') {
fprintf(f, "\\r");
p++;
} else if (*p == '\t') {
fprintf(f, "\\t");
p++;
} else if (*p > 0 && *p < 0x20) {
fprintf(f, "\\u00%02X", *p);
p++;
} else {
break;
}
}
}
fputc('"', f);
}
logfmt_t logfmtjson = {
"json", true, true,
logfmtjson_init,
logfmtjson_record_begin_jsonlines,
logfmtjson_record_end,
logfmtjson_dict_begin,
logfmtjson_dict_end,
logfmtjson_dict_item,
logfmtjson_list_begin,
logfmtjson_list_end,
logfmtjson_list_item,
logfmtjson_value_null,
logfmtjson_value_bool,
logfmtjson_value_int,
logfmtjson_value_uint,
logfmtjson_value_uint_oct,
logfmtjson_value_timespec,
logfmtjson_value_ttydev,
logfmtjson_value_buf_hex,
logfmtjson_value_string
};
logfmt_t logfmtjsonseq = {
"json-seq", true, true,
logfmtjson_init,
logfmtjson_record_begin_jsonseq,
logfmtjson_record_end,
logfmtjson_dict_begin,
logfmtjson_dict_end,
logfmtjson_dict_item,
logfmtjson_list_begin,
logfmtjson_list_end,
logfmtjson_list_item,
logfmtjson_value_null,
logfmtjson_value_bool,
logfmtjson_value_int,
logfmtjson_value_uint,
logfmtjson_value_uint_oct,
logfmtjson_value_timespec,
logfmtjson_value_ttydev,
logfmtjson_value_buf_hex,
logfmtjson_value_string
};