-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlogfmtyaml.c
195 lines (168 loc) · 3.57 KB
/
logfmtyaml.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
/*-
* xnumon - monitor macOS for malicious activity
* https://www.roe.ch/xnumon
*
* Copyright (c) 2017-2019, Daniel Roethlisberger <[email protected]>.
* All rights reserved.
*
* Licensed under the Open Software License version 3.0.
*/
/*
* YAML log format driver
*/
#include "logfmtyaml.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 indent[2*LOGFMT_INDENT_MAX+1] = {0};
static size_t indent_level = 0;
static bool reuse_line = false;
static int
logfmtyaml_init(UNUSED config_t *cfg) {
return 0;
}
static void
logfmtyaml_indent_inc(void) {
indent_level++;
assert(indent_level <= LOGFMT_INDENT_MAX);
indent[indent_level * 2 - 2] = ' ';
indent[indent_level * 2 - 1] = ' ';
indent[indent_level * 2] = '\0';
}
static void
logfmtyaml_indent_dec(void) {
assert(indent_level > 0);
indent_level--;
indent[indent_level * 2] = '\0';
}
static void
logfmtyaml_record_begin(FILE *f) {
fprintf(f, "---");
}
static void
logfmtyaml_record_end(FILE *f) {
fputc('\n', f);
}
static void
logfmtyaml_dict_begin(UNUSED FILE *f) {
logfmtyaml_indent_inc();
}
static void
logfmtyaml_dict_end(UNUSED FILE *f) {
logfmtyaml_indent_dec();
}
static void
logfmtyaml_dict_item(FILE *f, const char *label) {
if (reuse_line) {
fprintf(f, " %s:", label);
reuse_line = false;
} else {
fprintf(f, "\n%s%s:", indent, label);
}
}
static void
logfmtyaml_list_begin(UNUSED FILE *f) {
logfmtyaml_indent_inc();
}
static void
logfmtyaml_list_end(UNUSED FILE *f) {
logfmtyaml_indent_dec();
}
static void
logfmtyaml_list_item(FILE *f, UNUSED const char *label) {
fprintf(f, "\n%s-", indent);
reuse_line = true;
}
static void
logfmtyaml_value_null(FILE *f) {
fprintf(f, " null");
reuse_line = false;
}
static void
logfmtyaml_value_bool(FILE *f, bool value) {
fprintf(f, value ? " true" : " false");
reuse_line = false;
}
static void
logfmtyaml_value_int(FILE *f, int64_t value) {
fprintf(f, " %"PRId64, value);
reuse_line = false;
}
static void
logfmtyaml_value_uint(FILE *f, uint64_t value) {
fprintf(f, " %"PRIu64, value);
reuse_line = false;
}
static void
logfmtyaml_value_uint_oct(FILE *f, uint64_t value) {
fprintf(f, " 0o%"PRIo64, value);
reuse_line = false;
}
static void
logfmtyaml_value_timespec(FILE *f, struct timespec *tv) {
assert(tv->tv_sec > 0);
fputc(' ', f);
logutl_fwrite_timespec(f, tv);
reuse_line = false;
}
static void
logfmtyaml_value_ttydev(FILE *f, dev_t dev) {
fprintf(f, " /dev/%s", sys_ttydevname(dev));
reuse_line = false;
}
static void
logfmtyaml_value_buf_hex(FILE *f, const unsigned char *buf, size_t sz) {
fputc(' ', f);
logutl_fwrite_hex(f, buf, sz);
reuse_line = false;
}
/*
* YAML Double-Quoted Style string
*/
static void
logfmtyaml_value_string(FILE *f, const char *s) {
const char *p = s;
size_t sz;
fputc(' ', f);
fputc('"', f);
while (*p != '\0') {
sz = strcspn(p, "\\\"");
if (sz > 0) {
fwrite(p, sz, 1, f);
p = p + sz;
}
while (*p == '\\' || *p == '"') {
fputc('\\', f);
fputc(*p, f);
p++;
}
}
fputc('"', f);
reuse_line = false;
}
logfmt_t logfmtyaml = {
"yaml", false, true,
logfmtyaml_init,
logfmtyaml_record_begin,
logfmtyaml_record_end,
logfmtyaml_dict_begin,
logfmtyaml_dict_end,
logfmtyaml_dict_item,
logfmtyaml_list_begin,
logfmtyaml_list_end,
logfmtyaml_list_item,
logfmtyaml_value_null,
logfmtyaml_value_bool,
logfmtyaml_value_int,
logfmtyaml_value_uint,
logfmtyaml_value_uint_oct,
logfmtyaml_value_timespec,
logfmtyaml_value_ttydev,
logfmtyaml_value_buf_hex,
logfmtyaml_value_string
};