|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef TRACE_H |
|
20 #define TRACE_H |
|
21 |
|
22 #include "traceconfiguration.hrh" |
|
23 |
|
24 #ifdef TRACE_INTO_FILE |
|
25 #include <flogger.h> // RFileLogger |
|
26 #else |
|
27 #include <e32debug.h> // RDebug |
|
28 #endif |
|
29 |
|
30 //----------------------------------------------------------------------------- |
|
31 // Constants |
|
32 //----------------------------------------------------------------------------- |
|
33 // |
|
34 |
|
35 /** |
|
36 * Prefix trace macro to complete tracing with component name. |
|
37 * Returns TDesC which can be used directly with RDebug or RFileLogger. |
|
38 */ |
|
39 #define _PREFIX_TRACE( aMsg ) TPtrC( (const TText*)L"[SANIM]: " L##aMsg ) |
|
40 |
|
41 /** |
|
42 * Prefix error trace |
|
43 */ |
|
44 #define _PREFIX_ERROR( aMsg ) _PREFIX_TRACE( "[ERROR: %d]: " L##aMsg ) |
|
45 |
|
46 /** |
|
47 * Prefix info trace. |
|
48 */ |
|
49 #define _PREFIX_INFO( aMsg ) _PREFIX_TRACE( "[INFO]: " L##aMsg ) |
|
50 |
|
51 /** |
|
52 * Prefix macro for strings |
|
53 */ |
|
54 #define _PREFIX_CHAR( aMsg ) (const char*)"[SANIM]: " ##aMsg |
|
55 |
|
56 /** |
|
57 * Define needed directories if TRACE_INTO_FILE macro in use |
|
58 */ |
|
59 #ifdef TRACE_INTO_FILE |
|
60 |
|
61 _LIT( KDir, "SANIM" ); |
|
62 _LIT( KFile, "SANIM_log.txt" ); |
|
63 _LIT( KFullPath, "c:\\logs\\SANIM\\" ); |
|
64 |
|
65 #endif |
|
66 |
|
67 //----------------------------------------------------------------------------- |
|
68 // Error trace macros |
|
69 //----------------------------------------------------------------------------- |
|
70 // |
|
71 #ifdef ERROR_TRACE |
|
72 |
|
73 /** |
|
74 * Error trace definitions. |
|
75 */ |
|
76 #ifdef TRACE_INTO_FILE |
|
77 |
|
78 #define ERROR( aErr, aMsg )\ |
|
79 {\ |
|
80 if( aErr < KErrNone )\ |
|
81 {\ |
|
82 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \ |
|
83 _PREFIX_ERROR( aMsg ), aErr );\ |
|
84 }\ |
|
85 } |
|
86 #define ERROR_1( aErr, aMsg, aP1 )\ |
|
87 {\ |
|
88 if( aErr < KErrNone )\ |
|
89 {\ |
|
90 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \ |
|
91 _PREFIX_ERROR( aMsg ), aErr, aP1 );\ |
|
92 }\ |
|
93 } |
|
94 #define ERROR_2( aErr, aMsg, aP1, aP2 )\ |
|
95 {\ |
|
96 if( aErr < KErrNone )\ |
|
97 {\ |
|
98 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \ |
|
99 _PREFIX_ERROR( aMsg ), aErr, aP1, aP2 );\ |
|
100 }\ |
|
101 } |
|
102 #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\ |
|
103 {\ |
|
104 if( aErr < KErrNone )\ |
|
105 {\ |
|
106 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \ |
|
107 _PREFIX_ERROR( aMsg ), aErr, aP1, aP2, aP3 );\ |
|
108 }\ |
|
109 } |
|
110 |
|
111 #else//TRACE_INTO_FILE not defined |
|
112 |
|
113 #define ERROR( aErr, aMsg )\ |
|
114 {\ |
|
115 if( aErr < KErrNone )\ |
|
116 {\ |
|
117 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr );\ |
|
118 }\ |
|
119 } |
|
120 #define ERROR_1( aErr, aMsg, aP1 )\ |
|
121 {\ |
|
122 if( aErr < KErrNone )\ |
|
123 {\ |
|
124 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1 );\ |
|
125 }\ |
|
126 } |
|
127 #define ERROR_2( aErr, aMsg, aP1, aP2 )\ |
|
128 {\ |
|
129 if( aErr < KErrNone )\ |
|
130 {\ |
|
131 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1, aP2 );\ |
|
132 }\ |
|
133 } |
|
134 #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\ |
|
135 {\ |
|
136 if( aErr < KErrNone )\ |
|
137 {\ |
|
138 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1, aP2, aP3 );\ |
|
139 }\ |
|
140 } |
|
141 |
|
142 #endif//TRACE_INTO_FILE |
|
143 |
|
144 #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg ) |
|
145 #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 ) |
|
146 #define ERROR_GEN_2( aMsg, aP1, aP2 ) ERROR_2( KErrGeneral, aMsg, aP1, aP2 ) |
|
147 #define ERROR_GEN_3( aMsg, aP1, aP2, aP3 ) ERROR_3( KErrGeneral, aMsg, aP1, aP3 ) |
|
148 |
|
149 #define TRAPD_ERR( aErr, aStmt ) TRAPD( aErr, aStmt ) |
|
150 |
|
151 #else//ERROR_TRACE not defined |
|
152 |
|
153 #define ERROR( aErr, aMsg ) |
|
154 #define ERROR_1( aErr, aMsg, aP1 ) |
|
155 #define ERROR_2( aErr, aMsg, aP1, aP2 ) |
|
156 #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 ) |
|
157 #define ERROR_GEN( aMsg ) |
|
158 #define ERROR_GEN_1( aMsg, aP1 ) |
|
159 #define ERROR_GEN_2( aMsg, aP1, aP2 ) |
|
160 #define ERROR_GEN_3( aMsg, aP1, aP2, aP3 ) |
|
161 |
|
162 #define TRAPD_ERR( aErr, aStmt ) TRAP_IGNORE( aStmt ) |
|
163 |
|
164 #endif//ERROR_TRACE |
|
165 |
|
166 //----------------------------------------------------------------------------- |
|
167 // Info trace macros |
|
168 //----------------------------------------------------------------------------- |
|
169 // |
|
170 #ifdef INFO_TRACE |
|
171 |
|
172 /** |
|
173 * Info log message definitions. |
|
174 */ |
|
175 #ifdef TRACE_INTO_FILE |
|
176 |
|
177 #define INFO( aMsg )\ |
|
178 {\ |
|
179 RFileLogger::Write( \ |
|
180 KDir, KFile, EFileLoggingModeAppend, _PREFIX_INFO( aMsg ) );\ |
|
181 } |
|
182 #define INFO_1( aMsg, aP1 )\ |
|
183 {\ |
|
184 RFileLogger::WriteFormat( \ |
|
185 KDir, KFile, EFileLoggingModeAppend, _PREFIX_INFO( aMsg ), aP1 );\ |
|
186 } |
|
187 #define INFO_2( aMsg, aP1, aP2 )\ |
|
188 {\ |
|
189 RFileLogger::WriteFormat( \ |
|
190 KDir, KFile, EFileLoggingModeAppend, _PREFIX_INFO( aMsg ), aP1, aP2 );\ |
|
191 } |
|
192 #define INFO_3( aMsg, aP1, aP2, aP3 )\ |
|
193 {\ |
|
194 RFileLogger::WriteFormat( \ |
|
195 KDir, KFile, EFileLoggingModeAppend, _PREFIX_INFO( aMsg ), aP1, aP2, aP3 );\ |
|
196 } |
|
197 |
|
198 #else//TRACE_INTO_FILE not defined |
|
199 |
|
200 #define INFO( aMsg )\ |
|
201 {\ |
|
202 RDebug::Print( _PREFIX_INFO( aMsg ) );\ |
|
203 } |
|
204 #define INFO_1( aMsg, aP1 )\ |
|
205 {\ |
|
206 RDebug::Print( _PREFIX_INFO( aMsg ), aP1 );\ |
|
207 } |
|
208 #define INFO_2( aMsg, aP1, aP2 )\ |
|
209 {\ |
|
210 RDebug::Print( _PREFIX_INFO( aMsg ), aP1, aP2 );\ |
|
211 } |
|
212 #define INFO_3( aMsg, aP1, aP2, aP3 )\ |
|
213 {\ |
|
214 RDebug::Print( _PREFIX_INFO( aMsg ), aP1, aP2, aP3 );\ |
|
215 } |
|
216 |
|
217 #endif//TRACE_INTO_FILE |
|
218 |
|
219 #else//INFO_TRACE not defined |
|
220 |
|
221 #define INFO( aMsg ) |
|
222 #define INFO_1( aMsg, aP1 ) |
|
223 #define INFO_2( aMsg, aP1, aP2 ) |
|
224 #define INFO_3( aMsg, aP1, aP2, aP3 ) |
|
225 |
|
226 #endif//INFO_TRACE |
|
227 |
|
228 //----------------------------------------------------------------------------- |
|
229 // Trace current client thread name and process id |
|
230 //----------------------------------------------------------------------------- |
|
231 // |
|
232 #ifdef CLIENT_TRACE |
|
233 |
|
234 #define CLIENT( aMessage )\ |
|
235 {\ |
|
236 RThread thread;\ |
|
237 TInt err = aMessage.Client( thread );\ |
|
238 if( err == KErrNone )\ |
|
239 {\ |
|
240 RProcess process;\ |
|
241 err = thread.Process( process );\ |
|
242 if( err == KErrNone )\ |
|
243 {\ |
|
244 TPtrC thredName( thread.Name() );\ |
|
245 TUid processUid( process.SecureId() );\ |
|
246 INFO_2( "Current client process UID: [%x], thread name: [%S]",\ |
|
247 processUid,\ |
|
248 &thredName );\ |
|
249 }\ |
|
250 process.Close();\ |
|
251 }\ |
|
252 thread.Close();\ |
|
253 } |
|
254 |
|
255 #else |
|
256 |
|
257 #define CLIENT( aMessage ) |
|
258 |
|
259 #endif |
|
260 |
|
261 //----------------------------------------------------------------------------- |
|
262 // Function trace macros |
|
263 //----------------------------------------------------------------------------- |
|
264 // |
|
265 #ifdef FUNC_TRACE |
|
266 |
|
267 /** |
|
268 * Function logging definitions. |
|
269 */ |
|
270 #ifdef TRACE_INTO_FILE |
|
271 |
|
272 #define FUNC( aMsg, aP1 )\ |
|
273 {\ |
|
274 TPtrC8 trace( _S8( aMsg ) );\ |
|
275 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, trace, aP1 );\ |
|
276 }\ |
|
277 |
|
278 #else//TRACE_INTO_FILE not defined |
|
279 |
|
280 #define FUNC( aMsg, aP1 )\ |
|
281 {\ |
|
282 RDebug::Printf( aMsg, aP1 );\ |
|
283 }\ |
|
284 |
|
285 #endif//TRACE_INTO_FILE |
|
286 |
|
287 /** |
|
288 * Function trace helper class. |
|
289 * |
|
290 * NOTE: |
|
291 * LC -methods cannot be trapped. Therefore if LC -method leaves |
|
292 * END trace is used instead of LEAVE trace. |
|
293 * If you have an idea how to round this problem please tell. |
|
294 */ |
|
295 _LIT8( KFuncNameTerminator, "(" ); |
|
296 _LIT8( KFuncLeavePatternL, "L" ); |
|
297 class TFuncLog |
|
298 { |
|
299 public: |
|
300 static void Cleanup( TAny* aPtr ) |
|
301 { |
|
302 TFuncLog* self = static_cast< TFuncLog* >( aPtr ); |
|
303 self->iLeft = ETrue; |
|
304 FUNC( _PREFIX_CHAR("%S-LEAVE"), &self->iFunc ); // Leave detected |
|
305 } |
|
306 inline TFuncLog( const char* aFunc ) : |
|
307 iFunc( aFunc ? _S8( aFunc ) : _S8("") ), |
|
308 iLeft( EFalse ), |
|
309 iCleanupItem( Cleanup, this ), |
|
310 iCanLeave( EFalse ) |
|
311 { |
|
312 TInt pos( iFunc.Find( KFuncNameTerminator ) ); |
|
313 if( pos != KErrNotFound ) |
|
314 { |
|
315 iFunc.Set( iFunc.Left( pos ) ); |
|
316 iCanLeave = !iFunc.Right( |
|
317 KFuncLeavePatternL().Length() ).Compare( KFuncLeavePatternL ); |
|
318 if ( iCanLeave ) |
|
319 { |
|
320 CleanupStack::PushL( iCleanupItem ); // Ignore warnings |
|
321 } |
|
322 } |
|
323 FUNC( _PREFIX_CHAR("%S-START"), &iFunc ); |
|
324 } |
|
325 |
|
326 inline ~TFuncLog() |
|
327 { |
|
328 if ( !iLeft ) |
|
329 { |
|
330 if ( iCanLeave ) |
|
331 { |
|
332 CleanupStack::Pop( this ); // Pop the cleanup item |
|
333 } |
|
334 FUNC( _PREFIX_CHAR("%S-END"), &iFunc ); // Normally finished |
|
335 } |
|
336 } |
|
337 |
|
338 private: // Data |
|
339 TPtrC8 iFunc; |
|
340 TBool iLeft; |
|
341 TCleanupItem iCleanupItem; |
|
342 TBool iCanLeave; |
|
343 }; |
|
344 #define FUNC_LOG TFuncLog _fl( __PRETTY_FUNCTION__ ); |
|
345 |
|
346 #else//FUNC_TRACE not defined |
|
347 |
|
348 #define FUNC_LOG |
|
349 |
|
350 #endif//FUNC_TRACE |
|
351 |
|
352 //----------------------------------------------------------------------------- |
|
353 // Timestamp trace macros |
|
354 //----------------------------------------------------------------------------- |
|
355 // |
|
356 #ifdef TIMESTAMP_TRACE |
|
357 |
|
358 #ifdef TRACE_INTO_FILE |
|
359 |
|
360 #define TIMESTAMP( aCaption )\ |
|
361 {\ |
|
362 TTime t;\ |
|
363 t.HomeTime();\ |
|
364 TDateTime dt = t.DateTime();\ |
|
365 _LIT( KCaption, aCaption );\ |
|
366 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend,\ |
|
367 _PREFIX_TRACE("[TIMESTAMP] %S %d:%02d:%02d.%d us"),\ |
|
368 &KCaption, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\ |
|
369 } |
|
370 |
|
371 #else//TRACE_INTO_FILE not defined |
|
372 |
|
373 #define TIMESTAMP( aCaption )\ |
|
374 {\ |
|
375 TTime t;\ |
|
376 t.HomeTime();\ |
|
377 TDateTime dt = t.DateTime();\ |
|
378 _LIT( KCaption, aCaption );\ |
|
379 RDebug::Print( _PREFIX_TRACE("[TIMESTAMP] %S %d:%02d:%02d.%d us"),\ |
|
380 &KCaption, dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond() );\ |
|
381 } |
|
382 |
|
383 #endif//TRACE_INTO_FILE |
|
384 |
|
385 #else//TIMESTAMP_TRACE not defined |
|
386 |
|
387 #define TIMESTAMP( aCaption ) |
|
388 |
|
389 #endif//TIMESTAMP_TRACE |
|
390 |
|
391 #endif |