Coverage Report

Created: 2026-03-04 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/utils/wlog/Layout.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 <stdio.h>
23
#include <string.h>
24
#include <stdarg.h>
25
26
#include <winpr/crt.h>
27
#include <winpr/assert.h>
28
#include <winpr/print.h>
29
#include <winpr/sysinfo.h>
30
#include <winpr/environment.h>
31
32
#include "wlog.h"
33
34
#include "Layout.h"
35
36
#if defined __linux__ && !defined ANDROID
37
#include <unistd.h>
38
#include <sys/syscall.h>
39
#endif
40
41
#ifndef MIN
42
714k
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
43
#endif
44
45
struct format_option_recurse;
46
47
struct format_tid_arg
48
{
49
  char tid[32];
50
};
51
52
struct format_option
53
{
54
  const char* fmt;
55
  size_t fmtlen;
56
  const char* replace;
57
  size_t replacelen;
58
  const char* (*fkt)(void*);
59
  union
60
  {
61
    void* pv;
62
    const void* cpv;
63
    size_t s;
64
  } arg;
65
  const char* (*ext)(const struct format_option* opt, const char* str, size_t* preplacelen,
66
                     size_t* pskiplen);
67
  struct format_option_recurse* recurse;
68
};
69
70
struct format_option_recurse
71
{
72
  struct format_option* options;
73
  size_t nroptions;
74
  wLog* log;
75
  wLogLayout* layout;
76
  const wLogMessage* message;
77
  char buffer[WLOG_MAX_PREFIX_SIZE];
78
};
79
80
/**
81
 * Log Layout
82
 */
83
WINPR_ATTR_FORMAT_ARG(3, 0)
84
static void WLog_PrintMessagePrefixVA(char* prefix, size_t prefixlen,
85
                                      WINPR_FORMAT_ARG const char* format, va_list args)
86
6.64M
{
87
6.64M
  (void)vsnprintf(prefix, prefixlen, format, args);
88
6.64M
}
89
90
WINPR_ATTR_FORMAT_ARG(3, 4)
91
static void WLog_PrintMessagePrefix(char* prefix, size_t prefixlen,
92
                                    WINPR_FORMAT_ARG const char* format, ...)
93
6.64M
{
94
6.64M
  va_list args = WINPR_C_ARRAY_INIT;
95
6.64M
  va_start(args, format);
96
6.64M
  WLog_PrintMessagePrefixVA(prefix, prefixlen, format, args);
97
6.64M
  va_end(args);
98
6.64M
}
99
100
static const char* get_tid(void* arg)
101
6.64M
{
102
6.64M
  struct format_tid_arg* targ = arg;
103
6.64M
  WINPR_ASSERT(targ);
104
105
6.64M
  size_t tid = 0;
106
6.64M
#if defined __linux__ && !defined ANDROID
107
  /* On Linux we prefer to see the LWP id */
108
6.64M
  tid = (size_t)syscall(SYS_gettid);
109
#else
110
  tid = (size_t)GetCurrentThreadId();
111
#endif
112
6.64M
  (void)_snprintf(targ->tid, sizeof(targ->tid), "%08" PRIxz, tid);
113
6.64M
  return targ->tid;
114
6.64M
}
115
116
static BOOL log_invalid_fmt(const char* what)
117
0
{
118
0
  (void)fprintf(stderr, "Invalid format string '%s'\n", what);
119
0
  return FALSE;
120
0
}
121
122
static BOOL check_and_log_format_size(char* format, size_t size, size_t index, size_t add)
123
209M
{
124
  /* format string must be '\0' terminated, so abort at size - 1 */
125
209M
  if (index + add + 1 >= size)
126
0
  {
127
0
    (void)fprintf(stderr,
128
0
                  "Format string too long ['%s', max %" PRIuz ", used %" PRIuz
129
0
                  ", adding %" PRIuz "]\n",
130
0
                  format, size, index, add);
131
0
    return FALSE;
132
0
  }
133
209M
  return TRUE;
134
209M
}
135
136
static int opt_compare_fn(const void* a, const void* b)
137
827M
{
138
827M
  const char* what = a;
139
827M
  const struct format_option* opt = b;
140
827M
  if (!opt)
141
0
    return -1;
142
827M
  return strncmp(what, opt->fmt, opt->fmtlen);
143
827M
}
144
145
static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse,
146
                                  char* format, size_t formatlen);
