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