20 #include <winpr/config.h>
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>
36 #if defined __linux__ && !defined ANDROID
38 #include <sys/syscall.h>
42 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
45 struct format_option_recurse;
58 const char* (*fkt)(
void*);
60 const char* (*ext)(
const struct format_option* opt,
const char* str,
size_t* preplacelen,
62 struct format_option_recurse* recurse;
65 struct format_option_recurse
67 struct format_option* options;
72 char buffer[WLOG_MAX_PREFIX_SIZE];
78 WINPR_ATTR_FORMAT_ARG(3, 0)
79 static
void WLog_PrintMessagePrefixVA(wLog* log,
wLogMessage* message,
80 WINPR_FORMAT_ARG const
char* format, va_list args)
82 WINPR_ASSERT(message);
83 (void)vsnprintf(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args);
86 WINPR_ATTR_FORMAT_ARG(3, 4)
87 static
void WLog_PrintMessagePrefix(wLog* log,
wLogMessage* message,
88 WINPR_FORMAT_ARG const
char* format, ...)
91 va_start(args, format);
92 WLog_PrintMessagePrefixVA(log, message, format, args);
96 static const char* get_tid(
void* arg)
98 struct format_tid_arg* targ = arg;
102 #if defined __linux__ && !defined ANDROID
104 tid = (size_t)syscall(SYS_gettid);
106 tid = (size_t)GetCurrentThreadId();
108 (void)_snprintf(targ->tid,
sizeof(targ->tid),
"%08" PRIxz, tid);
112 static BOOL log_invalid_fmt(
const char* what)
114 (void)fprintf(stderr,
"Invalid format string '%s'\n", what);
118 static BOOL check_and_log_format_size(
char* format,
size_t size,
size_t index,
size_t add)
121 if (index + add + 1 >= size)
123 (void)fprintf(stderr,
124 "Format string too long ['%s', max %" PRIuz
", used %" PRIuz
125 ", adding %" PRIuz
"]\n",
126 format, size, index, add);
132 static int opt_compare_fn(
const void* a,
const void* b)
134 const char* what = a;
135 const struct format_option* opt = b;
138 return strncmp(what, opt->fmt, opt->fmtlen);
141 static BOOL replace_format_string(
const char* FormatString,
struct format_option_recurse* recurse,
142 char* format,
size_t formatlen);
144 static const char* skip_if_null(
const struct format_option* opt,
const char* fmt,
145 size_t* preplacelen,
size_t* pskiplen)
149 WINPR_ASSERT(preplacelen);
150 WINPR_ASSERT(pskiplen);
155 const char* str = &fmt[opt->fmtlen];
156 const char* end = strstr(str, opt->replace);
159 *pskiplen = WINPR_ASSERTING_INT_CAST(
size_t, end - fmt) + opt->replacelen;
164 const size_t replacelen = WINPR_ASSERTING_INT_CAST(
size_t, end - str);
166 char buffer[WLOG_MAX_PREFIX_SIZE] = { 0 };
167 memcpy(buffer, str, MIN(replacelen, ARRAYSIZE(buffer) - 1));
169 if (!replace_format_string(buffer, opt->recurse, opt->recurse->buffer,
170 ARRAYSIZE(opt->recurse->buffer)))
173 *preplacelen = strnlen(opt->recurse->buffer, ARRAYSIZE(opt->recurse->buffer));
174 return opt->recurse->buffer;
177 static BOOL replace_format_string(
const char* FormatString,
struct format_option_recurse* recurse,
178 char* format,
size_t formatlen)
180 WINPR_ASSERT(FormatString);
181 WINPR_ASSERT(recurse);
185 while (*FormatString)
187 const struct format_option* opt =
188 bsearch(FormatString, recurse->options, recurse->nroptions,
189 sizeof(
struct format_option), opt_compare_fn);
192 size_t replacelen = opt->replacelen;
193 size_t fmtlen = opt->fmtlen;
194 const char* replace = opt->replace;
195 const void* arg = opt->arg;
198 replace = opt->ext(opt, FormatString, &replacelen, &fmtlen);
200 arg = opt->fkt(opt->arg);
202 if (replace && (replacelen > 0))
204 WINPR_PRAGMA_DIAG_PUSH
205 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_NONLITERAL
206 const int rc = _snprintf(&format[index], formatlen - index, replace, arg);
207 WINPR_PRAGMA_DIAG_POP
210 if (!check_and_log_format_size(format, formatlen, index,
211 WINPR_ASSERTING_INT_CAST(
size_t, rc)))
213 index += WINPR_ASSERTING_INT_CAST(
size_t, rc);
215 FormatString += fmtlen;
220 if (*FormatString ==
'%')
221 return log_invalid_fmt(FormatString);
223 if (!check_and_log_format_size(format, formatlen, index, 1))
225 format[index++] = *FormatString++;
229 if (!check_and_log_format_size(format, formatlen, index, 0))
234 BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout,
wLogMessage* message)
236 char format[WLOG_MAX_PREFIX_SIZE] = { 0 };
238 WINPR_ASSERT(layout);
239 WINPR_ASSERT(message);
241 struct format_tid_arg targ = { 0 };
244 GetLocalTime(&localTime);
246 struct format_option_recurse recurse = {
247 .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message
250 #define ENTRY(x) x, sizeof(x) - 1
251 struct format_option options[] = {
252 { ENTRY(
"%ctx"), ENTRY(
"%s"), log->custom, log->context, NULL, &recurse },
253 { ENTRY(
"%dw"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wDayOfWeek, NULL,
255 { ENTRY(
"%dy"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wDay, NULL,
257 { ENTRY(
"%fl"), ENTRY(
"%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FileName,
void*),
259 { ENTRY(
"%fn"), ENTRY(
"%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FunctionName,
void*),
261 { ENTRY(
"%hr"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wHour, NULL,
263 { ENTRY(
"%ln"), ENTRY(
"%" PRIuz), NULL, (
void*)message->LineNumber, NULL,
265 { ENTRY(
"%lv"), ENTRY(
"%s"), NULL,
266 WINPR_CAST_CONST_PTR_AWAY(WLOG_LEVELS[message->Level],
void*), NULL,
268 { ENTRY(
"%mi"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wMinute, NULL,
270 { ENTRY(
"%ml"), ENTRY(
"%03u"), NULL, (
void*)(
size_t)localTime.wMilliseconds, NULL,
272 { ENTRY(
"%mn"), ENTRY(
"%s"), NULL, log->Name, NULL, &recurse },
273 { ENTRY(
"%mo"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wMonth, NULL,
275 { ENTRY(
"%pid"), ENTRY(
"%u"), NULL, (
void*)(
size_t)GetCurrentProcessId(), NULL,
277 { ENTRY(
"%se"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wSecond, NULL,
279 { ENTRY(
"%tid"), ENTRY(
"%s"), get_tid, &targ, NULL, &recurse },
280 { ENTRY(
"%yr"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wYear, NULL,
282 { ENTRY(
"%{"), ENTRY(
"%}"), NULL, log->context, skip_if_null,
286 recurse.options = options;
287 recurse.nroptions = ARRAYSIZE(options);
289 if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format)))
292 WINPR_PRAGMA_DIAG_PUSH
293 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY
295 WLog_PrintMessagePrefix(log, message, format);
297 WINPR_PRAGMA_DIAG_POP
302 wLogLayout* WLog_GetLogLayout(wLog* log)
304 wLogAppender* appender = NULL;
305 appender = WLog_GetLogAppender(log);
306 return appender->Layout;
309 BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout,
const char* format)
311 free(layout->FormatString);
312 layout->FormatString = NULL;
316 layout->FormatString = _strdup(format);
318 if (!layout->FormatString)
325 wLogLayout* WLog_Layout_New(wLog* log)
327 LPCSTR prefix =
"WLOG_PREFIX";
330 wLogLayout* layout = NULL;
331 layout = (wLogLayout*)calloc(1,
sizeof(wLogLayout));
336 nSize = GetEnvironmentVariableA(prefix, NULL, 0);
340 env = (LPSTR)malloc(nSize);
348 if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1)
357 layout->FormatString = env;
361 layout->FormatString = _strdup(
"[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: ");
363 layout->FormatString =
364 _strdup(
"[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: ");
367 if (!layout->FormatString)
377 void WLog_Layout_Free(wLog* log, wLogLayout* layout)
381 if (layout->FormatString)
383 free(layout->FormatString);
384 layout->FormatString = NULL;