libwinpr-utils: further split wlog framework into more source files, add some thread safety for write operations

This commit is contained in:
Marc-André Moreau 2013-10-02 13:38:21 -04:00
parent bc649e84ba
commit 0569de167d
21 changed files with 976 additions and 364 deletions

View File

@ -25,6 +25,9 @@
#include <winpr/winpr.h>
#include <winpr/wtypes.h>
#include <winpr/synch.h>
#include <winpr/thread.h>
typedef struct _wLog wLog;
typedef struct _wLogMessage wLogMessage;
typedef struct _wLogLayout wLogLayout;
@ -46,7 +49,9 @@ typedef struct _wLogAppender wLogAppender;
* Log Message
*/
#define WLOG_MESSAGE_STRING 0
#define WLOG_MESSAGE_TEXT 0
#define WLOG_MESSAGE_IMAGE 1
#define WLOG_MESSAGE_DATA 2
struct _wLogMessage
{
@ -81,6 +86,7 @@ struct _wLogLayout
#define WLOG_APPENDER_CONSOLE 0
#define WLOG_APPENDER_FILE 1
#define WLOG_APPENDER_BINARY 2
typedef int (*WLOG_APPENDER_OPEN_FN)(wLog* log, wLogAppender* appender);
typedef int (*WLOG_APPENDER_CLOSE_FN)(wLog* log, wLogAppender* appender);
@ -90,6 +96,7 @@ typedef int (*WLOG_APPENDER_WRITE_MESSAGE_FN)(wLog* log, wLogAppender* appender,
DWORD Type; \
DWORD State; \
wLogLayout* Layout; \
CRITICAL_SECTION lock; \
WLOG_APPENDER_OPEN_FN Open; \
WLOG_APPENDER_CLOSE_FN Close; \
WLOG_APPENDER_WRITE_MESSAGE_FN WriteMessage
@ -121,6 +128,17 @@ struct _wLogFileAppender
};
typedef struct _wLogFileAppender wLogFileAppender;
struct _wLogBinaryAppender
{
WLOG_APPENDER_COMMON();
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogBinaryAppender wLogBinaryAppender;
/**
* Logger
*/
@ -146,7 +164,31 @@ WINPR_API void WLog_PrintMessage(wLog* log, wLogMessage* message, ...);
#define WLog_Print(_log, _log_level, _fmt, ...) \
if (_log_level <= _log->Level) { \
wLogMessage _log_message; \
_log_message.Type = WLOG_MESSAGE_STRING; \
_log_message.Type = WLOG_MESSAGE_TEXT; \
_log_message.Level = _log_level; \
_log_message.FormatString = _fmt; \
_log_message.LineNumber = __LINE__; \
_log_message.FileName = __FILE__; \
_log_message.FunctionName = __FUNCTION__; \
WLog_PrintMessage(_log, &(_log_message), ## __VA_ARGS__ ); \
}
#define WLog_Image(_log, _log_level, ...) \
if (_log_level <= _log->Level) { \
wLogMessage _log_message; \
_log_message.Type = WLOG_MESSAGE_IMAGE; \
_log_message.Level = _log_level; \
_log_message.FormatString = _fmt; \
_log_message.LineNumber = __LINE__; \
_log_message.FileName = __FILE__; \
_log_message.FunctionName = __FUNCTION__; \
WLog_PrintMessage(_log, &(_log_message), ## __VA_ARGS__ ); \
}
#define WLog_Data(_log, _log_level, ...) \
if (_log_level <= _log->Level) { \
wLogMessage _log_message; \
_log_message.Type = WLOG_MESSAGE_IMAGE; \
_log_message.Level = _log_level; \
_log_message.FormatString = _fmt; \
_log_message.LineNumber = __LINE__; \

View File

@ -184,7 +184,6 @@ HANDLE CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
char* name;
int status;
HANDLE hNamedPipe;
unsigned long flags;
struct sockaddr_un s;
WINPR_NAMED_PIPE* pNamedPipe;

View File

@ -51,8 +51,22 @@ set(${MODULE_PREFIX}_TRIO_SRCS
set(${MODULE_PREFIX}_WLOG_SRCS
wlog/wlog.c
wlog/wlog.h
wlog/Layout.c
wlog/Layout.h
wlog/Message.c
wlog/Message.h
wlog/TextMessage.c
wlog/TextMessage.h
wlog/DataMessage.c
wlog/DataMessage.h
wlog/ImageMessage.c
wlog/ImageMessage.h
wlog/Appender.c
wlog/Appender.h
wlog/FileAppender.c
wlog/FileAppender.h
wlog/BinaryAppender.c
wlog/BinaryAppender.h
wlog/ConsoleAppender.c
wlog/ConsoleAppender.h)

View File

@ -16,13 +16,14 @@ int TestWLog(int argc, char* argv[])
root = WLog_GetRoot();
WLog_SetLogAppenderType(root, WLOG_APPENDER_CONSOLE);
WLog_SetLogAppenderType(root, WLOG_APPENDER_FILE);
appender = WLog_GetLogAppender(root);
WLog_ConsoleAppender_SetOutputStream(root, (wLogConsoleAppender*) appender, WLOG_CONSOLE_STDERR);
layout = WLog_GetLogLayout(root);
WLog_Layout_SetPrefixFormat(root, layout, "[%lv:%mn] [%fl|%fn|%ln] - ");
WLog_ConsoleAppender_SetOutputStream(root, (wLogConsoleAppender*) appender, WLOG_CONSOLE_STDERR);
WLog_OpenAppender(root);
logA = WLog_Get("com.test.ChannelA");

View File

@ -0,0 +1,148 @@
/**
* 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 <winpr/wlog.h>
#include "wlog/Layout.h"
#include "wlog/Appender.h"
wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType)
{
wLogAppender* appender = NULL;
if (logAppenderType == WLOG_APPENDER_CONSOLE)
{
appender = (wLogAppender*) WLog_ConsoleAppender_New(log);
}
else if (logAppenderType == WLOG_APPENDER_FILE)
{
appender = (wLogAppender*) WLog_FileAppender_New(log);
}
else if (logAppenderType == WLOG_APPENDER_BINARY)
{
appender = (wLogAppender*) WLog_BinaryAppender_New(log);
}
if (!appender)
appender = (wLogAppender*) WLog_ConsoleAppender_New(log);
appender->Layout = WLog_Layout_New(log);
InitializeCriticalSectionAndSpinCount(&appender->lock, 4000);
return appender;
}
void WLog_Appender_Free(wLog* log, wLogAppender* appender)
{
if (appender)
{
if (appender->Layout)
{
WLog_Layout_Free(log, appender->Layout);
appender->Layout = NULL;
}
DeleteCriticalSection(&appender->lock);
if (appender->Type == WLOG_APPENDER_CONSOLE)
{
WLog_ConsoleAppender_Free(log, (wLogConsoleAppender*) appender);
}
else if (appender->Type == WLOG_APPENDER_FILE)
{
WLog_FileAppender_Free(log, (wLogFileAppender*) appender);
}
else if (appender->Type == WLOG_APPENDER_BINARY)
{
WLog_BinaryAppender_Free(log, (wLogBinaryAppender*) appender);
}
}
}
wLogAppender* WLog_GetLogAppender(wLog* log)
{
if (!log)
return NULL;
if (!log->Appender)
return WLog_GetLogAppender(log->Parent);
return log->Appender;
}
void WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType)
{
if (log->Appender)
{
WLog_Appender_Free(log, log->Appender);
log->Appender = NULL;
}
log->Appender = WLog_Appender_New(log, logAppenderType);
}
int WLog_OpenAppender(wLog* log)
{
int status = 0;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return -1;
if (!appender->Open)
return 0;
if (!appender->State)
{
status = appender->Open(log, appender);
appender->State = 1;
}
return status;
}
int WLog_CloseAppender(wLog* log)
{
int status = 0;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return -1;
if (!appender->Close)
return 0;
if (appender->State)
{
status = appender->Close(log, appender);
appender->State = 0;
}
return status;
}

View File

@ -0,0 +1,34 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_APPENDER_PRIVATE_H
#define WINPR_WLOG_APPENDER_PRIVATE_H
#include <winpr/wlog.h>
#include "wlog/FileAppender.h"
#include "wlog/BinaryAppender.h"
#include "wlog/ConsoleAppender.h"
void WLog_Appender_Free(wLog* log, wLogAppender* appender);
#include "wlog/wlog.h"
#endif /* WINPR_WLOG_APPENDER_PRIVATE_H */

View File

@ -0,0 +1,171 @@
/**
* 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 <winpr/crt.h>
#include <winpr/file.h>
#include <winpr/path.h>
#include <winpr/thread.h>
#include <winpr/wlog.h>
#include "wlog/BinaryAppender.h"
/**
* Binary Appender
*/
void WLog_BinaryAppender_SetOutputFileName(wLog* log, wLogBinaryAppender* appender, const char* filename)
{
if (!appender)
return;
if (appender->Type != WLOG_APPENDER_BINARY)
return;
if (!filename)
return;
appender->FileName = _strdup(filename);
}
void WLog_BinaryAppender_SetOutputFilePath(wLog* log, wLogBinaryAppender* appender, const char* filepath)
{
if (!appender)
return;
if (appender->Type != WLOG_APPENDER_BINARY)
return;
if (!filepath)
return;
appender->FilePath = _strdup(filepath);
}
int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender)
{
DWORD ProcessId;
ProcessId = GetCurrentProcessId();
if (!appender->FilePath)
{
appender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
}
if (!PathFileExistsA(appender->FilePath))
{
CreateDirectoryA(appender->FilePath, 0);
UnixChangeFileMode(appender->FilePath, 0xFFFF);
}
if (!appender->FileName)
{
appender->FileName = (char*) malloc(256);
sprintf_s(appender->FileName, 256, "%u.bin.log", (unsigned int) ProcessId);
}
if (!appender->FullFileName)
{
appender->FullFileName = GetCombinedPath(appender->FilePath, appender->FileName);
}
appender->FileDescriptor = fopen(appender->FullFileName, "a+");
if (!appender->FileDescriptor)
return -1;
return 0;
}
int WLog_BinaryAppender_Close(wLog* log, wLogBinaryAppender* appender)
{
if (!appender->FileDescriptor)
return 0;
fclose(appender->FileDescriptor);
appender->FileDescriptor = NULL;
return 0;
}
int WLog_BinaryAppender_WriteMessage(wLog* log, wLogBinaryAppender* appender, wLogMessage* message)
{
FILE* fp;
char prefix[WLOG_MAX_PREFIX_SIZE];
if (message->Level > log->Level)
return 0;
fp = appender->FileDescriptor;
if (!fp)
return -1;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
return 1;
}
wLogBinaryAppender* WLog_BinaryAppender_New(wLog* log)
{
wLogBinaryAppender* BinaryAppender;
BinaryAppender = (wLogBinaryAppender*) malloc(sizeof(wLogBinaryAppender));
if (BinaryAppender)
{
ZeroMemory(BinaryAppender, sizeof(wLogBinaryAppender));
BinaryAppender->Open = (WLOG_APPENDER_OPEN_FN) WLog_BinaryAppender_Open;
BinaryAppender->Close = (WLOG_APPENDER_OPEN_FN) WLog_BinaryAppender_Close;
BinaryAppender->WriteMessage = (WLOG_APPENDER_WRITE_MESSAGE_FN) WLog_BinaryAppender_WriteMessage;
BinaryAppender->FileName = NULL;
BinaryAppender->FilePath = NULL;
BinaryAppender->FullFileName = NULL;
}
return BinaryAppender;
}
void WLog_BinaryAppender_Free(wLog* log, wLogBinaryAppender* appender)
{
if (appender)
{
if (appender->FileName)
free(appender->FileName);
if (appender->FilePath)
free(appender->FilePath);
if (appender->FullFileName)
free(appender->FullFileName);
free(appender);
}
}

View File

@ -0,0 +1,30 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_BINARY_APPENDER_PRIVATE_H
#define WINPR_WLOG_BINARY_APPENDER_PRIVATE_H
#include <winpr/wlog.h>
#include "wlog/wlog.h"
WINPR_API wLogBinaryAppender* WLog_BinaryAppender_New(wLog* log);
WINPR_API void WLog_BinaryAppender_Free(wLog* log, wLogBinaryAppender* appender);
#endif /* WINPR_WLOG_BINARY_APPENDER_PRIVATE_H */

View File

@ -0,0 +1,27 @@
/**
* 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 <winpr/wlog.h>
#include "wlog/DataMessage.h"

View File

@ -0,0 +1,29 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_DATA_MESSAGE_PRIVATE_H
#define WINPR_WLOG_DATA_MESSAGE_PRIVATE_H
#include <winpr/wlog.h>
#include "wlog/wlog.h"
#endif /* WINPR_WLOG_DATA_MESSAGE_PRIVATE_H */

View File

@ -82,7 +82,7 @@ int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender)
if (!appender->FileName)
{
appender->FileName = (char*) malloc(256);
sprintf_s(appender->FileName, 256, "%u.log", ProcessId);
sprintf_s(appender->FileName, 256, "%u.log", (unsigned int) ProcessId);
}
if (!appender->FullFileName)

View File

@ -0,0 +1,27 @@
/**
* 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 <winpr/wlog.h>
#include "wlog/ImageMessage.h"

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,270 @@
/**
* 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>
#include <stdarg.h>
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/wlog.h>
#include "wlog/Layout.h"
extern const char* WLOG_LEVELS[7];
/**
* Log Layout
*/
void WLog_PrintMessagePrefixVA(wLog* log, wLogMessage* message, const char* format, va_list args)
{
if (!strchr(format, '%'))
{
message->PrefixString = (LPSTR) format;
}
else
{
wvsnprintfx(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args);
}
}
void WLog_PrintMessagePrefix(wLog* log, wLogMessage* message, const char* format, ...)
{
va_list args;
va_start(args, format);
WLog_PrintMessagePrefixVA(log, message, format, args);
va_end(args);
}
void WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message)
{
char* p;
int index;
int argc = 0;
void* args[32];
char format[128];
index = 0;
p = (char*) layout->FormatString;
while (*p)
{
if (*p == '%')
{
p++;
if (*p)
{
if ((*p == 'l') && (*(p + 1) == 'v')) /* log level */
{
args[argc++] = (void*) WLOG_LEVELS[message->Level];
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'm') && (*(p + 1) == 'n')) /* module name */
{
args[argc++] = (void*) log->Name;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'f') && (*(p + 1) == 'l')) /* file */
{
char* file;
file = strrchr(message->FileName, '/');
if (!file)
file = strrchr(message->FileName, '\\');
if (file)
file++;
else
file = (char*) message->FileName;
args[argc++] = (void*) file;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'f') && (*(p + 1) == 'n')) /* function */
{
args[argc++] = (void*) message->FunctionName;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'l') && (*(p + 1) == 'n')) /* line number */
{
args[argc++] = (void*) message->LineNumber;
format[index++] = '%';
format[index++] = 'd';
p++;
}
}
}
else
{
format[index++] = *p;
}
p++;
}
format[index++] = '\0';
switch (argc)
{
case 0:
WLog_PrintMessagePrefix(log, message, format);
break;
case 1:
WLog_PrintMessagePrefix(log, message, format, args[0]);
break;
case 2:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1]);
break;
case 3:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2]);
break;
case 4:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3]);
break;
case 5:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4]);
break;
case 6:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5]);
break;
case 7:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6]);
break;
case 8:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7]);
break;
case 9:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8]);
break;
case 10:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9]);
break;
case 11:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
break;
case 12:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11]);
break;
case 13:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12]);
break;
case 14:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13]);
break;
case 15:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13], args[14]);
break;
case 16:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13], args[14], args[15]);
break;
}
}
wLogLayout* WLog_GetLogLayout(wLog* log)
{
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
return appender->Layout;
}
void WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format)
{
if (layout->FormatString)
free(layout->FormatString);
layout->FormatString = _strdup(format);
}
wLogLayout* WLog_Layout_New(wLog* log)
{
wLogLayout* layout;
layout = (wLogLayout*) malloc(sizeof(wLogLayout));
if (layout)
{
ZeroMemory(layout, sizeof(wLogLayout));
layout->FormatString = _strdup("[%lv][%mn] - ");
}
return layout;
}
void WLog_Layout_Free(wLog* log, wLogLayout* layout)
{
if (layout)
{
if (layout->FormatString)
free(layout->FormatString);
free(layout);
}
}

