master
c 203 lines 4.47 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "nd_log-internals.h"
4
5 int64_t log_field_to_int64(struct log_field *lf) {
6
7 // --- FIELD_PARSER_VERSIONS ---
8 //
9 // IMPORTANT:
10 // THERE ARE MULTIPLE VERSIONS OF THIS CODE
11 //
12 // 1. journal (direct socket API),
13 // 2. journal (libsystemd API),
14 // 3. logfmt,
15 // 4. json,
16 // 5. convert to uint64
17 // 6. convert to int64
18 //
19 // UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
20
21 CLEAN_BUFFER *tmp = NULL;
22 const char *s = NULL;
23
24 switch(lf->entry.type) {
25 default:
26 case NDFT_UUID:
27 case NDFT_UNSET:
28 return 0;
29
30 case NDFT_TXT:
31 s = lf->entry.txt;
32 break;
33
34 case NDFT_STR:
35 s = string2str(lf->entry.str);
36 break;
37
38 case NDFT_BFR:
39 s = buffer_tostring(lf->entry.bfr);
40 break;
41
42 case NDFT_CALLBACK:
43 tmp = buffer_create(0, NULL);
44
45 if(lf->entry.cb.formatter(tmp, lf->entry.cb.formatter_data))
46 s = buffer_tostring(tmp);
47 else
48 s = NULL;
49 break;
50
51 case NDFT_U64:
52 return (int64_t)lf->entry.u64;
53
54 case NDFT_I64:
55 return (int64_t)lf->entry.i64;
56
57 case NDFT_DBL:
58 return (int64_t)lf->entry.dbl;
59 }
60
61 if(s && *s)
62 return str2ll(s, NULL);
63
64 return 0;
65 }
66
67 uint64_t log_field_to_uint64(struct log_field *lf) {
68
69 // --- FIELD_PARSER_VERSIONS ---
70 //
71 // IMPORTANT:
72 // THERE ARE MULTIPLE VERSIONS OF THIS CODE
73 //
74 // 1. journal (direct socket API),
75 // 2. journal (libsystemd API),
76 // 3. logfmt,
77 // 4. json,
78 // 5. convert to uint64
79 // 6. convert to int64
80 //
81 // UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
82
83 CLEAN_BUFFER *tmp = NULL;
84 const char *s = NULL;
85
86 switch(lf->entry.type) {
87 default:
88 case NDFT_UUID:
89 case NDFT_UNSET:
90 return 0;
91
92 case NDFT_TXT:
93 s = lf->entry.txt;
94 break;
95
96 case NDFT_STR:
97 s = string2str(lf->entry.str);
98 break;
99
100 case NDFT_BFR:
101 s = buffer_tostring(lf->entry.bfr);
102 break;
103
104 case NDFT_CALLBACK:
105 tmp = buffer_create(0, NULL);
106
107 if(lf->entry.cb.formatter(tmp, lf->entry.cb.formatter_data))
108 s = buffer_tostring(tmp);
109 else
110 s = NULL;
111 break;
112
113 case NDFT_U64:
114 return lf->entry.u64;
115
116 case NDFT_I64:
117 return lf->entry.i64;
118
119 case NDFT_DBL:
120 return (uint64_t) lf->entry.dbl;
121 }
122
123 if(s && *s)
124 return str2uint64_t(s, NULL);
125
126 return 0;
127 }
128
129 const char *log_field_strdupz(struct log_field *lf) {
130
131 // --- FIELD_PARSER_VERSIONS ---
132 //
133 // IMPORTANT:
134 // THERE ARE MULTIPLE VERSIONS OF THIS CODE
135 //
136 // 1. journal (direct socket API),
137 // 2. journal (libsystemd API),
138 // 3. logfmt,
139 // 4. json,
140 // 5. convert to uint64
141 // 6. convert to int64
142 //
143 // UPDATE ALL OF THEM FOR NEW FEATURES OR FIXES
144
145 CLEAN_BUFFER *tmp = NULL;
146 const char *s = NULL;
147 char buf[DOUBLE_MAX_LENGTH];
148
149 if(lf->logfmt_annotator)
150 s = lf->logfmt_annotator(lf);
151 else {
152 switch (lf->entry.type) {
153 default:
154 case NDFT_UNSET:
155 return NULL;
156
157 case NDFT_UUID:
158 uuid_unparse_lower_compact(*lf->entry.uuid, buf);
159 s = buf;
160 break;
161
162 case NDFT_TXT:
163 s = lf->entry.txt;
164 break;
165
166 case NDFT_STR:
167 s = string2str(lf->entry.str);
168 break;
169
170 case NDFT_BFR:
171 s = buffer_tostring(lf->entry.bfr);
172 break;
173
174 case NDFT_CALLBACK:
175 tmp = buffer_create(0, NULL);
176
177 if (lf->entry.cb.formatter(tmp, lf->entry.cb.formatter_data))
178 s = buffer_tostring(tmp);
179 else
180 s = NULL;
181 break;
182
183 case NDFT_U64:
184 print_uint64(buf, lf->entry.u64);
185 s = buf;
186 break;
187
188 case NDFT_I64:
189 print_int64(buf, lf->entry.i64);
190 s = buf;
191 break;
192
193 case NDFT_DBL:
194 print_netdata_double(buf, lf->entry.dbl);
195 break;
196 }
197 }
198
199 if(s && *s)
200 return strdupz(s);
201
202 return NULL;
203 }