X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Flogger.h;h=e6701fe0d3c46e2aec46f97a8149f7893c2f0d97;hp=352b0ce37e2bd28d94f7f23411f59715e0ac67d2;hb=ce250a863bce3198096e65d4c7a68269495302dd;hpb=1e8ee5e5467db6fe9795f6a6e806e310bf07ef9d diff --git a/src/common/logger.h b/src/common/logger.h index 352b0ce3..e6701fe0 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005 by the Quassel Project * + * Copyright (C) 2005-2018 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -15,57 +15,98 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef LOGGER_H -#define LOGGER_H +#pragma once -#include "types.h" +#include +#include +#include +#include +#include #include -#include -#include -class Logger { +/** + * The Logger class encapsulates the various configured logging backends. + */ +class Logger : public QObject +{ + Q_OBJECT + public: - enum LogLevel { - DebugLevel, - InfoLevel, - WarningLevel, - ErrorLevel - }; + Logger(QObject *parent = nullptr); + ~Logger() override; - inline Logger(LogLevel level) : _stream(&_buffer, QIODevice::WriteOnly), _logLevel(level) {} - ~Logger(); + enum class LogLevel { + Debug, + Info, + Warning, + Error, + Fatal + }; - static void logMessage(QtMsgType type, const char *msg); + struct LogEntry { + QDateTime timeStamp; + LogLevel logLevel; + QString message; + }; - template - inline Logger &operator<<(const T &value) { _stream << value << " "; return *this; } - inline Logger &operator<<(const QStringList & t) { _stream << t.join(" ") << " "; return *this; } - inline Logger &operator<<(bool t) { _stream << (t ? "true" : "false") << " "; return *this; } + /** + * Initial setup, to be called ones command line options are available. + * + * Sets up the log file if appropriate. Outputs the log messages already accumulated since + * construction. If @c keepMessages is false, deletes the accumulated messages afterwards, + * and won't store further ones. + * + * @param keepMessages Whether messages should be kept + * @throws ExitException, if command line options are invalid + */ + void setup(bool keepMessages); -private: - void log(); - QTextStream _stream; - QString _buffer; - LogLevel _logLevel; -}; + /** + * Accesses the stores log messages, e.g. for consumption by DebugLogWidget. + * + * @returns The accumuates log messages + */ + std::vector messages() const; -class quInfo : public Logger { -public: - inline quInfo() : Logger(Logger::InfoLevel) {} -}; + static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message); -class quWarning : public Logger { -public: - inline quWarning() : Logger(Logger::WarningLevel) {} -}; + /** + * Takes the given message with the given log level, formats it and emits the @a messageLogged() signal. + * + * @note This method is thread-safe. + * + * @param logLevel The log leve of the message + * @param message The message + */ + void handleMessage(LogLevel logLevel, const QString &message); -class quError : public Logger { -public: - inline quError() : Logger(Logger::ErrorLevel) {} +signals: + /** + * Emitted whenever a message was logged. + * + * @param message The message that was logged + */ + void messageLogged(const Logger::LogEntry &message); + +private slots: + void onMessageLogged(const Logger::LogEntry &message); + +private: + void handleMessage(QtMsgType type, const QString &message); + void outputMessage(const LogEntry &message); + +private: + LogLevel _outputLevel{LogLevel::Info}; + QFile _logFile; + bool _syslogEnabled{false}; + + std::vector _messages; + bool _keepMessages{true}; + bool _initialized{false}; }; -#endif +Q_DECLARE_METATYPE(Logger::LogEntry)