modernize: Replace most remaining old-style connects by PMF ones
[quassel.git] / src / common / logger.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "common-export.h"
24
25 #include <vector>
26
27 #include <QDateTime>
28 #include <QFile>
29 #include <QMetaType>
30 #include <QObject>
31 #include <QString>
32
33 /**
34  * The Logger class encapsulates the various configured logging backends.
35  */
36 class COMMON_EXPORT Logger : public QObject
37 {
38     Q_OBJECT
39
40 public:
41     Logger(QObject *parent = nullptr);
42     ~Logger() override;
43
44     enum class LogLevel {
45         Debug,
46         Info,
47         Warning,
48         Error,
49         Fatal
50     };
51
52     struct LogEntry {
53         QDateTime timeStamp;
54         LogLevel logLevel;
55         QString message;
56     };
57
58     /**
59      * Initial setup, to be called ones command line options are available.
60      *
61      * Sets up the log file if appropriate. Outputs the log messages already accumulated since
62      * construction. If @c keepMessages is false, deletes the accumulated messages afterwards,
63      * and won't store further ones.
64      *
65      * @param keepMessages Whether messages should be kept
66      * @throws ExitException, if command line options are invalid
67      */
68     void setup(bool keepMessages);
69
70     /**
71      * Accesses the stores log messages, e.g. for consumption by DebugLogWidget.
72      *
73      * @returns The accumuates log messages
74      */
75     std::vector<Logger::LogEntry> messages() const;
76
77     static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message);
78
79     /**
80      * Takes the given message with the given log level, formats it and emits the @a messageLogged() signal.
81      *
82      * @note This method is thread-safe.
83      *
84      * @param logLevel The log leve of the message
85      * @param message  The message
86      */
87     void handleMessage(LogLevel logLevel, const QString &message);
88
89 signals:
90     /**
91      * Emitted whenever a message was logged.
92      *
93      * @param message The message that was logged
94      */
95     void messageLogged(const Logger::LogEntry &message);
96
97 private slots:
98     void onMessageLogged(const Logger::LogEntry &message);
99
100 private:
101     void handleMessage(QtMsgType type, const QString &message);
102     void outputMessage(const LogEntry &message);
103
104 private:
105     LogLevel _outputLevel{LogLevel::Info};
106     QFile _logFile;
107     bool _syslogEnabled{false};
108
109     std::vector<LogEntry> _messages;
110     bool _keepMessages{true};
111     bool _initialized{false};
112 };
113
114 Q_DECLARE_METATYPE(Logger::LogEntry)