Repository navigation
Expand file tree
/
Copy pathLogger.cpp
More file actions
199 lines (166 loc) · 4.35 KB
/
Copy pathLogger.cpp
File metadata and controls
199 lines (166 loc) · 4.35 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "Logger.h"
#include <QDebug>
#include <QDir>
#include <QTextStream>
#include <QMessageBox>
Logger::Logger(QObject *parent) : QObject(parent)
{
this->logFile = new QFile();
}
Logger::~Logger()
{
if(logFile->isOpen()){
qDebug() << "Logger: Closing logger forcefully...";
this->stopLogging();
}
delete this->logFile;
}
void Logger::setLogFile(QString path)
{
this->logFilePath = path;
}
QString Logger::getLogFilePath()
{
return this->logFilePath;
}
bool Logger::isLogging()
{
return this->logging;
}
void Logger::log(QString text)
{
if(this->logFile->isOpen()){
if(this->preserved_logInHexEnabled){
QStringList hexVals = convertTextToHex(text);
addHexToBuffer(hexVals);
}
else{
this->logText.append(text);
}
writeToFile(this->logText);
}
}
bool Logger::promptOverwrite()
{
bool overwriteFile = false;
QString promptString = QString("You are attempting to overwrite a log file (%1).\n"
"Do you wish to continue?").arg(logFilePath);
int ret = QMessageBox::warning(nullptr,
"Overwrite Confirmation",
promptString,
QMessageBox::Ok | QMessageBox::Cancel);
switch(ret){
case QMessageBox::Ok:
overwriteFile = true;
break;
default:
overwriteFile = false;
break;
}
return overwriteFile;
}
void Logger::writeToFile(QString text)
{
if(logFile->isOpen()){
logFile->resize(0);
logFile->write(text.toUtf8());
logFile->flush();
}
}
QStringList Logger::convertTextToHex(QString text)
{
const static std::string asciiLookup = "0123456789ABCDEF";
QStringList hexList;
for(QChar qc : text){
QString hex;
char c = qc.toLatin1();
//Preserve newlines (don't convert to hex)
if(c == '\r' || c == '\n'){
hex = c;
}
else{
char upper = asciiLookup[(c & 0xF0) >> 4];
char lower = asciiLookup[c & 0x0F];
hex.append(upper);
hex.append(lower);
}
hexList.append(hex);
}
return hexList;
}
void Logger::addHexToBuffer(QStringList hex)
{
//Append each hex character to the buffer
//If the character is a newline, append it as is
//If the character is not a newline, append it as is, but add a comma before it if there is data on the same line
for(QString val : hex){
if(val == "\r" || val == "\n"){
logText.append(val);
}
else{
if(!logText.endsWith("\r") && !logText.endsWith("\n") && !logText.isEmpty()){
logText.append(',');
}
logText.append(val);
}
}
}
void Logger::preserveStates()
{
this->preserved_logInHexEnabled = logInHexEnabled;
}
void Logger::logInHex(bool enabled)
{
this->logInHexEnabled = enabled;
}
bool Logger::isHexLoggingEnabled()
{
return logInHexEnabled;
}
void Logger::promptWhenOverwriting(bool enabled)
{
this->promptWhenOverwritingEnabled = enabled;
}
void Logger::startLogging()
{
//Create the directory if it does not exist
QDir dir = QFileInfo(logFilePath).absoluteDir();
if(!dir.exists()){
qDebug() << "Logger: log file path does not exist. Creating path...";
dir.mkpath(dir.path());
}
//Attempt to open the log file
logFile->setFileName(this->logFilePath);
//Prompt for an overwrite if applicable
bool overwriteFile = false;
if(promptWhenOverwritingEnabled){
//Check if file exists
if(logFile->exists()){
overwriteFile = this->promptOverwrite();
}
else{
overwriteFile = true;
}
}
else{
overwriteFile = true;
}
if(overwriteFile){
logFile->open(QIODevice::WriteOnly | QIODevice::Truncate);
if(logFile->isOpen()){
qDebug() << "Logger: Logging started.";
this->logging = true;
this->logText.clear();
this->preserveStates();
emit loggingStarted();
}
}
}
void Logger::stopLogging()
{
if(logFile->isOpen()){
qDebug() << "Logger: Logging stopped.";
logFile->close();
this->logging = false;
}
}