1 /* 2 This file is part of the Okteta Kasten Framework, made within the KDE community. 3 4 SPDX-FileCopyrightText: 2010, 2011, 2012 Alex Richardson <alex.richardson@gmx.de> 5 6 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 7 */ 8 9 #ifndef KASTEN_SCRIPTLOGGER_HPP 10 #define KASTEN_SCRIPTLOGGER_HPP 11 12 #include <QAbstractTableModel> 13 #include <QVector> 14 #include <QDebug> 15 #include <QTime> 16 17 class QIcon; 18 19 class DataInformation; 20 /** NOT THREAD SAFE! */ 21 class ScriptLogger : public QAbstractTableModel 22 { 23 Q_DISABLE_COPY(ScriptLogger) 24 Q_OBJECT 25 26 public: 27 explicit ScriptLogger(); 28 ~ScriptLogger() override; 29 30 enum Columns 31 { 32 ColumnTime = 0, 33 ColumnOrigin, 34 ColumnMessage, 35 COLUMN_COUNT 36 }; 37 // fits into 2 bits 38 enum LogLevel 39 { 40 LogInvalid = 0, 41 LogInfo = 1, 42 LogWarning = 2, 43 LogError = 3 44 }; 45 struct Data 46 { 47 inline Data() = default; DataScriptLogger::Data48 inline Data(LogLevel lvl, const QString& o) 49 : level(lvl) 50 , origin(o) 51 , time(QTime::currentTime()) 52 {} 53 inline Data(const Data& d) = default; 54 inline ~Data() = default; 55 ScriptLogger::LogLevel level = LogInvalid; 56 QString message; 57 QString origin; 58 QTime time; 59 }; 60 QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; 61 int rowCount(const QModelIndex& parent = QModelIndex()) const override; 62 int columnCount(const QModelIndex& parent = QModelIndex()) const override; 63 QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; 64 info(const DataInformation * origin)65 inline QDebug info(const DataInformation* origin) { return log(LogInfo, origin); } warn(const DataInformation * origin)66 inline QDebug warn(const DataInformation* origin) { return log(LogWarning, origin); } error(const DataInformation * origin)67 inline QDebug error(const DataInformation* origin) { return log(LogError, origin); } info(const QString & origin=QString ())68 inline QDebug info(const QString& origin = QString()) { return log(LogInfo, origin); } warn(const QString & origin=QString ())69 inline QDebug warn(const QString& origin = QString()) { return log(LogWarning, origin); } error(const QString & origin=QString ())70 inline QDebug error(const QString& origin = QString()) { return log(LogError, origin); } 71 /** 72 * @return a QDebug to write the message to. 73 * Do NOT save this object, since the string it writes to may become invalid! 74 * Just write the message using the << operators and do not touch it anymore after the line ends 75 */ 76 QDebug log(LogLevel level, const DataInformation* origin); 77 QDebug log(LogLevel level, const QString& origin); 78 void clear(); 79 /** 80 * @param minLevel the minimum level that the messages must have 81 * @return all the messages, mainly used for testing 82 */ 83 QStringList messages(LogLevel minLevel = LogInfo) const; 84 /** whether to log to stdout instead of saving the messages */ setLogToStdOut(bool val)85 inline void setLogToStdOut(bool val) { mLogToStdOut = val; } 86 87 static QIcon iconForLevel(LogLevel level); 88 89 private: 90 QVector<Data> mData; 91 bool mLogToStdOut = false; 92 }; 93 94 struct LoggerWithContext 95 { 96 public: LoggerWithContextLoggerWithContext97 LoggerWithContext(ScriptLogger* l, const QString& s) 98 : logger(l) 99 , context(s) 100 {} infoLoggerWithContext101 inline QDebug info() const { return logger ? logger->info(context) : qDebug(); } warnLoggerWithContext102 inline QDebug warn() const { return logger ? logger->warn(context) : qWarning(); } errorLoggerWithContext103 inline QDebug error() const { return logger ? logger->error(context) : qWarning(); } 104 105 private: 106 ScriptLogger* const logger; 107 QString context; 108 }; 109 110 #endif // KASTEN_SCRIPTLOGGER_HPP 111