FreeRDP/winpr/libwinpr/utils/wlog/wlog.c

943 lines
17 KiB
C
Raw Normal View History

/**
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
2013-09-16 00:48:09 +04:00
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/debug.h>
#include <winpr/environment.h>
#include <winpr/wlog.h>
#if defined(ANDROID)
#include <android/log.h>
2015-11-09 21:37:02 +03:00
#include "../log.h"
#endif
#include "wlog.h"
struct _wLogFilter
{
DWORD Level;
LPSTR* Names;
DWORD NameCount;
};
typedef struct _wLogFilter wLogFilter;
/**
* 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
*/
LPCSTR WLOG_LEVELS[7] =
{
"TRACE",
"DEBUG",
"INFO",
"WARN",
"ERROR",
"FATAL",
"OFF"
2013-09-16 00:48:09 +04:00
};
static INIT_ONCE _WLogInitialized = INIT_ONCE_STATIC_INIT;
static DWORD g_FilterCount = 0;
2014-08-18 21:34:47 +04:00
static wLogFilter* g_Filters = NULL;
static wLog* g_RootLog = NULL;
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);
static int WLog_ParseLogLevel(LPCSTR level);
static BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name);
static BOOL WLog_ParseFilters(void);
#if !defined(_WIN32)
static void WLog_Uninit_(void) __attribute__((destructor));
#endif
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;
}
static BOOL CALLBACK WLog_InitializeRoot(PINIT_ONCE InitOnce, PVOID Parameter, PVOID* Context)
{
char* env;
DWORD nSize;
DWORD logAppenderType;
LPCSTR appender = "WLOG_APPENDER";
if (!(g_RootLog = WLog_New("", NULL)))
return FALSE;
g_RootLog->IsRoot = TRUE;
WLog_ParseFilters();
logAppenderType = WLOG_APPENDER_CONSOLE;
nSize = GetEnvironmentVariableA(appender, NULL, 0);
if (nSize)
{
env = (LPSTR) malloc(nSize);
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;
#ifdef HAVE_SYSLOG_H
else if (_stricmp(env, "SYSLOG") == 0)
logAppenderType = WLOG_APPENDER_SYSLOG;
#endif /* HAVE_SYSLOG_H */
#ifdef HAVE_JOURNALD_H
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;
#if defined(_WIN32)
atexit(WLog_Uninit_);
#endif
return TRUE;
fail:
free(g_RootLog);
g_RootLog = NULL;
return FALSE;
}
static BOOL log_recursion(LPCSTR file, LPCSTR fkt, int line)
2013-09-16 00:48:09 +04:00
{
2015-10-23 13:08:20 +03:00
char** msg;
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)
LPCSTR tag = WINPR_TAG("utils.wlog");
2015-10-23 13:08:20 +03:00
#endif
if (!bt)
return FALSE;
2015-10-23 13:08:20 +03:00
msg = winpr_backtrace_symbols(bt, &used);
if (!msg)
return FALSE;
#if defined(ANDROID)
if (__android_log_print(ANDROID_LOG_FATAL, tag, "Recursion detected!!!") < 0)
return FALSE;
if (__android_log_print(ANDROID_LOG_FATAL, tag, "Check %s [%s:%d]", fkt, file,
line) < 0)
return FALSE;
2014-08-18 21:34:47 +04:00
for (i = 0; i < used; i++)
2016-12-02 12:15:17 +03:00
if (__android_log_print(ANDROID_LOG_FATAL, tag, "%zd: %s", i, msg[i]) < 0)
return FALSE;
2014-08-18 21:34:47 +04:00
#else
if (fprintf(stderr, "[%s]: Recursion detected!\n", fkt) < 0)
return FALSE;
if (fprintf(stderr, "[%s]: Check %s:%d\n", fkt, file, line) < 0)
return FALSE;
2014-08-18 21:34:47 +04:00
for (i = 0; i < used; i++)
if (fprintf(stderr, "%s: %"PRIuz": %s\n", fkt, i, msg[i]) < 0)
return FALSE;
2014-08-18 21:34:47 +04:00
#endif
2015-05-11 10:07:39 +03:00
free(msg);
winpr_backtrace_free(bt);
return TRUE;
}
static BOOL WLog_Write(wLog* log, wLogMessage* message)
{
BOOL status;
2014-08-18 21:34:47 +04:00
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return FALSE;
if (!appender->active)
if (!WLog_OpenAppender(log))
return FALSE;
if (!appender->WriteMessage)
return FALSE;
EnterCriticalSection(&appender->lock);
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;
}
LeaveCriticalSection(&appender->lock);
return status;
}
static BOOL WLog_WriteData(wLog* log, wLogMessage* message)
{
BOOL status;
2014-08-18 21:34:47 +04:00
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return FALSE;
if (!appender->active)
if (!WLog_OpenAppender(log))
return FALSE;
if (!appender->WriteDataMessage)
return FALSE;
EnterCriticalSection(&appender->lock);
if (appender->recursive)
status = log_recursion(message->FileName, message->FunctionName,
message->LineNumber);
else
{
appender->recursive = TRUE;
status = appender->WriteDataMessage(log, appender, message);
appender->recursive = FALSE;
}
LeaveCriticalSection(&appender->lock);
return status;
}
static BOOL WLog_WriteImage(wLog* log, wLogMessage* message)
{
BOOL status;
2014-08-18 21:34:47 +04:00
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return FALSE;
if (!appender->active)
if (!WLog_OpenAppender(log))
return FALSE;
if (!appender->WriteImageMessage)
return FALSE;
EnterCriticalSection(&appender->lock);
if (appender->recursive)
status = log_recursion(message->FileName, message->FunctionName,
message->LineNumber);
else
{
appender->recursive = TRUE;
status = appender->WriteImageMessage(log, appender, message);
appender->recursive = FALSE;
}
LeaveCriticalSection(&appender->lock);
return status;
}
static BOOL WLog_WritePacket(wLog* log, wLogMessage* message)
{
BOOL status;
2014-08-18 21:34:47 +04:00
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return FALSE;
if (!appender->active)
if (!WLog_OpenAppender(log))
return FALSE;
if (!appender->WritePacketMessage)
return FALSE;
EnterCriticalSection(&appender->lock);
if (appender->recursive)
status = log_recursion(message->FileName, message->FunctionName,
message->LineNumber);
else
{
appender->recursive = TRUE;
status = appender->WritePacketMessage(log, appender, message);
appender->recursive = FALSE;
}
LeaveCriticalSection(&appender->lock);
return status;
}
BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, DWORD line,
const char* file, const char* function, va_list args)
{
2015-10-29 20:33:42 +03:00
BOOL status = FALSE;
wLogMessage message = { 0 };
message.Level = level;
message.LineNumber = line;
message.FileName = file;
message.FunctionName = function;
switch (type)
{
case WLOG_MESSAGE_TEXT:
message.FormatString = va_arg(args, const char*);
if (!strchr(message.FormatString, '%'))
{
message.TextString = (LPSTR) message.FormatString;
status = WLog_Write(log, &message);
}
else
{
char formattedLogMessage[WLOG_MAX_STRING_SIZE];
if (wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1,
message.FormatString, args) < 0)
return FALSE;
message.TextString = formattedLogMessage;
status = WLog_Write(log, &message);
}
break;
case WLOG_MESSAGE_DATA:
message.Data = va_arg(args, void*);
message.Length = va_arg(args, int);
status = WLog_WriteData(log, &message);
break;
case WLOG_MESSAGE_IMAGE:
message.ImageData = va_arg(args, void*);
message.ImageWidth = va_arg(args, int);
message.ImageHeight = va_arg(args, int);
message.ImageBpp = va_arg(args, int);
status = WLog_WriteImage(log, &message);
break;
case WLOG_MESSAGE_PACKET:
message.PacketData = va_arg(args, void*);
message.PacketLength = va_arg(args, int);
message.PacketFlags = va_arg(args, int);
status = WLog_WritePacket(log, &message);
break;
default:
break;
}
return status;
}
BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, DWORD line,
const char* file, const char* function, ...)
{
BOOL status;
va_list args;
va_start(args, function);
status = WLog_PrintMessageVA(log, type, level, line, file, function, args);
va_end(args);
return status;
}
2014-08-18 21:34:47 +04:00
DWORD WLog_GetLogLevel(wLog* log)
{
if (log->FilterLevel < 0)
log->FilterLevel = WLog_GetFilterLogLevel(log);
2016-03-02 16:11:15 +03:00
if ((log->FilterLevel >= 0) && (log->FilterLevel != WLOG_LEVEL_INHERIT))
return log->FilterLevel;
2016-03-02 16:11:15 +03:00
else if (log->Level == WLOG_LEVEL_INHERIT)
log->Level = WLog_GetLogLevel(log->Parent);
return log->Level;
}
BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level)
{
int lvl;
if (!log || !level)
return FALSE;
lvl = WLog_ParseLogLevel(level);
if (lvl < 0)
return FALSE;
return WLog_SetLogLevel(log, lvl);
}
BOOL WLog_AddStringLogFilters(LPCSTR filter)
{
DWORD pos;
DWORD size;
DWORD count;
LPSTR p;
LPSTR filterStr;
LPSTR cp;
wLogFilter* tmp;
if (!filter)
return FALSE;
count = 1;
p = (LPSTR)filter;
while ((p = strchr(p, ',')) != NULL)
{
count++;
p++;
}
pos = g_FilterCount;
size = g_FilterCount + count;
tmp = (wLogFilter*) realloc(g_Filters, size * sizeof(wLogFilter));
if (!tmp)
return FALSE;
g_Filters = tmp;
cp = (LPSTR)_strdup(filter);
if (!cp)
return FALSE;
p = cp;
filterStr = cp;
do
{
p = strchr(p, ',');
if (p)
*p = '\0';
if (pos < size)
{
2016-02-03 14:45:49 +03:00
if (!WLog_ParseFilter(&g_Filters[pos++], filterStr))
{
free(cp);
return FALSE;
}
}
else
break;
if (p)
{
filterStr = p + 1;
p++;
}
}
while (p != NULL);
g_FilterCount = size;
free(cp);
return TRUE;
}
BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel)
{
if (!log)
return FALSE;
if ((logLevel > WLOG_OFF) && (logLevel != WLOG_LEVEL_INHERIT))
logLevel = WLOG_OFF;
2013-09-16 00:48:09 +04:00
log->Level = logLevel;
return TRUE;
}
int WLog_ParseLogLevel(LPCSTR level)
{
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;
}
BOOL WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
{
2014-08-18 21:34:47 +04:00
char* p;
char* q;
int count;
LPSTR names;
int iLevel;
count = 1;
if (!name)
return FALSE;
2014-08-18 21:34:47 +04:00
p = (char*) name;
2014-11-17 01:58:39 +03:00
if (p)
{
2014-11-17 01:58:39 +03:00
while ((p = strchr(p, '.')) != NULL)
{
count++;
p++;
}
}
names = _strdup(name);
if (!names)
return FALSE;
filter->NameCount = count;
filter->Names = (LPSTR*) calloc((count + 1UL), sizeof(LPSTR));
if (!filter->Names)
{
free(names);
filter->NameCount = 0;
return FALSE;
}
filter->Names[count] = NULL;
count = 0;
2014-08-18 21:34:47 +04:00
p = (char*) names;
filter->Names[count++] = p;
q = strrchr(p, ':');
if (!q)
{
free(names);
free(filter->Names);
filter->Names = NULL;
filter->NameCount = 0;
return FALSE;
}
*q = '\0';
q++;
iLevel = WLog_ParseLogLevel(q);
if (iLevel < 0)
{
free(names);
free(filter->Names);
filter->Names = NULL;
filter->NameCount = 0;
return FALSE;
}
filter->Level = (DWORD) iLevel;
while ((p = strchr(p, '.')) != NULL)
{
if (count < (int) filter->NameCount)
filter->Names[count++] = p + 1;
*p = '\0';
p++;
}
return TRUE;
}
2016-03-02 16:11:15 +03:00
BOOL WLog_ParseFilters(void)
{
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;
DWORD nSize;
g_Filters = NULL;
g_FilterCount = 0;
2017-03-03 14:37:27 +03:00
nSize = GetEnvironmentVariableA(filter, NULL, 0);
if (nSize < 1)
return TRUE;
env = (LPSTR) malloc(nSize);
if (!env)
return FALSE;
2017-03-03 14:37:27 +03:00
if (GetEnvironmentVariableA(filter, env, nSize) == nSize - 1)
res = WLog_AddStringLogFilters(env);
2014-12-27 23:34:45 +03:00
free(env);
return res;
}
2016-03-02 16:11:15 +03:00
LONG WLog_GetFilterLogLevel(wLog* log)
{
DWORD i, j;
BOOL match = FALSE;
2016-03-02 21:02:01 +03:00
if (log->FilterLevel >= 0)
return log->FilterLevel;
for (i = 0; i < g_FilterCount; i++)
{
for (j = 0; j < g_Filters[i].NameCount; j++)
{
if (j >= log->NameCount)
break;
if (_stricmp(g_Filters[i].Names[j], "*") == 0)
{
match = TRUE;
break;
}
if (_stricmp(g_Filters[i].Names[j], log->Names[j]) != 0)
break;
if (j == (log->NameCount - 1))
{
match = TRUE;
break;
}
}
if (match)
break;
}
if (match)
2016-03-02 21:02:01 +03:00
log->FilterLevel = g_Filters[i].Level;
else
log->FilterLevel = WLOG_LEVEL_INHERIT;
2016-03-02 21:02:01 +03:00
return log->FilterLevel;
}
static BOOL WLog_ParseName(wLog* log, LPCSTR name)
2013-10-02 00:08:26 +04:00
{
2014-08-18 21:34:47 +04:00
char* p;
2013-10-02 00:08:26 +04:00
int count;
LPSTR names;
count = 1;
2014-08-18 21:34:47 +04:00
p = (char*) name;
2013-10-02 00:08:26 +04:00
while ((p = strchr(p, '.')) != NULL)
{
count++;
p++;
}
names = _strdup(name);
if (!names)
return FALSE;
2013-10-02 00:08:26 +04:00
log->NameCount = count;
log->Names = (LPSTR*) calloc((count + 1UL), sizeof(LPSTR));
if (!log->Names)
{
free(names);
return FALSE;
}
2013-10-02 00:08:26 +04:00
log->Names[count] = NULL;
count = 0;
2014-08-18 21:34:47 +04:00
p = (char*) names;
2013-10-02 00:08:26 +04:00
log->Names[count++] = p;
while ((p = strchr(p, '.')) != NULL)
{
if (count < (int) log->NameCount)
log->Names[count++] = p + 1;
2013-10-02 00:08:26 +04:00
*p = '\0';
p++;
}
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)
{
wLog* log = NULL;
char* env = NULL;
DWORD nSize;
int iLevel;
log = (wLog*) calloc(1, sizeof(wLog));
if (!log)
return NULL;
2016-03-02 16:11:15 +03:00
log->Name = _strdup(name);
2016-03-02 16:11:15 +03:00
if (!log->Name)
goto out_fail;
2016-03-02 16:11:15 +03:00
if (!WLog_ParseName(log, name))
goto out_fail;
2016-03-02 16:11:15 +03:00
log->Parent = rootLogger;
log->ChildrenCount = 0;
log->ChildrenSize = 16;
2016-03-02 21:02:01 +03:00
log->FilterLevel = -1;
2016-03-02 16:11:15 +03:00
if (!(log->Children = (wLog**) calloc(log->ChildrenSize, sizeof(wLog*))))
goto out_fail;
2016-03-02 16:11:15 +03:00
log->Appender = NULL;
2016-03-02 16:11:15 +03:00
if (rootLogger)
{
log->Level = WLOG_LEVEL_INHERIT;
}
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);
2016-03-02 16:11:15 +03:00
if (nSize)
{
env = (LPSTR) malloc(nSize);
if (!env)
goto out_fail;
2017-03-03 14:37:27 +03:00
if (GetEnvironmentVariableA(level, env, nSize) != nSize - 1)
{
2017-03-03 14:37:27 +03:00
fprintf(stderr, "%s environment variable changed in my back !\n", level);
free(env);
goto out_fail;
}
iLevel = WLog_ParseLogLevel(env);
free(env);
if (iLevel >= 0)
log->Level = (DWORD) iLevel;
2016-03-02 16:11:15 +03:00
}
}
2016-03-02 16:11:15 +03:00
iLevel = WLog_GetFilterLogLevel(log);
if ((iLevel >= 0) && (iLevel != WLOG_LEVEL_INHERIT))
2016-03-02 16:11:15 +03:00
log->Level = (DWORD) iLevel;
2013-09-16 00:48:09 +04:00
return log;
out_fail:
free(log->Children);
free(log->Name);
free(log);
return NULL;
}
2014-08-18 21:34:47 +04:00
void WLog_Free(wLog* log)
{
2013-09-16 00:48:09 +04:00
if (log)
{
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);
2013-09-16 00:48:09 +04:00
free(log);
}
}
2016-03-02 16:11:15 +03:00
wLog* WLog_GetRoot(void)
{
if (!InitOnceExecuteOnce(&_WLogInitialized, WLog_InitializeRoot, NULL, NULL))
return NULL;
return g_RootLog;
}
static BOOL WLog_AddChild(wLog* parent, wLog* child)
{
if (parent->ChildrenCount >= parent->ChildrenSize)
{
wLog** tmp;
parent->ChildrenSize *= 2;
2014-11-17 01:41:28 +03:00
if (!parent->ChildrenSize)
{
if (parent->Children)
free(parent->Children);
2014-11-17 01:41:28 +03:00
parent->Children = NULL;
}
else
{
tmp = (wLog**) realloc(parent->Children, sizeof(wLog*) * parent->ChildrenSize);
2014-11-17 01:41:28 +03:00
if (!tmp)
{
if (parent->Children)
free(parent->Children);
2014-11-17 01:41:28 +03:00
parent->Children = NULL;
return FALSE;
2014-11-17 01:41:28 +03:00
}
2014-11-17 01:50:46 +03:00
parent->Children = tmp;
2014-11-17 01:41:28 +03:00
}
}
2014-11-17 02:50:15 +03:00
if (!parent->Children)
return FALSE;
2014-11-17 02:50:15 +03:00
parent->Children[parent->ChildrenCount++] = child;
child->Parent = parent;
return TRUE;
}
static wLog* WLog_FindChild(LPCSTR name)
{
2014-02-11 07:12:13 +04:00
DWORD index;
2014-08-18 21:34:47 +04:00
wLog* root;
wLog* child = NULL;
BOOL found = FALSE;
root = WLog_GetRoot();
if (!root)
return NULL;
for (index = 0; index < root->ChildrenCount; index++)
{
child = root->Children[index];
if (strcmp(child->Name, name) == 0)
{
found = TRUE;
break;
}
}
return (found) ? child : NULL;
}
2014-08-18 21:34:47 +04:00
wLog* WLog_Get(LPCSTR name)
{
2014-08-18 21:34:47 +04:00
wLog* log;
if (!(log = WLog_FindChild(name)))
{
wLog* root = WLog_GetRoot();
if (!root)
return NULL;
if (!(log = WLog_New(name, root)))
return NULL;
if (!WLog_AddChild(root, log))
{
WLog_Free(log);
return NULL;
}
}
return log;
}
BOOL WLog_Init(void)
{
return WLog_GetRoot() != NULL;
}
BOOL WLog_Uninit(void)
{
return TRUE;
}