/src/FreeRDP/winpr/libwinpr/utils/wlog/Layout.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #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 | | void* arg; |
60 | | const char* (*ext)(const struct format_option* opt, const char* str, size_t* preplacelen, |
61 | | size_t* pskiplen); |
62 | | struct format_option_recurse* recurse; |
63 | | }; |
64 | | |
65 | | struct format_option_recurse |
66 | | { |
67 | | struct format_option* options; |
68 | | size_t nroptions; |
69 | | wLog* log; |
70 | | wLogLayout* layout; |
71 | | wLogMessage* message; |
72 | | char buffer[WLOG_MAX_PREFIX_SIZE]; |
73 | | }; |
74 | | |
75 | | /** |
76 | | * Log Layout |
77 | | */ |
78 | | WINPR_ATTR_FORMAT_ARG(3, 0) |
79 | | static void WLog_PrintMessagePrefixVA(WINPR_ATTR_UNUSED wLog* log, wLogMessage* message, |
80 | | WINPR_FORMAT_ARG const char* format, va_list args) |
81 | 0 | { |
82 | 0 | WINPR_ASSERT(message); |
83 | 0 | (void)vsnprintf(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args); |
84 | 0 | } |
85 | | |
86 | | WINPR_ATTR_FORMAT_ARG(3, 4) |
87 | | static void WLog_PrintMessagePrefix(wLog* log, wLogMessage* message, |
88 | | WINPR_FORMAT_ARG const char* format, ...) |
89 | 0 | { |
90 | 0 | va_list args; |
91 | 0 | va_start(args, format); |
92 | 0 | WLog_PrintMessagePrefixVA(log, message, format, args); |
93 | 0 | va_end(args); |
94 | 0 | } |
95 | | |
96 | | static const char* get_tid(void* arg) |
97 | 0 | { |
98 | 0 | struct format_tid_arg* targ = arg; |
99 | 0 | WINPR_ASSERT(targ); |
100 | | |
101 | 0 | size_t tid = 0; |
102 | 0 | #if defined __linux__ && !defined ANDROID |
103 | | /* On Linux we prefer to see the LWP id */ |
104 | 0 | tid = (size_t)syscall(SYS_gettid); |
105 | | #else |
106 | | tid = (size_t)GetCurrentThreadId(); |
107 | | #endif |
108 | 0 | (void)_snprintf(targ->tid, sizeof(targ->tid), "%08" PRIxz, tid); |
109 | 0 | return targ->tid; |
110 | 0 | } |
111 | | |
112 | | static BOOL log_invalid_fmt(const char* what) |
113 | 0 | { |
114 | 0 | (void)fprintf(stderr, "Invalid format string '%s'\n", what); |
115 | 0 | return FALSE; |
116 | 0 | } |
117 | | |
118 | | static BOOL check_and_log_format_size(char* format, size_t size, size_t index, size_t add) |
119 | 0 | { |
120 | | /* format string must be '\0' terminated, so abort at size - 1 */ |
121 | 0 | if (index + add + 1 >= size) |
122 | 0 | { |
123 | 0 | (void)fprintf(stderr, |
124 | 0 | "Format string too long ['%s', max %" PRIuz ", used %" PRIuz |
125 | 0 | ", adding %" PRIuz "]\n", |
126 | 0 | format, size, index, add); |
127 | 0 | return FALSE; |
128 | 0 | } |
129 | 0 | return TRUE; |
130 | 0 | } |
131 | | |
132 | | static int opt_compare_fn(const void* a, const void* b) |
133 | 0 | { |
134 | 0 | const char* what = a; |
135 | 0 | const struct format_option* opt = b; |
136 | 0 | if (!opt) |
137 | 0 | return -1; |
138 | 0 | return strncmp(what, opt->fmt, opt->fmtlen); |
139 | 0 | } |
140 | | |
141 | | static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse, |
142 | | char* format, size_t formatlen); |
143 | | |
144 | | static const char* skip_if_null(const struct format_option* opt, const char* fmt, |
145 | | size_t* preplacelen, size_t* pskiplen) |
146 | 0 | { |
147 | 0 | WINPR_ASSERT(opt); |
148 | 0 | WINPR_ASSERT(fmt); |
149 | 0 | WINPR_ASSERT(preplacelen); |
150 | 0 | WINPR_ASSERT(pskiplen); |
151 | | |
152 | 0 | *preplacelen = 0; |
153 | 0 | *pskiplen = 0; |
154 | |
|
155 | 0 | const char* str = &fmt[opt->fmtlen]; /* Skip first %{ from string */ |
156 | 0 | const char* end = strstr(str, opt->replace); |
157 | 0 | if (!end) |
158 | 0 | return NULL; |
159 | 0 | *pskiplen = WINPR_ASSERTING_INT_CAST(size_t, end - fmt) + opt->replacelen; |
160 | | |
161 | 0 | if (!opt->arg) |
162 | 0 | return NULL; |
163 | | |
164 | 0 | const size_t replacelen = WINPR_ASSERTING_INT_CAST(size_t, end - str); |
165 | | |
166 | 0 | char buffer[WLOG_MAX_PREFIX_SIZE] = { 0 }; |
167 | 0 | memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1)); |
168 | |
|
169 | 0 | if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer, |
170 | 0 | ARRAYSIZE(opt->recurse->buffer))) |
171 | 0 | return NULL; |
172 | | |
173 | 0 | *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer)); |
174 | 0 | return opt->recurse->buffer; |
175 | 0 | } |
176 | | |
177 | | static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse, |
178 | | char* format, size_t formatlen) |
179 | 0 | { |
180 | 0 | WINPR_ASSERT(FormatString); |
181 | 0 | WINPR_ASSERT(recurse); |
182 | | |
183 | 0 | size_t index = 0; |
184 | |
|
185 | 0 | while (*FormatString) |
186 | 0 | { |
187 | 0 | const struct format_option* opt = |
188 | 0 | bsearch(FormatString, recurse->options, recurse->nroptions, |
189 | 0 | sizeof(struct format_option), opt_compare_fn); |
190 | 0 | if (opt) |
191 | 0 | { |
192 | 0 | size_t replacelen = opt->replacelen; |
193 | 0 | size_t fmtlen = opt->fmtlen; |
194 | 0 | const char* replace = opt->replace; |
195 | 0 | const void* arg = opt->arg; |
196 | |
|
197 | 0 | if (opt->ext) |
198 | 0 | replace = opt->ext(opt, FormatString, &replacelen, &fmtlen); |
199 | 0 | if (opt->fkt) |
200 | 0 | arg = opt->fkt(opt->arg); |
201 | |
|
202 | 0 | if (replace && (replacelen > 0)) |
203 | 0 | { |
204 | 0 | WINPR_PRAGMA_DIAG_PUSH |
205 | 0 | WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL |
206 | 0 | const int rc = _snprintf(&format[index], formatlen - index, replace, arg); |
207 | 0 | WINPR_PRAGMA_DIAG_POP |
208 | 0 | if (rc < 0) |
209 | 0 | return FALSE; |
210 | 0 | if (!check_and_log_format_size(format, formatlen, index, |
211 | 0 | WINPR_ASSERTING_INT_CAST(size_t, rc))) |
212 | 0 | return FALSE; |
213 | 0 | index += WINPR_ASSERTING_INT_CAST(size_t, rc); |
214 | 0 | } |
215 | 0 | FormatString += fmtlen; |
216 | 0 | } |
217 | 0 | else |
218 | 0 | { |
219 | | /* Unknown format string */ |
220 | 0 | if (*FormatString == '%') |
221 | 0 | return log_invalid_fmt(FormatString); |
222 | | |
223 | 0 | if (!check_and_log_format_size(format, formatlen, index, 1)) |
224 | 0 | return FALSE; |
225 | 0 | format[index++] = *FormatString++; |
226 | 0 | } |
227 | 0 | } |
228 | | |
229 | 0 | if (!check_and_log_format_size(format, formatlen, index, 0)) |
230 | 0 | return FALSE; |
231 | 0 | return TRUE; |
232 | 0 | } |
233 | | |
234 | | BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message) |
235 | 0 | { |
236 | 0 | char format[WLOG_MAX_PREFIX_SIZE] = { 0 }; |
237 | |
|
238 | 0 | WINPR_ASSERT(layout); |
239 | 0 | WINPR_ASSERT(message); |
240 | | |
241 | 0 | struct format_tid_arg targ = { 0 }; |
242 | |
|
243 | 0 | SYSTEMTIME localTime = { 0 }; |
244 | 0 | GetLocalTime(&localTime); |
245 | |
|
246 | 0 | struct format_option_recurse recurse = { |
247 | 0 | .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message |
248 | 0 | }; |
249 | |
|
250 | 0 | #define ENTRY(x) x, sizeof(x) - 1 |
251 | 0 | struct format_option options[] = { |
252 | 0 | { ENTRY("%ctx"), ENTRY("%s"), log->custom, log->context, NULL, &recurse }, /* log context */ |
253 | 0 | { ENTRY("%dw"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDayOfWeek, NULL, |
254 | 0 | &recurse }, /* day of week */ |
255 | 0 | { ENTRY("%dy"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDay, NULL, |
256 | 0 | &recurse }, /* day of year */ |
257 | 0 | { ENTRY("%fl"), ENTRY("%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FileName, void*), |
258 | 0 | NULL, &recurse }, /* file */ |
259 | 0 | { ENTRY("%fn"), ENTRY("%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FunctionName, void*), |
260 | 0 | NULL, &recurse }, /* function */ |
261 | 0 | { ENTRY("%hr"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wHour, NULL, |
262 | 0 | &recurse }, /* hours */ |
263 | 0 | { ENTRY("%ln"), ENTRY("%" PRIuz), NULL, (void*)message->LineNumber, NULL, |
264 | 0 | &recurse }, /* line number */ |
265 | 0 | { ENTRY("%lv"), ENTRY("%s"), NULL, |
266 | 0 | WINPR_CAST_CONST_PTR_AWAY(WLOG_LEVELS[message->Level], void*), NULL, |
267 | 0 | &recurse }, /* log level */ |
268 | 0 | { ENTRY("%mi"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wMinute, NULL, |
269 | 0 | &recurse }, /* minutes */ |
270 | 0 | { ENTRY("%ml"), ENTRY("%03u"), NULL, (void*)(size_t)localTime.wMilliseconds, NULL, |
271 | 0 | &recurse }, /* milliseconds */ |
272 | 0 | { ENTRY("%mn"), ENTRY("%s"), NULL, log->Name, NULL, &recurse }, /* module name */ |
273 | 0 | { ENTRY("%mo"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wMonth, NULL, |
274 | 0 | &recurse }, /* month */ |
275 | 0 | { ENTRY("%pid"), ENTRY("%u"), NULL, (void*)(size_t)GetCurrentProcessId(), NULL, |
276 | 0 | &recurse }, /* process id */ |
277 | 0 | { ENTRY("%se"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wSecond, NULL, |
278 | 0 | &recurse }, /* seconds */ |
279 | 0 | { ENTRY("%tid"), ENTRY("%s"), get_tid, &targ, NULL, &recurse }, /* thread id */ |
280 | 0 | { ENTRY("%yr"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wYear, NULL, |
281 | 0 | &recurse }, /* year */ |
282 | 0 | { ENTRY("%{"), ENTRY("%}"), NULL, log->context, skip_if_null, |
283 | 0 | &recurse }, /* skip if no context */ |
284 | 0 | }; |
285 | |
|
286 | 0 | recurse.options = options; |
287 | 0 | recurse.nroptions = ARRAYSIZE(options); |
288 | |
|
289 | 0 | if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format))) |
290 | 0 | return FALSE; |
291 | | |
292 | 0 | WINPR_PRAGMA_DIAG_PUSH |
293 | 0 | WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY |
294 | | |
295 | 0 | WLog_PrintMessagePrefix(log, message, format); |
296 | |
|
297 | 0 | WINPR_PRAGMA_DIAG_POP |
298 | |
|
299 | 0 | return TRUE; |
300 | 0 | } |
301 | | |
302 | | wLogLayout* WLog_GetLogLayout(wLog* log) |
303 | 0 | { |
304 | 0 | wLogAppender* appender = NULL; |
305 | 0 | appender = WLog_GetLogAppender(log); |
306 | 0 | return appender->Layout; |
307 | 0 | } |
308 | | |
309 | | BOOL WLog_Layout_SetPrefixFormat(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout, |
310 | | const char* format) |
311 | 0 | { |
312 | 0 | free(layout->FormatString); |
313 | 0 | layout->FormatString = NULL; |
314 | |
|
315 | 0 | if (format) |
316 | 0 | { |
317 | 0 | layout->FormatString = _strdup(format); |
318 | |
|
319 | 0 | if (!layout->FormatString) |
320 | 0 | return FALSE; |
321 | 0 | } |
322 | | |
323 | 0 | return TRUE; |
324 | 0 | } |
325 | | |
326 | | wLogLayout* WLog_Layout_New(WINPR_ATTR_UNUSED wLog* log) |
327 | 0 | { |
328 | 0 | LPCSTR prefix = "WLOG_PREFIX"; |
329 | 0 | DWORD nSize = 0; |
330 | 0 | char* env = NULL; |
331 | 0 | wLogLayout* layout = NULL; |
332 | 0 | layout = (wLogLayout*)calloc(1, sizeof(wLogLayout)); |
333 | |
|
334 | 0 | if (!layout) |
335 | 0 | return NULL; |
336 | | |
337 | 0 | nSize = GetEnvironmentVariableA(prefix, NULL, 0); |
338 | |
|
339 | 0 | if (nSize) |
340 | 0 | { |
341 | 0 | env = (LPSTR)malloc(nSize); |
342 | |
|
343 | 0 | if (!env) |
344 | 0 | { |
345 | 0 | free(layout); |
346 | 0 | return NULL; |
347 | 0 | } |
348 | | |
349 | 0 | if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1) |
350 | 0 | { |
351 | 0 | free(env); |
352 | 0 | free(layout); |
353 | 0 | return NULL; |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | 0 | if (env) |
358 | 0 | layout->FormatString = env; |
359 | 0 | else |
360 | 0 | { |
361 | | #ifdef ANDROID |
362 | | layout->FormatString = _strdup("[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: "); |
363 | | #else |
364 | 0 | layout->FormatString = |
365 | 0 | _strdup("[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: "); |
366 | 0 | #endif |
367 | |
|
368 | 0 | if (!layout->FormatString) |
369 | 0 | { |
370 | 0 | free(layout); |
371 | 0 | return NULL; |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | return layout; |
376 | 0 | } |
377 | | |
378 | | void WLog_Layout_Free(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout) |
379 | 0 | { |
380 | 0 | if (layout) |
381 | 0 | { |
382 | 0 | if (layout->FormatString) |
383 | 0 | { |
384 | 0 | free(layout->FormatString); |
385 | 0 | layout->FormatString = NULL; |
386 | 0 | } |
387 | |
|
388 | 0 | free(layout); |
389 | 0 | } |
390 | 0 | } |