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

167 lines
4.6 KiB
C
Raw Normal View History

2014-11-16 14:20:48 +03:00
/**
* WinPR: Windows Portable Runtime
* WinPR Logger
*
* Copyright 2014 Armin Novak <armin.novak@thincast.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 "CallbackAppender.h"
2014-11-16 14:20:48 +03:00
struct _wLogCallbackAppender
2014-11-16 14:20:48 +03:00
{
WLOG_APPENDER_COMMON();
2014-11-16 14:20:48 +03:00
wLogCallbacks *callbacks;
};
typedef struct _wLogCallbackAppender wLogCallbackAppender;
2014-11-16 14:20:48 +03:00
static BOOL WLog_CallbackAppender_Open(wLog* log, wLogAppender* appender)
2014-11-16 14:20:48 +03:00
{
return TRUE;
2014-11-16 14:20:48 +03:00
}
static BOOL WLog_CallbackAppender_Close(wLog* log, wLogAppender* appender)
2014-11-16 14:20:48 +03:00
{
return TRUE;
2014-11-16 14:20:48 +03:00
}
static BOOL WLog_CallbackAppender_WriteMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
2014-11-16 14:20:48 +03:00
{
char prefix[WLOG_MAX_PREFIX_SIZE];
wLogCallbackAppender* callbackAppender;
2014-11-16 14:20:48 +03:00
if (!appender)
return FALSE;
2014-11-16 14:20:48 +03:00
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
callbackAppender = (wLogCallbackAppender *)appender;
if (callbackAppender->callbacks && callbackAppender->callbacks->message)
return callbackAppender->callbacks->message(message);
2014-11-16 14:20:48 +03:00
else
return FALSE;
2014-11-16 14:20:48 +03:00
}
static BOOL WLog_CallbackAppender_WriteDataMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
2014-11-16 14:20:48 +03:00
{
char prefix[WLOG_MAX_PREFIX_SIZE];
wLogCallbackAppender* callbackAppender;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
callbackAppender = (wLogCallbackAppender *)appender;
if (callbackAppender->callbacks && callbackAppender->callbacks->data)
return callbackAppender->callbacks->data(message);
2014-11-16 14:20:48 +03:00
else
return FALSE;
2014-11-16 14:20:48 +03:00
}
static BOOL WLog_CallbackAppender_WriteImageMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
2014-11-16 14:20:48 +03:00
{
char prefix[WLOG_MAX_PREFIX_SIZE];
wLogCallbackAppender* callbackAppender;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
callbackAppender = (wLogCallbackAppender *)appender;
if (callbackAppender->callbacks && callbackAppender->callbacks->image)
return callbackAppender->callbacks->image(message);
2014-11-16 14:20:48 +03:00
else
return FALSE;
2014-11-16 14:20:48 +03:00
}
static BOOL WLog_CallbackAppender_WritePacketMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
2014-11-16 14:20:48 +03:00
{
char prefix[WLOG_MAX_PREFIX_SIZE];
wLogCallbackAppender* callbackAppender;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
callbackAppender = (wLogCallbackAppender *)appender;
if (callbackAppender->callbacks && callbackAppender->callbacks->package)
return callbackAppender->callbacks->package(message);
2014-11-16 14:20:48 +03:00
else
return FALSE;
}
static BOOL WLog_CallbackAppender_Set(wLogAppender* appender, const char *setting, void *value)
{
wLogCallbackAppender *callbackAppender = (wLogCallbackAppender *)appender;
if (!value || strcmp(setting, "callbacks"))
return FALSE;
if (!(callbackAppender->callbacks = calloc(1, sizeof(wLogCallbacks)))) {
return FALSE;
2014-11-16 14:20:48 +03:00
}
callbackAppender->callbacks = memcpy(callbackAppender->callbacks, value, sizeof(wLogCallbacks));
return TRUE;
}
static void WLog_CallbackAppender_Free(wLogAppender* appender)
{
wLogCallbackAppender *callbackAppender;
if (!appender) {
return;
}
callbackAppender = (wLogCallbackAppender *)appender;
free(callbackAppender->callbacks);
free(appender);
2014-11-16 14:20:48 +03:00
}
wLogAppender* WLog_CallbackAppender_New(wLog* log)
2014-11-16 14:20:48 +03:00
{
wLogCallbackAppender* CallbackAppender;
CallbackAppender = (wLogCallbackAppender*) calloc(1, sizeof(wLogCallbackAppender));
if (!CallbackAppender)
return NULL;
CallbackAppender->Type = WLOG_APPENDER_CALLBACK;
CallbackAppender->Open = WLog_CallbackAppender_Open;
CallbackAppender->Close = WLog_CallbackAppender_Close;
CallbackAppender->WriteMessage = WLog_CallbackAppender_WriteMessage;
CallbackAppender->WriteDataMessage = WLog_CallbackAppender_WriteDataMessage;
CallbackAppender->WriteImageMessage = WLog_CallbackAppender_WriteImageMessage;
CallbackAppender->WritePacketMessage = WLog_CallbackAppender_WritePacketMessage;
CallbackAppender->Free = WLog_CallbackAppender_Free;
CallbackAppender->Set = WLog_CallbackAppender_Set;
2014-11-16 14:20:48 +03:00
return (wLogAppender *)CallbackAppender;
2014-11-16 14:20:48 +03:00
}