Coverage Report

Created: 2024-09-08 06:16

/src/FreeRDP/winpr/libwinpr/utils/wlog/Layout.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 <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
0
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
43
#endif
44
45
struct format_option_recurse;
46
47
struct format_option
48
{
49
  const char* fmt;
50
  size_t fmtlen;
51
  const char* replace;
52
  size_t replacelen;
53
  const char* (*fkt)(void*);
54
  void* arg;
55
  const char* (*ext)(const struct format_option* opt, const char* str, size_t* preplacelen,
56
                     size_t* pskiplen);
57
  struct format_option_recurse* recurse;
58
};
59
60
struct format_option_recurse
61
{
62
  struct format_option* options;
63
  size_t nroptions;
64
  wLog* log;
65
  wLogLayout* layout;
66
  wLogMessage* message;
67
  char buffer[WLOG_MAX_PREFIX_SIZE];
68
};
69
70
/**
71
 * Log Layout
72
 */
73
WINPR_ATTR_FORMAT_ARG(3, 0)
74
static void WLog_PrintMessagePrefixVA(wLog* log, wLogMessage* message,
75
                                      WINPR_FORMAT_ARG const char* format, va_list args)
76
730
{
77
730
  WINPR_ASSERT(message);
78
730
  (void)vsnprintf(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args);
79
730
}
80
81
WINPR_ATTR_FORMAT_ARG(3, 4)
82
static void WLog_PrintMessagePrefix(wLog* log, wLogMessage* message,
83
                                    WINPR_FORMAT_ARG const char* format, ...)
84
730
{
85
730
  va_list args;
86
730
  va_start(args, format);
87
730
  WLog_PrintMessagePrefixVA(log, message, format, args);
88
730
  va_end(args);
89
730
}
90
91
static const char* get_tid(void* arg)
92
730
{
93
730
  char* str = arg;
94
730
  size_t tid = 0;
95
730
#if defined __linux__ && !defined ANDROID
96
  /* On Linux we prefer to see the LWP id */
97
730
  tid = (size_t)syscall(SYS_gettid);
98
#else
99
  tid = (size_t)GetCurrentThreadId();
100
#endif
101
730
  (void)sprintf(str, "%08" PRIxz, tid);
102
730
  return str;
103
730
}
104
105
static BOOL log_invalid_fmt(const char* what)
106
0
{
107
0
  (void)fprintf(stderr, "Invalid format string '%s'\n", what);
108
0
  return FALSE;
109
0
}
110
111
static BOOL check_and_log_format_size(char* format, size_t size, size_t index, size_t add)
112
22.6k
{
113
  /* format string must be '\0' terminated, so abort at size - 1 */
114
22.6k
  if (index + add + 1 >= size)
115
0
  {
116
0
    (void)fprintf(stderr,
117
0
                  "Format string too long ['%s', max %" PRIuz ", used %" PRIuz
118
0
                  ", adding %" PRIuz "]\n",
119
0
                  format, size, index, add);
120
0
    return FALSE;
121
0
  }
122
22.6k
  return TRUE;
123
22.6k
}
124
125
static int opt_compare_fn(const void* a, const void* b)
126
89.7k
{
127
89.7k
  const char* what = a;
128
89.7k
  const struct format_option* opt = b;
129
89.7k
  if (!opt)
130
0
    return -1;
131
89.7k
  return strncmp(what, opt->fmt, opt->fmtlen);
132
89.7k
}
133
134
static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse,
135
                                  char* format, size_t formatlen);
136
137
static const char* skip_if_null(const struct format_option* opt, const char* fmt,
138
                                size_t* preplacelen, size_t* pskiplen)
139
730
{
140
730
  WINPR_ASSERT(opt);
141
730
  WINPR_ASSERT(fmt);
142
730
  WINPR_ASSERT(preplacelen);
143
730
  WINPR_ASSERT(pskiplen);
144
145
730
  *preplacelen = 0;
146
730
  *pskiplen = 0;
147
148
730
  const char* str = &fmt[opt->fmtlen]; /* Skip first %{ from string */
149
730
  const char* end = strstr(str, opt->replace);
150
730
  if (!end)
151
0
    return NULL;
152
730
  *pskiplen = end - fmt + opt->replacelen;
153
154
730
  if (!opt->arg)
155
730
    return NULL;
156
157
0
  const size_t replacelen = end - str;
158
159
0
  char buffer[WLOG_MAX_PREFIX_SIZE] = { 0 };
160
0
  memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1));
