-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
94 lines (77 loc) · 2.21 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
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
#include "Logger.h"
#include <QDate>
#include <QDebug>
#include <QTextStream>
#include <iostream>
#include <QFile>
#include <QString>
#include <QCoreApplication>
#include <QDir>
Logger* Logger::m_instance = NULL;
void Logger::logOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QString debugInfo;
if(type == QtDebugMsg)
debugInfo = QString("[%1]:\t").arg(QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss"));
else
debugInfo = QString("[%2:%3][%1]:\t").arg(QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss")).arg(context.function).arg(context.line);
if(m_out)
(*m_out) << debugInfo << msg << endl;
else
std::cerr << debugInfo.toStdString() << msg.toStdString() << std::endl;
#ifdef CONSOLE_OUT
std::cout << msg.toStdString() << std::endl;
#endif
if(type == QtFatalMsg)
{
abort();
}
}
Logger::Logger()
{
m_out = nullptr;
m_log = nullptr;
setFilePath(filePath(".log"));
}
Logger::~Logger()
{
delete m_out;
delete m_log;
}
QString Logger::filePath(const char *suffix)
{
QString path = QCoreApplication::applicationFilePath();
#ifdef WIN32
path = path.replace(".exe", suffix);
#else
path = path + suffix;
#endif
return QDir::toNativeSeparators(path);
}
Logger *Logger::instance()
{
if(m_instance == NULL)
m_instance = new Logger;
return m_instance;
}
void Logger::logHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Logger::instance()->logOutput(type, context, msg);
}
bool Logger::setFilePath(const QString &path)
{
delete m_out;
delete m_log;
m_log = new QFile(path);
if(!m_log->open(QIODevice::WriteOnly | /*QIODevice::Append |*/ QIODevice::Text))
{
qDebug() << "Error opening log file '" << path << "'. All debug output redirected to console.";
delete m_log;
m_log = nullptr;
m_out = nullptr;
return false;
}
m_out = new QTextStream(m_log);
// (*m_out) << "=========================================================================================\n";
return true;
}