/src/FreeRDP/winpr/libwinpr/utils/wlog/Layout.c
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * WinPR Logger |
4 | | * |
5 | | * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <winpr/config.h> |
21 | | |
22 | | #include <stdio.h> |
23 | | #include <string.h> |
24 | | #include <stdarg.h> |
25 | | |
26 | | #include <winpr/crt.h> |
27 | | #include <winpr/assert.h> |
28 | | #include <winpr/print.h> |
29 | | #include <winpr/sysinfo.h> |
30 | | #include <winpr/environment.h> |
31 | | |
32 | | #include "wlog.h" |
33 | | |
34 | | #include "Layout.h" |
35 | | |
36 | | #if defined __linux__ && !defined ANDROID |
37 | | #include <unistd.h> |
38 | | #include <sys/syscall.h> |
39 | | #endif |
40 | | |
41 | | #ifndef MIN |
42 | 816k | #define MIN(x, y) (((x) < (y)) ? (x) : (y)) |
43 | | #endif |
44 | | |
45 | | struct format_option_recurse; |
46 | | |
47 | | struct format_tid_arg |
48 | | { |
49 | | char tid[32]; |
50 | | }; |
51 | | |
52 | | struct format_option |
53 | | { |
54 | | const char* fmt; |
55 | | size_t fmtlen; |
56 | | const char* replace; |
57 | | size_t replacelen; |
58 | | const char* (*fkt)(void*); |
59 | | union |
60 | | { |
61 | | void* pv; |
62 | | const void* cpv; |
63 | | size_t s; |
64 | | } arg; |
65 | | const char* (*ext)(const struct format_option* opt, const char* str, size_t* preplacelen, |
66 | | size_t* pskiplen); |
67 | | struct format_option_recurse* recurse; |
68 | | }; |
69 | | |
70 | | struct format_option_recurse |
71 | | { |
72 | | struct format_option* options; |
73 | | size_t nroptions; |
74 | | wLog* log; |
75 | | wLogLayout* layout; |
76 | | const wLogMessage* message; |
77 | | char buffer[WLOG_MAX_PREFIX_SIZE]; |
78 | | }; |
79 | | |
80 | | /** |
81 | | * Log Layout |
82 | | */ |
83 | | WINPR_ATTR_FORMAT_ARG(3, 0) |
84 | | static void WLog_PrintMessagePrefixVA(char* prefix, size_t prefixlen, |
85 | | WINPR_FORMAT_ARG const char* format, va_list args) |
86 | 10.8M | { |
87 | 10.8M | (void)vsnprintf(prefix, prefixlen, format, args); |
88 | 10.8M | } |
89 | | |
90 | | WINPR_ATTR_FORMAT_ARG(3, 4) |
91 | | static void WLog_PrintMessagePrefix(char* prefix, size_t prefixlen, |
92 | | WINPR_FORMAT_ARG const char* format, ...) |
93 | 10.8M | { |
94 | 10.8M | va_list args; |
95 | 10.8M | va_start(args, format); |
96 | 10.8M | WLog_PrintMessagePrefixVA(prefix, prefixlen, format, args); |
97 | 10.8M | va_end(args); |
98 | 10.8M | } |
99 | | |
100 | | static const char* get_tid(void* arg) |
101 | 10.8M | { |
102 | 10.8M | struct format_tid_arg* targ = arg; |
103 | 10.8M | WINPR_ASSERT(targ); |
104 | | |
105 | 10.8M | size_t tid = 0; |
106 | 10.8M | #if defined __linux__ && !defined ANDROID |
107 | | /* On Linux we prefer to see the LWP id */ |
108 | 10.8M | tid = (size_t)syscall(SYS_gettid); |
109 | | #else |
110 | | tid = (size_t)GetCurrentThreadId(); |
111 | | #endif |
112 | 10.8M | (void)_snprintf(targ->tid, sizeof(targ->tid), "%08" PRIxz, tid); |
113 | 10.8M | return targ->tid; |
114 | 10.8M | } |
115 | | |
116 | | static BOOL log_invalid_fmt(const char* what) |
117 | 0 | { |
118 | 0 | (void)fprintf(stderr, "Invalid format string '%s'\n", what); |
119 | 0 | return FALSE; |
120 | 0 | } |
121 | | |
122 | | static BOOL check_and_log_format_size(char* format, size_t size, size_t index, size_t add) |
123 | 340M | { |
124 | | /* format string must be '\0' terminated, so abort at size - 1 */ |
125 | 340M | if (index + add + 1 >= size) |
126 | 0 | { |
127 | 0 | (void)fprintf(stderr, |
128 | 0 | "Format string too long ['%s', max %" PRIuz ", used %" PRIuz |
129 | 0 | ", adding %" PRIuz "]\n", |
130 | 0 | format, size, index, add); |
131 | 0 | return FALSE; |
132 | 0 | } |
133 | 340M | return TRUE; |
134 | 340M | } |
135 | | |
136 | | static int opt_compare_fn(const void* a, const void* b) |
137 | 1.34G | { |
138 | 1.34G | const char* what = a; |
139 | 1.34G | const struct format_option* opt = b; |
140 | 1.34G | if (!opt) |
141 | 0 | return -1; |
142 | 1.34G | return strncmp(what, opt->fmt, opt->fmtlen); |
143 | 1.34G | } |
144 | | |
145 | | static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse, |
146 | | char* format, size_t formatlen); |
147 | | |
148 | | static const char* skip_if_null(const struct format_option* opt, const char* fmt, |
149 | | size_t* preplacelen, size_t* pskiplen) |
150 | 10.8M | { |
151 | 10.8M | WINPR_ASSERT(opt); |
152 | 10.8M | WINPR_ASSERT(fmt); |
153 | 10.8M | WINPR_ASSERT(preplacelen); |
154 | 10.8M | WINPR_ASSERT(pskiplen); |
155 | | |
156 | 10.8M | *preplacelen = 0; |
157 | 10.8M | *pskiplen = 0; |
158 | | |
159 | 10.8M | const char* str = &fmt[opt->fmtlen]; /* Skip first %{ from string */ |
160 | 10.8M | const char* end = strstr(str, opt->replace); |
161 | 10.8M | if (!end) |
162 | 0 | return NULL; |
163 | 21.6M | *pskiplen = WINPR_ASSERTING_INT_CAST(size_t, end - fmt) + opt->replacelen; |
164 | | |
165 | 21.6M | if (!opt->arg.cpv) |
166 | 10.0M | return NULL; |
167 | | |
168 | 1.63M | const size_t replacelen = WINPR_ASSERTING_INT_CAST(size_t, end - str); |
169 | | |
170 | 1.63M | char buffer[WLOG_MAX_PREFIX_SIZE] = { 0 }; |
171 | 1.63M | memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1)); |
172 | | |
173 | 1.63M | if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer, |
174 | 816k | ARRAYSIZE(opt->recurse->buffer))) |
175 | 0 | return NULL; |
176 | | |
177 | 816k | *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer)); |
178 | 816k | return opt->recurse->buffer; |
179 | 1.63M | } |
180 | | |
181 | | static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse, |
182 | | char* format, size_t formatlen) |
183 | 11.6M | { |
184 | 11.6M | WINPR_ASSERT(FormatString); |
185 | 11.6M | WINPR_ASSERT(recurse); |
186 | | |
187 | 11.6M | size_t index = 0; |
188 | | |
189 | 350M | while (*FormatString) |
190 | 338M | { |
191 | 338M | const struct format_option* opt = |
192 | 338M | bsearch(FormatString, recurse->options, recurse->nroptions, |
193 | 338M | sizeof(struct format_option), opt_compare_fn); |
194 | 338M | if (opt) |
195 | 109M | { |
196 | 109M | size_t replacelen = opt->replacelen; |
197 | 109M | size_t fmtlen = opt->fmtlen; |
198 | 109M | const char* replace = opt->replace; |
199 | 109M | const void* arg = opt->arg.cpv; |
200 | | |
201 | 109M | if (opt->ext) |
202 | 10.8M | replace = opt->ext(opt, FormatString, &replacelen, &fmtlen); |
203 | 109M | if (opt->fkt) |
204 | 10.8M | arg = opt->fkt(opt->arg.pv); |
205 | | |
206 | 109M | if (replace && (replacelen > 0)) |
207 | 99.2M | { |
208 | 99.2M | WINPR_PRAGMA_DIAG_PUSH |
209 | 99.2M | WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL |
210 | 99.2M | const int rc = _snprintf(&format[index], formatlen - index, replace, arg); |
211 | 99.2M | WINPR_PRAGMA_DIAG_POP |
212 | 99.2M | if (rc < 0) |
213 | 0 | return FALSE; |
214 | 99.2M | if (!check_and_log_format_size(format, formatlen, index, |
215 | 198M | WINPR_ASSERTING_INT_CAST(size_t, rc))) |
216 | 0 | return FALSE; |
217 | 198M | index += WINPR_ASSERTING_INT_CAST(size_t, rc); |
218 | 198M | } |
219 | 109M | FormatString += fmtlen; |
220 | 109M | } |
221 | 229M | else |
222 | 229M | { |
223 | | /* Unknown format string */ |
224 | 229M | if (*FormatString == '%') |
225 | 0 | return log_invalid_fmt(FormatString); |
226 | | |
227 | 229M | if (!check_and_log_format_size(format, formatlen, index, 1)) |
228 | 0 | return FALSE; |
229 | 229M | format[index++] = *FormatString++; |
230 | 229M | } |
231 | 338M | } |
232 | | |
233 | 11.6M | if (!check_and_log_format_size(format, formatlen, index, 0)) |
234 | 0 | return FALSE; |
235 | 11.6M | return TRUE; |
236 | 11.6M | } |
237 | | |
238 | | BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, const wLogMessage* message, |
239 | | char* prefix, size_t prefixlen) |
240 | 10.8M | { |
241 | 10.8M | char format[WLOG_MAX_PREFIX_SIZE] = { 0 }; |
242 | | |
243 | 10.8M | WINPR_ASSERT(layout); |
244 | 10.8M | WINPR_ASSERT(message); |
245 | 10.8M | WINPR_ASSERT(prefix); |
246 | | |
247 | 10.8M | struct format_tid_arg targ = { 0 }; |
248 | | |
249 | 10.8M | SYSTEMTIME localTime = { 0 }; |
250 | 10.8M | GetLocalTime(&localTime); |
251 | | |
252 | 10.8M | struct format_option_recurse recurse = { |
253 | 10.8M | .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message |
254 | 10.8M | }; |
255 | | |
256 | 368M | #define ENTRY(x) x, sizeof(x) - 1 |
257 | 10.8M | struct format_option options[] = { |
258 | 10.8M | { ENTRY("%ctx"), |
259 | 10.8M | ENTRY("%s"), |
260 | 10.8M | log->custom, |
261 | 10.8M | { .pv = log->context }, |
262 | 10.8M | NULL, |
263 | 10.8M | &recurse }, /* log context */ |
264 | 10.8M | { ENTRY("%dw"), |
265 | 10.8M | ENTRY("%u"), |
266 | 10.8M | NULL, |
267 | 10.8M | { .s = localTime.wDayOfWeek }, |
268 | 10.8M | NULL, |
269 | 10.8M | &recurse }, /* day of week */ |
270 | 10.8M | { ENTRY("%dy"), ENTRY("%u"), NULL, { .s = localTime.wDay }, NULL, &recurse }, /* day of year |
271 | | */ |
272 | 10.8M | { ENTRY("%fl"), ENTRY("%s"), NULL, { .cpv = message->FileName }, NULL, &recurse }, /* file |
273 | | */ |
274 | 10.8M | { ENTRY("%fn"), |
275 | 10.8M | ENTRY("%s"), |
276 | 10.8M | NULL, |
277 | 10.8M | { .cpv = message->FunctionName }, |
278 | 10.8M | NULL, |
279 | 10.8M | &recurse }, /* function |
280 | | */ |
281 | 10.8M | { ENTRY("%hr"), ENTRY("%02u"), NULL, { .s = localTime.wHour }, NULL, &recurse }, /* hours */ |
282 | 10.8M | { ENTRY("%ln"), |
283 | 10.8M | ENTRY("%" PRIuz), |
284 | 10.8M | NULL, |
285 | 10.8M | { .s = message->LineNumber }, |
286 | 10.8M | NULL, |
287 | 10.8M | &recurse }, /* line number */ |
288 | 10.8M | { ENTRY("%lv"), |
289 | 10.8M | ENTRY("%s"), |
290 | 10.8M | NULL, |
291 | 10.8M | { .cpv = WLOG_LEVELS[message->Level] }, |
292 | 10.8M | NULL, |
293 | 10.8M | &recurse }, /* log level */ |
294 | 10.8M | { ENTRY("%mi"), |
295 | 10.8M | ENTRY("%02u"), |
296 | 10.8M | NULL, |
297 | 10.8M | { .s = localTime.wMinute }, |
298 | 10.8M | NULL, |
299 | 10.8M | &recurse }, /* minutes |
300 | | */ |
301 | 10.8M | { ENTRY("%ml"), |
302 | 10.8M | ENTRY("%03u"), |
303 | 10.8M | NULL, |
304 | 10.8M | { .s = localTime.wMilliseconds }, |
305 | 10.8M | NULL, |
306 | 10.8M | &recurse }, /* milliseconds */ |
307 | 10.8M | { ENTRY("%mn"), ENTRY("%s"), NULL, { .cpv = log->Name }, NULL, &recurse }, /* module name */ |
308 | 10.8M | { ENTRY("%mo"), ENTRY("%u"), NULL, { .s = localTime.wMonth }, NULL, &recurse }, /* month */ |
309 | 10.8M | { ENTRY("%pid"), |
310 | 10.8M | ENTRY("%u"), |
311 | 10.8M | NULL, |
312 | 10.8M | { .s = GetCurrentProcessId() }, |
313 | 10.8M | NULL, |
314 | 10.8M | &recurse }, /* process id */ |
315 | 10.8M | { ENTRY("%se"), |
316 | 10.8M | ENTRY("%02u"), |
317 | 10.8M | NULL, |
318 | 10.8M | { .s = localTime.wSecond }, |
319 | 10.8M | NULL, |
320 | 10.8M | &recurse }, /* seconds |
321 | | */ |
322 | 10.8M | { ENTRY("%tid"), ENTRY("%s"), get_tid, { .pv = &targ }, NULL, &recurse }, /* thread id */ |
323 | 10.8M | { ENTRY("%yr"), ENTRY("%u"), NULL, { .s = localTime.wYear }, NULL, &recurse }, /* year */ |
324 | 10.8M | { ENTRY("%{"), |
325 | 10.8M | ENTRY("%}"), |
326 | 10.8M | NULL, |
327 | 10.8M | { .pv = log->context }, |
328 | 10.8M | skip_if_null, |
329 | 10.8M | &recurse }, /* skip if no context */ |
330 | 10.8M | }; |
331 | | |
332 | 10.8M | recurse.options = options; |
333 | 10.8M | recurse.nroptions = ARRAYSIZE(options); |
334 | | |
335 | 10.8M | if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format))) |
336 | 0 | return FALSE; |
337 | | |
338 | 10.8M | WINPR_PRAGMA_DIAG_PUSH |
339 | 10.8M | WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY |
340 | | |
341 | 10.8M | WLog_PrintMessagePrefix(prefix, prefixlen, format); |
342 | | |
343 | 10.8M | WINPR_PRAGMA_DIAG_POP |
344 | | |
345 | 10.8M | return TRUE; |
346 | 10.8M | } |
347 | | |
348 | | wLogLayout* WLog_GetLogLayout(wLog* log) |
349 | 0 | { |
350 | 0 | wLogAppender* appender = NULL; |
351 | 0 | appender = WLog_GetLogAppender(log); |
352 | 0 | return appender->Layout; |
353 | 0 | } |
354 | | |
355 | | BOOL WLog_Layout_SetPrefixFormat(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout, |
356 | | const char* format) |
357 | 0 | { |
358 | 0 | free(layout->FormatString); |
359 | 0 | layout->FormatString = NULL; |
360 | |
|
361 | 0 | if (format) |
362 | 0 | { |
363 | 0 | layout->FormatString = _strdup(format); |
364 | |
|
365 | 0 | if (!layout->FormatString) |
366 | 0 | return FALSE; |
367 | 0 | } |
368 | | |
369 | 0 | return TRUE; |
370 | 0 | } |
371 | | |
372 | | wLogLayout* WLog_Layout_New(WINPR_ATTR_UNUSED wLog* log) |
373 | 5 | { |
374 | 5 | LPCSTR prefix = "WLOG_PREFIX"; |
375 | 5 | DWORD nSize = 0; |
376 | 5 | char* env = NULL; |
377 | 5 | wLogLayout* layout = NULL; |
378 | 5 | layout = (wLogLayout*)calloc(1, sizeof(wLogLayout)); |
379 | | |
380 | 5 | if (!layout) |
381 | 0 | return NULL; |
382 | | |
383 | 5 | nSize = GetEnvironmentVariableA(prefix, NULL, 0); |
384 | | |
385 | 5 | if (nSize) |
386 | 0 | { |
387 | 0 | env = (LPSTR)malloc(nSize); |
388 | |
|
389 | 0 | if (!env) |
390 | 0 | { |
391 | 0 | free(layout); |
392 | 0 | return NULL; |
393 | 0 | } |
394 | | |
395 | 0 | if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1) |
396 | 0 | { |
397 | 0 | free(env); |
398 | 0 | free(layout); |
399 | 0 | return NULL; |
400 | 0 | } |
401 | 0 | } |
402 | | |
403 | 5 | if (env) |
404 | 0 | layout->FormatString = env; |
405 | 5 | else |
406 | 5 | { |
407 | | #ifdef ANDROID |
408 | | layout->FormatString = _strdup("[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: "); |
409 | | #else |
410 | 5 | layout->FormatString = |
411 | 5 | _strdup("[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: "); |
412 | 5 | #endif |
413 | | |
414 | 5 | if (!layout->FormatString) |
415 | 0 | { |
416 | 0 | free(layout); |
417 | 0 | return NULL; |
418 | 0 | } |
419 | 5 | } |
420 | | |
421 | 5 | return layout; |
422 | 5 | } |
423 | | |
424 | | void WLog_Layout_Free(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout) |
425 | 5 | { |
426 | 5 | if (layout) |
427 | 5 | { |
428 | 5 | if (layout->FormatString) |
429 | 5 | { |
430 | 5 | free(layout->FormatString); |
431 | 5 | layout->FormatString = NULL; |
432 | 5 | } |
433 | | |
434 | 5 | free(layout); |
435 | 5 | } |
436 | 5 | } |