161
162
0
  if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer,
163
0
                             ARRAYSIZE(opt->recurse->buffer)))
164
0
    return NULL;
165
166
0
  *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer));
167
0
  return opt->recurse->buffer;
168
0
}
169
170
static BOOL replace_format_string(const char* FormatString, struct format_option_recurse* recurse,
171
                                  char* format, size_t formatlen)
172
730
{
173
730
  WINPR_ASSERT(FormatString);
174
730
  WINPR_ASSERT(recurse);
175
176
730
  size_t index = 0;
177
178
23.3k
  while (*FormatString)
179
22.6k
  {
180
22.6k
    const struct format_option* opt =
181
22.6k
        bsearch(FormatString, recurse->options, recurse->nroptions,
182
22.6k
                sizeof(struct format_option), opt_compare_fn);
183
22.6k
    if (opt)
184
7.30k
    {
185
7.30k
      size_t replacelen = opt->replacelen;
186
7.30k
      size_t fmtlen = opt->fmtlen;
187
7.30k
      const char* replace = opt->replace;
188
7.30k
      const void* arg = opt->arg;
189
190
7.30k
      if (opt->ext)
191
730
        replace = opt->ext(opt, FormatString, &replacelen, &fmtlen);
192
7.30k
      if (opt->fkt)
193
730
        arg = opt->fkt(opt->arg);
194
195
7.30k
      if (replace && (replacelen > 0))
196
6.57k
      {
197
6.57k
        const int rc = _snprintf(&format[index], formatlen - index, replace, arg);
198
6.57k
        if (rc < 0)
199
0
          return FALSE;
200
6.57k
        if (!check_and_log_format_size(format, formatlen, index, rc))
201
0
          return FALSE;
202
6.57k
        index += rc;
203
6.57k
      }
204
7.30k
      FormatString += fmtlen;
205
7.30k
    }
206
15.3k
    else
207
15.3k
    {
208
      /* Unknown format string */
209
15.3k
      if (*FormatString == '%')
210
0
        return log_invalid_fmt(FormatString);
211
212
15.3k
      if (!check_and_log_format_size(format, formatlen, index, 1))
213
0
        return FALSE;
214
15.3k
      format[index++] = *FormatString++;
215
15.3k
    }
216
22.6k
  }
217
218
730
  if (!check_and_log_format_size(format, formatlen, index, 0))
219
0
    return FALSE;
220
730
  return TRUE;
221
730
}
222
223
BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message)
224
730
{
225
730
  char format[WLOG_MAX_PREFIX_SIZE] = { 0 };
226
227
730
  WINPR_ASSERT(layout);
228
730
  WINPR_ASSERT(message);
229
230
730
  char tid[32] = { 0 };
231
730
  SYSTEMTIME localTime = { 0 };
232
730
  GetLocalTime(&localTime);
233
234
730
  struct format_option_recurse recurse = {
235
730
    .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message
236
730
  };
237
238
24.8k
#define ENTRY(x) x, sizeof(x) - 1
239
730
  struct format_option options[] = {
240
730
    { ENTRY("%ctx"), ENTRY("%s"), log->custom, log->context, NULL, &recurse }, /* log context */
241
730
    { ENTRY("%dw"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDayOfWeek, NULL,
242
730
      &recurse }, /* day of week */
243
730
    { ENTRY("%dy"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wDay, NULL,
244
730
      &recurse }, /* day of year */
245
730
    { ENTRY("%fl"), ENTRY("%s"), NULL, (void*)message->FileName, NULL, &recurse }, /* file */
246
730
    { ENTRY("%fn"), ENTRY("%s"), NULL, (void*)message->FunctionName, NULL,
247
730
      &recurse }, /* function */
248
730
    { ENTRY("%hr"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wHour, NULL,
249
730
      &recurse }, /* hours */
250
730
    { ENTRY("%ln"), ENTRY("%" PRIuz), NULL, (void*)message->LineNumber, NULL,
251
730
      &recurse }, /* line number */
252
730
    { ENTRY("%lv"), ENTRY("%s"), NULL, (void*)WLOG_LEVELS[message->Level], NULL,
253
730
      &recurse }, /* log level */
254
730
    { ENTRY("%mi"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wMinute, NULL,
255
730
      &recurse }, /* minutes */
256
730
    { ENTRY("%ml"), ENTRY("%03u"), NULL, (void*)(size_t)localTime.wMilliseconds, NULL,
257
730
      &recurse },                                                   /* milliseconds */
258
730
    { ENTRY("%mn"), ENTRY("%s"), NULL, log->Name, NULL, &recurse }, /* module name */
259
730
    { ENTRY("%mo"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wMonth, NULL,
260
730
      &recurse }, /* month */
261
730
    { ENTRY("%pid"), ENTRY("%u"), NULL, (void*)(size_t)GetCurrentProcessId(), NULL,
262
730
      &recurse }, /* process id */
263
730
    { ENTRY("%se"), ENTRY("%02u"), NULL, (void*)(size_t)localTime.wSecond, NULL,
264
730
      &recurse },                                                 /* seconds */
265
730
    { ENTRY("%tid"), ENTRY("%s"), get_tid, tid, NULL, &recurse }, /* thread id */
266
730
    { ENTRY("%yr"), ENTRY("%u"), NULL, (void*)(size_t)localTime.wYear, NULL,
267
730
      &recurse }, /* year */
268
730
    { ENTRY("%{"), ENTRY("%}"), NULL, log->context, skip_if_null,
269
730
      &recurse }, /* skip if no context */
270
730
  };
271
272
730
  recurse.options = options;
273
730
  recurse.nroptions = ARRAYSIZE(options);
274
275
730
  if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format)))
276
0
    return FALSE;
277
278
730
  WINPR_PRAGMA_DIAG_PUSH
279
730
  WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY
280
281
730
  WLog_PrintMessagePrefix(log, message, format);
282
283
730
  WINPR_PRAGMA_DIAG_POP
284
285
730
  return TRUE;
286
730
}
287
288
wLogLayout* WLog_GetLogLayout(wLog* log)
289
0
{
290
0
  wLogAppender* appender = NULL;
291
0
  appender = WLog_GetLogAppender(log);
292
0
  return appender->Layout;
293
0
}
294
295
BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format)
296
0
{
297
0
  free(layout->FormatString);
298
0
  layout->FormatString = NULL;
299
300
0
  if (format)
301
0
  {
302
0
    layout->FormatString = _strdup(format);
303
304
0
    if (!layout->FormatString)
305
0
      return FALSE;
306
0
  }
307
308
0
  return TRUE;
309
0
}
310
311
wLogLayout* WLog_Layout_New(wLog* log)
312
1
{
313
1
  LPCSTR prefix = "WLOG_PREFIX";
314
1
  DWORD nSize = 0;
315
1
  char* env = NULL;
316
1
  wLogLayout* layout = NULL;
317
1
  layout = (wLogLayout*)calloc(1, sizeof(wLogLayout));
318
319
1
  if (!layout)
320
0
    return NULL;
321
322
1
  nSize = GetEnvironmentVariableA(prefix, NULL, 0);
323
324
1
  if (nSize)
325
0
  {
326
0
    env = (LPSTR)malloc(nSize);
327
328
0
    if (!env)
329
0
    {
330
0
      free(layout);
331
0
      return NULL;
332
0
    }
333
334
0
    if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1)
335
0
    {
336
0
      free(env);
337
0
      free(layout);
338
0
      return NULL;
339
0
    }
340
0
  }
341
342
1
  if (env)
343
0
    layout->FormatString = env;
344
1
  else
345
1
  {
346
#ifdef ANDROID
347
    layout->FormatString = _strdup("[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: ");
348
#else
349
1
    layout->FormatString =
350
1
        _strdup("[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: ");
351
1
#endif
352
353
1
    if (!layout->FormatString)
354
0
    {
355
0
      free(layout);
356
0
      return NULL;
357
0
    }
358
1
  }
359
360
1
  return layout;
361
1
}
362
363
void WLog_Layout_Free(wLog* log, wLogLayout* layout)
364
1
{
365
1
  if (layout)
366
1
  {
367
1
    if (layout->FormatString)
368
1
    {
369
1
      free(layout->FormatString);
370
1
      layout->FormatString = NULL;
371
1
    }
372
373
1
    free(layout);
374
1
  }
375
1
}