Added recursion guard to logging functions.

This commit is contained in:
Armin Novak 2014-08-18 14:19:48 +02:00
parent 763410a66d
commit c32c48fbb6
2 changed files with 142 additions and 165 deletions

View File

@ -33,14 +33,14 @@ extern "C" {
#include <winpr/synch.h>
#include <winpr/thread.h>
typedef struct _wLog wLog;
typedef struct _wLogMessage wLogMessage;
typedef struct _wLogLayout wLogLayout;
typedef struct _wLogAppender wLogAppender;
typedef struct _wLog wLog;
typedef struct _wLogMessage wLogMessage;
typedef struct _wLogLayout wLogLayout;
typedef struct _wLogAppender wLogAppender;
/**
* Log Levels
*/
/**
* Log Levels
*/
#define WLOG_TRACE 0
#define WLOG_DEBUG 1
@ -51,63 +51,63 @@ typedef struct _wLogAppender wLogAppender;
#define WLOG_OFF 6
#define WLOG_LEVEL_INHERIT 0xFFFF
/**
* Log Message
*/
/**
* Log Message
*/
#define WLOG_MESSAGE_TEXT 0
#define WLOG_MESSAGE_DATA 1
#define WLOG_MESSAGE_IMAGE 2
#define WLOG_MESSAGE_PACKET 3
struct _wLogMessage
{
DWORD Type;
struct _wLogMessage
{
DWORD Type;
DWORD Level;
DWORD Level;
LPSTR PrefixString;
LPSTR PrefixString;
LPCSTR FormatString;
LPSTR TextString;
LPCSTR FormatString;
LPSTR TextString;
DWORD LineNumber; /* __LINE__ */
LPCSTR FileName; /* __FILE__ */
LPCSTR FunctionName; /* __FUNCTION__ */
DWORD LineNumber; /* __LINE__ */
LPCSTR FileName; /* __FILE__ */
LPCSTR FunctionName; /* __FUNCTION__ */
/* Data Message */
/* Data Message */
void* Data;
int Length;
void* Data;
int Length;
/* Image Message */
/* Image Message */
void* ImageData;
int ImageWidth;
int ImageHeight;
int ImageBpp;
void* ImageData;
int ImageWidth;
int ImageHeight;
int ImageBpp;
/* Packet Message */
/* Packet Message */
void* PacketData;
int PacketLength;
DWORD PacketFlags;
};
void* PacketData;
int PacketLength;
DWORD PacketFlags;
};
/**
* Log Layout
*/
/**
* Log Layout
*/
struct _wLogLayout
{
DWORD Type;
struct _wLogLayout
{
DWORD Type;
LPSTR FormatString;
};
LPSTR FormatString;
};
/**
* Log Appenders
*/
/**
* Log Appenders
*/
#define WLOG_APPENDER_CONSOLE 0
#define WLOG_APPENDER_FILE 1
@ -116,18 +116,19 @@ struct _wLogLayout
#define WLOG_PACKET_INBOUND 1
#define WLOG_PACKET_OUTBOUND 2
typedef int (*WLOG_APPENDER_OPEN_FN)(wLog* log, wLogAppender* appender);
typedef int (*WLOG_APPENDER_CLOSE_FN)(wLog* log, wLogAppender* appender);
typedef int (*WLOG_APPENDER_WRITE_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_DATA_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_OPEN_FN)(wLog* log, wLogAppender* appender);
typedef int (*WLOG_APPENDER_CLOSE_FN)(wLog* log, wLogAppender* appender);
typedef int (*WLOG_APPENDER_WRITE_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_DATA_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
typedef int (*WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN)(wLog* log, wLogAppender* appender, wLogMessage* message);
#define WLOG_APPENDER_COMMON() \
DWORD Type; \
DWORD State; \
wLogLayout* Layout; \
CRITICAL_SECTION lock; \
BOOL recursive; \
void* TextMessageContext; \
void* DataMessageContext; \
void* ImageMessageContext; \
@ -139,79 +140,79 @@ typedef int (*WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN)(wLog* log, wLogAppender* ap
WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN WriteImageMessage; \
WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN WritePacketMessage
struct _wLogAppender
{
WLOG_APPENDER_COMMON();
};
struct _wLogAppender
{
WLOG_APPENDER_COMMON();
};
#define WLOG_CONSOLE_STDOUT 1
#define WLOG_CONSOLE_STDERR 2
#define WLOG_CONSOLE_DEBUG 3
struct _wLogConsoleAppender
{
WLOG_APPENDER_COMMON();
struct _wLogConsoleAppender
{
WLOG_APPENDER_COMMON();
int outputStream;
};
typedef struct _wLogConsoleAppender wLogConsoleAppender;
int outputStream;
};
typedef struct _wLogConsoleAppender wLogConsoleAppender;
struct _wLogFileAppender
{
WLOG_APPENDER_COMMON();
struct _wLogFileAppender
{
WLOG_APPENDER_COMMON();
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogFileAppender wLogFileAppender;
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogFileAppender wLogFileAppender;
struct _wLogBinaryAppender
{
WLOG_APPENDER_COMMON();
struct _wLogBinaryAppender
{
WLOG_APPENDER_COMMON();
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogBinaryAppender wLogBinaryAppender;
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogBinaryAppender wLogBinaryAppender;
/**
* Filter
*/
/**
* Filter
*/
struct _wLogFilter
{
DWORD Level;
LPSTR* Names;
DWORD NameCount;
};
typedef struct _wLogFilter wLogFilter;
struct _wLogFilter
{
DWORD Level;
LPSTR* Names;
DWORD NameCount;
};
typedef struct _wLogFilter wLogFilter;
/**
* Logger
*/
/**
* Logger
*/
struct _wLog
{
LPSTR Name;
DWORD Level;
struct _wLog
{
LPSTR Name;
DWORD Level;
BOOL IsRoot;
LPSTR* Names;
DWORD NameCount;
wLogAppender* Appender;
BOOL IsRoot;
LPSTR* Names;
DWORD NameCount;
wLogAppender* Appender;
wLog* Parent;
wLog** Children;
DWORD ChildrenCount;
DWORD ChildrenSize;
};
wLog* Parent;
wLog** Children;
DWORD ChildrenCount;
DWORD ChildrenSize;
};
WINPR_API void WLog_PrintMessage(wLog* log, wLogMessage* message, ...);
WINPR_API int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args);
WINPR_API void WLog_PrintMessage(wLog* log, wLogMessage* message, ...);
WINPR_API int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args);
#define WLog_Print(_log, _log_level, _fmt, ...) \
if (_log_level >= WLog_GetLogLevel(_log)) { \
@ -276,28 +277,28 @@ WINPR_API int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
#define WLog_IsLevelActive(_log, _log_level) \
(_log_level >= WLog_GetLogLevel(_log))
WINPR_API DWORD WLog_GetLogLevel(wLog* log);
WINPR_API void WLog_SetLogLevel(wLog* log, DWORD logLevel);
WINPR_API DWORD WLog_GetLogLevel(wLog* log);
WINPR_API void WLog_SetLogLevel(wLog* log, DWORD logLevel);
WINPR_API wLogAppender* WLog_GetLogAppender(wLog* log);
WINPR_API void WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType);
WINPR_API wLogAppender* WLog_GetLogAppender(wLog* log);
WINPR_API void WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType);
WINPR_API int WLog_OpenAppender(wLog* log);
WINPR_API int WLog_CloseAppender(wLog* log);
WINPR_API int WLog_OpenAppender(wLog* log);
WINPR_API int WLog_CloseAppender(wLog* log);
WINPR_API void WLog_ConsoleAppender_SetOutputStream(wLog* log, wLogConsoleAppender* appender, int outputStream);
WINPR_API void WLog_ConsoleAppender_SetOutputStream(wLog* log, wLogConsoleAppender* appender, int outputStream);
WINPR_API void WLog_FileAppender_SetOutputFileName(wLog* log, wLogFileAppender* appender, const char* filename);
WINPR_API void WLog_FileAppender_SetOutputFilePath(wLog* log, wLogFileAppender* appender, const char* filepath);
WINPR_API void WLog_FileAppender_SetOutputFileName(wLog* log, wLogFileAppender* appender, const char* filename);
WINPR_API void WLog_FileAppender_SetOutputFilePath(wLog* log, wLogFileAppender* appender, const char* filepath);
WINPR_API wLogLayout* WLog_GetLogLayout(wLog* log);
WINPR_API void WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format);
WINPR_API wLogLayout* WLog_GetLogLayout(wLog* log);
WINPR_API void WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format);
WINPR_API wLog* WLog_GetRoot(void);
WINPR_API wLog* WLog_Get(LPCSTR name);
WINPR_API wLog* WLog_GetRoot(void);
WINPR_API wLog* WLog_Get(LPCSTR name);
WINPR_API void WLog_Init(void);
WINPR_API void WLog_Uninit(void);
WINPR_API void WLog_Init(void);
WINPR_API void WLog_Uninit(void);
#ifdef __cplusplus
}

View File

@ -60,7 +60,6 @@ int WLog_Write(wLog* log, wLogMessage* message)
{
int status;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
@ -74,10 +73,13 @@ int WLog_Write(wLog* log, wLogMessage* message)
EnterCriticalSection(&appender->lock);
if (appender->recursive)
fprintf(stderr, "%s: Recursion detected!!!!", __FUNCTION__);
appender->recursive = TRUE;
status = appender->WriteMessage(log, appender, message);
appender->recursive = FALSE;
LeaveCriticalSection(&appender->lock);
return status;
}
@ -85,7 +87,6 @@ int WLog_WriteData(wLog* log, wLogMessage* message)
{
int status;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
@ -99,10 +100,13 @@ int WLog_WriteData(wLog* log, wLogMessage* message)
EnterCriticalSection(&appender->lock);
if (appender->recursive)
fprintf(stderr, "%s: Recursion detected!!!!", __FUNCTION__);
appender->recursive = TRUE;
status = appender->WriteDataMessage(log, appender, message);
appender->recursive = FALSE;
LeaveCriticalSection(&appender->lock);
return status;
}
@ -110,7 +114,6 @@ int WLog_WriteImage(wLog* log, wLogMessage* message)
{
int status;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
@ -124,10 +127,13 @@ int WLog_WriteImage(wLog* log, wLogMessage* message)
EnterCriticalSection(&appender->lock);
if (appender->recursive)
fprintf(stderr, "%s: Recursion detected!!!!", __FUNCTION__);
appender->recursive = TRUE;
status = appender->WriteImageMessage(log, appender, message);
appender->recursive = FALSE;
LeaveCriticalSection(&appender->lock);
return status;
}
@ -135,7 +141,6 @@ int WLog_WritePacket(wLog* log, wLogMessage* message)
{
int status;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
@ -149,10 +154,13 @@ int WLog_WritePacket(wLog* log, wLogMessage* message)
EnterCriticalSection(&appender->lock);
if (appender->recursive)
fprintf(stderr, "%s: Recursion detected!!!!", __FUNCTION__);
appender->recursive = TRUE;
status = appender->WritePacketMessage(log, appender, message);
appender->recursive = FALSE;
LeaveCriticalSection(&appender->lock);
return status;
}
@ -171,7 +179,6 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
{
char formattedLogMessage[WLOG_MAX_STRING_SIZE];
wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message->FormatString, args);
message->TextString = formattedLogMessage;
status = WLog_Write(log, message);
}
@ -180,7 +187,6 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
{
message->Data = va_arg(args, void*);
message->Length = va_arg(args, int);
status = WLog_WriteData(log, message);
}
else if (message->Type == WLOG_MESSAGE_IMAGE)
@ -189,7 +195,6 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
message->ImageWidth = va_arg(args, int);
message->ImageHeight = va_arg(args, int);
message->ImageBpp = va_arg(args, int);
status = WLog_WriteImage(log, message);
}
else if (message->Type == WLOG_MESSAGE_PACKET)
@ -197,7 +202,6 @@ int WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
message->PacketData = va_arg(args, void*);
message->PacketLength = va_arg(args, int);
message->PacketFlags = va_arg(args, int);
status = WLog_WritePacket(log, message);
}
@ -208,11 +212,8 @@ void WLog_PrintMessage(wLog* log, wLogMessage* message, ...)
{
int status;
va_list args;
va_start(args, message);
status = WLog_PrintMessageVA(log, message, args);
va_end(args);
}
@ -270,7 +271,6 @@ int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
int count;
LPSTR names;
int iLevel;
count = 1;
p = (char*) name;
@ -284,11 +284,9 @@ int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
filter->NameCount = count;
filter->Names = (LPSTR*) malloc(sizeof(LPSTR) * (count + 1));
filter->Names[count] = NULL;
count = 0;
p = (char*) names;
filter->Names[count++] = p;
q = strrchr(p, ':');
if (!q)
@ -296,7 +294,6 @@ int WLog_ParseFilter(wLogFilter* filter, LPCSTR name)
*q = '\0';
q++;
iLevel = WLog_ParseLogLevel(q);
if (iLevel < 0)
@ -322,7 +319,6 @@ int WLog_ParseFilters()
DWORD nSize;
int status;
char** strs;
nSize = GetEnvironmentVariableA("WLOG_FILTER", NULL, 0);
if (nSize < 1)
@ -334,7 +330,6 @@ int WLog_ParseFilters()
return -1;
nSize = GetEnvironmentVariableA("WLOG_FILTER", env, nSize);
count = 1;
p = env;
@ -345,11 +340,9 @@ int WLog_ParseFilters()
}
g_FilterCount = count;
p = env;
count = 0;
strs = (char**) calloc(g_FilterCount, sizeof(char*));
strs[count++] = p;
while ((p = strchr(p, ',')) != NULL)
@ -373,7 +366,6 @@ int WLog_ParseFilters()
}
free(strs);
return 0;
}
@ -423,7 +415,6 @@ int WLog_ParseName(wLog* log, LPCSTR name)
char* p;
int count;
LPSTR names;
count = 1;
p = (char*) name;
@ -437,7 +428,6 @@ int WLog_ParseName(wLog* log, LPCSTR name)
log->NameCount = count;
log->Names = (LPSTR*) malloc(sizeof(LPSTR) * (count + 1));
log->Names[count] = NULL;
count = 0;
p = (char*) names;
log->Names[count++] = p;
@ -458,7 +448,6 @@ wLog* WLog_New(LPCSTR name, wLog* rootLogger)
char* env;
DWORD nSize;
int iLevel;
log = (wLog*) calloc(1, sizeof(wLog));
if (log)
@ -469,10 +458,8 @@ wLog* WLog_New(LPCSTR name, wLog* rootLogger)
return NULL;
WLog_ParseName(log, name);
log->Parent = rootLogger;
log->ChildrenCount = 0;
log->ChildrenSize = 16;
log->Children = (wLog**) calloc(log->ChildrenSize, sizeof(wLog*));
@ -488,14 +475,12 @@ wLog* WLog_New(LPCSTR name, wLog* rootLogger)
else
{
log->Level = WLOG_WARN;
nSize = GetEnvironmentVariableA("WLOG_LEVEL", NULL, 0);
if (nSize)
{
env = (LPSTR) malloc(nSize);
nSize = GetEnvironmentVariableA("WLOG_LEVEL", env, nSize);
iLevel = WLog_ParseLogLevel(env);
if (iLevel >= 0)
@ -528,7 +513,6 @@ void WLog_Free(wLog* log)
free(log->Names[0]);
free(log->Names);
free(log->Children);
free(log);
}
}
@ -545,11 +529,8 @@ wLog* WLog_GetRoot()
{
g_RootLog = WLog_New("", NULL);
g_RootLog->IsRoot = TRUE;
WLog_ParseFilters();
logAppenderType = WLOG_APPENDER_CONSOLE;
nSize = GetEnvironmentVariableA("WLOG_APPENDER", NULL, 0);
if (nSize)
@ -586,7 +567,6 @@ int WLog_AddChild(wLog* parent, wLog* child)
parent->Children[parent->ChildrenCount++] = child;
child->Parent = parent;
return 0;
}
@ -596,7 +576,6 @@ wLog* WLog_FindChild(LPCSTR name)
wLog* root;
wLog* child = NULL;
BOOL found = FALSE;
root = WLog_GetRoot();
for (index = 0; index < root->ChildrenCount; index++)
@ -617,9 +596,7 @@ wLog* WLog_Get(LPCSTR name)
{
wLog* log;
wLog* root;
root = WLog_GetRoot();
log = WLog_FindChild(name);
if (!log)
@ -639,7 +616,6 @@ void WLog_Init()
void WLog_Uninit()
{
wLog* root = WLog_GetRoot();
DWORD index;
wLog* child = NULL;