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