/src/FreeRDP/winpr/libwinpr/utils/wlog/wlog.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 <assert.h> |
23 | | #include <stdio.h> |
24 | | #include <stdarg.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include <winpr/crt.h> |
28 | | #include <winpr/assert.h> |
29 | | #include <winpr/print.h> |
30 | | #include <winpr/debug.h> |
31 | | #include <winpr/environment.h> |
32 | | #include <winpr/wlog.h> |
33 | | |
34 | | #if defined(ANDROID) |
35 | | #include <android/log.h> |
36 | | #include "../log.h" |
37 | | #endif |
38 | | |
39 | | #include "wlog.h" |
40 | | |
41 | | typedef struct |
42 | | { |
43 | | DWORD Level; |
44 | | LPSTR* Names; |
45 | | size_t NameCount; |
46 | | } wLogFilter; |
47 | | |
48 | 1.02k | #define WLOG_FILTER_NOT_FILTERED (-1) |
49 | 1.02k | #define WLOG_FILTER_NOT_INITIALIZED (-2) |
50 | | /** |
51 | | * References for general logging concepts: |
52 | | * |
53 | | * Short introduction to log4j: |
54 | | * http://logging.apache.org/log4j/1.2/manual.html |
55 | | * |
56 | | * logging - Logging facility for Python: |
57 | | * http://docs.python.org/2/library/logging.html |
58 | | */ |
59 | | |
60 | | LPCSTR WLOG_LEVELS[7] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" }; |
61 | | |
62 | | static INIT_ONCE g_WLogInitialized = INIT_ONCE_STATIC_INIT; |
63 | | static DWORD g_FilterCount = 0; |
64 | | static wLogFilter* g_Filters = NULL; |
65 | | static wLog* g_RootLog = NULL; |
66 | | |
67 | | static wLog* WLog_New(LPCSTR name, wLog* rootLogger); |
68 | | static void WLog_Free(wLog* log); |
69 | | static LONG WLog_GetFilterLogLevel(wLog* log); |
70 | | static int WLog_ParseLogLevel(LPCSTR level); |
71 | | static BOOL WLog_ParseFilter(wLog* root, wLogFilter* filter, LPCSTR name); |
72 | | static BOOL WLog_ParseFilters(wLog* root); |
73 | | static wLog* WLog_Get_int(wLog* root, LPCSTR name); |
74 | | |
75 | | #if !defined(_WIN32) |
76 | | static void WLog_Uninit_(void) __attribute__((destructor)); |
77 | | #endif |
78 | | |
79 | | static void WLog_Uninit_(void) |
80 | 1 | { |
81 | 1 | wLog* child = NULL; |
82 | 1 | wLog* root = g_RootLog; |
83 | | |
84 | 1 | if (!root) |
85 | 0 | return; |
86 | | |
87 | 3 | for (DWORD index = 0; index < root->ChildrenCount; index++) |
88 | 2 | { |
89 | 2 | child = root->Children[index]; |
90 | 2 | WLog_Free(child); |
91 | 2 | } |
92 | | |
93 | 1 | WLog_Free(root); |
94 | 1 | g_RootLog = NULL; |
95 | 1 | } |
96 | | |
97 | | static void WLog_Lock(wLog* log) |
98 | 4 | { |
99 | 4 | WINPR_ASSERT(log); |
100 | 4 | EnterCriticalSection(&log->lock); |
101 | 4 | } |
102 | | |
103 | | static void WLog_Unlock(wLog* log) |
104 | 4 | { |
105 | 4 | WINPR_ASSERT(log); |
106 | 4 | LeaveCriticalSection(&log->lock); |
107 | 4 | } |
108 | | |
109 | | static BOOL CALLBACK WLog_InitializeRoot(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context) |
110 | 1 | { |
111 | 1 | char* env = NULL; |
112 | 1 | DWORD nSize = 0; |
113 | 1 | DWORD logAppenderType = 0; |
114 | 1 | LPCSTR appender = "WLOG_APPENDER"; |
115 | | |
116 | 1 | WINPR_UNUSED(InitOnce); |
117 | 1 | WINPR_UNUSED(Parameter); |
118 | 1 | WINPR_UNUSED(Context); |
119 | | |
120 | 1 | if (!(g_RootLog = WLog_New("", NULL))) |
121 | 0 | return FALSE; |
122 | | |
123 | 1 | g_RootLog->IsRoot = TRUE; |
124 | 1 | logAppenderType = WLOG_APPENDER_CONSOLE; |
125 | 1 | nSize = GetEnvironmentVariableA(appender, NULL, 0); |
126 | | |
127 | 1 | if (nSize) |
128 | 0 | { |
129 | 0 | env = (LPSTR)malloc(nSize); |
130 | |
|
131 | 0 | if (!env) |
132 | 0 | goto fail; |
133 | | |
134 | 0 | if (GetEnvironmentVariableA(appender, env, nSize) != nSize - 1) |
135 | 0 | { |
136 | 0 | (void)fprintf(stderr, "%s environment variable modified in my back", appender); |
137 | 0 | free(env); |
138 | 0 | goto fail; |
139 | 0 | } |
140 | | |
141 | 0 | if (_stricmp(env, "CONSOLE") == 0) |
142 | 0 | logAppenderType = WLOG_APPENDER_CONSOLE; |
143 | 0 | else if (_stricmp(env, "FILE") == 0) |
144 | 0 | logAppenderType = WLOG_APPENDER_FILE; |
145 | 0 | else if (_stricmp(env, "BINARY") == 0) |
146 | 0 | logAppenderType = WLOG_APPENDER_BINARY; |
147 | | |
148 | 0 | #ifdef WINPR_HAVE_SYSLOG_H |
149 | 0 | else if (_stricmp(env, "SYSLOG") == 0) |
150 | 0 | logAppenderType = WLOG_APPENDER_SYSLOG; |
151 | | |
152 | 0 | #endif /* WINPR_HAVE_SYSLOG_H */ |
153 | | #ifdef WINPR_HAVE_JOURNALD_H |
154 | | else if (_stricmp(env, "JOURNALD") == 0) |
155 | | logAppenderType = WLOG_APPENDER_JOURNALD; |
156 | | |
157 | | #endif |
158 | 0 | else if (_stricmp(env, "UDP") == 0) |
159 | 0 | logAppenderType = WLOG_APPENDER_UDP; |
160 | |
|
161 | 0 | free(env); |
162 | 0 | } |
163 | | |
164 | 1 | if (!WLog_SetLogAppenderType(g_RootLog, logAppenderType)) |
165 | 0 | goto fail; |
166 | | |
167 | 1 | if (!WLog_ParseFilters(g_RootLog)) |
168 | 0 | goto fail; |
169 | | |
170 | 1 | (void)atexit(WLog_Uninit_); |
171 | | |
172 | 1 | return TRUE; |
173 | 0 | fail: |
174 | 0 | WLog_Uninit_(); |
175 | 0 | return FALSE; |
176 | 1 | } |
177 | | |
178 | | static BOOL log_recursion(LPCSTR file, LPCSTR fkt, size_t line) |
179 | 0 | { |
180 | 0 | BOOL status = FALSE; |
181 | 0 | char** msg = NULL; |
182 | 0 | size_t used = 0; |
183 | 0 | void* bt = winpr_backtrace(20); |
184 | | #if defined(ANDROID) |
185 | | LPCSTR tag = WINPR_TAG("utils.wlog"); |
186 | | #endif |
187 | |
|
188 | 0 | if (!bt) |
189 | 0 | return FALSE; |
190 | | |
191 | 0 | msg = winpr_backtrace_symbols(bt, &used); |
192 | |
|
193 | 0 | if (!msg) |
194 | 0 | goto out; |
195 | | |
196 | | #if defined(ANDROID) |
197 | | |
198 | | if (__android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!") < 0) |
199 | | goto out; |
200 | | |
201 | | if (__android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%zu]", fkt, file, line) < 0) |
202 | | goto out; |
203 | | |
204 | | for (size_t i = 0; i < used; i++) |
205 | | if (__android_log_print(ANDROID_LOG_FATAL, tag, "%zu: %s", i, msg[i]) < 0) |
206 | | goto out; |
207 | | |
208 | | #else |
209 | | |
210 | 0 | if (fprintf(stderr, "[%s]: Recursion detected!\n", fkt) < 0) |
211 | 0 | goto out; |
212 | | |
213 | 0 | if (fprintf(stderr, "[%s]: Check %s:%" PRIuz "\n", fkt, file, line) < 0) |
214 | 0 | goto out; |
215 | | |
216 | 0 | for (size_t i = 0; i < used; i++) |
217 | 0 | if (fprintf(stderr, "%s: %" PRIuz ": %s\n", fkt, i, msg[i]) < 0) |
218 | 0 | goto out; |
219 | | |
220 | 0 | #endif |
221 | 0 | status = TRUE; |
222 | 0 | out: |
223 | 0 | free((void*)msg); |
224 | 0 | winpr_backtrace_free(bt); |
225 | 0 | return status; |
226 | 0 | } |
227 | | |
228 | | static BOOL WLog_Write(wLog* log, wLogMessage* message) |
229 | 1.01k | { |
230 | 1.01k | BOOL status = FALSE; |
231 | 1.01k | wLogAppender* appender = NULL; |
232 | 1.01k | appender = WLog_GetLogAppender(log); |
233 | | |
234 | 1.01k | if (!appender) |
235 | 0 | return FALSE; |
236 | | |
237 | 1.01k | if (!appender->active) |
238 | 1 | if (!WLog_OpenAppender(log)) |
239 | 0 | return FALSE; |
240 | | |
241 | 1.01k | EnterCriticalSection(&appender->lock); |
242 | | |
243 | 1.01k | if (appender->WriteMessage) |
244 | 1.01k | { |
245 | 1.01k | if (appender->recursive) |
246 | 0 | status = log_recursion(message->FileName, message->FunctionName, message->LineNumber); |
247 | 1.01k | else |
248 | 1.01k | { |
249 | 1.01k | appender->recursive = TRUE; |
250 | 1.01k | status = appender->WriteMessage(log, appender, message); |
251 | 1.01k | appender->recursive = FALSE; |
252 | 1.01k | } |
253 | 1.01k | } |
254 | | |
255 | 1.01k | LeaveCriticalSection(&appender->lock); |
256 | 1.01k | return status; |
257 | 1.01k | } |
258 | | |
259 | | static BOOL WLog_WriteData(wLog* log, wLogMessage* message) |
260 | 0 | { |
261 | 0 | BOOL status = 0; |
262 | 0 | wLogAppender* appender = NULL; |
263 | 0 | appender = WLog_GetLogAppender(log); |
264 | |
|
265 | 0 | if (!appender) |
266 | 0 | return FALSE; |
267 | | |
268 | 0 | if (!appender->active) |
269 | 0 | if (!WLog_OpenAppender(log)) |
270 | 0 | return FALSE; |
271 | | |
272 | 0 | if (!appender->WriteDataMessage) |
273 | 0 | return FALSE; |
274 | | |
275 | 0 | EnterCriticalSection(&appender->lock); |
276 | |
|
277 | 0 | if (appender->recursive) |
278 | 0 | status = log_recursion(message->FileName, message->FunctionName, message->LineNumber); |
279 | 0 | else |
280 | 0 | { |
281 | 0 | appender->recursive = TRUE; |
282 | 0 | status = appender->WriteDataMessage(log, appender, message); |
283 | 0 | appender->recursive = FALSE; |
284 | 0 | } |
285 | |
|
286 | 0 | LeaveCriticalSection(&appender->lock); |
287 | 0 | return status; |
288 | 0 | } |
289 | | |
290 | | static BOOL WLog_WriteImage(wLog* log, wLogMessage* message) |
291 | 0 | { |
292 | 0 | BOOL status = 0; |
293 | 0 | wLogAppender* appender = NULL; |
294 | 0 | appender = WLog_GetLogAppender(log); |
295 | |
|
296 | 0 | if (!appender) |
297 | 0 | return FALSE; |
298 | | |
299 | 0 | if (!appender->active) |
300 | 0 | if (!WLog_OpenAppender(log)) |
301 | 0 | return FALSE; |
302 | | |
303 | 0 | if (!appender->WriteImageMessage) |
304 | 0 | return FALSE; |
305 | | |
306 | 0 | EnterCriticalSection(&appender->lock); |
307 | |
|
308 | 0 | if (appender->recursive) |
309 | 0 | status = log_recursion(message->FileName, message->FunctionName, message->LineNumber); |
310 | 0 | else |
311 | 0 | { |
312 | 0 | appender->recursive = TRUE; |
313 | 0 | status = appender->WriteImageMessage(log, appender, message); |
314 | 0 | appender->recursive = FALSE; |
315 | 0 | } |
316 | |
|
317 | 0 | LeaveCriticalSection(&appender->lock); |
318 | 0 | return status; |
319 | 0 | } |
320 | | |
321 | | static BOOL WLog_WritePacket(wLog* log, wLogMessage* message) |
322 | 0 | { |
323 | 0 | BOOL status = 0; |
324 | 0 | wLogAppender* appender = NULL; |
325 | 0 | appender = WLog_GetLogAppender(log); |
326 | |
|
327 | 0 | if (!appender) |
328 | 0 | return FALSE; |
329 | | |
330 | 0 | if (!appender->active) |
331 | 0 | if (!WLog_OpenAppender(log)) |
332 | 0 | return FALSE; |
333 | | |
334 | 0 | if (!appender->WritePacketMessage) |
335 | 0 | return FALSE; |
336 | | |
337 | 0 | EnterCriticalSection(&appender->lock); |
338 | |
|
339 | 0 | if (appender->recursive) |
340 | 0 | status = log_recursion(message->FileName, message->FunctionName, message->LineNumber); |
341 | 0 | else |
342 | 0 | { |
343 | 0 | appender->recursive = TRUE; |
344 | 0 | status = appender->WritePacketMessage(log, appender, message); |
345 | 0 | appender->recursive = FALSE; |
346 | 0 | } |
347 | |
|
348 | 0 | LeaveCriticalSection(&appender->lock); |
349 | 0 | return status; |
350 | 0 | } |
351 | | |
352 | | BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, size_t line, const char* file, |
353 | | const char* function, va_list args) |
354 | 1.01k | { |
355 | 1.01k | BOOL status = FALSE; |
356 | 1.01k | wLogMessage message = { 0 }; |
357 | 1.01k | message.Type = type; |
358 | 1.01k | message.Level = level; |
359 | 1.01k | message.LineNumber = line; |
360 | 1.01k | message.FileName = file; |
361 | 1.01k | message.FunctionName = function; |
362 | | |
363 | 1.01k | switch (type) |
364 | 1.01k | { |
365 | 1.01k | case WLOG_MESSAGE_TEXT: |
366 | 1.01k | message.FormatString = va_arg(args, const char*); |
367 | | |
368 | 1.01k | if (!strchr(message.FormatString, '%')) |
369 | 51 | { |
370 | 51 | message.TextString = message.FormatString; |
371 | 51 | status = WLog_Write(log, &message); |
372 | 51 | } |
373 | 967 | else |
374 | 967 | { |
375 | 967 | char formattedLogMessage[WLOG_MAX_STRING_SIZE] = { 0 }; |
376 | | |
377 | 967 | WINPR_PRAGMA_DIAG_PUSH |
378 | 967 | WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL |
379 | 967 | if (vsnprintf(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message.FormatString, |
380 | 967 | args) < 0) |
381 | 0 | return FALSE; |
382 | 967 | WINPR_PRAGMA_DIAG_POP |
383 | | |
384 | 967 | message.TextString = formattedLogMessage; |
385 | 967 | status = WLog_Write(log, &message); |
386 | 967 | } |
387 | | |
388 | 1.01k | break; |
389 | | |
390 | 1.01k | case WLOG_MESSAGE_DATA: |
391 | 0 | message.Data = va_arg(args, void*); |
392 | 0 | message.Length = va_arg(args, size_t); |
393 | 0 | status = WLog_WriteData(log, &message); |
394 | 0 | break; |
395 | | |
396 | 0 | case WLOG_MESSAGE_IMAGE: |
397 | 0 | message.ImageData = va_arg(args, void*); |
398 | 0 | message.ImageWidth = va_arg(args, size_t); |
399 | 0 | message.ImageHeight = va_arg(args, size_t); |
400 | 0 | message.ImageBpp = va_arg(args, size_t); |
401 | 0 | status = WLog_WriteImage(log, &message); |
402 | 0 | break; |
403 | | |
404 | 0 | case WLOG_MESSAGE_PACKET: |
405 | 0 | message.PacketData = va_arg(args, void*); |
406 | 0 | message.PacketLength = va_arg(args, size_t); |
407 | 0 | message.PacketFlags = va_arg(args, unsigned); |
408 | 0 | status = WLog_WritePacket(log, &message); |
409 | 0 | break; |
410 | | |
411 | 0 | default: |
412 | 0 | break; |
413 | 1.01k | } |
414 | | |
415 | 1.01k | return status; |
416 | 1.01k | } |
417 | | |
418 | | BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, size_t line, const char* file, |
419 | | const char* function, ...) |
420 | 0 | { |
421 | 0 | BOOL status = 0; |
422 | 0 | va_list args; |
423 | 0 | va_start(args, function); |
424 | 0 | status = WLog_PrintMessageVA(log, type, level, line, file, function, args); |
425 | 0 | va_end(args); |
426 | 0 | return status; |
427 | 0 | } |
428 | | |
429 | | DWORD WLog_GetLogLevel(wLog* log) |
430 | 1.02k | { |
431 | 1.02k | if (!log) |
432 | 0 | return WLOG_OFF; |
433 | | |
434 | 1.02k | if (log->FilterLevel <= WLOG_FILTER_NOT_INITIALIZED) |
435 | 0 | log->FilterLevel = WLog_GetFilterLogLevel(log); |
436 | | |
437 | 1.02k | if (log->FilterLevel > WLOG_FILTER_NOT_FILTERED) |
438 | 0 | return (DWORD)log->FilterLevel; |
439 | 1.02k | else if (log->Level == WLOG_LEVEL_INHERIT) |
440 | 2 | log->Level = WLog_GetLogLevel(log->Parent); |
441 | | |
442 | 1.02k | return log->Level; |
443 | 1.02k | } |
444 | | |
445 | | BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level) |
446 | 1.01k | { |
447 | 1.01k | DWORD level = 0; |
448 | | |
449 | 1.01k | if (!_log) |
450 | 0 | return FALSE; |
451 | | |
452 | 1.01k | level = WLog_GetLogLevel(_log); |
453 | | |
454 | 1.01k | if (level == WLOG_OFF) |
455 | 0 | return FALSE; |
456 | | |
457 | 1.01k | return _log_level >= level; |
458 | 1.01k | } |
459 | | |
460 | | BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level) |
461 | 0 | { |
462 | 0 | int lvl = 0; |
463 | |
|
464 | 0 | if (!log || !level) |
465 | 0 | return FALSE; |
466 | | |
467 | 0 | lvl = WLog_ParseLogLevel(level); |
468 | |
|
469 | 0 | if (lvl < 0) |
470 | 0 | return FALSE; |
471 | | |
472 | 0 | return WLog_SetLogLevel(log, (DWORD)lvl); |
473 | 0 | } |
474 | | |
475 | | static BOOL WLog_reset_log_filters(wLog* log) |
476 | 0 | { |
477 | 0 | if (!log) |
478 | 0 | return FALSE; |
479 | | |
480 | 0 | log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED; |
481 | |
|
482 | 0 | for (DWORD x = 0; x < log->ChildrenCount; x++) |
483 | 0 | { |
484 | 0 | wLog* child = log->Children[x]; |
485 | |
|
486 | 0 | if (!WLog_reset_log_filters(child)) |
487 | 0 | return FALSE; |
488 | 0 | } |
489 | | |
490 | 0 | return TRUE; |
491 | 0 | } |
492 | | |
493 | | static BOOL WLog_AddStringLogFilters_int(wLog* root, LPCSTR filter) |
494 | 0 | { |
495 | 0 | LPSTR p = NULL; |
496 | 0 | LPCSTR filterStr = NULL; |
497 | |
|
498 | 0 | if (!filter) |
499 | 0 | return FALSE; |
500 | | |
501 | 0 | DWORD count = 1; |
502 | 0 | LPCSTR cpp = filter; |
503 | |
|
504 | 0 | while ((cpp = strchr(cpp, ',')) != NULL) |
505 | 0 | { |
506 | 0 | count++; |
507 | 0 | cpp++; |
508 | 0 | } |
509 | |
|
510 | 0 | DWORD pos = g_FilterCount; |
511 | 0 | DWORD size = g_FilterCount + count; |
512 | 0 | wLogFilter* tmp = (wLogFilter*)realloc(g_Filters, size * sizeof(wLogFilter)); |
513 | |
|
514 | 0 | if (!tmp) |
515 | 0 | return FALSE; |
516 | | |
517 | 0 | g_Filters = tmp; |
518 | 0 | LPSTR cp = (LPSTR)_strdup(filter); |
519 | |
|
520 | 0 | if (!cp) |
521 | 0 | return FALSE; |
522 | | |
523 | 0 | p = cp; |
524 | 0 | filterStr = cp; |
525 | |
|
526 | 0 | do |
527 | 0 | { |
528 | 0 | p = strchr(p, ','); |
529 | |
|
530 | 0 | if (p) |
531 | 0 | *p = '\0'; |
532 | |
|
533 | 0 | if (pos < size) |
534 | 0 | { |
535 | 0 | if (!WLog_ParseFilter(root, &g_Filters[pos++], filterStr)) |
536 | 0 | { |
537 | 0 | free(cp); |
538 | 0 | return FALSE; |
539 | 0 | } |
540 | 0 | } |
541 | 0 | else |
542 | 0 | break; |
543 | | |
544 | 0 | if (p) |
545 | 0 | { |
546 | 0 | filterStr = p + 1; |
547 | 0 | p++; |
548 | 0 | } |
549 | 0 | } while (p != NULL); |
550 | | |
551 | 0 | g_FilterCount = size; |
552 | 0 | free(cp); |
553 | 0 | return WLog_reset_log_filters(root); |
554 | 0 | } |
555 | | |
556 | | BOOL WLog_AddStringLogFilters(LPCSTR filter) |
557 | 0 | { |
558 | | /* Ensure logger is initialized */ |
559 | 0 | wLog* root = WLog_GetRoot(); |
560 | 0 | return WLog_AddStringLogFilters_int(root, filter); |
561 | 0 | } |
562 | | |
563 | | static BOOL WLog_UpdateInheritLevel(wLog* log, DWORD logLevel) |
564 | 0 | { |
565 | 0 | if (!log) |
566 | 0 | return FALSE; |
567 | | |
568 | 0 | if (log->inherit) |
569 | 0 | { |
570 | 0 | log->Level = logLevel; |
571 | |
|
572 | 0 | for (DWORD x = 0; x < log->ChildrenCount; x++) |
573 | 0 | { |
574 | 0 | wLog* child = log->Children[x]; |
575 | |
|
576 | 0 | if (!WLog_UpdateInheritLevel(child, logLevel)) |
577 | 0 | return FALSE; |
578 | 0 | } |
579 | 0 | } |
580 | | |
581 | 0 | return TRUE; |
582 | 0 | } |
583 | | |
584 | | BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel) |
585 | 0 | { |
586 | 0 | if (!log) |
587 | 0 | return FALSE; |
588 | | |
589 | 0 | if ((logLevel > WLOG_OFF) && (logLevel != WLOG_LEVEL_INHERIT)) |
590 | 0 | logLevel = WLOG_OFF; |
591 | |
|
592 | 0 | log->Level = logLevel; |
593 | 0 | log->inherit = (logLevel == WLOG_LEVEL_INHERIT) ? TRUE : FALSE; |
594 | |
|
595 | 0 | for (DWORD x = 0; x < log->ChildrenCount; x++) |
596 | 0 | { |
597 | 0 | wLog* child = log->Children[x]; |
598 | |
|
599 | 0 | if (!WLog_UpdateInheritLevel(child, logLevel)) |
600 | 0 | return FALSE; |
601 | 0 | } |
602 | | |
603 | 0 | return WLog_reset_log_filters(log); |
604 | 0 | } |
605 | | |
606 | | int WLog_ParseLogLevel(LPCSTR level) |
607 | 0 | { |
608 | 0 | int iLevel = -1; |
609 | |
|
610 | 0 | if (!level) |
611 | 0 | return -1; |
612 | | |
613 | 0 | if (_stricmp(level, "TRACE") == 0) |
614 | 0 | iLevel = WLOG_TRACE; |
615 | 0 | else if (_stricmp(level, "DEBUG") == 0) |
616 | 0 | iLevel = WLOG_DEBUG; |
617 | 0 | else if (_stricmp(level, "INFO") == 0) |
618 | 0 | iLevel = WLOG_INFO; |
619 | 0 | else if (_stricmp(level, "WARN") == 0) |
620 | 0 | iLevel = WLOG_WARN; |
621 | 0 | else if (_stricmp(level, "ERROR") == 0) |
622 | 0 | iLevel = WLOG_ERROR; |
623 | 0 | else if (_stricmp(level, "FATAL") == 0) |
624 | 0 | iLevel = WLOG_FATAL; |
625 | 0 | else if (_stricmp(level, "OFF") == 0) |
626 | 0 | iLevel = WLOG_OFF; |
627 | |
|
628 | 0 | return iLevel; |
629 | 0 | } |
630 | | |
631 | | BOOL WLog_ParseFilter(wLog* root, wLogFilter* filter, LPCSTR name) |
632 | 0 | { |
633 | 0 | const char* pc = NULL; |
634 | 0 | char* p = NULL; |
635 | 0 | char* q = NULL; |
636 | 0 | size_t count = 0; |
637 | 0 | LPSTR names = NULL; |
638 | 0 | int iLevel = 0; |
639 | 0 | count = 1; |
640 | |
|
641 | 0 | WINPR_UNUSED(root); |
642 | |
|
643 | 0 | if (!name) |
644 | 0 | return FALSE; |
645 | | |
646 | 0 | pc = name; |
647 | |
|
648 | 0 | if (pc) |
649 | 0 | { |
650 | 0 | while ((pc = strchr(pc, '.')) != NULL) |
651 | 0 | { |
652 | 0 | count++; |
653 | 0 | pc++; |
654 | 0 | } |
655 | 0 | } |
656 | |
|
657 | 0 | names = _strdup(name); |
658 | |
|
659 | 0 | if (!names) |
660 | 0 | return FALSE; |
661 | | |
662 | 0 | filter->NameCount = count; |
663 | 0 | filter->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR)); |
664 | |
|
665 | 0 | if (!filter->Names) |
666 | 0 | { |
667 | 0 | free(names); |
668 | 0 | filter->NameCount = 0; |
669 | 0 | return FALSE; |
670 | 0 | } |
671 | | |
672 | 0 | filter->Names[count] = NULL; |
673 | 0 | count = 0; |
674 | 0 | p = (char*)names; |
675 | 0 | filter->Names[count++] = p; |
676 | 0 | q = strrchr(p, ':'); |
677 | |
|
678 | 0 | if (!q) |
679 | 0 | { |
680 | 0 | free(names); |
681 | 0 | free((void*)filter->Names); |
682 | 0 | filter->Names = NULL; |
683 | 0 | filter->NameCount = 0; |
684 | 0 | return FALSE; |
685 | 0 | } |
686 | | |
687 | 0 | *q = '\0'; |
688 | 0 | q++; |
689 | 0 | iLevel = WLog_ParseLogLevel(q); |
690 | |
|
691 | 0 | if (iLevel < 0) |
692 | 0 | { |
693 | 0 | free(names); |
694 | 0 | free((void*)filter->Names); |
695 | 0 | filter->Names = NULL; |
696 | 0 | filter->NameCount = 0; |
697 | 0 | return FALSE; |
698 | 0 | } |
699 | | |
700 | 0 | filter->Level = (DWORD)iLevel; |
701 | |
|
702 | 0 | while ((p = strchr(p, '.')) != NULL) |
703 | 0 | { |
704 | 0 | if (count < filter->NameCount) |
705 | 0 | filter->Names[count++] = p + 1; |
706 | |
|
707 | 0 | *p = '\0'; |
708 | 0 | p++; |
709 | 0 | } |
710 | |
|
711 | 0 | return TRUE; |
712 | 0 | } |
713 | | |
714 | | BOOL WLog_ParseFilters(wLog* root) |
715 | 1 | { |
716 | 1 | LPCSTR filter = "WLOG_FILTER"; |
717 | 1 | BOOL res = FALSE; |
718 | 1 | char* env = NULL; |
719 | 1 | DWORD nSize = 0; |
720 | 1 | free(g_Filters); |
721 | 1 | g_Filters = NULL; |
722 | 1 | g_FilterCount = 0; |
723 | 1 | nSize = GetEnvironmentVariableA(filter, NULL, 0); |
724 | | |
725 | 1 | if (nSize < 1) |
726 | 1 | return TRUE; |
727 | | |
728 | 0 | env = (LPSTR)malloc(nSize); |
729 | |
|
730 | 0 | if (!env) |
731 | 0 | return FALSE; |
732 | | |
733 | 0 | if (GetEnvironmentVariableA(filter, env, nSize) == nSize - 1) |
734 | 0 | res = WLog_AddStringLogFilters_int(root, env); |
735 | |
|
736 | 0 | free(env); |
737 | 0 | return res; |
738 | 0 | } |
739 | | |
740 | | LONG WLog_GetFilterLogLevel(wLog* log) |
741 | 3 | { |
742 | 3 | BOOL match = FALSE; |
743 | | |
744 | 3 | if (log->FilterLevel >= 0) |
745 | 0 | return log->FilterLevel; |
746 | | |
747 | 3 | log->FilterLevel = WLOG_FILTER_NOT_FILTERED; |
748 | 3 | for (DWORD i = 0; i < g_FilterCount; i++) |
749 | 0 | { |
750 | 0 | const wLogFilter* filter = &g_Filters[i]; |
751 | 0 | for (DWORD j = 0; j < filter->NameCount; j++) |
752 | 0 | { |
753 | 0 | if (j >= log->NameCount) |
754 | 0 | break; |
755 | | |
756 | 0 | if (_stricmp(filter->Names[j], "*") == 0) |
757 | 0 | { |
758 | 0 | match = TRUE; |
759 | 0 | assert(filter->Level <= INT32_MAX); |
760 | 0 | log->FilterLevel = (LONG)filter->Level; |
761 | 0 | break; |
762 | 0 | } |
763 | | |
764 | 0 | if (_stricmp(filter->Names[j], log->Names[j]) != 0) |
765 | 0 | break; |
766 | | |
767 | 0 | if (j == (log->NameCount - 1)) |
768 | 0 | { |
769 | 0 | match = log->NameCount == filter->NameCount; |
770 | 0 | if (match) |
771 | 0 | { |
772 | 0 | assert(filter->Level <= INT32_MAX); |
773 | 0 | log->FilterLevel = (LONG)filter->Level; |
774 | 0 | } |
775 | 0 | break; |
776 | 0 | } |
777 | 0 | } |
778 | | |
779 | 0 | if (match) |
780 | 0 | break; |
781 | 0 | } |
782 | | |
783 | 3 | return log->FilterLevel; |
784 | 3 | } |
785 | | |
786 | | static BOOL WLog_ParseName(wLog* log, LPCSTR name) |
787 | 3 | { |
788 | 3 | const char* cp = name; |
789 | 3 | char* p = NULL; |
790 | 3 | size_t count = 1; |
791 | 3 | LPSTR names = NULL; |
792 | | |
793 | 7 | while ((cp = strchr(cp, '.')) != NULL) |
794 | 4 | { |
795 | 4 | count++; |
796 | 4 | cp++; |
797 | 4 | } |
798 | | |
799 | 3 | names = _strdup(name); |
800 | | |
801 | 3 | if (!names) |
802 | 0 | return FALSE; |
803 | | |
804 | 3 | log->NameCount = count; |
805 | 3 | log->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR)); |
806 | | |
807 | 3 | if (!log->Names) |
808 | 0 | { |
809 | 0 | free(names); |
810 | 0 | return FALSE; |
811 | 0 | } |
812 | | |
813 | 3 | log->Names[count] = NULL; |
814 | 3 | count = 0; |
815 | 3 | p = (char*)names; |
816 | 3 | log->Names[count++] = p; |
817 | | |
818 | 7 | while ((p = strchr(p, '.')) != NULL) |
819 | 4 | { |
820 | 4 | if (count < log->NameCount) |
821 | 4 | log->Names[count++] = p + 1; |
822 | | |
823 | 4 | *p = '\0'; |
824 | 4 | p++; |
825 | 4 | } |
826 | | |
827 | 3 | return TRUE; |
828 | 3 | } |
829 | | |
830 | | wLog* WLog_New(LPCSTR name, wLog* rootLogger) |
831 | 3 | { |
832 | 3 | wLog* log = NULL; |
833 | 3 | char* env = NULL; |
834 | 3 | DWORD nSize = 0; |
835 | 3 | int iLevel = 0; |
836 | 3 | log = (wLog*)calloc(1, sizeof(wLog)); |
837 | | |
838 | 3 | if (!log) |
839 | 0 | return NULL; |
840 | | |
841 | 3 | log->Name = _strdup(name); |
842 | | |
843 | 3 | if (!log->Name) |
844 | 0 | goto out_fail; |
845 | | |
846 | 3 | if (!WLog_ParseName(log, name)) |
847 | 0 | goto out_fail; |
848 | | |
849 | 3 | log->Parent = rootLogger; |
850 | 3 | log->ChildrenCount = 0; |
851 | 3 | log->ChildrenSize = 16; |
852 | 3 | log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED; |
853 | | |
854 | 3 | if (!(log->Children = (wLog**)calloc(log->ChildrenSize, sizeof(wLog*)))) |
855 | 0 | goto out_fail; |
856 | | |
857 | 3 | log->Appender = NULL; |
858 | | |
859 | 3 | if (rootLogger) |
860 | 2 | { |
861 | 2 | log->Level = WLOG_LEVEL_INHERIT; |
862 | 2 | log->inherit = TRUE; |
863 | 2 | } |
864 | 1 | else |
865 | 1 | { |
866 | 1 | LPCSTR level = "WLOG_LEVEL"; |
867 | 1 | log->Level = WLOG_INFO; |
868 | 1 | nSize = GetEnvironmentVariableA(level, NULL, 0); |
869 | | |
870 | 1 | if (nSize) |
871 | 0 | { |
872 | 0 | env = (LPSTR)malloc(nSize); |
873 | |
|
874 | 0 | if (!env) |
875 | 0 | goto out_fail; |
876 | | |
877 | 0 | if (GetEnvironmentVariableA(level, env, nSize) != nSize - 1) |
878 | 0 | { |
879 | 0 | (void)fprintf(stderr, "%s environment variable changed in my back !\n", level); |
880 | 0 | free(env); |
881 | 0 | goto out_fail; |
882 | 0 | } |
883 | | |
884 | 0 | iLevel = WLog_ParseLogLevel(env); |
885 | 0 | free(env); |
886 | |
|
887 | 0 | if (iLevel >= 0) |
888 | 0 | { |
889 | 0 | if (!WLog_SetLogLevel(log, (DWORD)iLevel)) |
890 | 0 | goto out_fail; |
891 | 0 | } |
892 | 0 | } |
893 | 1 | } |
894 | | |
895 | 3 | iLevel = WLog_GetFilterLogLevel(log); |
896 | | |
897 | 3 | if (iLevel >= 0) |
898 | 0 | { |
899 | 0 | if (!WLog_SetLogLevel(log, (DWORD)iLevel)) |
900 | 0 | goto out_fail; |
901 | 0 | } |
902 | | |
903 | 3 | InitializeCriticalSectionAndSpinCount(&log->lock, 4000); |
904 | | |
905 | 3 | return log; |
906 | 0 | out_fail: |
907 | 0 | WLog_Free(log); |
908 | 0 | return NULL; |
909 | 3 | } |
910 | | |
911 | | void WLog_Free(wLog* log) |
912 | 3 | { |
913 | 3 | if (log) |
914 | 3 | { |
915 | 3 | if (log->Appender) |
916 | 1 | { |
917 | 1 | WLog_Appender_Free(log, log->Appender); |
918 | 1 | log->Appender = NULL; |
919 | 1 | } |
920 | | |
921 | 3 | free(log->Name); |
922 | | |
923 | | /* The first element in this array is allocated, the rest are indices into this variable */ |
924 | 3 | if (log->Names) |
925 | 3 | free(log->Names[0]); |
926 | 3 | free((void*)log->Names); |
927 | 3 | free((void*)log->Children); |
928 | 3 | DeleteCriticalSection(&log->lock); |
929 | 3 | free(log); |
930 | 3 | } |
931 | 3 | } |
932 | | |
933 | | wLog* WLog_GetRoot(void) |
934 | 2 | { |
935 | 2 | if (!InitOnceExecuteOnce(&g_WLogInitialized, WLog_InitializeRoot, NULL, NULL)) |
936 | 0 | return NULL; |
937 | | |
938 | 2 | return g_RootLog; |
939 | 2 | } |
940 | | |
941 | | static BOOL WLog_AddChild(wLog* parent, wLog* child) |
942 | 2 | { |
943 | 2 | BOOL status = FALSE; |
944 | | |
945 | 2 | WLog_Lock(parent); |
946 | | |
947 | 2 | if (parent->ChildrenCount >= parent->ChildrenSize) |
948 | 0 | { |
949 | 0 | wLog** tmp = NULL; |
950 | 0 | parent->ChildrenSize *= 2; |
951 | |
|
952 | 0 | if (!parent->ChildrenSize) |
953 | 0 | { |
954 | 0 | free((void*)parent->Children); |
955 | 0 | parent->Children = NULL; |
956 | 0 | } |
957 | 0 | else |
958 | 0 | { |
959 | 0 | tmp = (wLog**)realloc((void*)parent->Children, sizeof(wLog*) * parent->ChildrenSize); |
960 | |
|
961 | 0 | if (!tmp) |
962 | 0 | { |
963 | 0 | free((void*)parent->Children); |
964 | 0 | parent->Children = NULL; |
965 | 0 | goto exit; |
966 | 0 | } |
967 | | |
968 | 0 | parent->Children = tmp; |
969 | 0 | } |
970 | 0 | } |
971 | | |
972 | 2 | if (!parent->Children) |
973 | 0 | goto exit; |
974 | | |
975 | 2 | parent->Children[parent->ChildrenCount++] = child; |
976 | 2 | child->Parent = parent; |
977 | | |
978 | 2 | WLog_Unlock(parent); |
979 | | |
980 | 2 | status = TRUE; |
981 | 2 | exit: |
982 | 2 | return status; |
983 | 2 | } |
984 | | |
985 | | static wLog* WLog_FindChild(wLog* root, LPCSTR name) |
986 | 2 | { |
987 | 2 | wLog* child = NULL; |
988 | 2 | BOOL found = FALSE; |
989 | | |
990 | 2 | if (!root) |
991 | 0 | return NULL; |
992 | | |
993 | 2 | WLog_Lock(root); |
994 | | |
995 | 3 | for (DWORD index = 0; index < root->ChildrenCount; index++) |
996 | 1 | { |
997 | 1 | child = root->Children[index]; |
998 | | |
999 | 1 | if (strcmp(child->Name, name) == 0) |
1000 | 0 | { |
1001 | 0 | found = TRUE; |
1002 | 0 | break; |
1003 | 0 | } |
1004 | 1 | } |
1005 | | |
1006 | 2 | WLog_Unlock(root); |
1007 | | |
1008 | 2 | return (found) ? child : NULL; |
1009 | 2 | } |
1010 | | |
1011 | | static wLog* WLog_Get_int(wLog* root, LPCSTR name) |
1012 | 2 | { |
1013 | 2 | wLog* log = NULL; |
1014 | | |
1015 | 2 | if (!(log = WLog_FindChild(root, name))) |
1016 | 2 | { |
1017 | 2 | if (!root) |
1018 | 0 | return NULL; |
1019 | | |
1020 | 2 | if (!(log = WLog_New(name, root))) |
1021 | 0 | return NULL; |
1022 | | |
1023 | 2 | if (!WLog_AddChild(root, log)) |
1024 | 0 | { |
1025 | 0 | WLog_Free(log); |
1026 | 0 | return NULL; |
1027 | 0 | } |
1028 | 2 | } |
1029 | | |
1030 | 2 | return log; |
1031 | 2 | } |
1032 | | |
1033 | | wLog* WLog_Get(LPCSTR name) |
1034 | 2 | { |
1035 | 2 | wLog* root = WLog_GetRoot(); |
1036 | 2 | return WLog_Get_int(root, name); |
1037 | 2 | } |
1038 | | |
1039 | | #if defined(WITH_WINPR_DEPRECATED) |
1040 | | BOOL WLog_Init(void) |
1041 | | { |
1042 | | return WLog_GetRoot() != NULL; |
1043 | | } |
1044 | | |
1045 | | BOOL WLog_Uninit(void) |
1046 | | { |
1047 | | wLog* root = g_RootLog; |
1048 | | |
1049 | | if (!root) |
1050 | | return FALSE; |
1051 | | |
1052 | | WLog_Lock(root); |
1053 | | |
1054 | | for (DWORD index = 0; index < root->ChildrenCount; index++) |
1055 | | { |
1056 | | wLog* child = root->Children[index]; |
1057 | | WLog_Free(child); |
1058 | | } |
1059 | | |
1060 | | WLog_Unlock(root); |
1061 | | |
1062 | | WLog_Free(root); |
1063 | | g_RootLog = NULL; |
1064 | | |
1065 | | return TRUE; |
1066 | | } |
1067 | | #endif |
1068 | | |
1069 | | BOOL WLog_SetContext(wLog* log, const char* (*fkt)(void*), void* context) |
1070 | 0 | { |
1071 | 0 | WINPR_ASSERT(log); |
1072 | | |
1073 | 0 | log->custom = fkt; |
1074 | 0 | log->context = context; |
1075 | 0 | return TRUE; |
1076 | 0 | } |