Coverage Report

Created: 2025-11-24 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
18.5M
#define WLOG_FILTER_NOT_FILTERED (-1)
51
18.5M
#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
5
{
83
5
  wLog* child = NULL;
84
5
  wLog* root = g_RootLog;
85
86
5
  if (!root)
87
0
    return;
88
89
78
  for (DWORD index = 0; index < root->ChildrenCount; index++)
90
73
  {
91
73
    child = root->Children[index];
92
73
    WLog_Free(child);
93
73
  }
94
95
5
  WLog_Free(root);
96
5
  g_RootLog = NULL;
97
5
}
98
99
static void WLog_Lock(wLog* log)
100
940k
{
101
940k
  WINPR_ASSERT(log);
102
940k
  EnterCriticalSection(&log->lock);
103
940k
}
104
105
static void WLog_Unlock(wLog* log)
106
940k
{
107
940k
  WINPR_ASSERT(log);
108
940k
  LeaveCriticalSection(&log->lock);
109
940k
}
110
111
static BOOL CALLBACK WLog_InitializeRoot(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
112
5
{
113
5
  char* env = NULL;
114
5
  DWORD nSize = 0;
115
5
  DWORD logAppenderType = 0;
116
5
  LPCSTR appender = "WLOG_APPENDER";
117
118
5
  WINPR_UNUSED(InitOnce);
119
5
  WINPR_UNUSED(Parameter);
120
5
  WINPR_UNUSED(Context);
121
122
5
  if (!(g_RootLog = WLog_New("", NULL)))
123
0
    return FALSE;
124
125
5
  g_RootLog->IsRoot = TRUE;
126
5
  logAppenderType = WLOG_APPENDER_CONSOLE;
127
5
  nSize = GetEnvironmentVariableA(appender, NULL, 0);
128
129
5
  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
5
  if (!WLog_SetLogAppenderType(g_RootLog, logAppenderType))
167
0
    goto fail;
168
169
5
  if (!WLog_ParseFilters(g_RootLog))
170
0
    goto fail;
171
172
5
  (void)atexit(WLog_Uninit_);
173
174
5
  return TRUE;
175
0
fail:
176
0
  WLog_Uninit_();
177
0
  return FALSE;
178
5
}
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
10.8M
{
232
10.8M
  BOOL status = FALSE;
233
10.8M
  wLogAppender* appender = WLog_GetLogAppender(log);
234
235
10.8M
  if (!appender)
236
0
    return FALSE;
237
238
10.8M
  if (!appender->active)
239
4
    if (!WLog_OpenAppender(log))
240
0
      return FALSE;
241
242
10.8M
  EnterCriticalSection(&appender->lock);
243
244
10.8M
  if (appender->WriteMessage)
245
10.8M
  {
246
10.8M
    if (appender->recursive)
247
0
      status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
248
10.8M
    else
249
10.8M
    {
250
10.8M
      appender->recursive = TRUE;
251
10.8M
      status = appender->WriteMessage(log, appender, message);
252
10.8M
      appender->recursive = FALSE;
253
10.8M
    }
254
10.8M
  }
255
256
10.8M
  LeaveCriticalSection(&appender->lock);
257
10.8M
  return status;
258
10.8M
}
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
10.8M
{
354
10.8M
  assert(cmessage);
355
356
10.8M
  char formattedLogMessage[WLOG_MAX_STRING_SIZE] = { 0 };
357
10.8M
  wLogMessage message = *cmessage;
358
10.8M
  message.TextString = formattedLogMessage;
359
360
10.8M
  WINPR_PRAGMA_DIAG_PUSH
361
10.8M
  WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
362
10.8M
  if (vsnprintf(formattedLogMessage, ARRAYSIZE(formattedLogMessage) - 1, cmessage->FormatString,
363
10.8M
                args) < 0)
364
0
    return FALSE;
365
10.8M
  WINPR_PRAGMA_DIAG_POP
366
367
10.8M
  return WLog_Write(log, &message);
368
10.8M
}
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
6.15M
{
373
6.15M
  BOOL status = FALSE;
374
6.15M
  wLogMessage message = { 0 };
375
6.15M
  message.Type = type;
376
6.15M
  message.Level = level;
377
6.15M
  message.LineNumber = line;
378
6.15M
  message.FileName = file;
379
6.15M
  message.FunctionName = function;
380
381
6.15M
  switch (type)
382
6.15M
  {
383
6.15M
    case WLOG_MESSAGE_TEXT:
384
6.15M
      message.FormatString = va_arg(args, const char*);
385
386
6.15M
      status = WLog_PrintTextMessageInternal(log, &message, args);
387
6.15M
      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
6.15M
  }
413
414
6.15M
  return status;
415
6.15M
}
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
4.68M
{
420
4.68M
  wLogMessage message = { 0 };
421
4.68M
  message.Type = WLOG_MESSAGE_TEXT;
422
4.68M
  message.Level = level;
423
4.68M
  message.LineNumber = line;
424
4.68M
  message.FileName = file;
425
4.68M
  message.FunctionName = function;
426
427
4.68M
  message.FormatString = fmt;
428
429
4.68M
  return WLog_PrintTextMessageInternal(log, &message, args);
430
4.68M
}
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
4.68M
{
446
4.68M
  BOOL status = 0;
447
4.68M
  va_list args;
448
4.68M
  va_start(args, fmt);
449
4.68M
  status = WLog_PrintTextMessageVA(log, level, line, file, function, fmt, args);
450
4.68M
  va_end(args);
451
4.68M
  return status;
452
4.68M
}
453
454
DWORD WLog_GetLogLevel(wLog* log)
455
18.5M
{
456
18.5M
  if (!log)
457
0
    return WLOG_OFF;
458
459
18.5M
  if (log->FilterLevel <= WLOG_FILTER_NOT_INITIALIZED)
460
0
    log->FilterLevel = WLog_GetFilterLogLevel(log);
461
462
18.5M
  if (log->FilterLevel > WLOG_FILTER_NOT_FILTERED)
463
0
    return (DWORD)log->FilterLevel;
464
18.5M
  else if (log->Level == WLOG_LEVEL_INHERIT)
465
62
    log->Level = WLog_GetLogLevel(log->Parent);
466
467
18.5M
  return log->Level;
468
18.5M
}
469
470
BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level)
471
18.5M
{
472
18.5M
  DWORD level = 0;
473
474
18.5M
  if (!_log)
475
0
    return FALSE;
476
477
18.5M
  level = WLog_GetLogLevel(_log);
478
479
18.5M
  if (level == WLOG_OFF)
480
0
    return FALSE;
481
482
18.5M
  return _log_level >= level;
483
18.5M
}
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
5
{
741
5
  LPCSTR filter = "WLOG_FILTER";
742
5
  BOOL res = FALSE;
743
5
  char* env = NULL;
744
5
  DWORD nSize = 0;
745
5
  free(g_Filters);
746
5
  g_Filters = NULL;
747
5
  g_FilterCount = 0;
748
5
  nSize = GetEnvironmentVariableA(filter, NULL, 0);
749
750
5
  if (nSize < 1)
751
5
    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
78
{
767
78
  BOOL match = FALSE;
768
769
78
  if (log->FilterLevel >= 0)
770
0
    return log->FilterLevel;
771
772
78
  log->FilterLevel = WLOG_FILTER_NOT_FILTERED;
773
78
  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
78
  return log->FilterLevel;
809
78
}
810
811
static BOOL WLog_ParseName(wLog* log, LPCSTR name)
812
78
{
813
78
  const char* cp = name;
814
78
  char* p = NULL;
815
78
  size_t count = 1;
816
78
  LPSTR names = NULL;
817
818
273
  while ((cp = strchr(cp, '.')) != NULL)
819
195
  {
820
195
    count++;
821
195
    cp++;
822
195
  }
823
824
78
  names = _strdup(name);
825
826
78
  if (!names)
827
0
    return FALSE;
828
829
78
  log->NameCount = count;
830
78
  log->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR));