147
148
static const char* skip_if_null(const struct format_option* opt, const char* fmt,
149
                                size_t* preplacelen, size_t* pskiplen)
150
6.64M
{
151
6.64M
  WINPR_ASSERT(opt);
152
6.64M
  WINPR_ASSERT(fmt);
153
6.64M
  WINPR_ASSERT(preplacelen);
154
6.64M
  WINPR_ASSERT(pskiplen);
155
156
6.64M
  *preplacelen = 0;
157
6.64M
  *pskiplen = 0;
158
159
6.64M
  const char* str = &fmt[opt->fmtlen]; /* Skip first %{ from string */
160
6.64M
  const char* end = strstr(str, opt->replace);
161
6.64M
  if (!end)
162
0
    return nullptr;
163
6.64M
  *pskiplen = WINPR_ASSERTING_INT_CAST(size_t, end - fmt) + opt->replacelen;
164
165
6.64M
  if (!opt->arg.cpv)
166
5.93M
    return nullptr;
167
168
714k
  const size_t replacelen = WINPR_ASSERTING_INT_CAST(size_t, end - str);
169
170
714k
  char buffer[WLOG_MAX_PREFIX_SIZE] = WINPR_C_ARRAY_INIT;
171
714k
  memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1));
172
173
714k
  if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer,
174
714k
                             ARRAYSIZE(opt->recurse->buffer)))
175
0
    return nullptr;
176
177
714k
  *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer));
178
714k
  return opt->recurse->buffer;
179
714k
}
180
181
static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse,
182
                                  char* format, size_t formatlen)
183
7.36M
{
184
7.36M
  WINPR_ASSERT(FormatString);
185
7.36M
  WINPR_ASSERT(recurse);
186
187
7.36M
  size_t index = 0;
188
189
215M
  while (*FormatString)
190
208M
  {
191
208M
    const struct format_option* opt =
192
208M
        bsearch(FormatString, recurse->options, recurse->nroptions,
193
208M
                sizeof(struct format_option), opt_compare_fn);
194
208M
    if (opt)
195
67.1M
    {
196
67.1M
      size_t replacelen = opt->replacelen;
197
67.1M
      size_t fmtlen = opt->fmtlen;
198
67.1M
      const char* replace = opt->replace;
199
67.1M
      const void* arg = opt->arg.cpv;
200
201
67.1M
      if (opt->ext)
202
6.64M
        replace = opt->ext(opt, FormatString, &replacelen, &fmtlen);
203
67.1M
      if (opt->fkt)
204
6.64M
        arg = opt->fkt(opt->arg.pv);
205
206
67.1M
      if (replace && (replacelen > 0))
207
61.2M
      {
208
61.2M
        WINPR_PRAGMA_DIAG_PUSH
209
61.2M
        WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
210
61.2M
        const int rc = _snprintf(&format[index], formatlen - index, replace, arg);
211
61.2M
        WINPR_PRAGMA_DIAG_POP
212
61.2M
        if (rc < 0)
213
0
          return FALSE;
214
61.2M
        if (!check_and_log_format_size(format, formatlen, index,
215
61.2M
                                       WINPR_ASSERTING_INT_CAST(size_t, rc)))
216
0
          return FALSE;
217
61.2M
        index += WINPR_ASSERTING_INT_CAST(size_t, rc);
218
61.2M
      }
219
67.1M
      FormatString += fmtlen;
220
67.1M
    }
221
141M
    else
222
141M
    {
223
      /* Unknown format string */
224
141M
      if (*FormatString == '%')
225
0
        return log_invalid_fmt(FormatString);
226
227
141M
      if (!check_and_log_format_size(format, formatlen, index, 1))
228
0
        return FALSE;
229
141M
      format[index++] = *FormatString++;
230
141M
    }
231
208M
  }
232
233
7.36M
  return check_and_log_format_size(format, formatlen, index, 0);
234
7.36M
}
235
236
BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, const wLogMessage* message,
237
                                  char* prefix, size_t prefixlen)
