2013-09-15 23:46:02 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* WinPR Logger
|
|
|
|
*
|
|
|
|
* Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2013-09-15 23:46:02 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-04-27 22:18:48 +03:00
|
|
|
#include <stdarg.h>
|
2013-09-15 23:46:02 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-16 00:48:09 +04:00
|
|
|
#include <winpr/crt.h>
|
2021-06-16 19:09:14 +03:00
|
|
|
#include <winpr/assert.h>
|
2013-09-15 23:46:02 +04:00
|
|
|
#include <winpr/print.h>
|
2014-08-18 21:24:16 +04:00
|
|
|
#include <winpr/debug.h>
|
2013-10-09 06:35:14 +04:00
|
|
|
#include <winpr/environment.h>
|
2018-04-03 10:59:42 +03:00
|
|
|
#include <winpr/wlog.h>
|
2013-09-15 23:46:02 +04:00
|
|
|
|
2014-08-18 20:57:08 +04:00
|
|
|
#if defined(ANDROID)
|
|
|
|
#include <android/log.h>
|
2015-11-09 21:37:02 +03:00
|
|
|
#include "../log.h"
|
2014-08-18 20:57:08 +04:00
|
|
|
#endif
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
#include "wlog.h"
|
2013-09-15 23:46:02 +04:00
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef struct
|
2015-11-09 19:56:44 +03:00
|
|
|
{
|
|
|
|
DWORD Level;
|
|
|
|
LPSTR* Names;
|
2021-06-16 19:09:14 +03:00
|
|
|
size_t NameCount;
|
2022-02-14 16:59:22 +03:00
|
|
|
} wLogFilter;
|
2014-08-18 20:57:08 +04:00
|
|
|
|
2018-11-19 15:04:40 +03:00
|
|
|
#define WLOG_FILTER_NOT_FILTERED -1
|
|
|
|
#define WLOG_FILTER_NOT_INITIALIZED -2
|
2013-09-16 20:05:08 +04:00
|
|
|
/**
|
|
|
|
* References for general logging concepts:
|
|
|
|
*
|
|
|
|
* Short introduction to log4j:
|
|
|
|
* http://logging.apache.org/log4j/1.2/manual.html
|
|
|
|
*
|
|
|
|
* logging - Logging facility for Python:
|
|
|
|
* http://docs.python.org/2/library/logging.html
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
LPCSTR WLOG_LEVELS[7] = { "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF" };
|
2013-09-16 00:48:09 +04:00
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static INIT_ONCE _WLogInitialized = INIT_ONCE_STATIC_INIT;
|
2014-05-29 23:23:15 +04:00
|
|
|
static DWORD g_FilterCount = 0;
|
2014-08-18 21:34:47 +04:00
|
|
|
static wLogFilter* g_Filters = NULL;
|
2018-04-03 10:59:42 +03:00
|
|
|
static wLog* g_RootLog = NULL;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static wLog* WLog_New(LPCSTR name, wLog* rootLogger);
|
|
|
|
static void WLog_Free(wLog* log);
|
2016-03-02 16:11:15 +03:00
|
|
|
static LONG WLog_GetFilterLogLevel(wLog* log);
|
2016-01-23 17:16:13 +03:00
|
|
|
static int WLog_ParseLogLevel(LPCSTR level);
|
2021-04-16 14:55:59 +03:00
|
|
|
static BOOL WLog_ParseFilter(wLog* root, wLogFilter* filter, LPCSTR name);
|
|
|
|
static BOOL WLog_ParseFilters(wLog* root);
|
|
|
|
static wLog* WLog_Get_int(wLog* root, LPCSTR name);
|
2018-04-03 10:59:42 +03:00
|
|
|
|
2018-04-03 15:04:22 +03:00
|
|
|
#if !defined(_WIN32)
|
|
|
|
static void WLog_Uninit_(void) __attribute__((destructor));
|
|
|
|
#endif
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static void WLog_Uninit_(void)
|
|
|
|
{
|
|
|
|
DWORD index;
|
|
|
|
wLog* child = NULL;
|
|
|
|
wLog* root = g_RootLog;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (index = 0; index < root->ChildrenCount; index++)
|
|
|
|
{
|
|
|
|
child = root->Children[index];
|
|
|
|
WLog_Free(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_Free(root);
|
|
|
|
g_RootLog = NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
static void WLog_Lock(wLog* log)
|
2021-05-26 22:42:30 +03:00
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
WINPR_ASSERT(log);
|
2021-05-26 22:42:30 +03:00
|
|
|
EnterCriticalSection(&log->lock);
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
static void WLog_Unlock(wLog* log)
|
2021-05-26 22:42:30 +03:00
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
WINPR_ASSERT(log);
|
2021-05-26 22:42:30 +03:00
|
|
|
LeaveCriticalSection(&log->lock);
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL CALLBACK WLog_InitializeRoot(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
|
|
|
|
{
|
|
|
|
char* env;
|
|
|
|
DWORD nSize;
|
|
|
|
DWORD logAppenderType;
|
|
|
|
LPCSTR appender = "WLOG_APPENDER";
|
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
WINPR_UNUSED(InitOnce);
|
|
|
|
WINPR_UNUSED(Parameter);
|
|
|
|
WINPR_UNUSED(Context);
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
if (!(g_RootLog = WLog_New("", NULL)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
g_RootLog->IsRoot = TRUE;
|
|
|
|
logAppenderType = WLOG_APPENDER_CONSOLE;
|
|
|
|
nSize = GetEnvironmentVariableA(appender, NULL, 0);
|
|
|
|
|
|
|
|
if (nSize)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
env = (LPSTR)malloc(nSize);
|
2018-04-03 10:59:42 +03:00
|
|
|
|
|
|
|
if (!env)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (GetEnvironmentVariableA(appender, env, nSize) != nSize - 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s environment variable modified in my back", appender);
|
|
|
|
free(env);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_stricmp(env, "CONSOLE") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_CONSOLE;
|
|
|
|
else if (_stricmp(env, "FILE") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_FILE;
|
|
|
|
else if (_stricmp(env, "BINARY") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_BINARY;
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#ifdef WINPR_HAVE_SYSLOG_H
|
2018-04-03 10:59:42 +03:00
|
|
|
else if (_stricmp(env, "SYSLOG") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_SYSLOG;
|
|
|
|
|
2023-01-10 16:30:23 +03:00
|
|
|
#endif /* WINPR_HAVE_SYSLOG_H */
|
|
|
|
#ifdef WINPR_HAVE_JOURNALD_H
|
2018-04-03 10:59:42 +03:00
|
|
|
else if (_stricmp(env, "JOURNALD") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_JOURNALD;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
else if (_stricmp(env, "UDP") == 0)
|
|
|
|
logAppenderType = WLOG_APPENDER_UDP;
|
|
|
|
|
|
|
|
free(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!WLog_SetLogAppenderType(g_RootLog, logAppenderType))
|
|
|
|
goto fail;
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
if (!WLog_ParseFilters(g_RootLog))
|
|
|
|
goto fail;
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
atexit(WLog_Uninit_);
|
2023-05-30 11:01:03 +03:00
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
return TRUE;
|
|
|
|
fail:
|
2021-04-16 14:55:59 +03:00
|
|
|
WLog_Uninit_();
|
2018-04-03 10:59:42 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2016-01-23 17:16:13 +03:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
static BOOL log_recursion(LPCSTR file, LPCSTR fkt, size_t line)
|
2013-09-16 00:48:09 +04:00
|
|
|
{
|
2018-08-21 11:27:56 +03:00
|
|
|
BOOL status = FALSE;
|
|
|
|
char** msg = NULL;
|
2014-08-18 21:24:16 +04:00
|
|
|
size_t used, i;
|
2014-08-18 21:34:47 +04:00
|
|
|
void* bt = winpr_backtrace(20);
|
2015-10-23 13:08:20 +03:00
|
|
|
#if defined(ANDROID)
|
2016-01-23 17:16:13 +03:00
|
|
|
LPCSTR tag = WINPR_TAG("utils.wlog");
|
2015-10-23 13:08:20 +03:00
|
|
|
#endif
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!bt)
|
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-10-23 13:08:20 +03:00
|
|
|
msg = winpr_backtrace_symbols(bt, &used);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!msg)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-08-18 19:11:40 +04:00
|
|
|
#if defined(ANDROID)
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (__android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!") < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2022-09-29 15:55:27 +03:00
|
|
|
if (__android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%zu]", fkt, file, line) < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
for (i = 0; i < used; i++)
|
2022-09-29 15:55:27 +03:00
|
|
|
if (__android_log_print(ANDROID_LOG_FATAL, tag, "%zu: %s", i, msg[i]) < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2014-08-18 19:11:40 +04:00
|
|
|
#else
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (fprintf(stderr, "[%s]: Recursion detected!\n", fkt) < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
if (fprintf(stderr, "[%s]: Check %s:%" PRIuz "\n", fkt, file, line) < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
for (i = 0; i < used; i++)
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fprintf(stderr, "%s: %" PRIuz ": %s\n", fkt, i, msg[i]) < 0)
|
2018-08-21 11:27:56 +03:00
|
|
|
goto out;
|
2014-08-18 21:34:47 +04:00
|
|
|
|
2014-08-18 19:11:40 +04:00
|
|
|
#endif
|
2018-08-21 11:27:56 +03:00
|
|
|
status = TRUE;
|
|
|
|
out:
|
2015-05-11 10:07:39 +03:00
|
|
|
free(msg);
|
2014-08-18 21:24:16 +04:00
|
|
|
winpr_backtrace_free(bt);
|
2018-08-21 11:27:56 +03:00
|
|
|
return status;
|
2014-08-18 19:11:40 +04:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL WLog_Write(wLog* log, wLogMessage* message)
|
2014-08-18 19:11:40 +04:00
|
|
|
{
|
2021-09-17 09:59:10 +03:00
|
|
|
BOOL status = FALSE;
|
2014-08-18 21:34:47 +04:00
|
|
|
wLogAppender* appender;
|
2013-10-02 02:19:50 +04:00
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-09-15 23:46:02 +04:00
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (!appender->active)
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!WLog_OpenAppender(log))
|
2022-06-23 08:57:38 +03:00
|
|
|
return FALSE;
|
2013-09-15 23:46:02 +04:00
|
|
|
|
2013-10-02 21:38:21 +04:00
|
|
|
EnterCriticalSection(&appender->lock);
|
|
|
|
|
2021-09-17 09:59:10 +03:00
|
|
|
if (appender->WriteMessage)
|
2014-08-18 19:11:40 +04:00
|
|
|
{
|
2021-09-17 09:59:10 +03:00
|
|
|
if (appender->recursive)
|
|
|
|
status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
appender->recursive = TRUE;
|
|
|
|
status = appender->WriteMessage(log, appender, message);
|
|
|
|
appender->recursive = FALSE;
|
|
|
|
}
|
2014-08-18 19:11:40 +04:00
|
|
|
}
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
LeaveCriticalSection(&appender->lock);
|
|
|
|
return status;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL WLog_WriteData(wLog* log, wLogMessage* message)
|
2013-10-09 07:18:59 +04:00
|
|
|
{
|
2015-10-21 11:11:06 +03:00
|
|
|
BOOL status;
|
2014-08-18 21:34:47 +04:00
|
|
|
wLogAppender* appender;
|
2013-10-09 07:18:59 +04:00
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-09 07:18:59 +04:00
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (!appender->active)
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!WLog_OpenAppender(log))
|
|
|
|
return FALSE;
|
2013-10-09 07:18:59 +04:00
|
|
|
|
|
|
|
if (!appender->WriteDataMessage)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-09 07:18:59 +04:00
|
|
|
|
|
|
|
EnterCriticalSection(&appender->lock);
|
|
|
|
|
2014-08-18 16:19:48 +04:00
|
|
|
if (appender->recursive)
|
2019-11-06 17:24:51 +03:00
|
|
|
status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
|
2014-08-18 19:11:40 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
appender->recursive = TRUE;
|
|
|
|
status = appender->WriteDataMessage(log, appender, message);
|
|
|
|
appender->recursive = FALSE;
|
|
|
|
}
|
2013-10-09 07:18:59 +04:00
|
|
|
|
|
|
|
LeaveCriticalSection(&appender->lock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL WLog_WriteImage(wLog* log, wLogMessage* message)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2015-10-21 11:11:06 +03:00
|
|
|
BOOL status;
|
2014-08-18 21:34:47 +04:00
|
|
|
wLogAppender* appender;
|
2013-10-02 23:41:46 +04:00
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (!appender->active)
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!WLog_OpenAppender(log))
|
|
|
|
return FALSE;
|
2013-10-09 06:35:14 +04:00
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
if (!appender->WriteImageMessage)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
|
|
|
EnterCriticalSection(&appender->lock);
|
|
|
|
|
2014-08-18 16:19:48 +04:00
|
|
|
if (appender->recursive)
|
2019-11-06 17:24:51 +03:00
|
|
|
status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
|
2014-08-18 19:11:40 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
appender->recursive = TRUE;
|
|
|
|
status = appender->WriteImageMessage(log, appender, message);
|
|
|
|
appender->recursive = FALSE;
|
|
|
|
}
|
2013-10-02 23:41:46 +04:00
|
|
|
|
|
|
|
LeaveCriticalSection(&appender->lock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL WLog_WritePacket(wLog* log, wLogMessage* message)
|
2013-10-02 23:41:46 +04:00
|
|
|
{
|
2015-10-21 11:11:06 +03:00
|
|
|
BOOL status;
|
2014-08-18 21:34:47 +04:00
|
|
|
wLogAppender* appender;
|
2013-10-02 23:41:46 +04:00
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (!appender->active)
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!WLog_OpenAppender(log))
|
|
|
|
return FALSE;
|
2013-10-09 06:35:14 +04:00
|
|
|
|
2013-10-09 07:18:59 +04:00
|
|
|
if (!appender->WritePacketMessage)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
|
|
|
EnterCriticalSection(&appender->lock);
|
|
|
|
|
2014-08-18 16:19:48 +04:00
|
|
|
if (appender->recursive)
|
2019-11-06 17:24:51 +03:00
|
|
|
status = log_recursion(message->FileName, message->FunctionName, message->LineNumber);
|
2014-08-18 19:11:40 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
appender->recursive = TRUE;
|
|
|
|
status = appender->WritePacketMessage(log, appender, message);
|
|
|
|
appender->recursive = FALSE;
|
|
|
|
}
|
2013-10-02 23:41:46 +04:00
|
|
|
|
|
|
|
LeaveCriticalSection(&appender->lock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, size_t line, const char* file,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* function, va_list args)
|
2013-10-02 23:41:46 +04:00
|
|
|
{
|
2015-10-29 20:33:42 +03:00
|
|
|
BOOL status = FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
wLogMessage message = { 0 };
|
2019-10-01 13:14:45 +03:00
|
|
|
message.Type = type;
|
2016-10-07 15:04:40 +03:00
|
|
|
message.Level = level;
|
|
|
|
message.LineNumber = line;
|
|
|
|
message.FileName = file;
|
|
|
|
message.FunctionName = function;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
switch (type)
|
2013-10-09 07:43:57 +04:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
case WLOG_MESSAGE_TEXT:
|
|
|
|
message.FormatString = va_arg(args, const char*);
|
|
|
|
|
|
|
|
if (!strchr(message.FormatString, '%'))
|
|
|
|
{
|
2023-06-29 10:54:51 +03:00
|
|
|
message.TextString = message.FormatString;
|
2016-10-07 15:04:40 +03:00
|
|
|
status = WLog_Write(log, &message);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-10 18:55:48 +03:00
|
|
|
char formattedLogMessage[WLOG_MAX_STRING_SIZE] = { 0 };
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2022-04-27 22:18:48 +03:00
|
|
|
if (vsnprintf(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message.FormatString,
|
|
|
|
args) < 0)
|
2016-10-07 15:04:40 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
message.TextString = formattedLogMessage;
|
|
|
|
status = WLog_Write(log, &message);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WLOG_MESSAGE_DATA:
|
|
|
|
message.Data = va_arg(args, void*);
|
2021-06-16 19:09:14 +03:00
|
|
|
message.Length = va_arg(args, size_t);
|
2016-10-07 15:04:40 +03:00
|
|
|
status = WLog_WriteData(log, &message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WLOG_MESSAGE_IMAGE:
|
|
|
|
message.ImageData = va_arg(args, void*);
|
2021-06-16 19:09:14 +03:00
|
|
|
message.ImageWidth = va_arg(args, size_t);
|
|
|
|
message.ImageHeight = va_arg(args, size_t);
|
|
|
|
message.ImageBpp = va_arg(args, size_t);
|
2016-10-07 15:04:40 +03:00
|
|
|
status = WLog_WriteImage(log, &message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WLOG_MESSAGE_PACKET:
|
|
|
|
message.PacketData = va_arg(args, void*);
|
2021-06-16 19:09:14 +03:00
|
|
|
message.PacketLength = va_arg(args, size_t);
|
|
|
|
message.PacketFlags = va_arg(args, unsigned);
|
2016-10-07 15:04:40 +03:00
|
|
|
status = WLog_WritePacket(log, &message);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
2013-10-02 23:41:46 +04:00
|
|
|
|
|
|
|
return status;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, size_t line, const char* file,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* function, ...)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2015-10-21 11:11:06 +03:00
|
|
|
BOOL status;
|
2013-09-15 23:46:02 +04:00
|
|
|
va_list args;
|
2016-10-07 15:04:40 +03:00
|
|
|
va_start(args, function);
|
|
|
|
status = WLog_PrintMessageVA(log, type, level, line, file, function, args);
|
2013-09-15 23:46:02 +04:00
|
|
|
va_end(args);
|
2015-10-21 11:11:06 +03:00
|
|
|
return status;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
DWORD WLog_GetLogLevel(wLog* log)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2018-11-19 14:09:04 +03:00
|
|
|
if (!log)
|
|
|
|
return WLOG_OFF;
|
|
|
|
|
2018-11-19 15:04:40 +03:00
|
|
|
if (log->FilterLevel <= WLOG_FILTER_NOT_INITIALIZED)
|
2016-03-03 14:07:10 +03:00
|
|
|
log->FilterLevel = WLog_GetFilterLogLevel(log);
|
2016-03-02 16:11:15 +03:00
|
|
|
|
2018-11-19 15:04:40 +03:00
|
|
|
if (log->FilterLevel > WLOG_FILTER_NOT_FILTERED)
|
2018-11-19 14:09:04 +03:00
|
|
|
return (DWORD)log->FilterLevel;
|
2016-03-02 16:11:15 +03:00
|
|
|
else if (log->Level == WLOG_LEVEL_INHERIT)
|
2016-03-03 14:07:10 +03:00
|
|
|
log->Level = WLog_GetLogLevel(log->Parent);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2016-03-03 14:07:10 +03:00
|
|
|
return log->Level;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2018-11-22 18:34:38 +03:00
|
|
|
BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level)
|
|
|
|
{
|
|
|
|
DWORD level;
|
|
|
|
|
|
|
|
if (!_log)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
level = WLog_GetLogLevel(_log);
|
|
|
|
|
|
|
|
if (level == WLOG_OFF)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return _log_level >= level;
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level)
|
|
|
|
{
|
|
|
|
int lvl;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
if (!log || !level)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
lvl = WLog_ParseLogLevel(level);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
if (lvl < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2018-11-19 14:09:04 +03:00
|
|
|
return WLog_SetLogLevel(log, (DWORD)lvl);
|
2016-01-23 17:16:13 +03:00
|
|
|
}
|
|
|
|
|
2018-11-19 15:04:40 +03:00
|
|
|
static BOOL WLog_reset_log_filters(wLog* log)
|
|
|
|
{
|
|
|
|
DWORD x;
|
|
|
|
|
|
|
|
if (!log)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
for (x = 0; x < log->ChildrenCount; x++)
|
|
|
|
{
|
|
|
|
wLog* child = log->Children[x];
|
|
|
|
|
|
|
|
if (!WLog_reset_log_filters(child))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
static BOOL WLog_AddStringLogFilters_int(wLog* root, LPCSTR filter)
|
2016-01-23 17:16:13 +03:00
|
|
|
{
|
|
|
|
LPSTR p;
|
2021-06-16 19:09:14 +03:00
|
|
|
LPCSTR filterStr;
|
2016-01-23 17:16:13 +03:00
|
|
|
|
|
|
|
if (!filter)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-06-05 13:16:57 +03:00
|
|
|
DWORD count = 1;
|
|
|
|
LPCSTR cpp = filter;
|
2016-01-23 17:16:13 +03:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
while ((cpp = strchr(cpp, ',')) != NULL)
|
2016-01-23 17:16:13 +03:00
|
|
|
{
|
|
|
|
count++;
|
2021-06-16 19:09:14 +03:00
|
|
|
cpp++;
|
2016-01-23 17:16:13 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 13:16:57 +03:00
|
|
|
DWORD pos = g_FilterCount;
|
|
|
|
DWORD size = g_FilterCount + count;
|
|
|
|
wLogFilter* tmp = (wLogFilter*)realloc(g_Filters, size * sizeof(wLogFilter));
|
2016-01-23 17:16:13 +03:00
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return FALSE;
|
2016-01-27 13:21:04 +03:00
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
g_Filters = tmp;
|
2023-06-05 13:16:57 +03:00
|
|
|
LPSTR cp = (LPSTR)_strdup(filter);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
if (!cp)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
p = cp;
|
2016-01-27 13:21:04 +03:00
|
|
|
filterStr = cp;
|
2016-01-23 17:16:13 +03:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
p = strchr(p, ',');
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if (pos < size)
|
|
|
|
{
|
2021-04-16 14:55:59 +03:00
|
|
|
if (!WLog_ParseFilter(root, &g_Filters[pos++], filterStr))
|
2016-01-23 17:16:13 +03:00
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
free(cp);
|
2016-01-23 17:16:13 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
2016-01-27 13:21:04 +03:00
|
|
|
filterStr = p + 1;
|
2016-01-23 17:16:13 +03:00
|
|
|
p++;
|
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
} while (p != NULL);
|
2016-01-23 17:16:13 +03:00
|
|
|
|
|
|
|
g_FilterCount = size;
|
2016-10-07 15:04:40 +03:00
|
|
|
free(cp);
|
2021-04-16 14:55:59 +03:00
|
|
|
return WLog_reset_log_filters(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WLog_AddStringLogFilters(LPCSTR filter)
|
|
|
|
{
|
|
|
|
/* Ensure logger is initialized */
|
|
|
|
wLog* root = WLog_GetRoot();
|
|
|
|
return WLog_AddStringLogFilters_int(root, filter);
|
2016-01-23 17:16:13 +03:00
|
|
|
}
|
|
|
|
|
2018-11-19 14:09:04 +03:00
|
|
|
static BOOL WLog_UpdateInheritLevel(wLog* log, DWORD logLevel)
|
|
|
|
{
|
|
|
|
if (!log)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (log->inherit)
|
|
|
|
{
|
|
|
|
DWORD x;
|
|
|
|
log->Level = logLevel;
|
|
|
|
|
|
|
|
for (x = 0; x < log->ChildrenCount; x++)
|
|
|
|
{
|
|
|
|
wLog* child = log->Children[x];
|
|
|
|
|
|
|
|
if (!WLog_UpdateInheritLevel(child, logLevel))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2018-11-19 14:09:04 +03:00
|
|
|
DWORD x;
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!log)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-01-15 17:52:48 +04:00
|
|
|
if ((logLevel > WLOG_OFF) && (logLevel != WLOG_LEVEL_INHERIT))
|
|
|
|
logLevel = WLOG_OFF;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2013-09-16 00:48:09 +04:00
|
|
|
log->Level = logLevel;
|
2018-11-19 14:09:04 +03:00
|
|
|
log->inherit = (logLevel == WLOG_LEVEL_INHERIT) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
for (x = 0; x < log->ChildrenCount; x++)
|
|
|
|
{
|
|
|
|
wLog* child = log->Children[x];
|
|
|
|
|
|
|
|
if (!WLog_UpdateInheritLevel(child, logLevel))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2018-11-19 15:04:40 +03:00
|
|
|
return WLog_reset_log_filters(log);
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2016-01-23 17:16:13 +03:00
|
|
|
int WLog_ParseLogLevel(LPCSTR level)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
|
|
|
int iLevel = -1;
|
|
|
|
|
|
|
|
if (!level)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (_stricmp(level, "TRACE") == 0)
|
|
|
|
iLevel = WLOG_TRACE;
|
|
|
|
else if (_stricmp(level, "DEBUG") == 0)
|
|
|
|
iLevel = WLOG_DEBUG;
|
|
|
|
else if (_stricmp(level, "INFO") == 0)
|
|
|
|
iLevel = WLOG_INFO;
|
|
|
|
else if (_stricmp(level, "WARN") == 0)
|
|
|
|
iLevel = WLOG_WARN;
|
|
|
|
else if (_stricmp(level, "ERROR") == 0)
|
|
|
|
iLevel = WLOG_ERROR;
|
|
|
|
else if (_stricmp(level, "FATAL") == 0)
|
|
|
|
iLevel = WLOG_FATAL;
|
|
|
|
else if (_stricmp(level, "OFF") == 0)
|
|
|
|
iLevel = WLOG_OFF;
|
|
|
|
|
|
|
|
return iLevel;
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
BOOL WLog_ParseFilter(wLog* root, wLogFilter* filter, LPCSTR name)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
const char* pc;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* p;
|
|
|
|
char* q;
|
2021-06-16 19:09:14 +03:00
|
|
|
size_t count;
|
2014-05-29 23:23:15 +04:00
|
|
|
LPSTR names;
|
|
|
|
int iLevel;
|
|
|
|
count = 1;
|
2015-03-31 16:20:10 +03:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
WINPR_UNUSED(root);
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
if (!name)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-03-31 16:20:10 +03:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
pc = name;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2021-06-16 19:09:14 +03:00
|
|
|
if (pc)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
while ((pc = strchr(pc, '.')) != NULL)
|
2014-11-17 01:58:39 +03:00
|
|
|
{
|
|
|
|
count++;
|
2021-06-16 19:09:14 +03:00
|
|
|
pc++;
|
2014-11-17 01:58:39 +03:00
|
|
|
}
|
2014-05-29 23:23:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
names = _strdup(name);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-03-31 16:20:10 +03:00
|
|
|
if (!names)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-29 23:23:15 +04:00
|
|
|
filter->NameCount = count;
|
2019-11-06 17:24:51 +03:00
|
|
|
filter->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR));
|
2016-10-07 15:04:40 +03:00
|
|
|
|
|
|
|
if (!filter->Names)
|
2015-03-31 16:20:10 +03:00
|
|
|
{
|
|
|
|
free(names);
|
|
|
|
filter->NameCount = 0;
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-03-31 16:20:10 +03:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-29 23:23:15 +04:00
|
|
|
filter->Names[count] = NULL;
|
|
|
|
count = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)names;
|
2014-05-29 23:23:15 +04:00
|
|
|
filter->Names[count++] = p;
|
|
|
|
q = strrchr(p, ':');
|
|
|
|
|
|
|
|
if (!q)
|
2015-03-31 16:20:10 +03:00
|
|
|
{
|
|
|
|
free(names);
|
|
|
|
free(filter->Names);
|
|
|
|
filter->Names = NULL;
|
|
|
|
filter->NameCount = 0;
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-03-31 16:20:10 +03:00
|
|
|
}
|
2014-05-29 23:23:15 +04:00
|
|
|
|
|
|
|
*q = '\0';
|
|
|
|
q++;
|
|
|
|
iLevel = WLog_ParseLogLevel(q);
|
|
|
|
|
|
|
|
if (iLevel < 0)
|
2015-03-31 16:20:10 +03:00
|
|
|
{
|
|
|
|
free(names);
|
|
|
|
free(filter->Names);
|
|
|
|
filter->Names = NULL;
|
|
|
|
filter->NameCount = 0;
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-03-31 16:20:10 +03:00
|
|
|
}
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
filter->Level = (DWORD)iLevel;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
|
|
|
while ((p = strchr(p, '.')) != NULL)
|
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
if (count < filter->NameCount)
|
2015-03-24 11:18:06 +03:00
|
|
|
filter->Names[count++] = p + 1;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-05-29 23:23:15 +04:00
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2014-05-29 23:23:15 +04:00
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
BOOL WLog_ParseFilters(wLog* root)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
2017-03-03 14:37:27 +03:00
|
|
|
LPCSTR filter = "WLOG_FILTER";
|
|
|
|
BOOL res = FALSE;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* env;
|
2014-05-29 23:23:15 +04:00
|
|
|
DWORD nSize;
|
2018-11-19 15:04:40 +03:00
|
|
|
free(g_Filters);
|
2016-01-23 17:16:13 +03:00
|
|
|
g_Filters = NULL;
|
|
|
|
g_FilterCount = 0;
|
2017-03-03 14:37:27 +03:00
|
|
|
nSize = GetEnvironmentVariableA(filter, NULL, 0);
|
2014-05-29 23:23:15 +04:00
|
|
|
|
|
|
|
if (nSize < 1)
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
env = (LPSTR)malloc(nSize);
|
2014-05-29 23:23:15 +04:00
|
|
|
|
|
|
|
if (!env)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
if (GetEnvironmentVariableA(filter, env, nSize) == nSize - 1)
|
2021-04-16 14:55:59 +03:00
|
|
|
res = WLog_AddStringLogFilters_int(root, env);
|
2018-04-03 10:59:42 +03:00
|
|
|
|
2014-12-27 23:34:45 +03:00
|
|
|
free(env);
|
2016-01-23 17:16:13 +03:00
|
|
|
return res;
|
2014-05-29 23:23:15 +04:00
|
|
|
}
|
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
LONG WLog_GetFilterLogLevel(wLog* log)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
|
|
|
DWORD i, j;
|
|
|
|
BOOL match = FALSE;
|
|
|
|
|
2016-03-02 21:02:01 +03:00
|
|
|
if (log->FilterLevel >= 0)
|
|
|
|
return log->FilterLevel;
|
|
|
|
|
2022-12-21 20:56:52 +03:00
|
|
|
log->FilterLevel = WLOG_FILTER_NOT_FILTERED;
|
2014-05-29 23:23:15 +04:00
|
|
|
for (i = 0; i < g_FilterCount; i++)
|
|
|
|
{
|
2022-12-21 20:56:52 +03:00
|
|
|
const wLogFilter* filter = &g_Filters[i];
|
|
|
|
for (j = 0; j < filter->NameCount; j++)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
|
|
|
if (j >= log->NameCount)
|
|
|
|
break;
|
|
|
|
|
2022-12-21 20:56:52 +03:00
|
|
|
if (_stricmp(filter->Names[j], "*") == 0)
|
2014-05-29 23:23:15 +04:00
|
|
|
{
|
|
|
|
match = TRUE;
|
2022-12-21 20:56:52 +03:00
|
|
|
log->FilterLevel = filter->Level;
|
2014-05-29 23:23:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:56:52 +03:00
|
|
|
if (_stricmp(filter->Names[j], log->Names[j]) != 0)
|
2014-05-29 23:23:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (j == (log->NameCount - 1))
|
|
|
|
{
|
2022-12-21 20:56:52 +03:00
|
|
|
match = log->NameCount == filter->NameCount;
|
|
|
|
if (match)
|
|
|
|
log->FilterLevel = filter->Level;
|
2014-05-29 23:23:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-03-02 21:02:01 +03:00
|
|
|
return log->FilterLevel;
|
2014-05-29 23:23:15 +04:00
|
|
|
}
|
|
|
|
|
2021-08-26 12:23:32 +03:00
|
|
|
static BOOL WLog_ParseName(wLog* log, LPCSTR name)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
const char* cp = name;
|
2014-08-18 21:34:47 +04:00
|
|
|
char* p;
|
2022-04-27 22:02:18 +03:00
|
|
|
size_t count = 1;
|
2013-10-02 00:08:26 +04:00
|
|
|
LPSTR names;
|
|
|
|
|
2022-04-27 22:02:18 +03:00
|
|
|
while ((cp = strchr(cp, '.')) != NULL)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
|
|
|
count++;
|
2022-04-27 22:02:18 +03:00
|
|
|
cp++;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
names = _strdup(name);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!names)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
log->NameCount = count;
|
2019-11-06 17:24:51 +03:00
|
|
|
log->Names = (LPSTR*)calloc((count + 1UL), sizeof(LPSTR));
|
2016-10-07 15:04:40 +03:00
|
|
|
|
|
|
|
if (!log->Names)
|
2015-03-23 19:25:23 +03:00
|
|
|
{
|
|
|
|
free(names);
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-03-23 19:25:23 +03:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
log->Names[count] = NULL;
|
|
|
|
count = 0;
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)names;
|
2013-10-02 00:08:26 +04:00
|
|
|
log->Names[count++] = p;
|
|
|
|
|
|
|
|
while ((p = strchr(p, '.')) != NULL)
|
|
|
|
{
|
2021-06-16 19:09:14 +03:00
|
|
|
if (count < log->NameCount)
|
2015-03-24 11:18:06 +03:00
|
|
|
log->Names[count++] = p + 1;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
*p = '\0';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
wLog* WLog_New(LPCSTR name, wLog* rootLogger)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2015-03-23 19:25:23 +03:00
|
|
|
wLog* log = NULL;
|
|
|
|
char* env = NULL;
|
2013-10-09 06:35:14 +04:00
|
|
|
DWORD nSize;
|
2014-05-29 23:23:15 +04:00
|
|
|
int iLevel;
|
2019-11-06 17:24:51 +03:00
|
|
|
log = (wLog*)calloc(1, sizeof(wLog));
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!log)
|
2015-03-25 19:37:46 +03:00
|
|
|
return NULL;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
log->Name = _strdup(name);
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
if (!log->Name)
|
2015-03-23 19:25:23 +03:00
|
|
|
goto out_fail;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
if (!WLog_ParseName(log, name))
|
2015-03-23 19:25:23 +03:00
|
|
|
goto out_fail;
|
2013-10-02 02:19:50 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
log->Parent = rootLogger;
|
|
|
|
log->ChildrenCount = 0;
|
|
|
|
log->ChildrenSize = 16;
|
2018-11-19 15:04:40 +03:00
|
|
|
log->FilterLevel = WLOG_FILTER_NOT_INITIALIZED;
|
2013-10-09 06:35:14 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!(log->Children = (wLog**)calloc(log->ChildrenSize, sizeof(wLog*))))
|
2015-03-23 19:25:23 +03:00
|
|
|
goto out_fail;
|
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
log->Appender = NULL;
|
2013-10-09 06:35:14 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
if (rootLogger)
|
|
|
|
{
|
|
|
|
log->Level = WLOG_LEVEL_INHERIT;
|
2018-11-19 14:09:04 +03:00
|
|
|
log->inherit = TRUE;
|
2016-03-02 16:11:15 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-03 14:37:27 +03:00
|
|
|
LPCSTR level = "WLOG_LEVEL";
|
2016-03-02 16:11:15 +03:00
|
|
|
log->Level = WLOG_INFO;
|
2017-03-03 14:37:27 +03:00
|
|
|
nSize = GetEnvironmentVariableA(level, NULL, 0);
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
if (nSize)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
env = (LPSTR)malloc(nSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-06-14 01:55:09 +03:00
|
|
|
if (!env)
|
|
|
|
goto out_fail;
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2017-03-03 14:37:27 +03:00
|
|
|
if (GetEnvironmentVariableA(level, env, nSize) != nSize - 1)
|
2015-06-14 01:55:09 +03:00
|
|
|
{
|
2017-03-03 14:37:27 +03:00
|
|
|
fprintf(stderr, "%s environment variable changed in my back !\n", level);
|
2014-05-29 23:23:15 +04:00
|
|
|
free(env);
|
2015-06-14 01:55:09 +03:00
|
|
|
goto out_fail;
|
2013-10-09 06:35:14 +04:00
|
|
|
}
|
2015-06-14 01:55:09 +03:00
|
|
|
|
|
|
|
iLevel = WLog_ParseLogLevel(env);
|
|
|
|
free(env);
|
|
|
|
|
|
|
|
if (iLevel >= 0)
|
2018-11-19 14:09:04 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WLog_SetLogLevel(log, (DWORD)iLevel))
|
2018-11-19 14:09:04 +03:00
|
|
|
goto out_fail;
|
|
|
|
}
|
2016-03-02 16:11:15 +03:00
|
|
|
}
|
|
|
|
}
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
iLevel = WLog_GetFilterLogLevel(log);
|
2014-05-29 23:23:15 +04:00
|
|
|
|
2018-11-19 14:09:04 +03:00
|
|
|
if (iLevel >= 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!WLog_SetLogLevel(log, (DWORD)iLevel))
|
2018-11-19 14:09:04 +03:00
|
|
|
goto out_fail;
|
|
|
|
}
|
2013-09-16 00:48:09 +04:00
|
|
|
|
2021-05-26 22:42:30 +03:00
|
|
|
InitializeCriticalSectionAndSpinCount(&log->lock, 4000);
|
|
|
|
|
2013-09-16 00:48:09 +04:00
|
|
|
return log;
|
2015-03-23 19:25:23 +03:00
|
|
|
out_fail:
|
2016-10-07 15:04:40 +03:00
|
|
|
free(log->Children);
|
|
|
|
free(log->Name);
|
|
|
|
free(log);
|
2015-03-23 19:25:23 +03:00
|
|
|
return NULL;
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
|
|
|
|
2014-08-18 21:34:47 +04:00
|
|
|
void WLog_Free(wLog* log)
|
2013-09-15 23:46:02 +04:00
|
|
|
{
|
2013-09-16 00:48:09 +04:00
|
|
|
if (log)
|
|
|
|
{
|
2013-09-16 20:05:08 +04:00
|
|
|
if (log->Appender)
|
|
|
|
{
|
|
|
|
WLog_Appender_Free(log, log->Appender);
|
|
|
|
log->Appender = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-16 00:48:09 +04:00
|
|
|
free(log->Name);
|
2013-10-02 00:08:26 +04:00
|
|
|
free(log->Names[0]);
|
|
|
|
free(log->Names);
|
2013-10-04 14:41:33 +04:00
|
|
|
free(log->Children);
|
2021-05-26 22:42:30 +03:00
|
|
|
DeleteCriticalSection(&log->lock);
|
2013-09-16 00:48:09 +04:00
|
|
|
free(log);
|
|
|
|
}
|
2013-09-15 23:46:02 +04:00
|
|
|
}
|
2013-10-02 02:19:50 +04:00
|
|
|
|
2016-03-02 16:11:15 +03:00
|
|
|
wLog* WLog_GetRoot(void)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2018-04-03 10:59:42 +03:00
|
|
|
if (!InitOnceExecuteOnce(&_WLogInitialized, WLog_InitializeRoot, NULL, NULL))
|
|
|
|
return NULL;
|
2013-10-02 02:19:50 +04:00
|
|
|
|
|
|
|
return g_RootLog;
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:59:42 +03:00
|
|
|
static BOOL WLog_AddChild(wLog* parent, wLog* child)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2021-05-26 22:42:30 +03:00
|
|
|
BOOL status = FALSE;
|
|
|
|
|
|
|
|
WLog_Lock(parent);
|
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
if (parent->ChildrenCount >= parent->ChildrenSize)
|
|
|
|
{
|
2016-10-07 15:04:40 +03:00
|
|
|
wLog** tmp;
|
2013-10-02 02:19:50 +04:00
|
|
|
parent->ChildrenSize *= 2;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-11-17 01:41:28 +03:00
|
|
|
if (!parent->ChildrenSize)
|
|
|
|
{
|
|
|
|
if (parent->Children)
|
2016-10-07 15:04:40 +03:00
|
|
|
free(parent->Children);
|
|
|
|
|
2014-11-17 01:41:28 +03:00
|
|
|
parent->Children = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
tmp = (wLog**)realloc(parent->Children, sizeof(wLog*) * parent->ChildrenSize);
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-11-17 01:41:28 +03:00
|
|
|
if (!tmp)
|
|
|
|
{
|
|
|
|
if (parent->Children)
|
2016-10-07 15:04:40 +03:00
|
|
|
free(parent->Children);
|
|
|
|
|
2014-11-17 01:41:28 +03:00
|
|
|
parent->Children = NULL;
|
2021-05-26 22:42:30 +03:00
|
|
|
goto exit;
|
2014-11-17 01:41:28 +03:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2014-11-17 01:50:46 +03:00
|
|
|
parent->Children = tmp;
|
2014-11-17 01:41:28 +03:00
|
|
|
}
|
2013-10-02 02:19:50 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 02:50:15 +03:00
|
|
|
if (!parent->Children)
|
2021-05-26 22:42:30 +03:00
|
|
|
goto exit;
|
2014-11-17 02:50:15 +03:00
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
parent->Children[parent->ChildrenCount++] = child;
|
|
|
|
child->Parent = parent;
|
2021-05-26 22:42:30 +03:00
|
|
|
|
|
|
|
WLog_Unlock(parent);
|
|
|
|
|
|
|
|
status = TRUE;
|
|
|
|
exit:
|
|
|
|
return status;
|
2013-10-02 02:19:50 +04:00
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
static wLog* WLog_FindChild(wLog* root, LPCSTR name)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
DWORD index;
|
2014-08-18 21:34:47 +04:00
|
|
|
wLog* child = NULL;
|
2013-10-02 02:19:50 +04:00
|
|
|
BOOL found = FALSE;
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!root)
|
|
|
|
return NULL;
|
|
|
|
|
2021-05-26 22:42:30 +03:00
|
|
|
WLog_Lock(root);
|
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
for (index = 0; index < root->ChildrenCount; index++)
|
|
|
|
{
|
|
|
|
child = root->Children[index];
|
|
|
|
|
|
|
|
if (strcmp(child->Name, name) == 0)
|
|
|
|
{
|
|
|
|
found = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 22:42:30 +03:00
|
|
|
WLog_Unlock(root);
|
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
return (found) ? child : NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
static wLog* WLog_Get_int(wLog* root, LPCSTR name)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2014-08-18 21:34:47 +04:00
|
|
|
wLog* log;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
if (!(log = WLog_FindChild(root, name)))
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!root)
|
2015-03-23 19:25:23 +03:00
|
|
|
return NULL;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!(log = WLog_New(name, root)))
|
2015-03-23 19:25:23 +03:00
|
|
|
return NULL;
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
if (!WLog_AddChild(root, log))
|
|
|
|
{
|
|
|
|
WLog_Free(log);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-02 02:19:50 +04:00
|
|
|
}
|
2016-10-07 15:04:40 +03:00
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2021-04-16 14:55:59 +03:00
|
|
|
wLog* WLog_Get(LPCSTR name)
|
|
|
|
{
|
|
|
|
wLog* root = WLog_GetRoot();
|
|
|
|
return WLog_Get_int(root, name);
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
#if defined(WITH_WINPR_DEPRECATED)
|
2016-10-07 15:04:40 +03:00
|
|
|
BOOL WLog_Init(void)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2015-06-14 01:55:09 +03:00
|
|
|
return WLog_GetRoot() != NULL;
|
2013-10-02 02:19:50 +04:00
|
|
|
}
|
|
|
|
|
2016-10-07 15:04:40 +03:00
|
|
|
BOOL WLog_Uninit(void)
|
2013-10-02 02:19:50 +04:00
|
|
|
{
|
2021-05-26 22:42:30 +03:00
|
|
|
DWORD index;
|
|
|
|
wLog* root = g_RootLog;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
WLog_Lock(root);
|
|
|
|
|
2021-06-01 10:54:27 +03:00
|
|
|
for (index = 0; index < root->ChildrenCount; index++)
|
2021-05-26 22:42:30 +03:00
|
|
|
{
|
2021-06-01 10:54:27 +03:00
|
|
|
wLog* child = root->Children[index];
|
2021-05-26 22:42:30 +03:00
|
|
|
WLog_Free(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
WLog_Unlock(root);
|
|
|
|
|
|
|
|
WLog_Free(root);
|
|
|
|
g_RootLog = NULL;
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 02:19:50 +04:00
|
|
|
}
|
2021-07-28 16:18:03 +03:00
|
|
|
#endif
|
2023-06-29 10:54:51 +03:00
|
|
|
|
|
|
|
BOOL WLog_SetContext(wLog* log, const char* (*fkt)(void*), void* context)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(log);
|
|
|
|
|
|
|
|
log->custom = fkt;
|
|
|
|
log->context = context;
|
|
|
|
return TRUE;
|
|
|
|
}
|