forked from olive-editor/olive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.cpp
112 lines (96 loc) · 3.47 KB
/
debug.cpp
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
/***
Olive - Non-Linear Video Editor
Copyright (C) 2019 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "debug.h"
#include <QFile>
#include <QDateTime>
#include <QStandardPaths>
#include <QDir>
#include <QMutex>
#include "dialogs/debugdialog.h"
QString debug_info;
QMutex debug_mutex;
QFile debug_file;
QTextStream debug_stream;
void open_debug_file() {
QDir debug_dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
debug_dir.mkpath(".");
if (debug_dir.exists()) {
debug_file.setFileName(debug_dir.path() + "/debug_log");
if (debug_file.open(QFile::WriteOnly)) {
debug_stream.setDevice(&debug_file);
} else {
qWarning() << "Couldn't open debug log file, debug log will not be saved";
}
}
}
void close_debug_file()
{
if (debug_file.isOpen()) {
debug_file.close();
}
}
void debug_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
debug_mutex.lock();
const QByteArray localMsg = msg.toLocal8Bit();
const QDateTime now = QDateTime::currentDateTime();
const QByteArray timeRepr(now.toString(Qt::ISODate).toLocal8Bit());
QString msgTag;
QString fontColor;
switch (type) {
case QtDebugMsg:
msgTag = "DEBUG";
fontColor = "grey";
break;
case QtInfoMsg:
msgTag = "INFO";
fontColor = "blue";
break;
case QtWarningMsg:
msgTag = "WARNING";
fontColor = "yellow";
break;
case QtCriticalMsg:
msgTag = "ERROR";
fontColor = "red";
break;
case QtFatalMsg:
msgTag = "FATAL";
fontColor = "red";
break;
default:
fprintf(stderr, "Unknown debug msg type");
fflush(stderr);
break;
}//switch
/*fprintf(stderr, "%s [%s] %s (%s:%u, %s)\n", timeRepr.data(), msgTag.toLocal8Bit().constData(), localMsg.data(),
context.file, context.line, context.function);*/
fprintf(stderr, "%s [%s] %s\n", timeRepr.data(), msgTag.toLocal8Bit().constData(), localMsg.data());
if (debug_file.isOpen()) {
debug_stream << QString("[%1] %2 (%3:%4, %5)\n")
.arg(msgTag, localMsg, context.file, QString::number(context.line), context.function);
}
debug_info.prepend(QString("<font color='%1'><b>[%2]</b> %3 (%4:%5, %6)</font><br>")
.arg(fontColor, msgTag, localMsg, context.file, QString::number(context.line), context.function));
fflush(stderr);
if (olive::DebugDialog != nullptr && olive::DebugDialog->isVisible()) {
QMetaObject::invokeMethod(olive::DebugDialog, "update_log", Qt::QueuedConnection);
}
debug_mutex.unlock();
}
const QString &get_debug_str()
{
return debug_info;
}