X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Flogger.h;h=9d80305bb38f19a7953346832e746453e22666a1;hp=ba8eba42dcf219b174576116fe9302f1deb88c83;hb=6eefdfc697067d184a589fc8a231b16316c09106;hpb=077d44f36d2f5c730283ef6be839aea7dd073d56 diff --git a/src/common/logger.h b/src/common/logger.h index ba8eba42..9d80305b 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005 by The Quassel Team * + * Copyright (C) 2005-2018 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 * @@ -15,27 +15,100 @@ * 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 "common-export.h" -class Logger : public QObject { - Q_OBJECT +#include - public: - Logger(); - virtual ~Logger(); +#include +#include +#include +#include +#include +/** + * The Logger class encapsulates the various configured logging backends. + */ +class COMMON_EXPORT Logger : public QObject +{ + Q_OBJECT +public: + Logger(QObject *parent = nullptr); + ~Logger() override; - private: - //void messageHandler(QtMsgType type, const char *msg); -}; + enum class LogLevel { + Debug, + Info, + Warning, + Error, + Fatal + }; + + 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; + + 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)