-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.cpp
69 lines (62 loc) · 1.66 KB
/
logger.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
#include "globalsettings.h"
#include "helpers.h"
#include "logger.h"
#include <QDebug>
#include <QFileInfo>
#include <QTextStream>
Logger *Logger::m_instance = nullptr;
Logger *Logger::getInstance(){
if (m_instance == nullptr){
m_instance = new Logger();
}
return m_instance;
}
void Logger::_init() {
this->refreshLogPrefs();
if (_userWantsLogging && _logToFile) {
// Make sure log file exists
QFile file(_logFileDest);
if (!file.exists()) {
qDebug() << "log file does not exist -> creating now";
// Create
file.open(QIODevice::ReadWrite | QIODevice::Text);
file.write("");
file.close();
}
// Double check
_logToFile = QFileInfo::exists(_logFileDest);
}
}
void Logger::refreshLogPrefs() {
this->_userWantsLogging = GlobalSettings::getInstance()->getUserDebugLogOn();
// For now, no separate setting for file vs other
this->_logToFile = this->_userWantsLogging;
}
QFile *Logger::_openLogFile() {
QFile *filePtr = nullptr;
try {
filePtr = new QFile(_logFileDest);
filePtr->open(QIODevice::Append | QIODevice::Text);
} catch (...) {
qDebug() << "Could not open debug-log file";
}
return filePtr;
}
void Logger::clear() {
if (_logToFile) {
QFile *file = this->_openLogFile();
file->resize(0);
file->close();
}
}
void Logger::logStr(QString msg) {
if (_logToFile) {
QFile *file = this->_openLogFile();
QTextStream out(file);
out << "\n" << msg;
file->close();
}
if (Helpers::getIsDebug()) {
qDebug() << msg;
}
}