diff --git a/CMakeLists.txt b/CMakeLists.txt index 64cc2075c..94e807499 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -334,6 +334,7 @@ if(NOT IOS) check_include_files(sys/sockio.h HAVE_SYS_SOCKIO_H) check_include_files(sys/strtio.h HAVE_SYS_STRTIO_H) check_include_files(sys/select.h HAVE_SYS_SELECT_H) + check_include_files(syslog.h HAVE_SYSLOG_H) else() set(HAVE_FCNTL_H 1) set(HAVE_UNISTD_H 1) diff --git a/config.h.in b/config.h.in index cf1e7f082..83f3ee641 100644 --- a/config.h.in +++ b/config.h.in @@ -32,6 +32,7 @@ #cmakedefine HAVE_TM_GMTOFF #cmakedefine HAVE_AIO_H #cmakedefine HAVE_POLL_H +#cmakedefine HAVE_SYSLOG_H #cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK #cmakedefine HAVE_VALGRIND_MEMCHECK_H #cmakedefine HAVE_EXECINFO_H diff --git a/winpr/include/winpr/wlog.h b/winpr/include/winpr/wlog.h index 7e76d2c6e..f60e706a0 100644 --- a/winpr/include/winpr/wlog.h +++ b/winpr/include/winpr/wlog.h @@ -113,6 +113,7 @@ struct _wLogLayout #define WLOG_APPENDER_FILE 1 #define WLOG_APPENDER_BINARY 2 #define WLOG_APPENDER_CALLBACK 3 +#define WLOG_APPENDER_SYSLOG 4 #define WLOG_PACKET_INBOUND 1 #define WLOG_PACKET_OUTBOUND 2 @@ -197,6 +198,14 @@ struct _wLogCallbackAppender }; typedef struct _wLogCallbackAppender wLogCallbackAppender; +#ifdef HAVE_SYSLOG_H +struct _wLogSyslogAppender +{ + WLOG_APPENDER_COMMON(); +}; +typedef struct _wLogSyslogAppender wLogSyslogAppender; +#endif + /** * Filter */ diff --git a/winpr/libwinpr/utils/CMakeLists.txt b/winpr/libwinpr/utils/CMakeLists.txt index 949908ca3..654a321bd 100644 --- a/winpr/libwinpr/utils/CMakeLists.txt +++ b/winpr/libwinpr/utils/CMakeLists.txt @@ -51,6 +51,13 @@ set(${MODULE_PREFIX}_TRIO_SRCS trio/triop.h trio/triostr.c trio/triostr.h) + +if (HAVE_SYSLOG_H) + set(SYSLOG_SRCS + wlog/SyslogAppender.c + wlog/SyslogAppender.h + ) +endif() set(${MODULE_PREFIX}_WLOG_SRCS wlog/wlog.c @@ -76,7 +83,10 @@ set(${MODULE_PREFIX}_WLOG_SRCS wlog/CallbackAppender.c wlog/CallbackAppender.h wlog/ConsoleAppender.c - wlog/ConsoleAppender.h) + wlog/ConsoleAppender.h + ${SYSLOG_SRCS} + ) + set(${MODULE_PREFIX}_SRCS ini.c diff --git a/winpr/libwinpr/utils/wlog/Appender.c b/winpr/libwinpr/utils/wlog/Appender.c index 33b422c15..3ca867912 100644 --- a/winpr/libwinpr/utils/wlog/Appender.c +++ b/winpr/libwinpr/utils/wlog/Appender.c @@ -29,25 +29,34 @@ wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType) { - wLogAppender* appender = NULL; + wLogAppender* appender; + if (!log) return NULL; - if (logAppenderType == WLOG_APPENDER_CONSOLE) + switch (logAppenderType) { + case WLOG_APPENDER_CONSOLE: appender = (wLogAppender*) WLog_ConsoleAppender_New(log); - } - else if (logAppenderType == WLOG_APPENDER_FILE) - { + break; + case WLOG_APPENDER_FILE: appender = (wLogAppender*) WLog_FileAppender_New(log); - } - else if (logAppenderType == WLOG_APPENDER_BINARY) - { + break; + case WLOG_APPENDER_BINARY: appender = (wLogAppender*) WLog_BinaryAppender_New(log); - } - else if (logAppenderType == WLOG_APPENDER_CALLBACK) - { + break; + case WLOG_APPENDER_CALLBACK: appender = (wLogAppender*) WLog_CallbackAppender_New(log); + break; +#ifdef HAVE_SYSLOG_H + case WLOG_APPENDER_SYSLOG: + appender = (wLogAppender*) WLog_SyslogAppender_New(log); + break; +#endif + default: + fprintf(stderr, "%s: unknown handler type %d\n", __FUNCTION__, logAppenderType); + appender = NULL; + break; } if (!appender) @@ -69,32 +78,39 @@ wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType) void WLog_Appender_Free(wLog* log, wLogAppender* appender) { - if (appender) + if (!appender) + return; + + if (appender->Layout) { - if (appender->Layout) - { - WLog_Layout_Free(log, appender->Layout); - appender->Layout = NULL; - } + WLog_Layout_Free(log, appender->Layout); + appender->Layout = NULL; + } - DeleteCriticalSection(&appender->lock); + 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); - } - else if (appender->Type == WLOG_APPENDER_CALLBACK) - { - WLog_CallbackAppender_Free(log, (wLogCallbackAppender*) appender); - } + switch (appender->Type) + { + case WLOG_APPENDER_CONSOLE: + WLog_ConsoleAppender_Free(log, (wLogConsoleAppender*) appender); + break; + case WLOG_APPENDER_FILE: + WLog_FileAppender_Free(log, (wLogFileAppender*) appender); + break; + case WLOG_APPENDER_BINARY: + WLog_BinaryAppender_Free(log, (wLogBinaryAppender*) appender); + break; + case WLOG_APPENDER_CALLBACK: + WLog_CallbackAppender_Free(log, (wLogCallbackAppender*) appender); + break; +#ifdef HAVE_SYSLOG_H + case WLOG_APPENDER_SYSLOG: + WLog_SyslogAppender_Free(log, (wLogSyslogAppender *) appender); + break; +#endif + default: + fprintf(stderr, "%s: don't know how to free appender type %d\n", __FUNCTION__, appender->Type); + break; } } diff --git a/winpr/libwinpr/utils/wlog/Appender.h b/winpr/libwinpr/utils/wlog/Appender.h index 4be3306a0..165e348b8 100644 --- a/winpr/libwinpr/utils/wlog/Appender.h +++ b/winpr/libwinpr/utils/wlog/Appender.h @@ -27,6 +27,10 @@ #include "wlog/ConsoleAppender.h" #include "wlog/CallbackAppender.h" +#ifdef HAVE_SYSLOG_H +#include "wlog/SyslogAppender.h" +#endif + void WLog_Appender_Free(wLog* log, wLogAppender* appender); #include "wlog/wlog.h" diff --git a/winpr/libwinpr/utils/wlog/SyslogAppender.c b/winpr/libwinpr/utils/wlog/SyslogAppender.c new file mode 100644 index 000000000..1126fdc36 --- /dev/null +++ b/winpr/libwinpr/utils/wlog/SyslogAppender.c @@ -0,0 +1,144 @@ +/** + * WinPR: Windows Portable Runtime + * WinPR Logger + * + * Copyright 2013 David FORT + * + * 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 + +#include +#include +#include + +#include + +#include "wlog/Message.h" +#include "wlog/SyslogAppender.h" + +static int getSyslogLevel(DWORD level) +{ + switch (level) + { + case WLOG_TRACE: + case WLOG_DEBUG: + return LOG_DEBUG; + case WLOG_INFO: + return LOG_INFO; + case WLOG_WARN: + return LOG_WARNING; + case WLOG_ERROR: + return LOG_ERR; + case WLOG_FATAL: + return LOG_CRIT; + case WLOG_OFF: + default: + return -1; + } +} + +static int WLog_SyslogAppender_Open(wLog* log, wLogSyslogAppender* appender) +{ + if (!log || !appender) + return -1; + + return 0; +} + +static int WLog_SyslogAppender_Close(wLog* log, wLogSyslogAppender* appender) +{ + if (!log || !appender) + return -1; + + return 0; +} + +static int WLog_SyslogAppender_WriteMessage(wLog* log, wLogSyslogAppender* appender, wLogMessage* message) +{ + int syslogLevel; + + if (!log || !appender || !message) + return -1; + + syslogLevel = getSyslogLevel(message->Level); + if (syslogLevel >= 0) + syslog(syslogLevel, "%s", message->TextString); + + return 1; +} + +static int WLog_SyslogAppender_WriteDataMessage(wLog* log, wLogSyslogAppender* appender, wLogMessage* message) +{ + int syslogLevel; + + if (!log || !appender || !message) + return -1; + + syslogLevel = getSyslogLevel(message->Level); + if (syslogLevel >= 0) + syslog(syslogLevel, "skipped data message of %d bytes", message->Length); + + return 0; +} + + + +static int WLog_SyslogAppender_WriteImageMessage(wLog* log, wLogSyslogAppender* appender, wLogMessage* message) +{ + int syslogLevel; + + if (!log || !appender || !message) + return -1; + + syslogLevel = getSyslogLevel(message->Level); + if (syslogLevel >= 0) + syslog(syslogLevel, "skipped image (%dx%dx%d)", message->ImageWidth, message->ImageHeight, message->ImageBpp); + + return 0; +} + +wLogSyslogAppender* WLog_SyslogAppender_New(wLog* log) +{ + wLogSyslogAppender* appender; + + appender = (wLogSyslogAppender*) calloc(1, sizeof(wLogSyslogAppender)); + if (!appender) + return NULL; + + appender->Type = WLOG_APPENDER_SYSLOG; + + appender->Open = (WLOG_APPENDER_OPEN_FN) WLog_SyslogAppender_Open; + appender->Close = (WLOG_APPENDER_OPEN_FN) WLog_SyslogAppender_Close; + appender->WriteMessage = + (WLOG_APPENDER_WRITE_MESSAGE_FN) WLog_SyslogAppender_WriteMessage; + appender->WriteDataMessage = + (WLOG_APPENDER_WRITE_DATA_MESSAGE_FN) WLog_SyslogAppender_WriteDataMessage; + appender->WriteImageMessage = + (WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN) WLog_SyslogAppender_WriteImageMessage; + + return appender; +} + +void WLog_SyslogAppender_Free(wLog* log, wLogSyslogAppender* appender) +{ + if (appender) + { + free(appender); + } +} diff --git a/winpr/libwinpr/utils/wlog/SyslogAppender.h b/winpr/libwinpr/utils/wlog/SyslogAppender.h new file mode 100644 index 000000000..2ce6b527c --- /dev/null +++ b/winpr/libwinpr/utils/wlog/SyslogAppender.h @@ -0,0 +1,35 @@ +/** + * Copyright © 2015 Thincast Technologies GmbH + * Copyright © 2015 David FORT + * + * Permission to use, copy, modify, distribute, and sell this software and + * its documentation for any purpose is hereby granted without fee, provided + * that the above copyright notice appear in all copies and that both that + * copyright notice and this permission notice appear in supporting + * documentation, and that the name of the copyright holders not be used in + * advertising or publicity pertaining to distribution of the software + * without specific, written prior permission. The copyright holders make + * no representations about the suitability of this software for any + * purpose. It is provided "as is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER + * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF + * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef WINPR_LIBWINPR_UTILS_WLOG_SYSLOGAPPENDER_H_ +#define WINPR_LIBWINPR_UTILS_WLOG_SYSLOGAPPENDER_H_ + +#include + +#include "wlog/wlog.h" + +WINPR_API wLogSyslogAppender* WLog_SyslogAppender_New(wLog* log); +WINPR_API void WLog_SyslogAppender_Free(wLog* log, wLogSyslogAppender* appender); + + +#endif /* WINPR_LIBWINPR_UTILS_WLOG_SYSLOGAPPENDER_H_ */