1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
23 #include "common-export.h"
34 * The Logger class encapsulates the various configured logging backends.
36 class COMMON_EXPORT Logger : public QObject
41 Logger(QObject* parent = nullptr);
53 struct COMMON_EXPORT LogEntry
60 * Gets this log entry in a printable format, with timestamp and log level
62 * @return the log entry, formatted with timestamp and log level
64 QString toString() const;
68 * Initial setup, to be called ones command line options are available.
70 * Sets up the log file if appropriate. Outputs the log messages already accumulated since
71 * construction. If @c keepMessages is false, deletes the accumulated messages afterwards,
72 * and won't store further ones.
74 * @param keepMessages Whether messages should be kept
75 * @throws ExitException, if command line options are invalid
77 void setup(bool keepMessages);
80 * Accesses the stores log messages, e.g. for consumption by DebugLogWidget.
82 * @returns The accumuates log messages
84 std::vector<Logger::LogEntry> messages() const;
86 static void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message);
89 * Takes the given message with the given log level, formats it and emits the @a messageLogged() signal.
91 * @note This method is thread-safe.
93 * @param logLevel The log leve of the message
94 * @param message The message
96 void handleMessage(LogLevel logLevel, const QString& message);
100 * Emitted whenever a message was logged.
102 * @param message The message that was logged
104 void messageLogged(const Logger::LogEntry& message);
107 void onMessageLogged(const Logger::LogEntry& message);
110 void handleMessage(QtMsgType type, const QString& message);
111 void outputMessage(const LogEntry& message);
114 LogLevel _outputLevel{LogLevel::Info};
116 bool _syslogEnabled{false};
118 std::vector<LogEntry> _messages;
119 bool _keepMessages{true};
120 bool _initialized{false};
124 Q_DECLARE_METATYPE(Logger::LogEntry)