File indexing completed on 2024-09-22 05:15:54

0001 /*
0002     This file is part of the Okteta Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #ifndef KASTEN_SCRIPTLOGGER_HPP
0010 #define KASTEN_SCRIPTLOGGER_HPP
0011 
0012 #include <QAbstractTableModel>
0013 #include <QVector>
0014 #include <QDebug>
0015 #include <QTime>
0016 
0017 class QIcon;
0018 
0019 class DataInformation;
0020 /** NOT THREAD SAFE! */
0021 class ScriptLogger : public QAbstractTableModel
0022 {
0023     Q_OBJECT
0024 
0025 public:
0026     explicit ScriptLogger();
0027     ~ScriptLogger() override;
0028 
0029     enum Columns
0030     {
0031         ColumnTime = 0,
0032         ColumnOrigin,
0033         ColumnMessage,
0034         COLUMN_COUNT
0035     };
0036     // fits into 2 bits
0037     enum LogLevel
0038     {
0039         LogInvalid = 0,
0040         LogInfo = 1,
0041         LogWarning = 2,
0042         LogError = 3
0043     };
0044     struct Data
0045     {
0046         inline Data() = default;
0047         inline Data(LogLevel lvl, const QString& o)
0048             : level(lvl)
0049             , origin(o)
0050             , time(QTime::currentTime())
0051         {}
0052         inline Data(const Data& d) = default;
0053         inline ~Data() = default;
0054 
0055         Data& operator=(const Data&) = default;
0056 
0057         ScriptLogger::LogLevel level = LogInvalid;
0058         QString message;
0059         QString origin;
0060         QTime time;
0061     };
0062     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0063     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0064     int columnCount(const QModelIndex& parent = QModelIndex()) const override;
0065     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0066 
0067     inline QDebug info(const DataInformation* origin) { return log(LogInfo, origin); }
0068     inline QDebug warn(const DataInformation* origin) { return log(LogWarning, origin); }
0069     inline QDebug error(const DataInformation* origin) { return log(LogError, origin); }
0070     inline QDebug info(const QString& origin = QString()) { return log(LogInfo, origin); }
0071     inline QDebug warn(const QString& origin = QString()) { return log(LogWarning, origin); }
0072     inline QDebug error(const QString& origin = QString()) { return log(LogError, origin); }
0073     /**
0074      * @return a QDebug to write the message to.
0075      * Do NOT save this object, since the string it writes to may become invalid!
0076      * Just write the message using the << operators and do not touch it anymore after the line ends
0077      */
0078     QDebug log(LogLevel level, const DataInformation* origin);
0079     QDebug log(LogLevel level, const QString& origin);
0080     void clear();
0081     /**
0082      * @param minLevel the minimum level that the messages must have
0083      * @return all the messages, mainly used for testing
0084      */
0085     QStringList messages(LogLevel minLevel = LogInfo) const;
0086     /** whether to log to stdout instead of saving the messages */
0087     inline void setLogToStdOut(bool val) { mLogToStdOut = val; }
0088 
0089     static QIcon iconForLevel(LogLevel level);
0090 
0091 private:
0092     QVector<Data> mData;
0093     bool mLogToStdOut = false;
0094 };
0095 
0096 struct LoggerWithContext
0097 {
0098 public:
0099     LoggerWithContext(ScriptLogger* l, const QString& s)
0100         : logger(l)
0101         , context(s)
0102     {}
0103     LoggerWithContext(const LoggerWithContext&) = delete;
0104 
0105     ~LoggerWithContext() = default;
0106 
0107     LoggerWithContext& operator=(const LoggerWithContext&) = delete;
0108 
0109     inline QDebug info() const { return logger ? logger->info(context) : qDebug(); }
0110     inline QDebug warn() const { return logger ? logger->warn(context) : qWarning(); }
0111     inline QDebug error() const { return logger ? logger->error(context) : qWarning(); }
0112 
0113 private:
0114     ScriptLogger* const logger;
0115     QString context;
0116 };
0117 
0118 #endif // KASTEN_SCRIPTLOGGER_HPP