libwinpr-utils: add some configurability to wlog with environment variables

This commit is contained in:
Marc-André Moreau 2013-10-08 22:35:14 -04:00
parent a0c0d7b998
commit 07afc88878
2 changed files with 76 additions and 3 deletions

View File

@ -130,6 +130,8 @@ int WLog_FileAppender_WriteMessage(wLog* log, wLogFileAppender* appender, wLogMe
fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
//fflush(fp); /* slow! */
return 1;
}

View File

@ -27,6 +27,7 @@
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/environment.h>
#include <winpr/wlog.h>
@ -63,6 +64,9 @@ int WLog_Write(wLog* log, wLogMessage* message)
if (!appender)
return -1;
if (!appender->State)
WLog_OpenAppender(log);
if (!appender->WriteMessage)
return -1;
@ -85,6 +89,9 @@ int WLog_WriteImage(wLog* log, wLogMessage* message)
if (!appender)
return -1;
if (!appender->State)
WLog_OpenAppender(log);
if (!appender->WriteImageMessage)
return -1;
@ -107,6 +114,9 @@ int WLog_WriteData(wLog* log, wLogMessage* message)
if (!appender)
return -1;
if (!appender->State)
WLog_OpenAppender(log);
if (!appender->WriteDataMessage)
return -1;
@ -221,6 +231,8 @@ int WLog_ParseName(wLog* log, LPCSTR name)
wLog* WLog_New(LPCSTR name)
{
wLog* log;
char* env;
DWORD nSize;
log = (wLog*) malloc(sizeof(wLog));
@ -231,8 +243,6 @@ wLog* WLog_New(LPCSTR name)
log->Name = _strdup(name);
WLog_ParseName(log, name);
log->Level = WLOG_TRACE;
log->Parent = NULL;
log->ChildrenCount = 0;
@ -240,6 +250,40 @@ wLog* WLog_New(LPCSTR name)
log->Children = (wLog**) malloc(sizeof(wLog*) * log->ChildrenSize);
log->Appender = NULL;
log->Level = WLOG_WARN;
nSize = GetEnvironmentVariableA("WLOG_LEVEL", NULL, 0);
if (nSize)
{
env = (LPSTR) malloc(nSize);
nSize = GetEnvironmentVariableA("WLOG_LEVEL", env, nSize);
if (env)
{
if (_stricmp(env, "TRACE") == 0)
log->Level = WLOG_TRACE;
else if (_stricmp(env, "DEBUG") == 0)
log->Level = WLOG_DEBUG;
else if (_stricmp(env, "INFO") == 0)
log->Level = WLOG_INFO;
else if (_stricmp(env, "WARN") == 0)
log->Level = WLOG_WARN;
else if (_stricmp(env, "ERROR") == 0)
log->Level = WLOG_ERROR;
else if (_stricmp(env, "FATAL") == 0)
log->Level = WLOG_FATAL;
else if (_stricmp(env, "OFF") == 0)
log->Level = WLOG_OFF;
else if (_strnicmp(env, "0x", 2) == 0)
{
/* TODO: read custom hex value */
}
free(env);
}
}
}
return log;
@ -268,11 +312,38 @@ static wLog* g_RootLog = NULL;
wLog* WLog_GetRoot()
{
char* env;
DWORD nSize;
DWORD logAppenderType;
if (!g_RootLog)
{
g_RootLog = WLog_New("");
g_RootLog->IsRoot = TRUE;
WLog_SetLogAppenderType(g_RootLog, WLOG_APPENDER_CONSOLE);
logAppenderType = WLOG_APPENDER_CONSOLE;
nSize = GetEnvironmentVariableA("WLOG_APPENDER", NULL, 0);
if (nSize)
{
env = (LPSTR) malloc(nSize);
nSize = GetEnvironmentVariableA("WLOG_APPENDER", env, nSize);
if (env)
{
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;
free(env);
}
}
WLog_SetLogAppenderType(g_RootLog, logAppenderType);
}
return g_RootLog;