32
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0""
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
52
|
14 |
* Description: This class provide log functionality.
|
32
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include <logger.h>
|
|
18 |
#include "logoutput.h"
|
|
19 |
#include "logoutputimpl.h"
|
|
20 |
#include <QtAlgorithms>
|
|
21 |
#include <QHash>
|
|
22 |
#include <QString>
|
|
23 |
#include <QStringList>
|
|
24 |
#include <QDateTime>
|
|
25 |
#include <QSettings>
|
|
26 |
#include <QFileInfo>
|
|
27 |
#include "loglogger.h"
|
|
28 |
#include "logoutputfactory.h"
|
|
29 |
|
|
30 |
typedef QHash<QString,Logger*> LoggerContainer;
|
|
31 |
|
|
32 |
Q_GLOBAL_STATIC(LoggerContainer,theLoggerList)
|
|
33 |
|
|
34 |
class InitLogLogger
|
|
35 |
{
|
|
36 |
public:
|
|
37 |
InitLogLogger() {
|
|
38 |
LogOutput *output = new DebugLogOutput();
|
|
39 |
output->init();
|
|
40 |
Logger::instance(LOGLOGGER_NAME)->addLogOutput(output);
|
|
41 |
}
|
|
42 |
} dummy_instance;
|
|
43 |
|
|
44 |
class LoggerPrivate
|
|
45 |
{
|
|
46 |
public:
|
|
47 |
LoggerPrivate(const QString &name = QString())
|
|
48 |
: mName(name),
|
|
49 |
mDateTimeFormat("hh:mm:ss:zzz"),
|
|
50 |
mLogDateTime(true),
|
|
51 |
mLogLoggerName(true)
|
|
52 |
{
|
|
53 |
}
|
|
54 |
|
|
55 |
~LoggerPrivate()
|
|
56 |
{
|
|
57 |
clearAllLogOutput();
|
|
58 |
}
|
|
59 |
|
|
60 |
void clearAllLogOutput()
|
|
61 |
{
|
|
62 |
qDeleteAll(mLogOutputs.begin(),mLogOutputs.end());
|
|
63 |
mLogOutputs.clear();
|
|
64 |
}
|
|
65 |
|
|
66 |
QString mName;
|
|
67 |
QString mDateTimeFormat;
|
|
68 |
QList<LogOutput*> mLogOutputs;
|
|
69 |
bool mLogDateTime;
|
|
70 |
bool mLogLoggerName;
|
|
71 |
};
|
|
72 |
|
|
73 |
Logger *Logger::instance(const QString &name)
|
|
74 |
{
|
|
75 |
if (name.isEmpty()) {
|
|
76 |
LOGGER_LOG("Logger::instance() >> name is empty.");
|
|
77 |
return 0;
|
|
78 |
}
|
|
79 |
|
|
80 |
QString nameLower(name);
|
|
81 |
nameLower.toLower();
|
|
82 |
|
|
83 |
Logger *logger = theLoggerList()->value(nameLower);
|
|
84 |
if (!logger) {
|
|
85 |
logger = new Logger(nameLower);
|
|
86 |
theLoggerList()->insert(nameLower,logger);
|
|
87 |
}
|
|
88 |
|
|
89 |
return logger;
|
|
90 |
}
|
|
91 |
|
|
92 |
void Logger::closeAll()
|
|
93 |
{
|
|
94 |
qDeleteAll(theLoggerList()->begin(),theLoggerList()->end());
|
|
95 |
theLoggerList()->clear();
|
|
96 |
}
|
|
97 |
|
|
98 |
void Logger::close(const QString &name)
|
|
99 |
{
|
|
100 |
QString nameLower(name);
|
|
101 |
nameLower.toLower();
|
|
102 |
delete theLoggerList()->take(nameLower);
|
|
103 |
}
|
|
104 |
|
|
105 |
Logger::Logger(const QString &name /*= QString()*/,QObject *parent /*= 0*/) : QObject(parent),
|
|
106 |
d_ptr(new LoggerPrivate(name))
|
|
107 |
{
|
|
108 |
|
|
109 |
}
|
|
110 |
|
|
111 |
Logger::~Logger()
|
|
112 |
{
|
|
113 |
delete d_ptr;
|
|
114 |
}
|
|
115 |
|
|
116 |
void Logger::log(const QString &log)
|
|
117 |
{
|
|
118 |
if (d_ptr->mLogOutputs.isEmpty()) {
|
|
119 |
return;
|
|
120 |
}
|
|
121 |
|
|
122 |
QString logStr;
|
|
123 |
|
|
124 |
if (d_ptr->mLogDateTime) {
|
34
|
125 |
logStr += QDateTime::currentDateTime().toString(d_ptr->mDateTimeFormat) + ' ';
|
32
|
126 |
}
|
|
127 |
|
|
128 |
if (d_ptr->mLogLoggerName) {
|
|
129 |
logStr += '[';
|
|
130 |
logStr += name();
|
|
131 |
logStr += "]:";
|
|
132 |
}
|
|
133 |
|
|
134 |
logStr += log;
|
|
135 |
logStr += LINE_SEPERATOR;
|
|
136 |
|
|
137 |
foreach(LogOutput *logOutput, d_ptr->mLogOutputs) {
|
|
138 |
logOutput->output(logStr);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
void Logger::configure(const QString &configFile,QSettings::Format format /*= QSettings::NativeFormat*/)
|
|
143 |
{
|
|
144 |
if (QFileInfo(configFile).exists()) {
|
|
145 |
QSettings settings(configFile,format);
|
|
146 |
configure(settings);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
void Logger::configure(QSettings &settings)
|
|
151 |
{
|
|
152 |
clearAllLogOutput();
|
|
153 |
|
|
154 |
settings.beginGroup(name());
|
|
155 |
QString strOutput = settings.value(LOGGER_OUTPUT).toString();
|
|
156 |
d_ptr->mLogDateTime = settings.value(LOGGER_LOGDATETIME,true).toBool();
|
|
157 |
d_ptr->mLogLoggerName = settings.value(LOGGER_LOGLOGGERNAME,true).toBool();
|
|
158 |
QString strDateTimeFormat = settings.value(LOGGER_DATETIMEFORMAT).toString();
|
|
159 |
if (!strDateTimeFormat.isEmpty()) {
|
|
160 |
d_ptr->mDateTimeFormat = strDateTimeFormat;
|
|
161 |
}
|
|
162 |
settings.endGroup();
|
|
163 |
|
|
164 |
QStringList outputNameList = strOutput.split(' ',QString::SkipEmptyParts);
|
|
165 |
foreach(const QString &outputName,outputNameList) {
|
|
166 |
LogOutput *output = LogOutputFactory::createLogOutput(outputName);
|
|
167 |
if (output) {
|
|
168 |
output->setParentLogger(this);
|
|
169 |
if (output->load(settings)) {
|
|
170 |
addLogOutput(output);
|
|
171 |
}
|
|
172 |
else {
|
|
173 |
delete output;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
bool Logger::addLogOutput(LogOutput *output)
|
|
180 |
{
|
|
181 |
if (!output) {
|
|
182 |
LOGGER_LOG("Logger::addLogOutput >> output is NULL.");
|
|
183 |
return false;
|
|
184 |
}
|
|
185 |
|
|
186 |
if ( d_ptr->mLogOutputs.indexOf(output) >= 0) {
|
|
187 |
LOGGER_LOG("Logger::addLogOutput >> output is already in list.");
|
|
188 |
return false;
|
|
189 |
}
|
|
190 |
|
|
191 |
if (logOutput(output->name())) {
|
|
192 |
LOGGER_LOG( QString("Logger::addLogOutput >> output with the name <") + output->name() + "> is already in list.");
|
|
193 |
}
|
|
194 |
|
|
195 |
output->setParentLogger(this);
|
|
196 |
d_ptr->mLogOutputs.append(output);
|
|
197 |
|
|
198 |
return true;
|
|
199 |
}
|
|
200 |
|
|
201 |
void Logger::removeLogOutput(LogOutput *output)
|
|
202 |
{
|
|
203 |
d_ptr->mLogOutputs.removeAll(output);
|
|
204 |
}
|
|
205 |
|
|
206 |
LogOutput *Logger::logOutput(const QString &name)
|
|
207 |
{
|
|
208 |
foreach(LogOutput *output,d_ptr->mLogOutputs) {
|
|
209 |
if (output->name().compare(name,Qt::CaseInsensitive) == 0) {
|
|
210 |
return output;
|
|
211 |
}
|
|
212 |
}
|
|
213 |
|
|
214 |
return 0;
|
|
215 |
}
|
|
216 |
|
|
217 |
void Logger::clearAllLogOutput()
|
|
218 |
{
|
|
219 |
d_ptr->clearAllLogOutput();
|
|
220 |
}
|
|
221 |
|
|
222 |
QString Logger::name() const
|
|
223 |
{
|
|
224 |
return d_ptr->mName;
|
|
225 |
}
|
|
226 |
|
|
227 |
bool Logger::logDateTime() const
|
|
228 |
{
|
|
229 |
return d_ptr->mLogDateTime;
|
|
230 |
}
|
|
231 |
|
|
232 |
void Logger::setLogDateTime(bool on)
|
|
233 |
{
|
|
234 |
d_ptr->mLogDateTime = on;
|
|
235 |
}
|
|
236 |
|
|
237 |
bool Logger::logLoggerName() const
|
|
238 |
{
|
|
239 |
return d_ptr->mLogLoggerName;
|
|
240 |
}
|
|
241 |
|
|
242 |
void Logger::setLogLoggerName(bool on)
|
|
243 |
{
|
|
244 |
d_ptr->mLogLoggerName = on;
|
|
245 |
}
|
|
246 |
|
|
247 |
QString Logger::dateTimeFormat() const
|
|
248 |
{
|
|
249 |
return d_ptr->mDateTimeFormat;
|
|
250 |
}
|
|
251 |
|
|
252 |
void Logger::setDateTimeFormat(const QString &format)
|
|
253 |
{
|
|
254 |
d_ptr->mDateTimeFormat = format;
|
|
255 |
}
|