238
6.64M
{
239
6.64M
  char format[WLOG_MAX_PREFIX_SIZE] = WINPR_C_ARRAY_INIT;
240
241
6.64M
  WINPR_ASSERT(layout);
242
6.64M
  WINPR_ASSERT(message);
243
6.64M
  WINPR_ASSERT(prefix);
244
245
6.64M
  struct format_tid_arg targ = WINPR_C_ARRAY_INIT;
246
247
6.64M
  SYSTEMTIME localTime = WINPR_C_ARRAY_INIT;
248
6.64M
  GetLocalTime(&localTime);
249
250
6.64M
  struct format_option_recurse recurse = {
251
6.64M
    .options = nullptr, .nroptions = 0, .log = log, .layout = layout, .message = message
252
6.64M
  };
253
254
226M
#define ENTRY(x) x, sizeof(x) - 1
255
6.64M
  struct format_option options[] = {
256
6.64M
    { ENTRY("%ctx"),
257
6.64M
      ENTRY("%s"),
258
6.64M
      log->custom,
259
6.64M
      { .pv = log->context },
260
6.64M
      nullptr,
261
6.64M
      &recurse }, /* log context */
262
6.64M
    { ENTRY("%dw"),
263
6.64M
      ENTRY("%u"),
264
6.64M
      nullptr,
265
6.64M
      { .s = localTime.wDayOfWeek },
266
6.64M
      nullptr,
267
6.64M
      &recurse }, /* day of week */
268
6.64M
    { ENTRY("%dy"),
269
6.64M
      ENTRY("%u"),
270
6.64M
      nullptr,
271
6.64M
      { .s = localTime.wDay },
272
6.64M
      nullptr,
273
6.64M
      &recurse }, /* day of year
274
                   */
275
6.64M
    { ENTRY("%fl"),
276
6.64M
      ENTRY("%s"),
277
6.64M
      nullptr,
278
6.64M
      { .cpv = message->FileName },
279
6.64M
      nullptr,
280
6.64M
      &recurse }, /* file
281
                   */
282
6.64M
    { ENTRY("%fn"),
283
6.64M
      ENTRY("%s"),
284
6.64M
      nullptr,
285
6.64M
      { .cpv = message->FunctionName },
286
6.64M
      nullptr,
287
6.64M
      &recurse }, /* function
288
                   */
289
6.64M
    { ENTRY("%hr"),
290
6.64M
      ENTRY("%02u"),
291
6.64M
      nullptr,
292
6.64M
      { .s = localTime.wHour },
293
6.64M
      nullptr,
294
6.64M
      &recurse }, /* hours
295
                   */
296
6.64M
    { ENTRY("%ln"),
297
6.64M
      ENTRY("%" PRIuz),
298
6.64M
      nullptr,
299
6.64M
      { .s = message->LineNumber },
300
6.64M
      nullptr,
301
6.64M
      &recurse }, /* line number */
302
6.64M
    { ENTRY("%lv"),
303
6.64M
      ENTRY("%s"),
304
6.64M
      nullptr,
305
6.64M
      { .cpv = WLOG_LEVELS[message->Level] },
306
6.64M
      nullptr,
307
6.64M
      &recurse }, /* log level */
308
6.64M
    { ENTRY("%mi"),
309
6.64M
      ENTRY("%02u"),
310
6.64M
      nullptr,
311
6.64M
      { .s = localTime.wMinute },
312
6.64M
      nullptr,
313
6.64M
      &recurse }, /* minutes
314
                   */
315
6.64M
    { ENTRY("%ml"),
316
6.64M
      ENTRY("%03u"),
317
6.64M
      nullptr,
318
6.64M
      { .s = localTime.wMilliseconds },
319
6.64M
      nullptr,
320
6.64M
      &recurse }, /* milliseconds */
321
6.64M
    { ENTRY("%mn"), ENTRY("%s"), nullptr, { .cpv = log->Name }, nullptr, &recurse }, /* module
322
                                                                                        name */
323
6.64M
    { ENTRY("%mo"),
324
6.64M
      ENTRY("%u"),
325
6.64M
      nullptr,
326
6.64M
      { .s = localTime.wMonth },
327
6.64M
      nullptr,
328
6.64M
      &recurse }, /* month
329
                   */
330
6.64M
    { ENTRY("%pid"),
331
6.64M
      ENTRY("%u"),
332
6.64M
      nullptr,
333
6.64M
      { .s = GetCurrentProcessId() },
334
6.64M
      nullptr,
335
6.64M
      &recurse }, /* process id */
336
6.64M
    { ENTRY("%se"),
337
6.64M
      ENTRY("%02u"),
338
6.64M
      nullptr,
339
6.64M
      { .s = localTime.wSecond },
340
6.64M
      nullptr,
341
6.64M
      &recurse },                                                                /* seconds
342
                                                                                  */
343
6.64M
    { ENTRY("%tid"), ENTRY("%s"), get_tid, { .pv = &targ }, nullptr, &recurse }, /* thread id */
344
6.64M
    { ENTRY("%yr"), ENTRY("%u"), nullptr, { .s = localTime.wYear }, nullptr, &recurse }, /* year
345
                                                                                          */
346
6.64M
    { ENTRY("%{"),
347
6.64M
      ENTRY("%}"),
348
6.64M
      nullptr,
349
6.64M
      { .pv = log->context },
350
6.64M
      skip_if_null,
351
6.64M
      &recurse }, /* skip if no context */
352
6.64M
  };
353
354
6.64M
  recurse.options = options;
355
6.64M
  recurse.nroptions = ARRAYSIZE(options);
356
357
6.64M
  if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format)))
