Optional systemd journal logging

This commit is contained in:
Emery 2012-11-11 15:21:00 -06:00
parent 6af0f293a6
commit ce68c446d1
3 changed files with 43 additions and 2 deletions

View File

@ -19,6 +19,7 @@ option(LINK_SUPPORT "Allow hub linking" OFF)
option(SSL_SUPPORT "Enable SSL support" ON) option(SSL_SUPPORT "Enable SSL support" ON)
option(USE_OPENSSL "Use OpenSSL's SSL support" ON ) option(USE_OPENSSL "Use OpenSSL's SSL support" ON )
option(SQLITE_SUPPORT "Enable SQLite support" ON) option(SQLITE_SUPPORT "Enable SQLite support" ON)
option(SYSTEMD_SUPPORT "Enable logging to the systemd journal" OFF)
option(ADC_STRESS "Enable the stress tester client" OFF) option(ADC_STRESS "Enable the stress tester client" OFF)
find_package(Git) find_package(Git)
@ -34,6 +35,11 @@ if (SSL_SUPPORT)
endif() endif()
endif() endif()
if (SYSTEMD_SUPPORT)
INCLUDE(FindPkgConfig)
pkg_search_module(JOURNAL REQUIRED libsystemd-journal)
endif()
if (MSVC) if (MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS) add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif() endif()
@ -175,6 +181,15 @@ if(SSL_SUPPORT)
endif() endif()
endif() endif()
if (SYSTEMD_SUPPORT)
target_link_libraries(uhub ${JOURNAL_LIBRARIES})
target_link_libraries(test ${JOURNAL_LIBRARIES})
target_link_libraries(uhub-passwd ${JOURNAL_LIBRARIES})
target_link_libraries(uhub-admin ${JOURNAL_LIBRARIES})
include_directories(${JOURNAL_INCLUDE_DIRS})
add_definitions(-DSYSTEMD)
endif()
configure_file ("${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_SOURCE_DIR}/version.h") configure_file ("${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_SOURCE_DIR}/version.h")
mark_as_advanced(FORCE CMAKE_BUILD_TYPE) mark_as_advanced(FORCE CMAKE_BUILD_TYPE)

View File

@ -216,13 +216,17 @@ void print_usage(char* program)
" -q Quiet mode - no output\n" " -q Quiet mode - no output\n"
" -f Fork to background\n" " -f Fork to background\n"
" -l <file> Log messages to given file (default: stderr)\n" " -l <file> Log messages to given file (default: stderr)\n"
" -L Log messages to syslog\n"
" -c <file> Specify configuration file (default: " SERVER_CONFIG ")\n" " -c <file> Specify configuration file (default: " SERVER_CONFIG ")\n"
" -C Check configuration and return\n" " -C Check configuration and return\n"
" -s Show configuration parameters\n" " -s Show configuration parameters\n"
" -S Show configuration parameters, but ignore defaults\n" " -S Show configuration parameters, but ignore defaults\n"
" -h This message\n" " -h This message\n"
#ifndef WIN32 #ifndef WIN32
#ifdef SYSTEMD
" -L Log messages to journal\n"
#else
" -L Log messages to syslog\n"
#endif
" -u <user> Run as given user\n" " -u <user> Run as given user\n"
" -g <group> Run with given group permissions\n" " -g <group> Run with given group permissions\n"
" -p <file> Store pid in file (process id)\n" " -p <file> Store pid in file (process id)\n"

View File

@ -21,7 +21,15 @@
#include <locale.h> #include <locale.h>
#ifndef WIN32 #ifndef WIN32
#ifdef SYSTEMD
#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
#else
#include <syslog.h> #include <syslog.h>
#endif
static int use_syslog = 0; static int use_syslog = 0;
#endif #endif
@ -83,7 +91,9 @@ void hub_log_initialize(const char* file, int syslog)
if (syslog) if (syslog)
{ {
use_syslog = 1; use_syslog = 1;
#ifndef SYSTEMD
openlog("uhub", LOG_PID, LOG_USER); openlog("uhub", LOG_PID, LOG_USER);
#endif
} }
#endif #endif
@ -132,7 +142,9 @@ void hub_log_shutdown()
if (use_syslog) if (use_syslog)
{ {
use_syslog = 0; use_syslog = 0;
#ifndef SYSTEMD
closelog(); closelog();
#endif
} }
#endif #endif
} }
@ -212,7 +224,12 @@ void hub_log(int log_verbosity, const char *format, ...)
case log_fatal: level = LOG_CRIT; break; case log_fatal: level = LOG_CRIT; break;
case log_error: level = LOG_ERR; break; case log_error: level = LOG_ERR; break;
case log_warning: level = LOG_WARNING; break; case log_warning: level = LOG_WARNING; break;
#ifdef SYSTEMD
case log_user: level = LOG_INFO; break;
#else
case log_user: level = LOG_INFO | LOG_AUTH; break; case log_user: level = LOG_INFO | LOG_AUTH; break;
#endif
case log_info: level = LOG_INFO; break; case log_info: level = LOG_INFO; break;
case log_debug: level = LOG_DEBUG; break; case log_debug: level = LOG_DEBUG; break;
@ -224,8 +241,13 @@ void hub_log(int log_verbosity, const char *format, ...)
if (level == 0) if (level == 0)
return; return;
#ifdef SYSTEMD
sd_journal_print(level, "%s", logmsg);
#else
level |= (LOG_USER | LOG_DAEMON); level |= (LOG_USER | LOG_DAEMON);
syslog(level, "%s", logmsg); syslog(level, "%s", logmsg);
#endif
} }
#endif #endif