|
1 /* |
|
2 * Copyright (c) 2008-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: Logging macros |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #ifndef GLXTRACER_H |
|
22 #define GLXTRACER_H |
|
23 |
|
24 /** |
|
25 * This file has been copied from the forum nokia wiki and renamed to glxtracer.h |
|
26 * http://wiki.forum.nokia.com/index.php/Trace_Function_Enter%2C_Exit_and_Leave |
|
27 */ |
|
28 |
|
29 #include <e32base.h> |
|
30 |
|
31 // Define tracer logging method |
|
32 // 0 = Logging off |
|
33 // 1 = Log to RDebug |
|
34 // 2 = Log to file (RFileLogger) |
|
35 #define TRACER_LOG_METHOD 0 |
|
36 |
|
37 // ============================================================================ |
|
38 |
|
39 // Logging off, define empty macros and skip all the rest |
|
40 #if TRACER_LOG_METHOD == 0 |
|
41 |
|
42 #define TRACER(func) |
|
43 #define TRACER_RET(func,format) |
|
44 |
|
45 #else // Logging on |
|
46 |
|
47 // Macro to print function entry, exit and leave. |
|
48 // Example: TRACER("CMyClass::MyFunction"); |
|
49 #define TRACER(func) TTracer function_tracer( _S(func), _S("") ); |
|
50 |
|
51 // Macro to print function return value in addition to entry, exit |
|
52 // and leave conditions Second parameter is a formatting string used |
|
53 // to print the return value Example to print an integer return value: |
|
54 // TRACER_RET("CMyclass::MyFunction", "%d"); |
|
55 #define TRACER_RET(func,format) TTracer func_tracer( _S(func), _S(format) ); |
|
56 |
|
57 #if TRACER_LOG_METHOD == 1 // Print to RDebug |
|
58 |
|
59 #include <e32debug.h> |
|
60 #define TRACER_PRINT(a) RDebug::Print(a,&iFunc); |
|
61 #define TRACER_PRINT_RET(a,b) RDebug::Print(a,&iFunc,b); |
|
62 |
|
63 #elif TRACER_LOG_METHOD == 2 // Print to file |
|
64 |
|
65 #include <flogger.h> |
|
66 _LIT( KLogDir, "tracer" ); // Log directory: C:\logs\tracer |
|
67 _LIT( KLogFile, "tracer.txt" ); // Log file: c:\logs\tracer\tracer.txt |
|
68 #define TRACER_PRINT(a) RFileLogger::WriteFormat(KLogDir, \ |
|
69 KLogFile,EFileLoggingModeAppend,a,&iFunc); |
|
70 #define TRACER_PRINT_RET(a,b) RFileLogger::WriteFormat(KLogDir, \ |
|
71 KLogFile,EFileLoggingModeAppend,a,&iFunc,b); |
|
72 |
|
73 #endif |
|
74 |
|
75 _LIT( KLogEnter, "%S: ENTER" ); |
|
76 _LIT( KLogExit, "%S: EXIT" ); |
|
77 _LIT( KLogLeave, "%S: LEAVE!" ); |
|
78 _LIT( KLogExitRet, "%S: EXIT, Returning " ); |
|
79 |
|
80 /** |
|
81 * Simple tracer class that logs function enter, exit or leave |
|
82 */ |
|
83 class TTracer |
|
84 { |
|
85 public: |
|
86 |
|
87 /** |
|
88 * inline constructor to write log of entering a function |
|
89 */ |
|
90 TTracer( const TText* aFunc, const TText* aRetFormat ) |
|
91 : iFunc( aFunc ) |
|
92 , iRetFormat( aRetFormat ) |
|
93 { |
|
94 TRACER_PRINT( KLogEnter ); |
|
95 } |
|
96 |
|
97 /** |
|
98 * inline destructor to write log of exiting a function |
|
99 * normally or with a leave |
|
100 */ |
|
101 ~TTracer() |
|
102 { |
|
103 if ( std::uncaught_exception() ) // Leave is an exception |
|
104 { |
|
105 // The function exited with a leave |
|
106 TRACER_PRINT( KLogLeave ); |
|
107 } |
|
108 else |
|
109 { |
|
110 // The function exited normally |
|
111 if ( iRetFormat.Length() == 0 ) |
|
112 { |
|
113 TRACER_PRINT( KLogExit ); |
|
114 } |
|
115 else |
|
116 { |
|
117 // Log the return value |
|
118 #ifdef __WINS__ |
|
119 TInt32 retVal = 0; |
|
120 |
|
121 // The assembly bit. This needs to be reimplemented |
|
122 // for every target. |
|
123 _asm( mov retVal, ebx ); |
|
124 |
|
125 TBuf<100> format( KLogExitRet ); |
|
126 format.Append( iRetFormat ); |
|
127 TRACER_PRINT_RET( format, retVal ); |
|
128 #else |
|
129 TRACER_PRINT( KLogExit ); |
|
130 #endif |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 private: |
|
136 |
|
137 /** |
|
138 * Pointer descriptor to function signature that is to be logged. |
|
139 */ |
|
140 TPtrC iFunc; |
|
141 |
|
142 /** |
|
143 * Formatting string used to print the function return value |
|
144 */ |
|
145 TPtrC iRetFormat; |
|
146 |
|
147 }; |
|
148 |
|
149 #endif // TRACER_LOG_METHOD == 0 |
|
150 |
|
151 #endif // GLXTRACER_H |