Introduce possibility to use syslog.
authorTomas Chvatal <scarabeus@gentoo.org>
Fri, 1 Oct 2010 08:31:13 +0000 (10:31 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 20 Jan 2012 18:31:13 +0000 (19:31 +0100)
Signed-off-by: Tomas Chvatal <scarabeus@gentoo.org>
CMakeLists.txt
src/common/logger.cpp
src/common/main.cpp

index 2e5faf0..319d637 100644 (file)
@@ -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)
index 17c473b..d8da4c7 100644 (file)
 #include "logger.h"
 #include "quassel.h"
 
+#ifdef HAVE_SYSLOG_H
+#include <syslog.h>
+#else
 #include <QFile>
 #include <QTextStream>
 #include <QDateTime>
+#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
 }
 
 
index 9823f85..2bb2cda 100644 (file)
@@ -104,8 +104,12 @@ int main(int argc, char **argv) {
   cliParser->addOption("listen <address>[,<address[,...]]>", 0, "The address(es) quasselcore will listen on", "::,0.0.0.0");
   cliParser->addOption("port <port>",'p', "The port quasselcore will listen at", QString("4242"));
   cliParser->addSwitch("norestore", 'n', "Don't restore last core's state");
-  cliParser->addOption("logfile <path>", 'l', "Path to logfile");
   cliParser->addOption("loglevel <level>", 'L', "Loglevel Debug|Info|Warning|Error", "Info");
+#ifdef HAVE_SYSLOG_H
+  cliParser->addSwitch("syslog", 0, "Send the log to syslog.");
+#else
+  cliParser->addOption("logfile <path>", 'l', "Path to logfile");
+#endif
   cliParser->addOption("select-backend <backendidentifier>", 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 <username>", 0, "Starts an interactive session to change the password of the user identified by username");