forked from Larswad/uno2iec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cpp
123 lines (94 loc) · 2.94 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
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
#include "logger.hpp"
#include <iso646.h>
#include <QDate>
#include <QSettings>
namespace Logging {
Logger::Logger(QObject *parent) : QObject(parent)
{
m_levels.fill(true, NUM_SEVERITY_LEVELS);
} // ctor
void Logger::log(const QString& facility, const QString& message, LogLevelE level)
{
LogFilterMap::const_iterator it(m_filters.find(facility));
if(it == m_filters.end())
m_filters[facility] = true; // add to existing facilities, enabled by default.
else
if(!it.value())
return; // filtered out!
// manage unlikely out-of-range value.
level = level >= NUM_SEVERITY_LEVELS ? info : level;
if(!m_levels[level]) // check if severity level is filtered out.
return;
QString dateTime(QDate::currentDate().toString("yyyy-MM-dd") +
QTime::currentTime().toString(" hh:mm:ss:zzz"));
// The logging levels are: [E]RROR [W]ARNING [I]NFORMATION [S]UCCESS.
QString levelFacility(QString("EWIS")[level] + " " + facility);
foreach(ILogTransport* transport, m_transports) {
transport->appendTime(dateTime);
transport->appendLevelAndFacility(level, levelFacility);
transport->appendMessage(message);
}
} // Log
bool Logger::addTransport(ILogTransport* pTransport)
{
if(m_transports.contains(pTransport))
return false;
m_transports.append(pTransport);
return true;
} // addTransport
bool Logger::removeTransport(ILogTransport* pTransport)
{
int index = m_transports.indexOf(pTransport);
if(-1 == index)
return false;
m_transports.removeAt(index);
return true;
} // removeTransport
void Logger::configureFilters(QWidget* parent = 0)
{
LogFilterSetup dlgSetup(m_filters, m_levels, parent);
dlgSetup.exec();
} // configureFilters
void Logger::saveFilters(QSettings& sets)
{
sets.beginGroup("logFilters");
LogFilterMap::const_iterator i = m_filters.constBegin();
while(i not_eq m_filters.constEnd()) {
sets.setValue(i.key(), i.value());
++i;
}
sets.endGroup();
// write severity levels.
sets.beginWriteArray("logLevels");
for(int i = 0; i < m_levels.size(); ++i) {
sets.setArrayIndex(i);
sets.setValue("isEnabled", m_levels[i]);
}
sets.endArray();
} // saveFilters
void Logger::loadFilters(QSettings& sets)
{
sets.beginGroup("logFilters");
QStringList keys = sets.childKeys();
foreach(QString key, keys)
m_filters[key] = sets.value(key).toBool();
sets.endGroup();
// read severity levels.
int numEntries = sets.beginReadArray("logLevels");
m_levels.resize(qMax(numEntries, m_levels.size()));
for(int i = 0; i < numEntries; ++i) {
sets.setArrayIndex(i);
m_levels[i] = sets.value("isEnabled", true).toBool();
}
sets.endArray();
} // loadFilters
Logger& loggerInstance()
{
static Logger theInstance;
return theInstance;
} // loggerInstance
void Log(const QString &facility, LogLevelE level, const QString &message)
{
loggerInstance().log(facility, message, level);
} // Log
} // namespace Logging