X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Flogger.cpp;h=59efa6b6d1b2741c0269ca11b8b832c3102e3b4c;hp=76fa916ec11b6bb57298de6c90f2ff1e9aeb6c97;hb=c9b7f5cfe4377cc242c24212fff48aad70192b48;hpb=0ac9ce4d7cf768d13993d6aa1d6b791c4149a843 diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 76fa916e..59efa6b6 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -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 * @@ -19,32 +19,42 @@ ***************************************************************************/ #include "logger.h" +#include "global.h" -#include - - +#include +#include +#include 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 (Global::parser.value("loglevel") == "Debug") lvl = DebugLevel; + else if (Global::parser.value("loglevel") == "Info") lvl = InfoLevel; + else if (Global::parser.value("loglevel") == "Warning") lvl = WarningLevel; + else if (Global::parser.value("loglevel") == "Error") lvl = ErrorLevel; + else lvl = InfoLevel; + + if(_logLevel < lvl) return; -Logger::Logger() { - //qInstallMsgHandler(messageHandler); + // if we can't open logfile we log to stdout + QTextStream out(stdout); + QFile file; + if(!Global::parser.value("logfile").isEmpty()) { + file.setFileName(Global::parser.value("logfile")); + if (file.open(QIODevice::Append | QIODevice::Text)) { + out.setDevice(&file); + _buffer.remove(QChar('\n')); + } + } + out << _buffer << endl; + if(file.isOpen()) file.close(); }