logger: export LogEntry symbols
[quassel.git] / src / common / logger.h
index 1162fa4..f55590c 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2016 by the Quassel Project                        *
+ *   Copyright (C) 2005-2019 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
-#ifndef LOGGER_H
-#define LOGGER_H
+#pragma once
 
-#include <QStringList>
-#include <QTextStream>
+#include "common-export.h"
 
-#include "quassel.h"
-#include "types.h"
+#include <vector>
 
-class Logger
+#include <QDateTime>
+#include <QFile>
+#include <QMetaType>
+#include <QObject>
+#include <QString>
+
+/**
+ * The Logger class encapsulates the various configured logging backends.
+ */
+class COMMON_EXPORT Logger : public QObject
 {
+    Q_OBJECT
+
 public:
-    inline Logger(Quassel::LogLevel level) : _stream(&_buffer, QIODevice::WriteOnly), _logLevel(level) {}
-    ~Logger();
+    Logger(QObject* parent = nullptr);
+    ~Logger() override;
 
-#if QT_VERSION < 0x050000
-    static void logMessage(QtMsgType type, const char *msg);
-#else
-    static void logMessage(QtMsgType, const QMessageLogContext&, const QString&);
-#endif
+    enum class LogLevel
+    {
+        Debug,
+        Info,
+        Warning,
+        Error,
+        Fatal
+    };
 
-    template<typename T>
-    inline Logger &operator<<(const T &value) { _stream << value << " "; return *this; }
-    inline Logger &operator<<(const QStringList &t) { _stream << t.join(" ") << " "; return *this; }
-    inline Logger &operator<<(bool t) { _stream << (t ? "true" : "false") << " "; return *this; }
+    struct COMMON_EXPORT LogEntry
+    {
+        QDateTime timeStamp;
+        LogLevel logLevel;
+        QString message;
 
-private:
-    void log();
-    QTextStream _stream;
-    QString _buffer;
-    Quassel::LogLevel _logLevel;
-};
+        /**
+         * Gets this log entry in a printable format, with timestamp and log level
+         *
+         * @return the log entry, formatted with timestamp and log level
+         */
+        QString toString() const;
+    };
 
+    /**
+     * 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);
 
-class quInfo : public Logger
-{
-public:
-    inline quInfo() : Logger(Quassel::InfoLevel) {}
-};
+    /**
+     * Accesses the stores log messages, e.g. for consumption by DebugLogWidget.
+     *
+     * @returns The accumuates log messages
+     */
+    std::vector<Logger::LogEntry> messages() const;
 
+    static void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message);
 
-class quWarning : public Logger
-{
-public:
-    inline quWarning() : Logger(Quassel::WarningLevel) {}
-};
+    /**
+     * 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);
 
-class quError : public Logger
-{
-public:
-    inline quError() : Logger(Quassel::ErrorLevel) {}
-};
+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<LogEntry> _messages;
+    bool _keepMessages{true};
+    bool _initialized{false};
+    QByteArray _prgname;
+};
 
-#endif
+Q_DECLARE_METATYPE(Logger::LogEntry)