831
832
78
  if (!log->Names)
833
0
  {
834
0
    free(names);
835
0
    return FALSE;
836
0
  }
837
838
78
  log->Names[count] = NULL;
839
78
  count = 0;
840
78
  p = (char*)names;
841
78
  log->Names[count++] = p;
842
843
273
  while ((p = strchr(p, '.')) != NULL)
844
195
  {
845
195
    if (count < log->NameCount)
846
195
      log->Names[count++] = p + 1;
847
848
195
    *p = '\0';
849
195
    p++;
850
195
  }
851
852
78
  return TRUE;
853
78
}
854
855
wLog* WLog_New(LPCSTR name, wLog* rootLogger)
856
78
{
857
78
  wLog* log = NULL;
858
78
  char* env = NULL;
859
78
  DWORD nSize = 0;
860
78
  int iLevel = 0;
861
78
  log = (wLog*)calloc(1, sizeof(wLog));
862
863
78
  if (!log)
864
0
    return NULL;
865
866
78
  log->Name = _strdup(name);
867
868
78
  if (!log->Name)
869
0
    goto out_fail;
870
871
78
  if (!WLog_ParseName(log, name))
872
0
    goto out_fail;
873
874
78
  log->Parent = rootLogger;
875
78
  log->ChildrenCount = 0;
876
78
  log->ChildrenSize = 16;
877
78
  log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED;
878
879
78
  if (!(log->Children = (wLog**)calloc(log->ChildrenSize, sizeof(wLog*))))
880
0
    goto out_fail;
881
882
78
  log->Appender = NULL;
883
884
78
  if (rootLogger)
885
73
  {
886
73
    log->Level = WLOG_LEVEL_INHERIT;
887
73
    log->inherit = TRUE;
888
73
  }
889
5
  else
890
5
  {
891
5
    LPCSTR level = "WLOG_LEVEL";
892
5
    log->Level = WLOG_INFO;
893
5
    nSize = GetEnvironmentVariableA(level, NULL, 0);
894
895
5
    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
5
  }
919
920
78
  iLevel = WLog_GetFilterLogLevel(log);
921
922
78
  if (iLevel >= 0)
923
0
  {
924
0
    if (!WLog_SetLogLevel(log, (DWORD)iLevel))
925
0
      goto out_fail;
926
0
  }
927
928
78
  InitializeCriticalSectionAndSpinCount(&log->lock, 4000);
929
930
78
  return log;
931
0
out_fail:
932
0
  WLog_Free(log);
933
0
  return NULL;
934
78
}
935
936
void WLog_Free(wLog* log)
937
78
{
938
78
  if (log)
939
78
  {
940
78
    if (log->Appender)
941
5
    {
942
5
      WLog_Appender_Free(log, log->Appender);
943
5
      log->Appender = NULL;
944
5
    }
945
946
78
    free(log->Name);
947
948
    /* The first element in this array is allocated, the rest are indices into this variable */
949
78
    if (log->Names)
950
78
      free(log->Names[0]);
951
78
    free((void*)log->Names);
952
78
    free((void*)log->Children);
953
78
    DeleteCriticalSection(&log->lock);
954
78
    free(log);
955
78
  }
956
78
}
957
958
wLog* WLog_GetRoot(void)
959
940k
{
960
940k
  if (!InitOnceExecuteOnce(&g_WLogInitialized, WLog_InitializeRoot, NULL, NULL))
961
0
    return NULL;
962
963
940k
  return g_RootLog;
964
940k
}
965
966
static BOOL WLog_AddChild(wLog* parent, wLog* child)
967
73
{
968
73
  BOOL status = FALSE;
969
970
73
  WLog_Lock(parent);
971
972
73
  if (parent->ChildrenCount >= parent->ChildrenSize)
973
2
  {
974
2
    wLog** tmp = NULL;
975
2
    parent->ChildrenSize *= 2;
976
977
2
    if (!parent->ChildrenSize)
978
0
    {
979
0
      free((void*)parent->Children);
980
0
      parent->Children = NULL;
981
0
    }
982
2
    else
983
2
    {
984
2
      tmp = (wLog**)realloc((void*)parent->Children, sizeof(wLog*) * parent->ChildrenSize);
985
986
2
      if (!tmp)
987
0
      {
988
0
        free((void*)parent->Children);
989
0
        parent->Children = NULL;
990
0
        goto exit;
991
0
      }
992
993
2
      parent->Children = tmp;
994
2
    }
995
2
  }
996
997
73
  if (!parent->Children)
998
0
    goto exit;
999
1000
73
  parent->Children[parent->ChildrenCount++] = child;
1001
73
  child->Parent = parent;
1002
1003
73
  WLog_Unlock(parent);
1004
1005
73
  status = TRUE;
1006
73
exit:
1007
73
  return status;
1008
73
}
1009
1010
static wLog* WLog_FindChild(wLog* root, LPCSTR name)
1011
940k
{
1012
940k
  wLog* child = NULL;
1013
940k
  BOOL found = FALSE;
1014
1015
940k
  if (!root)
1016
0
    return NULL;
1017
1018
940k
  WLog_Lock(root);
1019
1020
10.4M
  for (DWORD index = 0; index < root->ChildrenCount; index++)
1021
10.4M
  {
1022
10.4M
    child = root->Children[index];
1023
1024
10.4M
    if (strcmp(child->Name, name) == 0)
1025
940k
    {
1026
940k
      found = TRUE;
1027
940k
      break;
1028
940k
    }
1029
10.4M
  }
1030
1031
940k
  WLog_Unlock(root);
1032
1033
940k
  return (found) ? child : NULL;
1034
940k
}
1035
1036
static wLog* WLog_Get_int(wLog* root, LPCSTR name)
1037
940k
{
1038
940k
  wLog* log = NULL;
1039
1040
940k
  if (!(log = WLog_FindChild(root, name)))
1041
73
  {
1042
73
    if (!root)
1043
0
      return NULL;
1044
1045
73
    if (!(log = WLog_New(name, root)))
1046
0
      return NULL;
1047
1048
73
    if (!WLog_AddChild(root, log))
1049
0
    {
1050
0
      WLog_Free(log);
1051
0
      return NULL;
1052
0
    }
1053
73
  }
1054
1055
940k
  return log;
1056
940k
}
1057
1058
wLog* WLog_Get(LPCSTR name)
1059
940k
{
1060
940k
  wLog* root = WLog_GetRoot();
1061
940k
  return WLog_Get_int(root, name);
1062
940k
}
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
17.6k
{
1096
17.6k
  WINPR_ASSERT(log);
1097
1098
17.6k
  log->custom = fkt;
1099
17.6k
  log->context = context;
1100
  return TRUE;
1101
17.6k
}