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
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "nd_log-internals.h"
static bool needs_quotes_for_logfmt(const char *s)
{
static bool safe_for_logfmt[256] = {
[' '] = true, ['!'] = true, ['"'] = false, ['#'] = true, ['$'] = true, ['%'] = true, ['&'] = true,
['\''] = true, ['('] = true, [')'] = true, ['*'] = true, ['+'] = true, [','] = true, ['-'] = true,
['.'] = true, ['/'] = true, ['0'] = true, ['1'] = true, ['2'] = true, ['3'] = true, ['4'] = true,
['5'] = true, ['6'] = true, ['7'] = true, ['8'] = true, ['9'] = true, [':'] = true, [';'] = true,
['<'] = true, ['='] = true, ['>'] = true, ['?'] = true, ['@'] = true, ['A'] = true, ['B'] = true,
['C'] = true, ['D'] = true, ['E'] = true, ['F'] = true, ['G'] = true, ['H'] = true, ['I'] = true,
['J'] = true, ['K'] = true, ['L'] = true, ['M'] = true, ['N'] = true, ['O'] = true, ['P'] = true,
['Q'] = true, ['R'] = true, ['S'] = true, ['T'] = true, ['U'] = true, ['V'] = true, ['W'] = true,
['X'] = true, ['Y'] = true, ['Z'] = true, ['['] = true, ['\\'] = false, [']'] = true, ['^'] = true,
['_'] = true, ['`'] = true, ['a'] = true, ['b'] = true, ['c'] = true, ['d'] = true, ['e'] = true,
['f'] = true, ['g'] = true, ['h'] = true, ['i'] = true, ['j'] = true, ['k'] = true, ['l'] = true,
['m'] = true, ['n'] = true, ['o'] = true, ['p'] = true, ['q'] = true, ['r'] = true, ['s'] = true,
['t'] = true, ['u'] = true, ['v'] = true, ['w'] = true, ['x'] = true, ['y'] = true, ['z'] = true,
['{'] = true, ['|'] = true, ['}'] = true, ['~'] = true, [0x7f] = true,
};
if(!*s)
return true;
while(*s) {
if(*s == '=' || isspace((uint8_t)*s) || !safe_for_logfmt[(uint8_t)*s])
return true;
s++;
}
return false;
}
static void string_to_logfmt(BUFFER *wb, const char *s)
{
bool spaces = needs_quotes_for_logfmt(s);
if(spaces)
buffer_fast_strcat(wb, "\"", 1);
buffer_json_strcat(wb, s);
if(spaces)
buffer_fast_strcat(wb, "\"", 1);
}
void nd_logger_logfmt(BUFFER *wb, struct log_field *fields, size_t fields_max) {
// --- FIELD_PARSER_VERSIONS ---
//
// IMPORTANT:
// THERE ARE 6 VERSIONS OF THIS CODE
//
// 1. journal (direct socket API),
// 2. journal (libsystemd API),
// 3. logfmt,
// 4. json,
// 5. convert to uint64
// 6. convert to int64
//
// UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
CLEAN_BUFFER *tmp = NULL;
for (size_t i = 0; i < fields_max; i++) {
if (!fields[i].entry.set || !fields[i].logfmt)
continue;
const char *key = fields[i].logfmt;
if(fields[i].annotator) {
const char *s = fields[i].annotator(&fields[i]);
if(!s) continue;
if(buffer_strlen(wb))
buffer_fast_strcat(wb, " ", 1);
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
string_to_logfmt(wb, s);
}
else {
if(buffer_strlen(wb))
buffer_fast_strcat(wb, " ", 1);
switch(fields[i].entry.type) {
case NDFT_TXT:
if(*fields[i].entry.txt) {
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
string_to_logfmt(wb, fields[i].entry.txt);
}
break;
case NDFT_STR:
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
string_to_logfmt(wb, string2str(fields[i].entry.str));
break;
case NDFT_BFR:
if(buffer_strlen(fields[i].entry.bfr)) {
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
string_to_logfmt(wb, buffer_tostring(fields[i].entry.bfr));
}
break;
case NDFT_U64:
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
buffer_print_uint64(wb, fields[i].entry.u64);
break;
case NDFT_I64:
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
buffer_print_int64(wb, fields[i].entry.i64);
break;
case NDFT_DBL:
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
buffer_print_netdata_double(wb, fields[i].entry.dbl);
break;
case NDFT_UUID:
if(!uuid_is_null(*fields[i].entry.uuid)) {
char u[UUID_COMPACT_STR_LEN];
uuid_unparse_lower_compact(*fields[i].entry.uuid, u);
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
buffer_fast_strcat(wb, u, sizeof(u) - 1);
}
break;
case NDFT_CALLBACK: {
if(!tmp)
tmp = buffer_create(1024, NULL);
else
buffer_flush(tmp);
if(fields[i].entry.cb.formatter(tmp, fields[i].entry.cb.formatter_data)) {
buffer_strcat(wb, key);
buffer_fast_strcat(wb, "=", 1);
string_to_logfmt(wb, buffer_tostring(tmp));
}
}
break;
default:
buffer_strcat(wb, "UNHANDLED");
break;
}
}
}
}
|