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

169 lines
4.8 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.
*/
2022-02-16 12:08:00 +03:00
#include <winpr/config.h>
2014-11-16 14:20:48 +03:00
#include "CallbackAppender.h"
2014-11-16 14:20:48 +03:00
typedef struct
2014-11-16 14:20:48 +03:00
{
WLOG_APPENDER_COMMON();
2014-11-16 14:20:48 +03:00
2019-11-06 17:24:51 +03:00
wLogCallbacks* callbacks;
} 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
}
2019-11-06 17:24:51 +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] = { 0 };
wLogCallbackAppender* callbackAppender = NULL;
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);
2019-11-06 17:24:51 +03:00
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
}
2019-11-06 17:24:51 +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] = { 0 };
wLogCallbackAppender* callbackAppender = NULL;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
2019-11-06 17:24:51 +03:00
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
}
2019-11-06 17:24:51 +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] = { 0 };
wLogCallbackAppender* callbackAppender = NULL;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
2019-11-06 17:24:51 +03:00
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
}
2019-11-06 17:24:51 +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] = { 0 };
wLogCallbackAppender* callbackAppender = NULL;
if (!appender)
return FALSE;
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
2019-11-06 17:24:51 +03:00
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;
}
2019-11-06 17:24:51 +03:00
static BOOL WLog_CallbackAppender_Set(wLogAppender* appender, const char* setting, void* value)
{
2019-11-06 17:24:51 +03:00
wLogCallbackAppender* callbackAppender = (wLogCallbackAppender*)appender;
if (!value || strcmp(setting, "callbacks"))
return FALSE;
2019-11-06 17:24:51 +03:00
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 = NULL;
2019-11-06 17:24:51 +03:00
if (!appender)
{
return;
}
2019-11-06 17:24:51 +03:00
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 = NULL;
2014-11-16 14:20:48 +03:00
2019-11-06 17:24:51 +03:00
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
2019-11-06 17:24:51 +03:00
return (wLogAppender*)CallbackAppender;
2014-11-16 14:20:48 +03:00
}