X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcommon%2Flogger.h;h=e6701fe0d3c46e2aec46f97a8149f7893c2f0d97;hb=6718d7a1ccd42d7aae75e57d6974e0b1e0384044;hp=f49d270da7aea5a59cc3530b45a17c6ca2a3b9db;hpb=6b75ac568098c17de5f0a98ce0d96781b86e98b3;p=quassel.git diff --git a/src/common/logger.h b/src/common/logger.h index f49d270d..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,61 +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 + +#include +#include +#include +#include #include -#include - -class Logger { - public: - enum LogLevel { - DebugLevel, - InfoLevel, - WarningLevel, - ErrorLevel + +/** + * The Logger class encapsulates the various configured logging backends. + */ +class Logger : public QObject +{ + Q_OBJECT + +public: + Logger(QObject *parent = nullptr); + ~Logger() override; + + enum class LogLevel { + Debug, + Info, + Warning, + Error, + Fatal }; - inline Logger(LogLevel level) : stream(new Stream(level)) {} - ~Logger(); - - inline Logger &operator<<(const char* t) { stream->internalStream << QString::fromAscii(t); return *this; } - inline Logger &operator<<(QChar t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(bool t) { stream->internalStream << (t ? "true" : "false"); return *this; } - inline Logger &operator<<(char t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(signed short t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned short t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(signed int t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned int t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(signed long t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned long t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(qint64 t) { stream->internalStream << QString::number(t); return *this; } - inline Logger &operator<<(quint64 t) { stream->internalStream << QString::number(t); return *this; } - inline Logger &operator<<(float t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(double t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(const QString & t) { stream->internalStream << t; return *this; } - inline Logger &operator<<(const QLatin1String &t) { stream->internalStream << t.latin1(); return *this; } - inline Logger &operator<<(const QByteArray & t) { stream->internalStream << t ; return *this; } - inline Logger &operator<<(const void * t) { stream->internalStream << t; return *this; } - - void log(); - private: - struct Stream { - Stream(LogLevel level) - : internalStream(&buffer, QIODevice::WriteOnly), - logLevel(level) {} - QTextStream internalStream; - QString buffer; - LogLevel logLevel; - } *stream; -}; + struct LogEntry { + QDateTime timeStamp; + LogLevel logLevel; + QString message; + }; + + /** + * 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); + + /** + * Accesses the stores log messages, e.g. for consumption by DebugLogWidget. + * + * @returns The accumuates log messages + */ + std::vector messages() const; -inline Logger quDebug() { return Logger(Logger::DebugLevel); } -inline Logger quInfo() { return Logger(Logger::InfoLevel); } -inline Logger quWarning() { return Logger(Logger::WarningLevel); } -inline Logger quError() { return Logger(Logger::ErrorLevel); } + static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message); + + /** + * 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); + +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)