-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.cpp
128 lines (115 loc) · 3.32 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
124
125
126
127
/***************************************************************************
**
** Copyright (C) 2012 Jolla Ltd.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include <QDateTime>
#include "logger.h"
Logger::Logger(const QString &logPath, const QString &logLevel) :
m_logfile(NULL),
m_outputLevel(QtDebugMsg),
m_gconfLogPath("/meegotouch/theme/logpath"),
m_gconfLogLevel("/meegotouch/theme/loglevel")
{
if (!logPath.isEmpty()) {
setLogPath(logPath);
} else if (!m_gconfLogPath.value().isNull()) {
// use GConf for default log path
setLogPath(m_gconfLogPath.value().toString());
}
if (!logLevel.isEmpty()) {
setLogLevel(logLevel);
} else if (!m_gconfLogLevel.value().isNull()) {
// use GConf for default log path
setLogLevel(m_gconfLogLevel.value().toString());
}
// reopen log file if re-configured
connect(&m_gconfLogPath,
SIGNAL(valueChanged()),
this,
SLOT(logPathChanged()));
connect(&m_gconfLogLevel,
SIGNAL(valueChanged()),
this,
SLOT(logLevelChanged()));
}
Logger::~Logger() {}
void Logger::log(const QtMsgType type, const QString &msg)
{
if (type < m_outputLevel)
return;
if (!m_logstream.device())
return;
QString severity;
switch (type) {
case QtDebugMsg:
severity = "debug ";
break;
case QtWarningMsg:
severity = "warning ";
break;
case QtCriticalMsg:
severity = "critical ";
break;
case QtFatalMsg:
severity = "fatal ";
}
m_logstream << QDateTime::currentDateTime().toString("[dd.MM.yyyy hh:mm:ss.zzz] ") << severity << msg << "\n";
m_logstream.flush();
}
void Logger::setLogPath(const QString &logPath)
{
if (m_logfile) {
m_logfile->close();
delete m_logfile;
m_logfile = NULL;
}
if (!logPath.isEmpty()) {
m_logfile = new QFile(logPath, this);
if (!m_logfile->open(QIODevice::Append | QIODevice::Text)) {
fprintf(stderr, "%s\n", "Can't open log file");
delete m_logfile;
m_logfile = NULL;
return;
}
m_logstream.setDevice(m_logfile);
}
}
void Logger::setLogLevel(const QString &logLevel)
{
if (logLevel == "debug") {
m_outputLevel = QtDebugMsg;
} else if (logLevel == "warning") {
m_outputLevel = QtWarningMsg;
} else if (logLevel == "critical") {
m_outputLevel = QtCriticalMsg;
} else {
#ifdef __arm__
m_outputLevel = QtCriticalMsg;
#else
m_outputLevel = QtWarningMsg;
#endif
}
}
void Logger::logPathChanged()
{
if (m_gconfLogPath.value().isNull()) {
setLogPath(QString());
} else {
setLogPath(m_gconfLogPath.value().toString());
}
}
void Logger::logLevelChanged()
{
if (m_gconfLogLevel.value().isNull()) {
setLogLevel(QString());
} else {
setLogLevel(m_gconfLogLevel.value().toString());
}
}