View File

@ -0,0 +1,31 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_LAYOUT_PRIVATE_H
#define WINPR_WLOG_LAYOUT_PRIVATE_H
#include <winpr/wlog.h>
wLogLayout* WLog_Layout_New(wLog* log);
void WLog_Layout_Free(wLog* log, wLogLayout* layout);
#include "wlog/wlog.h"
#endif /* WINPR_WLOG_LAYOUT_PRIVATE_H */

View File

@ -0,0 +1,27 @@
/**
* 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 <winpr/wlog.h>
#include "wlog/Message.h"

View File

@ -0,0 +1,29 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_MESSAGE_PRIVATE_H
#define WINPR_WLOG_MESSAGE_PRIVATE_H
#include <winpr/wlog.h>
#include "wlog/wlog.h"
#endif /* WINPR_WLOG_MESSAGE_PRIVATE_H */

View File

@ -0,0 +1,27 @@
/**
* 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 <winpr/wlog.h>
#include "wlog/TextMessage.h"

View File

@ -0,0 +1,30 @@
/**
* 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.
*/
#ifndef WINPR_WLOG_TEXT_MESSAGE_PRIVATE_H
#define WINPR_WLOG_TEXT_MESSAGE_PRIVATE_H
#include <winpr/wlog.h>
#include "wlog/wlog.h"
#endif /* WINPR_WLOG_TEXT_MESSAGE_PRIVATE_H */

