-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogUtil.cpp
More file actions
85 lines (73 loc) · 1.39 KB
/
LogUtil.cpp
File metadata and controls
85 lines (73 loc) · 1.39 KB
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
#include <string>
#include "LogUtil.h"
#include <ctime>
#include <chrono>
#include <sstream>
#include <qwidget.h>
#include <Log.h>
#include <qstring.h>
#include <Game.h>
void initLog()
{
l = new Log();
}
void logI(std::string message)
{
//Short form for info log
log(Info, message);
}
void logW(std::string message)
{
//Short form for warning log
log(Warning, message);
}
void logE(std::string message)
{
//Short form for error log
log(Error, message);
}
void logT(LogType type, std::string message)
{
//Log message invoked on the gui thread for cross-thread logging
QObject* a = app;
QMetaObject::invokeMethod(app, [a, type, message]() {
log(type, message);
}, Qt::QueuedConnection);
}
void log(LogType type, std::string message)
{
std::string stype;
//Get string from type
switch (type)
{
case Info:
stype = "INFO";
break;
case Warning:
stype = "WARNING";
break;
case Error:
stype = "ERROR";
break;
}
//Get current time
std::time_t t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
//Construct and log message
message = std::format("[{}] [{}] {}", t, stype, message);
messages.push_back(message);
updateLog();
}
void showLog()
{
l->show();
updateLog();
}
void updateLog()
{
//Clear the log textbox and append all messages
l->ui.teLog->clear();
for (std::string str : messages)
{
l->ui.teLog->append(QString::fromStdString(str));
}
}