2013-10-02 21:38:21 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
#include "Appender.h"
|
2013-10-02 21:38:21 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
void WLog_Appender_Free(wLog* log, wLogAppender* appender)
|
|
|
|
{
|
|
|
|
if (!appender)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (appender->Layout)
|
|
|
|
{
|
|
|
|
WLog_Layout_Free(log, appender->Layout);
|
|
|
|
appender->Layout = NULL;
|
|
|
|
}
|
2013-10-02 21:38:21 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
DeleteCriticalSection(&appender->lock);
|
|
|
|
appender->Free(appender);
|
|
|
|
}
|
|
|
|
|
|
|
|
wLogAppender* WLog_GetLogAppender(wLog* log)
|
|
|
|
{
|
|
|
|
if (!log)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!log->Appender)
|
|
|
|
return WLog_GetLogAppender(log->Parent);
|
|
|
|
|
|
|
|
return log->Appender;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WLog_OpenAppender(wLog* log)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
wLogAppender* appender;
|
|
|
|
|
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!appender->Open)
|
|
|
|
return TRUE;
|
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (!appender->active)
|
2015-11-09 19:56:44 +03:00
|
|
|
{
|
|
|
|
status = appender->Open(log, appender);
|
2015-11-10 14:05:23 +03:00
|
|
|
appender->active = TRUE;
|
2015-11-09 19:56:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WLog_CloseAppender(wLog* log)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
wLogAppender* appender;
|
|
|
|
|
|
|
|
appender = WLog_GetLogAppender(log);
|
|
|
|
|
|
|
|
if (!appender)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!appender->Close)
|
|
|
|
return TRUE;
|
|
|
|
|
2015-11-10 14:05:23 +03:00
|
|
|
if (appender->active)
|
2015-11-09 19:56:44 +03:00
|
|
|
{
|
|
|
|
status = appender->Close(log, appender);
|
2015-11-10 14:05:23 +03:00
|
|
|
appender->active = FALSE;
|
2015-11-09 19:56:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType)
|
|
|
|
{
|
2015-10-21 17:13:15 +03:00
|
|
|
wLogAppender* appender;
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!log)
|
|
|
|
return NULL;
|
2013-10-02 21:38:21 +04:00
|
|
|
|
2015-10-21 17:13:15 +03:00
|
|
|
switch (logAppenderType)
|
2013-10-02 21:38:21 +04:00
|
|
|
{
|
2015-10-21 17:13:15 +03:00
|
|
|
case WLOG_APPENDER_CONSOLE:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_ConsoleAppender_New(log);
|
2015-10-21 17:13:15 +03:00
|
|
|
break;
|
|
|
|
case WLOG_APPENDER_FILE:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_FileAppender_New(log);
|
2015-10-21 17:13:15 +03:00
|
|
|
break;
|
|
|
|
case WLOG_APPENDER_BINARY:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_BinaryAppender_New(log);
|
2015-10-21 17:13:15 +03:00
|
|
|
break;
|
|
|
|
case WLOG_APPENDER_CALLBACK:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_CallbackAppender_New(log);
|
2015-10-21 17:13:15 +03:00
|
|
|
break;
|
|
|
|
#ifdef HAVE_SYSLOG_H
|
|
|
|
case WLOG_APPENDER_SYSLOG:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_SyslogAppender_New(log);
|
2015-10-21 17:13:15 +03:00
|
|
|
break;
|
2015-10-30 16:50:14 +03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_JOURNALD_H
|
|
|
|
case WLOG_APPENDER_JOURNALD:
|
2015-11-09 19:56:44 +03:00
|
|
|
appender = WLog_JournaldAppender_New(log);
|
2015-10-30 16:50:14 +03:00
|
|
|
break;
|
2015-10-21 17:13:15 +03:00
|
|
|
#endif
|
2015-11-04 20:11:19 +03:00
|
|
|
case WLOG_APPENDER_UDP:
|
|
|
|
appender = (wLogAppender*) WLog_UdpAppender_New(log);
|
|
|
|
break;
|
2015-10-21 17:13:15 +03:00
|
|
|
default:
|
2016-12-14 00:47:08 +03:00
|
|
|
fprintf(stderr, "%s: unknown handler type %"PRIu32"\n", __FUNCTION__, logAppenderType);
|
2015-10-21 17:13:15 +03:00
|
|
|
appender = NULL;
|
|
|
|
break;
|
2014-11-16 14:20:48 +03:00
|
|
|
}
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
if (!appender)
|
|
|
|
appender = (wLogAppender*) WLog_ConsoleAppender_New(log);
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!appender)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(appender->Layout = WLog_Layout_New(log)))
|
|
|
|
{
|
|
|
|
WLog_Appender_Free(log, appender);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
InitializeCriticalSectionAndSpinCount(&appender->lock, 4000);
|
|
|
|
|
|
|
|
return appender;
|
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType)
|
2013-10-02 21:38:21 +04:00
|
|
|
{
|
|
|
|
if (!log)
|
2015-11-09 19:56:44 +03:00
|
|
|
return FALSE;
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
if (log->Appender)
|
|
|
|
{
|
|
|
|
WLog_Appender_Free(log, log->Appender);
|
|
|
|
log->Appender = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
log->Appender = WLog_Appender_New(log, logAppenderType);
|
2015-06-14 01:55:09 +03:00
|
|
|
return log->Appender != NULL;
|
2013-10-02 21:38:21 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
BOOL WLog_ConfigureAppender(wLogAppender *appender, const char *setting, void *value)
|
2013-10-02 21:38:21 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!appender || !setting || !strlen(setting))
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 21:38:21 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (appender->Set)
|
|
|
|
return appender->Set(appender, setting, value);
|
|
|
|
else
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 21:38:21 +04:00
|
|
|
|
|
|
|
}
|