-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnrlogger.cpp
More file actions
153 lines (120 loc) · 2.95 KB
/
Copy pathdnrlogger.cpp
File metadata and controls
153 lines (120 loc) · 2.95 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
#include "dnrlogger.h"
#include <QDebug>
#include <QCoreApplication>
#include <QTime>
#include <QFileInfo>
#include <syslog.h>
DNRLogger* DNRLogger::_singleton = NULL;
DNRLogger::~DNRLogger()
{
free();
}
DNRLogger::DNRLogger() : QObject(NULL)
{
_outputDebug = false;
_logName = QFileInfo( QCoreApplication::applicationFilePath() ).fileName().toLatin1().data();
openlog(_logName.toStdString().c_str(), LOG_PID, LOG_SYSLOG);
}
DNRLogger *DNRLogger::instance()
{
if( NULL == _singleton )
{
_singleton = new DNRLogger();
}
return _singleton;
}
void DNRLogger::free()
{
if( NULL != _singleton )
{
closelog();
delete _singleton;
_singleton = NULL;
}
}
std::string DNRLogger::levelToStr(int level)
{
switch(level)
{
case LOG_INFO:
return "Info";
case LOG_ERR:
return "Error";
case LOG_WARNING:
return "Warning";
case LOG_NOTICE:
return "Notice";
case LOG_CRIT:
return "Critical";
default:
return "DK";
}
}
void DNRLogger::logInfoQ(const QString& message)
{
logMessage(LOG_INFO, message.toStdString());
}
void DNRLogger::logErrorQ(const QString& message)
{
logMessage(LOG_ERR, message.toStdString());
}
void DNRLogger::logDebugQ(const QString& message)
{
outputDebug(message.toStdString());
}
void DNRLogger::logWarningQ(const QString& message)
{
logMessage(LOG_WARNING, message.toStdString());
}
void DNRLogger::logNoticeQ(const QString& message)
{
logMessage(LOG_NOTICE, message.toStdString());
}
void DNRLogger::logCriticalQ(const QString& message)
{
logMessage(LOG_CRIT, message.toStdString());
}
void DNRLogger::logInfo(const std::string& message)
{
logMessage(LOG_INFO, message);
}
void DNRLogger::logError(const std::string& message)
{
logMessage(LOG_ERR, message);
}
void DNRLogger::logDebug(const std::string& message)
{
outputDebug(message);
}
void DNRLogger::logWarning(const std::string& message)
{
logMessage(LOG_WARNING, message);
}
void DNRLogger::logNotice(const std::string& message)
{
logMessage(LOG_NOTICE, message);
}
void DNRLogger::logCritical(const std::string& message)
{
logMessage(LOG_CRIT, message);
}
void DNRLogger::outputDebug(const std::string& message)
{
QTime rightNow = QTime::currentTime();
qDebug() << rightNow.toString() << "**" << message.c_str();
}
void DNRLogger::logMessage(int level, const std::string& message)
{
if (message.length())
{
QTime rightNow = QTime::currentTime();
if(_outputDebug)
{
qDebug() << rightNow.toString() << "|- " << levelToStr(level).c_str() << " - " << message.c_str();
}
// openlog(_logName.toStdString().c_str(), (LOG_CONS|LOG_PERROR|LOG_PID), LOG_SYSLOG);
// openlog(_logName.toStdString().c_str(), LOG_PID, LOG_SYSLOG);
syslog(level, "%s", message.c_str());
// closelog();
}
}