summaryrefslogtreecommitdiff
path: root/logworker.h
diff options
context:
space:
mode:
Diffstat (limited to 'logworker.h')
-rw-r--r--logworker.h137
1 files changed, 137 insertions, 0 deletions
diff --git a/logworker.h b/logworker.h
new file mode 100644
index 0000000..dc7c3fb
--- /dev/null
+++ b/logworker.h
@@ -0,0 +1,137 @@
+#pragma once
+
+// qt
+#include <QAbstractListModel>
+#include <QApplication>
+#include <QDebug>
+#include <QDir>
+#include <QFile>
+#include <QMutex>
+#include <QStandardPaths>
+#include <QTextStream>
+
+// goodies
+#include "g_object.h"
+#include "g_property.h"
+
+#define FILENAME(fn) (strrchr(fn, '/') ? strrchr(fn, '/') + 1 : fn)
+#define WRAP_ENUM(ext_enum, field) field = ext_enum::field
+
+namespace goodies
+{
+
+static QtMessageHandler defaultMessageHandler;
+
+QString appLogFileName();
+void myMessageHandler(
+ QtMsgType type,
+ const QMessageLogContext& context,
+ const QString& msg
+);
+
+class LogMsg : public G_Object
+{
+ Q_OBJECT
+ G_OBJECT(G_Object, LogMsg)
+
+public:
+ enum MessageType
+ {
+ QtDebugMsg = QtMsgType::QtDebugMsg,
+ QtWarningMsg = QtMsgType::QtWarningMsg,
+ QtCriticalMsg = QtMsgType::QtCriticalMsg,
+ QtFatalMsg = QtMsgType::QtFatalMsg,
+ QtInfoMsg = QtMsgType::QtInfoMsg
+ };
+ Q_ENUM(MessageType)
+
+ G_PROPERTY(QDateTime, dateTime);
+ G_PROPERTY(MessageType, messageType);
+ G_PROPERTY(QString, message);
+
+public:
+ LogMsg(
+ QDateTime dateTime,
+ MessageType messageType,
+ QString message,
+ QObject* parent = nullptr
+ );
+};
+
+class LogWorker : public QAbstractListModel
+{
+ Q_OBJECT
+
+ INI_PROPERTY(bool, isLoggingEnabled, true);
+ INI_PROPERTY(int, maxLinesCount, 10 * 1000);
+
+public:
+ enum LogWorkerRoles
+ {
+ Date = Qt::UserRole + 1,
+ MsgType,
+ Msg
+ };
+ Q_ENUM(LogWorkerRoles)
+
+public:
+ Q_INVOKABLE LogWorker(const QString filepath = appLogFileName());
+ Q_INVOKABLE ~LogWorker();
+
+public:
+ Q_INVOKABLE QString filepath() const;
+ static void initialize(QApplication& app);
+
+private:
+ friend void myMessageHandler(
+ QtMsgType type,
+ const QMessageLogContext& context,
+ const QString& msg
+ );
+
+public:
+ Q_INVOKABLE void writeMessage(
+ QtMsgType type,
+ const QMessageLogContext& context,
+ const QString& msg
+ );
+
+private:
+ QString m_filepath;
+ QFile m_file;
+ QTextStream m_fileStream;
+ QMutex m_mtx;
+ QVector<LogMsg> m_messages;
+
+ // QAbstractListModel stuff
+public:
+ Q_INVOKABLE int rowCount(
+ const QModelIndex& parent = QModelIndex()
+ ) const Q_DECL_OVERRIDE;
+ Q_INVOKABLE QVariant data(
+ const QModelIndex& index,
+ int role = Qt::DisplayRole
+ ) const Q_DECL_OVERRIDE;
+
+protected:
+ inline QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE
+ {
+ return m_roleNames;
+ }
+
+protected:
+ QHash<int, QByteArray> m_roleNames = {
+ {int(LogWorkerRoles::Date), "date"},
+ {int(LogWorkerRoles::MsgType), "msg_type"},
+ {int(LogWorkerRoles::Msg), "msg"}
+ };
+
+ // Q_REGISTER_METATYPE stuff
+public:
+ LogWorker(const LogWorker& other);
+};
+
+} // namespace goodies
+
+Q_DECLARE_METATYPE(goodies::LogWorker)
+Q_DECLARE_METATYPE(goodies::LogMsg)