migrating identities from QSettings to the storage backend
[quassel.git] / src / common / logger.cpp
index 76fa916..17c473b 100644 (file)
@@ -1,11 +1,11 @@
 /***************************************************************************
- *   Copyright (C) 2005 by The Quassel Team                                *
+ *   Copyright (C) 2005 by the Quassel Project                             *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
  *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
+ *   (at your option) version 3.                                           *
  *                                                                         *
  *   This program is distributed in the hope that it will be useful,       *
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  ***************************************************************************/
 
 #include "logger.h"
+#include "quassel.h"
 
-#include <iostream>
-
-
+#include <QFile>
+#include <QTextStream>
+#include <QDateTime>
 
 Logger::~Logger() {
-  //qInstallMsgHandler(0);
+  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: ");
+  _buffer.prepend(date.toString("yyyy-MM-dd hh:mm:ss "));
+  log();
 }
 
-void messageHandler(QtMsgType type, const char *msg) {
-  switch (type) {
-    case QtDebugMsg:
-      std::cerr << "[DEBUG] " << msg << "\n";
-      break;
-    case QtWarningMsg:
-      std::cerr << "[WARNING] " << msg << "\n";
-      break;
-    case QtCriticalMsg:
-      std::cerr << "[CRITICAL] " << msg << "\n";
-      break;
-    case QtFatalMsg:
-      std::cerr << "[FATAL] " << msg << "\n";
-      abort(); // deliberately core dump
+void Logger::log() {
+  LogLevel lvl;
+  if (Quassel::optionValue("loglevel") == "Debug") lvl = DebugLevel;
+  else if (Quassel::optionValue("loglevel") == "Info") lvl = InfoLevel;
+  else if (Quassel::optionValue("loglevel") == "Warning") lvl = WarningLevel;
+  else if (Quassel::optionValue("loglevel") == "Error") lvl = ErrorLevel;
+  else lvl = InfoLevel;
+
+  if(_logLevel < lvl) return;
+
+  // if we can't open logfile we log to stdout
+  QTextStream out(stdout);
+  QFile file;
+  if(!Quassel::optionValue("logfile").isEmpty()) {
+    file.setFileName(Quassel::optionValue("logfile"));
+    if (file.open(QIODevice::Append | QIODevice::Text)) {
+      out.setDevice(&file);
+      _buffer.remove(QChar('\n'));
+    }
   }
+  out << _buffer << endl;
+  if(file.isOpen()) file.close();
 }
 
-Logger::Logger() {
-  //qInstallMsgHandler(messageHandler);
+
+void Logger::logMessage(QtMsgType type, const char *msg) {
+  switch (type) {
+  case QtDebugMsg:
+    Logger(Logger::DebugLevel) << msg;
+    break;
+  case QtWarningMsg:
+    Logger(Logger::WarningLevel) << msg;
+    break;
+  case QtCriticalMsg:
+    Logger(Logger::ErrorLevel) << msg;
+    break;
+  case QtFatalMsg:
+    Logger(Logger::ErrorLevel) << msg;
+    Quassel::logFatalMessage(msg);
+    return;
+  }
 }