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 * |
|
14 * Description: This class provide log functionality. |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef LOGGER_H |
|
19 #define LOGGER_H |
|
20 |
|
21 #include <QObject> |
|
22 #include <QSettings> |
|
23 #include <loggerglobal.h> |
|
24 |
|
25 class LogOutput; |
|
26 class QSettings; |
|
27 class LoggerPrivate; |
|
28 |
|
29 /* |
|
30 |
|
31 configuration format: |
|
32 |
|
33 [myapplog] |
|
34 logDateTime = 1 |
|
35 logLoggerName = 1 |
|
36 datetimeformat = hh:mm:ss |
|
37 output = debugoutput consoleoutput fileoutput |
|
38 fileoutput/logfile = C:/data/log/myapp.log |
|
39 fileoutput/truncate = 1 |
|
40 |
|
41 code examples: |
|
42 |
|
43 QSettings settings("myapp.ini",QSettings::IniFormat); |
|
44 Logger::instance("myapplog")->configure(settings); |
|
45 Logger::instance("myapplog")->log("Hello world!"); |
|
46 |
|
47 */ |
|
48 |
|
49 class LOGGER_EXPORT Logger : public QObject |
|
50 { |
|
51 Q_OBJECT |
|
52 Q_PROPERTY(bool logDateTime READ logDateTime WRITE setLogDateTime) |
|
53 Q_PROPERTY(bool logLoggerName READ logLoggerName WRITE setLogLoggerName) |
|
54 Q_PROPERTY(QString dateTimeFormat READ dateTimeFormat WRITE setDateTimeFormat) |
|
55 public: |
|
56 static Logger *instance(const QString &name); |
|
57 static void close(const QString &name); |
|
58 static void closeAll(); |
|
59 public: |
|
60 virtual ~Logger(); |
|
61 |
|
62 void log(const QString &log); |
|
63 |
|
64 void configure(const QString &configFile,QSettings::Format format = QSettings::NativeFormat); |
|
65 |
|
66 void configure(QSettings &settings); |
|
67 |
|
68 bool addLogOutput(LogOutput *output); |
|
69 |
|
70 void removeLogOutput(LogOutput *output); |
|
71 |
|
72 LogOutput *logOutput(const QString &name); |
|
73 |
|
74 void clearAllLogOutput(); |
|
75 |
|
76 QString name() const; |
|
77 |
|
78 bool logDateTime() const; |
|
79 void setLogDateTime(bool on); |
|
80 |
|
81 bool logLoggerName() const; |
|
82 void setLogLoggerName(bool on); |
|
83 |
|
84 QString dateTimeFormat() const; |
|
85 void setDateTimeFormat(const QString &format); |
|
86 private: |
|
87 explicit Logger(const QString &name = QString(),QObject *parent = 0); |
|
88 LoggerPrivate *d_ptr; |
|
89 }; |
|
90 |
|
91 class InitLoggerHelper |
|
92 { |
|
93 public: |
|
94 InitLoggerHelper(const QString &loggerName,const QString &configPath) |
|
95 : mLoggerName(loggerName) |
|
96 { |
|
97 Logger::instance(loggerName)->configure(configPath,QSettings::IniFormat); |
|
98 } |
|
99 ~InitLoggerHelper() { |
|
100 Logger::close(mLoggerName); |
|
101 } |
|
102 private: |
|
103 QString mLoggerName; |
|
104 }; |
|
105 |
|
106 class LogFunctionEntryHelper |
|
107 { |
|
108 public: |
|
109 LogFunctionEntryHelper(const QString &loggerName,const QString &func) |
|
110 : mLoggerName(loggerName), mFunc(func) |
|
111 { |
|
112 Logger::instance(mLoggerName)->log(QLatin1String(">>>> ") + mFunc); |
|
113 } |
|
114 ~LogFunctionEntryHelper() |
|
115 { |
|
116 Logger::instance(mLoggerName)->log(QLatin1String("<<<< ") + mFunc); |
|
117 } |
|
118 private: |
|
119 QString mLoggerName; |
|
120 QString mFunc; |
|
121 }; |
|
122 |
|
123 #define INIT_LOGGER(loggerName,configPath) \ |
|
124 InitLoggerHelper __init##loggerName(loggerName,configPath); |
|
125 |
|
126 #endif //LOGGER_H |
|