From 2deb46a3ce819c709800f1ded9072e62807b9574 Mon Sep 17 00:00:00 2001 From: Tomas Chvatal Date: Fri, 1 Oct 2010 10:31:13 +0200 Subject: [PATCH] Introduce possibility to use syslog. Signed-off-by: Tomas Chvatal --- CMakeLists.txt | 6 +++++ src/common/logger.cpp | 52 +++++++++++++++++++++++++++++++++++++++---- src/common/main.cpp | 6 ++++- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e5faf02..319d6377 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ # -DWITH_KDE=ON : Enable KDE4 support # -DWITH_CRYPT=OFF : Disable encryption support # -DWITH_OXYGEN=(ON|OFF) : Whether to install Oxygen icons (default: yes, unless KDE > 4.3.0 is present and enabled) +# -DWITH_SYSLOG=OFF : Use syslog for logging # # -DEMBED_DATA=ON : Embed all data files in icons the binary, rather than installing them separately # @@ -46,6 +47,7 @@ option(WITH_PHONON "Enable Phonon support (for audio notifications)" ON) option(WITH_LIBINDICATE "Enable Ayatana notification support" ON) option(WITH_KDE "Enable KDE4 integration" OFF) option(WITH_CRYPT "Enable encryption support if present on system" ON) +option(WITH_SYSLOG "Use syslog for storing log data" OFF) # We use icon paths from KDE 4.3.x, which are partially invalid on older and possibly # even on newer KDE versions. Do not disable this unless you are sure that your Quassel will @@ -58,6 +60,10 @@ if(APPLE) option(DEPLOY "Mac OS X only! Adds required libs to bundle resources and create a dmg. Note: requires Qt to be built with 10.4u SDK" OFF) endif(APPLE) +if(WITH_SYSLOG) + check_include_file(syslog.h HAVE_SYSLOG_H) +endif(WITH_SYSLOG) + # Default to embedding data in the static case if(STATIC OR WIN32) set(EMBED_DEFAULT ON) diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 17c473b5..d8da4c7d 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -21,17 +21,37 @@ #include "logger.h" #include "quassel.h" +#ifdef HAVE_SYSLOG_H +#include +#else #include #include #include +#endif Logger::~Logger() { QDateTime date = QDateTime::currentDateTime(); - if(_logLevel == DebugLevel) _buffer.prepend("Debug: "); - else if (_logLevel == InfoLevel) _buffer.prepend("Info: "); - else if (_logLevel == WarningLevel) _buffer.prepend("Warning: "); - else if (_logLevel == ErrorLevel) _buffer.prepend("Error: "); + + switch (_logLevel) { + case DebugLevel: + _buffer.prepend("Debug: "); + break; + case InfoLevel: + _buffer.prepend("Info: "); + break; + case WarningLevel: + _buffer.prepend("Warning: "); + break; + case ErrorLevel: + _buffer.prepend("Error: "); + break; + default: + Q_ASSERT(_logLevel); // There should be no unknown log level + break; + } +#ifndef HAVE_SYSLOG_H _buffer.prepend(date.toString("yyyy-MM-dd hh:mm:ss ")); +#endif log(); } @@ -45,6 +65,29 @@ void Logger::log() { if(_logLevel < lvl) return; +#ifdef HAVE_SYSLOG_H + if (Quassel::isOptionSet("syslog")) { + int prio; + switch (lvl) { + case DebugLevel: + prio = LOG_DEBUG; + break; + case InfoLevel: + prio = LOG_INFO; + break; + case WarningLevel: + prio = LOG_WARNING; + break; + case ErrorLevel: + prio = LOG_ERR; + break; + default: + prio = LOG_INFO; + break; + } + syslog(LOG_USER & prio, "%s", qPrintable(_buffer)); + } +#else // if we can't open logfile we log to stdout QTextStream out(stdout); QFile file; @@ -57,6 +100,7 @@ void Logger::log() { } out << _buffer << endl; if(file.isOpen()) file.close(); +#endif } diff --git a/src/common/main.cpp b/src/common/main.cpp index 9823f85b..2bb2cdaf 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -104,8 +104,12 @@ int main(int argc, char **argv) { cliParser->addOption("listen
[,", 0, "The address(es) quasselcore will listen on", "::,0.0.0.0"); cliParser->addOption("port ",'p', "The port quasselcore will listen at", QString("4242")); cliParser->addSwitch("norestore", 'n', "Don't restore last core's state"); - cliParser->addOption("logfile ", 'l', "Path to logfile"); cliParser->addOption("loglevel ", 'L', "Loglevel Debug|Info|Warning|Error", "Info"); +#ifdef HAVE_SYSLOG_H + cliParser->addSwitch("syslog", 0, "Send the log to syslog."); +#else + cliParser->addOption("logfile ", 'l', "Path to logfile"); +#endif cliParser->addOption("select-backend ", 0, "Starts an interactive session and switches your current storage backend to the new one. Attempts a merge if the new backend is uninitialized and the old backend supports migration. Otherwise prompts for new user credentials!"); cliParser->addSwitch("add-user", 0, "Starts an interactive session to add a new core user"); cliParser->addOption("change-userpass ", 0, "Starts an interactive session to change the password of the user identified by username"); -- 2.20.1