358
0
    return FALSE;
359
360
6.64M
  WINPR_PRAGMA_DIAG_PUSH
361
6.64M
  WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY
362
363
6.64M
  WLog_PrintMessagePrefix(prefix, prefixlen, format);
364
365
6.64M
  WINPR_PRAGMA_DIAG_POP
366
367
6.64M
  return TRUE;
368
6.64M
}
369
370
wLogLayout* WLog_GetLogLayout(wLog* log)
371
0
{
372
0
  wLogAppender* appender = nullptr;
373
0
  appender = WLog_GetLogAppender(log);
374
0
  return appender->Layout;
375
0
}
376
377
BOOL WLog_Layout_SetPrefixFormat(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout,
378
                                 const char* format)
379
0
{
380
0
  free(layout->FormatString);
381
0
  layout->FormatString = nullptr;
382
383
0
  if (format)
384
0
  {
385
0
    layout->FormatString = _strdup(format);
386
387
0
    if (!layout->FormatString)
388
0
      return FALSE;
389
0
  }
390
391
0
  return TRUE;
392
0
}
393
394
wLogLayout* WLog_Layout_New(WINPR_ATTR_UNUSED wLog* log)
395
5
{
396
5
  LPCSTR prefix = "WLOG_PREFIX";
397
5
  DWORD nSize = 0;
398
5
  char* env = nullptr;
399
5
  wLogLayout* layout = nullptr;
400
5
  layout = (wLogLayout*)calloc(1, sizeof(wLogLayout));
401
402
5
  if (!layout)
403
0
    return nullptr;
404
405
5
  nSize = GetEnvironmentVariableA(prefix, nullptr, 0);
406
407
5
  if (nSize)
408
0
  {
409
0
    env = (LPSTR)malloc(nSize);
410
411
0
    if (!env)
412
0
    {
413
0
      free(layout);
414
0
      return nullptr;
415
0
    }
416
417
0
    if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1)
418
0
    {
419
0
      free(env);
420
0
      free(layout);
421
0
      return nullptr;
422
0
    }
423
0
  }
424
425
5
  if (env)
426
0
    layout->FormatString = env;
427
5
  else
428
5
  {
429
#ifdef ANDROID
430
    layout->FormatString = _strdup("[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: ");
431
#else
432
5
    layout->FormatString =
433
5
        _strdup("[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: ");
434
5
#endif
435
436
5
    if (!layout->FormatString)
437
0
    {
438
0
      free(layout);
439
0
      return nullptr;
440
0
    }
441
5
  }
442
443
5
  return layout;
444
5
}
445
446
void WLog_Layout_Free(WINPR_ATTR_UNUSED wLog* log, wLogLayout* layout)
447
5
{
448
5
  if (layout)
449
5
  {
450
5
    if (layout->FormatString)
451
5
    {
452
5
      free(layout->FormatString);
453
5
      layout->FormatString = nullptr;
454
5
    }
455
456
5
    free(layout);
457
5
  }
458
5
}