1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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)
|