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 = end - fmt + opt->replacelen;
164 const size_t replacelen = 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, rc))
214 FormatString += fmtlen;
219 if (*FormatString ==
'%')
220 return log_invalid_fmt(FormatString);
222 if (!check_and_log_format_size(format, formatlen, index, 1))
224 format[index++] = *FormatString++;
228 if (!check_and_log_format_size(format, formatlen, index, 0))
233 BOOL WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout,
wLogMessage* message)
235 char format[WLOG_MAX_PREFIX_SIZE] = { 0 };
237 WINPR_ASSERT(layout);
238 WINPR_ASSERT(message);
240 struct format_tid_arg targ = { 0 };
242 SYSTEMTIME localTime = { 0 };
243 GetLocalTime(&localTime);
245 struct format_option_recurse recurse = {
246 .options = NULL, .nroptions = 0, .log = log, .layout = layout, .message = message
249 #define ENTRY(x) x, sizeof(x) - 1
250 struct format_option options[] = {
251 { ENTRY(
"%ctx"), ENTRY(
"%s"), log->custom, log->context, NULL, &recurse },
252 { ENTRY(
"%dw"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wDayOfWeek, NULL,
254 { ENTRY(
"%dy"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wDay, NULL,
256 { ENTRY(
"%fl"), ENTRY(
"%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FileName,
void*),
258 { ENTRY(
"%fn"), ENTRY(
"%s"), NULL, WINPR_CAST_CONST_PTR_AWAY(message->FunctionName,
void*),
260 { ENTRY(
"%hr"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wHour, NULL,
262 { ENTRY(
"%ln"), ENTRY(
"%" PRIuz), NULL, (
void*)message->LineNumber, NULL,
264 { ENTRY(
"%lv"), ENTRY(
"%s"), NULL,
265 WINPR_CAST_CONST_PTR_AWAY(WLOG_LEVELS[message->Level],
void*), NULL,
267 { ENTRY(
"%mi"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wMinute, NULL,
269 { ENTRY(
"%ml"), ENTRY(
"%03u"), NULL, (
void*)(
size_t)localTime.wMilliseconds, NULL,
271 { ENTRY(
"%mn"), ENTRY(
"%s"), NULL, log->Name, NULL, &recurse },
272 { ENTRY(
"%mo"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wMonth, NULL,
274 { ENTRY(
"%pid"), ENTRY(
"%u"), NULL, (
void*)(
size_t)GetCurrentProcessId(), NULL,
276 { ENTRY(
"%se"), ENTRY(
"%02u"), NULL, (
void*)(
size_t)localTime.wSecond, NULL,
278 { ENTRY(
"%tid"), ENTRY(
"%s"), get_tid, &targ, NULL, &recurse },
279 { ENTRY(
"%yr"), ENTRY(
"%u"), NULL, (
void*)(
size_t)localTime.wYear, NULL,
281 { ENTRY(
"%{"), ENTRY(
"%}"), NULL, log->context, skip_if_null,
285 recurse.options = options;
286 recurse.nroptions = ARRAYSIZE(options);
288 if (!replace_format_string(layout->FormatString, &recurse, format, ARRAYSIZE(format)))
291 WINPR_PRAGMA_DIAG_PUSH
292 WINPR_PRAGMA_DIAG_IGNORED_FORMAT_SECURITY
294 WLog_PrintMessagePrefix(log, message, format);
296 WINPR_PRAGMA_DIAG_POP
301 wLogLayout* WLog_GetLogLayout(wLog* log)
303 wLogAppender* appender = NULL;
304 appender = WLog_GetLogAppender(log);
305 return appender->Layout;
308 BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout,
const char* format)
310 free(layout->FormatString);
311 layout->FormatString = NULL;
315 layout->FormatString = _strdup(format);
317 if (!layout->FormatString)
324 wLogLayout* WLog_Layout_New(wLog* log)
326 LPCSTR prefix =
"WLOG_PREFIX";
329 wLogLayout* layout = NULL;
330 layout = (wLogLayout*)calloc(1,
sizeof(wLogLayout));
335 nSize = GetEnvironmentVariableA(prefix, NULL, 0);
339 env = (LPSTR)malloc(nSize);
347 if (GetEnvironmentVariableA(prefix, env, nSize) != nSize - 1)
356 layout->FormatString = env;
360 layout->FormatString = _strdup(
"[pid=%pid:tid=%tid] - [%fn]%{[%ctx]%}: ");
362 layout->FormatString =
363 _strdup(
"[%hr:%mi:%se:%ml] [%pid:%tid] [%lv][%mn] - [%fn]%{[%ctx]%}: ");
366 if (!layout->FormatString)
376 void WLog_Layout_Free(wLog* log, wLogLayout* layout)
380 if (layout->FormatString)
382 free(layout->FormatString);
383 layout->FormatString = NULL;