View File

@ -32,9 +32,6 @@
#include "wlog/wlog.h"
#include "wlog/FileAppender.h"
#include "wlog/ConsoleAppender.h"
/**
* References for general logging concepts:
*
@ -58,6 +55,7 @@ const char* WLOG_LEVELS[7] =
int WLog_Write(wLog* log, wLogMessage* message)
{
int status;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
@ -68,23 +66,40 @@ int WLog_Write(wLog* log, wLogMessage* message)
if (!appender->WriteMessage)
return -1;
return appender->WriteMessage(log, appender, message);
EnterCriticalSection(&appender->lock);
status = appender->WriteMessage(log, appender, message);
LeaveCriticalSection(&appender->lock);
return status;
}
void WLog_PrintMessageVA(wLog* log, wLogMessage* message, va_list args)
{
if (!strchr(message->FormatString, '%'))
if (message->Type == WLOG_MESSAGE_TEXT)
{
if (!strchr(message->FormatString, '%'))
{
message->TextString = (LPSTR) message->FormatString;
WLog_Write(log, message);
}
else
{
char formattedLogMessage[WLOG_MAX_STRING_SIZE];
wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message->FormatString, args);
message->TextString = formattedLogMessage;
WLog_Write(log, message);
}
}
else if (message->Type == WLOG_MESSAGE_IMAGE)
{
}
else if (message->Type == WLOG_MESSAGE_DATA)
{
message->TextString = (LPSTR) message->FormatString;
WLog_Write(log, message);
}
else
{
char formattedLogMessage[WLOG_MAX_STRING_SIZE];
wvsnprintfx(formattedLogMessage, WLOG_MAX_STRING_SIZE - 1, message->FormatString, args);
message->TextString = formattedLogMessage;
WLog_Write(log, message);
}
}
@ -109,349 +124,6 @@ void WLog_SetLogLevel(wLog* log, DWORD logLevel)
log->Level = logLevel;
}
/**
* Log Layout
*/
void WLog_PrintMessagePrefixVA(wLog* log, wLogMessage* message, const char* format, va_list args)
{
if (!strchr(format, '%'))
{
message->PrefixString = (LPSTR) format;
}
else
{
wvsnprintfx(message->PrefixString, WLOG_MAX_PREFIX_SIZE - 1, format, args);
}
}
void WLog_PrintMessagePrefix(wLog* log, wLogMessage* message, const char* format, ...)
{
va_list args;
va_start(args, format);
WLog_PrintMessagePrefixVA(log, message, format, args);
va_end(args);
}
void WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message)
{
char* p;
int index;
int argc = 0;
void* args[32];
char format[128];
index = 0;
p = (char*) layout->FormatString;
while (*p)
{
if (*p == '%')
{
p++;
if (*p)
{
if ((*p == 'l') && (*(p + 1) == 'v')) /* log level */
{
args[argc++] = (void*) WLOG_LEVELS[message->Level];
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'm') && (*(p + 1) == 'n')) /* module name */
{
args[argc++] = (void*) log->Name;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'f') && (*(p + 1) == 'l')) /* file */
{
char* file;
file = strrchr(message->FileName, '/');
if (!file)
file = strrchr(message->FileName, '\\');
if (file)
file++;
else
file = (char*) message->FileName;
args[argc++] = (void*) file;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'f') && (*(p + 1) == 'n')) /* function */
{
args[argc++] = (void*) message->FunctionName;
format[index++] = '%';
format[index++] = 's';
p++;
}
else if ((*p == 'l') && (*(p + 1) == 'n')) /* line number */
{
args[argc++] = (void*) message->LineNumber;
format[index++] = '%';
format[index++] = 'd';
p++;
}
}
}
else
{
format[index++] = *p;
}
p++;
}
format[index++] = '\0';
switch (argc)
{
case 0:
WLog_PrintMessagePrefix(log, message, format);
break;
case 1:
WLog_PrintMessagePrefix(log, message, format, args[0]);
break;
case 2:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1]);
break;
case 3:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2]);
break;
case 4:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3]);
break;
case 5:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4]);
break;
case 6:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5]);
break;
case 7:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6]);
break;
case 8:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7]);
break;
case 9:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8]);
break;
case 10:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9]);
break;
case 11:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10]);
break;
case 12:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11]);
break;
case 13:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12]);
break;
case 14:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13]);
break;
case 15:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13], args[14]);
break;
case 16:
WLog_PrintMessagePrefix(log, message, format, args[0], args[1], args[2], args[3],
args[4], args[5], args[6], args[7], args[8], args[9], args[10],
args[11], args[12], args[13], args[14], args[15]);
break;
}
}
wLogLayout* WLog_GetLogLayout(wLog* log)
{
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
return appender->Layout;
}
void WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format)
{
if (layout->FormatString)
free(layout->FormatString);
layout->FormatString = _strdup(format);
}
wLogLayout* WLog_Layout_New(wLog* log)
{
wLogLayout* layout;
layout = (wLogLayout*) malloc(sizeof(wLogLayout));
if (layout)
{
ZeroMemory(layout, sizeof(wLogLayout));
layout->FormatString = _strdup("[%lv][%mn] - ");
}
return layout;
}
void WLog_Layout_Free(wLog* log, wLogLayout* layout)
{
if (layout)
{
if (layout->FormatString)
free(layout->FormatString);
free(layout);
}
}
wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType)
{
wLogAppender* appender = NULL;
if (logAppenderType == WLOG_APPENDER_CONSOLE)
{
appender = (wLogAppender*) WLog_ConsoleAppender_New(log);
}
else if (logAppenderType == WLOG_APPENDER_FILE)
{
appender = (wLogAppender*) WLog_FileAppender_New(log);
}
if (!appender)
appender = (wLogAppender*) WLog_ConsoleAppender_New(log);
appender->Layout = WLog_Layout_New(log);
return appender;
}
void WLog_Appender_Free(wLog* log, wLogAppender* appender)
{
if (appender)
{
if (appender->Layout)
{
WLog_Layout_Free(log, appender->Layout);
appender->Layout = NULL;
}
if (appender->Type == WLOG_APPENDER_CONSOLE)
{
WLog_ConsoleAppender_Free(log, (wLogConsoleAppender*) appender);
}
else if (appender->Type == WLOG_APPENDER_FILE)
{
WLog_FileAppender_Free(log, (wLogFileAppender*) appender);
}
}
}
wLogAppender* WLog_GetLogAppender(wLog* log)
{
if (!log)
return NULL;
if (!log->Appender)
return WLog_GetLogAppender(log->Parent);
return log->Appender;
}
void WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType)
{
if (log->Appender)
{
WLog_Appender_Free(log, log->Appender);
log->Appender = NULL;
}
log->Appender = WLog_Appender_New(log, logAppenderType);
}
int WLog_OpenAppender(wLog* log)
{
int status = 0;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return -1;
if (!appender->Open)
return 0;
if (!appender->State)
{
status = appender->Open(log, appender);
appender->State = 1;
}
return status;
}
int WLog_CloseAppender(wLog* log)
{
int status = 0;
wLogAppender* appender;
appender = WLog_GetLogAppender(log);
if (!appender)
return -1;
if (!appender->Close)
return 0;
if (appender->State)
{
status = appender->Close(log, appender);
appender->State = 0;
}
return status;
}
int WLog_ParseName(wLog* log, LPCSTR name)
{
char* p;

View File

@ -25,6 +25,9 @@
#define WLOG_MAX_PREFIX_SIZE 512
#define WLOG_MAX_STRING_SIZE 8192
WINPR_API void WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message);
void WLog_Layout_GetMessagePrefix(wLog* log, wLogLayout* layout, wLogMessage* message);
#include "wlog/Layout.h"
#include "wlog/Appender.h"
#endif /* WINPR_WLOG_PRIVATE_H */