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