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