Coverage Report

Created: 2025-07-01 06:46

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