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