-
Notifications
You must be signed in to change notification settings - Fork 30
/
logfmtxml.c
247 lines (211 loc) · 4.77 KB
/
logfmtxml.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
/*-
* 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.
*/
/*
* XML log format driver
*
* The produced XML is XML log record elements (<event>...</event>) separated
* by newlines, without global root element, XML declaration or doctype,
* i.e. not well-formed.
*/
#include "logfmtxml.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;
static int
logfmtxml_init(config_t *cfg) {
if (cfg->logoneline) {
opteol = "";
} else {
opteol = "\n";
}
return 0;
}
/* double the max indent because lists need two levels in XML */
#define LOGFMTXML_INDENT_MAX (LOGFMT_INDENT_MAX*2)
static bool indent_used[LOGFMTXML_INDENT_MAX+1] = {0};
static char indent[2*LOGFMTXML_INDENT_MAX+1] = {0};
static size_t indent_level = 0;
static void
logfmtxml_indent_inc(void) {
indent_level++;
assert(indent_level <= LOGFMTXML_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
logfmtxml_indent_dec(void) {
assert(indent_level > 0);
indent_level--;
if (opteol[0] == '\0')
return;
indent[indent_level * 2] = '\0';
}
static const char *tags[LOGFMTXML_INDENT_MAX+1] = {0};
static size_t tags_next = 0;
static void
logfmtxml_tag_open(FILE *f, const char *label) {
fprintf(f, "%s<%s>", indent, label);
tags[tags_next] = label;
tags_next++;
assert(tags_next <= LOGFMTXML_INDENT_MAX);
}
static void
logfmtxml_tag_close(FILE *f) {
assert(tags_next > 0);
tags_next--;
fprintf(f, "</%s>%s", tags[tags_next], opteol);
}
static void
logfmtxml_record_begin(FILE *f) {
fprintf(f, "<event>");
}
static void
logfmtxml_record_end(FILE *f) {
fprintf(f, "</event>\n");
}
static void
logfmtxml_dict_begin(UNUSED FILE *f) {
logfmtxml_indent_inc();
}
static void
logfmtxml_dict_end(FILE *f) {
logfmtxml_indent_dec();
fprintf(f, "%s", indent);
if (tags_next > 0)
logfmtxml_tag_close(f);
}
static void
logfmtxml_dict_item(FILE *f, const char *label) {
bool first = !indent_used[indent_level];
if (first) {
indent_used[indent_level] = true;
fprintf(f, "%s", opteol);
}
logfmtxml_tag_open(f, label);
}
static void
logfmtxml_list_begin(UNUSED FILE *f) {
logfmtxml_indent_inc();
}
static void
logfmtxml_list_end(FILE *f) {
logfmtxml_dict_end(f);
}
static void
logfmtxml_list_item(FILE *f, const char *label) {
logfmtxml_dict_item(f, label);
}
static void
logfmtxml_value_null(FILE *f) {
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_bool(FILE *f, bool value) {
fprintf(f, value ? "true" : "false");
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_int(FILE *f, int64_t value) {
fprintf(f, "%"PRId64, value);
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_uint(FILE *f, uint64_t value) {
fprintf(f, "%"PRIu64, value);
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_uint_oct(FILE *f, uint64_t value) {
fprintf(f, "0%"PRIo64, value);
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_timespec(FILE *f, struct timespec *tv) {
assert(tv->tv_sec > 0);
logutl_fwrite_timespec(f, tv);
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_ttydev(FILE *f, dev_t dev) {
fprintf(f, "/dev/%s", sys_ttydevname(dev));
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_buf_hex(FILE *f, const unsigned char *buf, size_t sz) {
logutl_fwrite_hex(f, buf, sz);
logfmtxml_tag_close(f);
}
static void
logfmtxml_value_string(FILE *f, const char *s) {
const unsigned char *p = (const unsigned char *)s;
size_t sz;
while (*p != '\0') {
sz = 0;
while (p[sz] && p[sz] != '<' && p[sz] != '>' &&
p[sz] != '&' && p[sz] != '"' && p[sz] != '\'')
sz++;
if (sz > 0) {
fwrite(p, sz, 1, f);
p = p + sz;
}
for (;;) {
if (*p == '<') {
fprintf(f, "<");
p++;
} else if (*p == '>') {
fprintf(f, ">");
p++;
} else if (*p == '&') {
fprintf(f, "&");
p++;
} else if (*p == '"') {
fprintf(f, """);
p++;
} else if (*p == '\'') {
fprintf(f, "'");
p++;
} else {
break;
}
}
}
logfmtxml_tag_close(f);
}
logfmt_t logfmtxml = {
"xml", true, true,
logfmtxml_init,
logfmtxml_record_begin,
logfmtxml_record_end,
logfmtxml_dict_begin,
logfmtxml_dict_end,
logfmtxml_dict_item,
logfmtxml_list_begin,
logfmtxml_list_end,
logfmtxml_list_item,
logfmtxml_value_null,
logfmtxml_value_bool,
logfmtxml_value_int,
logfmtxml_value_uint,
logfmtxml_value_uint_oct,
logfmtxml_value_timespec,
logfmtxml_value_ttydev,
logfmtxml_value_buf_hex,
logfmtxml_value_string
};