/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_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  | 0  | { | 
77  | 0  |   WINPR_ASSERT(message);  | 
78  | 0  |   (void)vsnprintf(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args);  | 
79  | 0  | }  | 
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  | 0  | { | 
85  | 0  |   va_list args;  | 
86  | 0  |   va_start(args, format);  | 
87  | 0  |   WLog_PrintMessagePrefixVA(log, message, format, args);  | 
88  | 0  |   va_end(args);  | 
89  | 0  | }  | 
90  |  |  | 
91  |  | static const char* get_tid(void* arg)  | 
92  | 0  | { | 
93  | 0  |   char* str = arg;  | 
94  | 0  |   size_t tid = 0;  | 
95  | 0  | #if defined __linux__ && !defined ANDROID  | 
96  |  |   /* On Linux we prefer to see the LWP id */  | 
97  | 0  |   tid = (size_t)syscall(SYS_gettid);  | 
98  |  | #else  | 
99  |  |   tid = (size_t)GetCurrentThreadId();  | 
100  |  | #endif  | 
101  | 0  |   (void)sprintf(str, "%08" PRIxz, tid);  | 
102  | 0  |   return str;  | 
103  | 0  | }  | 
104  |  |  | 
105  |  | static BOOL log_invalid_fmt(const char* what)  | 
106  | 0  | { | 
107  | 0  |   (void)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  | 0  | { | 
113  |  |   /* format string must be '\0' terminated, so abort at size - 1 */  | 
114  | 0  |   if (index + add + 1 >= size)  | 
115  | 0  |   { | 
116  | 0  |     (void)fprintf(stderr,  | 
117  | 0  |                   "Format string too long ['%s', max %" PRIuz ", used %" PRIuz  | 
118  | 0  |                   ", adding %" PRIuz "]\n",  | 
119  | 0  |                   format, size, index, add);  | 
120  | 0  |     return FALSE;  | 
121  | 0  |   }  | 
122  | 0  |   return TRUE;  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | static int opt_compare_fn(const void* a, const void* b)  | 
126  | 0  | { | 
127  | 0  |   const char* what = a;  | 
128  | 0  |   const struct format_option* opt = b;  | 
129  | 0  |   if (!opt)  | 
130  | 0  |     return -1;  | 
131  | 0  |   return strncmp(what, opt->fmt, opt->fmtlen);  | 
132  | 0  | }  | 
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  | 0  | { | 
140  | 0  |   WINPR_ASSERT(opt);  | 
141  | 0  |   WINPR_ASSERT(fmt);  | 
142  | 0  |   WINPR_ASSERT(preplacelen);  | 
143  | 0  |   WINPR_ASSERT(pskiplen);  | 
144  |  |  | 
145  | 0  |   *preplacelen = 0;  | 
146  | 0  |   *pskiplen = 0;  | 
147  |  | 
  | 
148  | 0  |   const char* str = &fmt[opt->fmtlen]; /* Skip first %{ from string */ | 
149  | 0  |   const char* end = strstr(str, opt->replace);  | 
150  | 0  |   if (!end)  | 
151  | 0  |     return NULL;  | 
152  | 0  |   *pskiplen = end - fmt + opt->replacelen;  | 
153  |  | 
  | 
154  | 0  |   if (!opt->arg)  | 
155  | 0  |     return NULL;  | 
156  |  |  | 
157  | 0  |   const size_t replacelen = end - str;  | 
158  |  | 
  | 
159  | 0  |   char buffer[WLOG_MAX_PREFIX_SIZE] = { 0 }; | 
160  | 0  |   memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1));  | 
161  |  | 
  | 
162  | 0  |   if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer,  | 
163  | 0  |                              ARRAYSIZE(opt->recurse->buffer)))  | 
164  | 0  |     return NULL;  | 
165  |  |  | 
166  | 0  |   *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer));  | 
167  | 0  |   return opt->recurse->buffer;  | 
168  | 0  | }  | 
169  |  |  | 
170  |  | static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse,  | 
171  |  |                                   char* format, size_t formatlen)  | 
172  | 0  | { | 
173  | 0  |   WINPR_ASSERT(FormatString);  | 
174  | 0  |   WINPR_ASSERT(recurse);  | 
175  |  |  | 
176  | 0  |   size_t index = 0;  | 
177  |  | 
  | 
178  | 0  |   while (*FormatString)  | 
179  | 0  |   { | 
180  | 0  |     const struct format_option* opt =  | 
181  | 0  |         bsearch(FormatString, recurse->options, recurse->nroptions,  | 
182  | 0  |                 sizeof(struct format_option), opt_compare_fn);  | 
183  | 0  |     if (opt)  | 
184  | 0  |     { | 
185  | 0  |       size_t replacelen = opt->replacelen;  | 
186  | 0  |       size_t fmtlen = opt->fmtlen;  | 
187  | 0  |       const char* replace = opt->replace;  | 
188  | 0  |       const void* arg = opt->arg;  | 
189  |  | 
  | 
190  | 0  |       if (opt->ext)  | 
191  | 0  |         replace = opt->ext(opt, FormatString, &replacelen, &fmtlen);  | 
192  | 0  |       if (opt->fkt)  | 
193  | 0  |         arg = opt->fkt(opt->arg);  | 
194  |  | 
  | 
195  | 0  |       if (replace && (replacelen > 0))  | 
196  | 0  |       { | 
197  | 0  |         const int rc = _snprintf(&format[index], formatlen - index, replace, arg);  | 
198  | 0  |         if (rc < 0)  | 
199  | 0  |           return FALSE;  | 
200  | 0  |         if (!check_and_log_format_size(format, formatlen, index, rc))  | 
201  | 0  |           return FALSE;  | 
202  | 0  |         index += rc;  | 
203  | 0  |       }  | 
204  | 0  |       FormatString += fmtlen;  | 
205  | 0  |     }  | 
206  | 0  |     else  | 
207  | 0  |     { | 
208  |  |       /* Unknown format string */  | 
209  | 0  |       if (*FormatString == '%')  | 
210  | 0  |         return log_invalid_fmt(FormatString);  | 
211  |  |  | 
212  | 0  |       if (!check_and_log_format_size(format, formatlen, index, 1))  | 
213  | 0  |         return FALSE;  | 
214  | 0  |       format[index++] = *FormatString++;  | 
215  | 0  |     }  | 
216  | 0  |   }  | 
217  |  |  | 
218  | 0  |   if (!check_and_log_format_size(format, formatlen, index, 0))  | 
219  | 0  |     return FALSE;  | 
220  | 0  |   return TRUE;  | 
221  | 0  | }  | 
222  |  |  | 
223  |  | BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message)  | 
224  | 0  | { | 
225  | 0  |   char format[WLOG_MAX_PREFIX_SIZE] = { 0 }; | 
226  |  | 
  | 
227  | 0  |   WINPR_ASSERT(layout);  | 
228  | 0  |   WINPR_ASSERT(message);  | 
229  |  |  | 
230  | 0  |   char tid[32] = { 0 }; | 
231  | 0  |   SYSTEMTIME localTime = { 0 }; | 
232  | 0  |   GetLocalTime(&localTime);  | 
233  |  | 
  | 
234  | 0  |   struct format_option_recurse recurse = { | 
235  | 0  |     .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message  | 
236  | 0  |   };  | 
237  |  | 
  | 
238  | 0  | #define ENTRY(x) x, sizeof(x) - 1  | 
239  | 0  |   struct format_option options[] = { | 
240  | 0  |     { ENTRY("%ctx"), ENTRY("%s"), log->custom, log->context, NULL, &recurse }, /* log context */ | 
241  | 0  |     { ENTRY("%dw"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDayOfWeek, NULL, | 
242  | 0  |       &recurse }, /* day of week */  | 
243  | 0  |     { ENTRY("%dy"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDay, NULL, | 
244  | 0  |       &recurse }, /* day of year */  | 
245  | 0  |     { ENTRY("%fl"), ENTRY("%s"), NULL, (void*)message->FileName, NULL, &recurse }, /* file */ | 
246  | 0  |     { ENTRY("%fn"), ENTRY("%s"), NULL, (void*)message->FunctionName, NULL, | 
247  | 0  |       &recurse }, /* function */  | 
248  | 0  |     { ENTRY("%hr"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wHour, NULL, | 
249  | 0  |       &recurse }, /* hours */  | 
250  | 0  |     { ENTRY("%ln"), ENTRY("%" PRIuz), NULL, (void*)message->LineNumber, NULL, | 
251  | 0  |       &recurse }, /* line number */  | 
252  | 0  |     { ENTRY("%lv"), ENTRY("%s"), NULL, (void*)WLOG_LEVELS[message->Level], NULL, | 
253  | 0  |       &recurse }, /* log level */  | 
254  | 0  |     { ENTRY("%mi"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wMinute, NULL, | 
255  | 0  |       &recurse }, /* minutes */  | 
256  | 0  |     { ENTRY("%ml"), ENTRY("%03u"), NULL, (void*)(size_t)localTime.wMilliseconds, NULL, | 
257  | 0  |       &recurse },                                                   /* milliseconds */  | 
258  | 0  |     { ENTRY("%mn"), ENTRY("%s"), NULL, log->Name, NULL, &recurse }, /* module name */ | 
259  | 0  |     { ENTRY("%mo"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wMonth, NULL, | 
260  | 0  |       &recurse }, /* month */  | 
261  | 0  |     { ENTRY("%pid"), ENTRY("%u"), NULL, (void*)(size_t)GetCurrentProcessId(), NULL, | 
262  | 0  |       &recurse }, /* process id */  | 
263  | 0  |     { ENTRY("%se"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wSecond, NULL, | 
264  | 0  |       &recurse },                                                 /* seconds */  | 
265  | 0  |     { ENTRY("%tid"), ENTRY("%s"), get_tid, tid, NULL, &recurse }, /* thread id */ | 
266  | 0  |     { ENTRY("%yr"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wYear, NULL, | 
267  | 0  |       &recurse }, /* year */  | 
268  | 0  |     { ENTRY("%{"), ENTRY("%}"), NULL, log->context, skip_if_null, | 
269  | 0  |       &recurse }, /* skip if no context */  | 
270  | 0  |   };  | 
271  |  | 
  | 
272  | 0  |   recurse.options = options;  | 
273  | 0  |   recurse.nroptions = ARRAYSIZE(options);  | 
274  |  | 
  | 
275  | 0  |   if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format)))  | 
276  | 0  |     return FALSE;  | 
277  |  |  | 
278  | 0  |   WINPR_PRAGMA_DIAG_PUSH  | 
279  | 0  |   WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY  | 
280  |  |  | 
281  | 0  |   WLog_PrintMessagePrefix(log, message, format);  | 
282  |  | 
  | 
283  | 0  |   WINPR_PRAGMA_DIAG_POP  | 
284  |  | 
  | 
285  | 0  |   return TRUE;  | 
286  | 0  | }  | 
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  | 0  | { | 
313  | 0  |   LPCSTR prefix = "WLOG_PREFIX";  | 
314  | 0  |   DWORD nSize = 0;  | 
315  | 0  |   char* env = NULL;  | 
316  | 0  |   wLogLayout* layout = NULL;  | 
317  | 0  |   layout = (wLogLayout*)calloc(1, sizeof(wLogLayout));  | 
318  |  | 
  | 
319  | 0  |   if (!layout)  | 
320  | 0  |     return NULL;  | 
321  |  |  | 
322  | 0  |   nSize = GetEnvironmentVariableA(prefix, NULL, 0);  | 
323  |  | 
  | 
324  | 0  |   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  | 0  |   if (env)  | 
343  | 0  |     layout->FormatString = env;  | 
344  | 0  |   else  | 
345  | 0  |   { | 
346  |  | #ifdef ANDROID  | 
347  |  |     layout->FormatString = _strdup("[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: "); | 
348  |  | #else  | 
349  | 0  |     layout->FormatString =  | 
350  | 0  |         _strdup("[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: "); | 
351  | 0  | #endif  | 
352  |  | 
  | 
353  | 0  |     if (!layout->FormatString)  | 
354  | 0  |     { | 
355  | 0  |       free(layout);  | 
356  | 0  |       return NULL;  | 
357  | 0  |     }  | 
358  | 0  |   }  | 
359  |  |  | 
360  | 0  |   return layout;  | 
361  | 0  | }  | 
362  |  |  | 
363  |  | void WLog_Layout_Free(wLog* log, wLogLayout* layout)  | 
364  | 0  | { | 
365  | 0  |   if (layout)  | 
366  | 0  |   { | 
367  | 0  |     if (layout->FormatString)  | 
368  | 0  |     { | 
369  | 0  |       free(layout->FormatString);  | 
370  | 0  |       layout->FormatString = NULL;  | 
371  | 0  |     }  | 
372  |  | 
  | 
373  | 0  |     free(layout);  | 
374  | 0  |   }  | 
375  | 0  | }  |