author | hgs |
Thu, 30 Sep 2010 11:43:07 +0300 | |
changeset 74 | 6c59112cfd31 |
parent 68 | 83cc6bae1de8 |
permissions | -rw-r--r-- |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
1 |
/* |
40 | 2 |
* Copyright (c) 2010 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: |
|
15 |
* |
|
16 |
*/ |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
17 |
|
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
18 |
#ifndef EMAILTRACE_H |
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
19 |
#define EMAILTRACE_H |
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
20 |
|
47 | 21 |
#include <QCoreApplication> |
68 | 22 |
#include <QDateTime> |
47 | 23 |
#include <QDebug> |
24 |
#include <QFile> |
|
68 | 25 |
#include <QTextStream> |
26 |
#include <QAbstractFileEngine> |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
27 |
|
44 | 28 |
/* |
68 | 29 |
* The macros NM_COMMENT_TRACES, NM_ERROR_TRACES, NM_FUNCTION_TRACES, and |
30 |
* NM_TIMESTAMP_TRACES control which debug messages are printed. The trace |
|
31 |
* logging is controlled with the NM_LOG_TO_FILE macro, whereas the |
|
32 |
* NM_LOG_DIRECTORY macro defines which directory is to be used to store the |
|
33 |
* log files. The print_trace() helper function implements printing. Log |
|
34 |
* files are named according to process thread IDs. If NM_LOG_TO_FILE is zero |
|
35 |
* or a log file cannot be opened, the messages are printed to qDebug(). The |
|
36 |
* DSC2STR() function can be used to convert Symbian descriptors to QString |
|
37 |
* objects. |
|
38 |
* |
|
39 |
* NOTICE that you can control the tracing system for a component tree by |
|
40 |
* using qmake. For example, if you only want to enable timestamps for a |
|
41 |
* component, you can invoke |
|
42 |
* |
|
43 |
* qmake "DEFINES += DEBUG NM_TIMESTAMP_TRACES=1" -r |
|
44 |
* |
|
45 |
* before compiling the component. You can add as many macro definitions |
|
46 |
* between the quotation marks as you wish. For example, if you want to log |
|
47 |
* the trace messages to "c:/logs", add the definitions |
|
48 |
* |
|
49 |
* NM_LOG_TO_FILE=1 NM_LOG_DIRECTORY=\\\"c:/logs/\\\" |
|
50 |
* |
|
51 |
* between the quotation marks. Do not forget to add the terminating slash |
|
52 |
* character and notice how the quotation marks around the directory path |
|
53 |
* have been escaped. This does not work if the log directory does not exist. |
|
44 | 54 |
*/ |
68 | 55 |
|
56 |
// Timestamp traces always ON |
|
57 |
#ifndef NM_TIMESTAMP_TRACES |
|
58 |
#define NM_TIMESTAMP_TRACES 1 |
|
59 |
#endif |
|
60 |
||
61 |
#ifndef NM_LOG_TO_FILE |
|
62 |
#define NM_LOG_TO_FILE 1 |
|
63 |
#endif |
|
64 |
#ifndef NM_LOG_DIRECTORY |
|
65 |
#define NM_LOG_DIRECTORY "c:/data/logs/" |
|
66 |
#endif |
|
67 |
||
68 |
#if defined(DEBUG) || defined(_DEBUG) |
|
40 | 69 |
|
56 | 70 |
#ifndef NM_COMMENT_TRACES |
68 | 71 |
#define NM_COMMENT_TRACES 0 |
56 | 72 |
#endif |
73 |
#ifndef NM_ERROR_TRACES |
|
68 | 74 |
#define NM_ERROR_TRACES 0 |
56 | 75 |
#endif |
76 |
#ifndef NM_FUNCTION_TRACES |
|
68 | 77 |
#define NM_FUNCTION_TRACES 0 |
56 | 78 |
#endif |
40 | 79 |
|
44 | 80 |
#if NM_COMMENT_TRACES || NM_ERROR_TRACES || NM_FUNCTION_TRACES |
81 |
||
40 | 82 |
inline void print_trace(const QString& msg) |
83 |
{ |
|
47 | 84 |
static QFile file(NM_LOG_DIRECTORY+ |
68 | 85 |
QString("nmail_p%1.log"). |
86 |
arg(QCoreApplication::applicationPid())); |
|
44 | 87 |
if (NM_LOG_TO_FILE && !file.isOpen()) { |
88 |
file.open(QIODevice::Append | QIODevice::Text); |
|
89 |
} |
|
90 |
if (file.isWritable()) { |
|
68 | 91 |
QTextStream(&file) << msg << '\n'; |
40 | 92 |
} else { |
68 | 93 |
qDebug("[Nmail] %s",msg.toLatin1().data()); |
40 | 94 |
} |
95 |
} |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
96 |
|
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
97 |
#endif |
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
98 |
|
47 | 99 |
inline QString DSC2STR(const TDesC& dsc) |
100 |
{ |
|
101 |
return QString::fromRawData(reinterpret_cast<const QChar*>(dsc.Ptr()), |
|
102 |
dsc.Length()); |
|
103 |
} |
|
104 |
||
105 |
#else |
|
106 |
||
107 |
#define DSC2STR(dsc) |
|
108 |
||
40 | 109 |
#endif /* DEBUG */ |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
110 |
|
40 | 111 |
/* |
44 | 112 |
* The function NM_COMMENT() prints a debug message. The INFO macros and the |
43 | 113 |
* NMLOG macro are provided for legacy compatibility. They are deprecated and |
114 |
* should not be used. If sprintf() type of formatting is desired, consider |
|
115 |
* using QString::arg() or QTextStream. |
|
40 | 116 |
*/ |
44 | 117 |
#if NM_COMMENT_TRACES |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
118 |
|
40 | 119 |
inline void NM_COMMENT(const QString& msg) |
120 |
{ |
|
121 |
print_trace("COMMENT : " + msg); |
|
122 |
} |
|
123 |
#define INFO(msg) NM_COMMENT(msg) |
|
124 |
#define INFO_1(msg,arg1)\ |
|
125 |
do {\ |
|
126 |
QString __msg;\ |
|
127 |
__msg.sprintf(msg,arg1);\ |
|
128 |
NM_COMMENT(__msg);\ |
|
129 |
} while (0) |
|
130 |
#define INFO_2(msg,arg1,arg2)\ |
|
131 |
do {\ |
|
132 |
QString __msg;\ |
|
133 |
__msg.sprintf(msg,arg1,arg2);\ |
|
134 |
NM_COMMENT(__msg);\ |
|
135 |
} while (0) |
|
136 |
#define INFO_3(msg,arg1,arg2,arg3)\ |
|
137 |
do {\ |
|
138 |
QString __msg;\ |
|
139 |
__msg.sprintf(msg,arg1,arg2,arg3);\ |
|
140 |
NM_COMMENT(__msg);\ |
|
141 |
} while (0) |
|
43 | 142 |
#define NMLOG(msg) NM_COMMENT(msg) |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
143 |
|
40 | 144 |
#else |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
145 |
|
40 | 146 |
#define NM_COMMENT(msg) |
147 |
#define INFO(msg) |
|
148 |
#define INFO_1(msg,arg1) |
|
149 |
#define INFO_2(msg,arg1,arg2) |
|
150 |
#define INFO_3(msg,arg1,arg2,arg3) |
|
43 | 151 |
#define NMLOG(msg) |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
152 |
|
44 | 153 |
#endif /* NM_COMMENT_TRACES */ |
18
578830873419
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
|
40 | 155 |
/* |
156 |
* The function NM_ERROR() prints its second argument if the first argument |
|
157 |
* is non-zero. The ERROR macros are provided for legacy compatibility. They |
|
158 |
* are deprecated and should not be used. If sprintf() type of formatting is |
|
159 |
* desired, consider using QString::arg() or QTextStream. |
|
160 |
*/ |
|
44 | 161 |
#if NM_ERROR_TRACES |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
162 |
|
40 | 163 |
inline void NM_ERROR(int err, const QString& msg) |
164 |
{ |
|
165 |
if (err) { |
|
166 |
print_trace("ERROR : " + msg); |
|
167 |
} |
|
168 |
} |
|
169 |
#define ERROR(err,msg) NM_ERROR(err,msg) |
|
170 |
#define ERROR_1(err,msg,arg1)\ |
|
171 |
do {\ |
|
172 |
QString __msg;\ |
|
173 |
__msg.sprintf(msg,arg1);\ |
|
174 |
NM_ERROR(err,__msg);\ |
|
175 |
} while (0) |
|
176 |
#define ERROR_2(err,msg,arg1,arg2)\ |
|
177 |
do {\ |
|
178 |
QString __msg;\ |
|
179 |
__msg.sprintf(msg,arg1,arg2);\ |
|
180 |
NM_ERROR(err,__msg);\ |
|
181 |
} while(0) |
|
182 |
#define ERROR_3(err,msg,arg1,arg2,arg3)\ |
|
183 |
do {\ |
|
184 |
QString __msg;\ |
|
47 | 185 |
__msg.sprintf(msg,arg1,arg2,arg3);\ |
40 | 186 |
NM_ERROR(err,__msg);\ |
187 |
} while(0) |
|
188 |
#define ERROR_GEN(msg) ERROR(KErrGeneral,msg) |
|
189 |
#define ERROR_GEN_1(msg,arg1) ERROR_1(KErrGeneral,msg,arg1) |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
190 |
|
40 | 191 |
#else |
18
578830873419
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
|
40 | 193 |
#define NM_ERROR(err,msg) |
194 |
#define ERROR(err,msg) |
|
195 |
#define ERROR_1(err,msg,arg1) |
|
196 |
#define ERROR_2(err,msg,arg1,arg2) |
|
197 |
#define ERROR_3(err,msg,arg1,arg2,arg3) |
|
198 |
#define ERROR_GEN(msg) |
|
199 |
#define ERROR_GEN_1(msg,arg1) |
|
18
578830873419
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
|
44 | 201 |
#endif /* NM_ERROR_TRACES */ |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
202 |
|
40 | 203 |
/* |
204 |
* The macro NM_FUNCTION, when used inside a function body, enables tracing |
|
47 | 205 |
* for a function. If used, it should be placed on the first line of the |
206 |
* function body. ENTER and RETURN messages are printed when entering into |
|
44 | 207 |
* and returning from a function, respectively. In case of an exception, |
47 | 208 |
* UNWIND (for stack unwinding) is printed instead of RETURN. The FUNC_LOG |
209 |
* macro is provided for legacy compatibility. It is deprecated and should |
|
210 |
* not be used. |
|
40 | 211 |
*/ |
44 | 212 |
#if NM_FUNCTION_TRACES |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
213 |
|
40 | 214 |
class __ftracer |
215 |
{ |
|
216 |
public: |
|
217 |
__ftracer(const QString& _fn) |
|
218 |
: fn(_fn) |
|
219 |
{ |
|
220 |
print_trace("ENTER : " + fn); |
|
221 |
} |
|
222 |
~__ftracer() |
|
223 |
{ |
|
224 |
if (std::uncaught_exception()) { |
|
225 |
print_trace("UNWIND : " + fn); |
|
226 |
} else { |
|
227 |
print_trace("RETURN : " + fn); |
|
228 |
} |
|
229 |
} |
|
230 |
private: |
|
231 |
QString fn; |
|
232 |
}; |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
233 |
|
43 | 234 |
#define NM_FUNCTION __ftracer __ft(__PRETTY_FUNCTION__) |
40 | 235 |
#define FUNC_LOG NM_FUNCTION |
236 |
||
237 |
#else |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
238 |
|
40 | 239 |
#define NM_FUNCTION |
240 |
#define FUNC_LOG |
|
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
241 |
|
44 | 242 |
#endif /* NM_FUNCTION_TRACES */ |
0
8466d47a6819
Revision: 200949
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff
changeset
|
243 |
|
68 | 244 |
/* |
245 |
* The function NM_TIMESTAMP() prints a timestamp with a message. These traces do not |
|
246 |
* go to class specific log -files, but to one file called nmail_timestamps.log. |
|
247 |
*/ |
|
248 |
#if NM_TIMESTAMP_TRACES |
|
249 |
||
250 |
inline void NM_TIMESTAMP(const QString& msg) |
|
251 |
{ |
|
252 |
QString timestamp ("TIMESTAMP : " + |
|
253 |
QDateTime::currentDateTime().toUTC().toString("hh:mm:ss.zzz ") + |
|
254 |
msg); |
|
255 |
||
256 |
QDir directory("c:/data/"); |
|
257 |
if (directory.exists()) { |
|
258 |
directory.mkdir("logs"); |
|
259 |
} |
|
260 |
||
261 |
static QFile file(NM_LOG_DIRECTORY + |
|
262 |
QString("nmail_timestamps.log")); |
|
74 | 263 |
if (NM_LOG_TO_FILE && !file.isOpen()) { |
264 |
file.open(QIODevice::Append | QIODevice::Text | QIODevice::ReadWrite); |
|
265 |
} |
|
266 |
if (file.isWritable()) { |
|
267 |
QTextStream(&file) << timestamp << '\n'; |
|
268 |
} else { |
|
269 |
qDebug("[Nmail] %s",timestamp.toLatin1().data()); |
|
270 |
} |
|
271 |
if(file.isOpen()) { |
|
272 |
file.close(); |
|
273 |
} |
|
68 | 274 |
} |
275 |
||
276 |
#else |
|
277 |
||
278 |
#define NM_TIMESTAMP(msg) |
|
279 |
||
280 |
#endif /* NM_TIMESTAMP_TRACES */ |
|
281 |
||
40 | 282 |
#endif /* EMAILTRACE_H */ |