uiservicetab/inc/uiservicetabtracer.h
branchRCL_3
changeset 14 9fdee5e1da30
child 18 36d367c62acb
equal deleted inserted replaced
13:796276a1bdcc 14:9fdee5e1da30
       
     1 /*
       
     2 * Copyright (c) 2008 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: trace logs
       
    15  *
       
    16 */
       
    17 
       
    18 #ifndef UISERVICETABTRACER_H
       
    19 #define UISERVICETABTRACER_H
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <flogger.h>
       
    23 #include "vimpstbuilddefinitions.h"
       
    24 
       
    25 #ifdef CHAT_ENABLE_DEBUG_PRINT
       
    26 
       
    27 // --------------------------------------------------------------------------
       
    28 // Define tracer logging method
       
    29 // 0    = Logging off
       
    30 // 1    = Log to RDebug
       
    31 // 2    = Log to file (RFileLogger)
       
    32 // --------------------------------------------------------------------------
       
    33 //
       
    34 #ifdef __WINS__
       
    35     #define TRACER_LOG_METHOD 1 // for WINS
       
    36 #else
       
    37     #define TRACER_LOG_METHOD 2 // for device
       
    38 #endif
       
    39 
       
    40 #endif
       
    41 
       
    42 // --------------------------------------------------------------------------
       
    43 // Whether to use TRAP_INSTRUMENTATIONs to
       
    44 // log trapped leaves
       
    45 // --------------------------------------------------------------------------
       
    46 //
       
    47 #define TRACE_TRAPS
       
    48 //============================================================================
       
    49 
       
    50 // Logging off, define empty macros and skip all the rest
       
    51 #if TRACER_LOG_METHOD == 0
       
    52 
       
    53 #define TRACER(func)
       
    54 #define TRACER_AUTO
       
    55 #define TRACER_RET(func,format)
       
    56 #define TRACER_AUTO_RET(format)
       
    57 #define TRACER_LEAVE(func)
       
    58 #define TRACER_AUTO_LEAVE
       
    59 #define TRACE(args...)
       
    60 #define TRACE8(args...)
       
    61 #define TRACER_TO16(a,b)
       
    62 #define TRACER_TO8(a,b)
       
    63 #define TRACER_CLOSELOG
       
    64 
       
    65 #else  // Logging on
       
    66 
       
    67 // --------------------------------------------------------------------------
       
    68 // Change these to modify line syntax
       
    69 // --------------------------------------------------------------------------
       
    70 //
       
    71 _LIT( KLogBase, "UiServiceTab %S(%d): %S");
       
    72 _LIT( KLogEnter, "UiServiceTab %S: ENTER" );
       
    73 _LIT( KLogExit, "UiServiceTab %S: EXIT" );
       
    74 _LIT( KLogLeave, "UiServiceTab %S: LEAVE!" );
       
    75 _LIT( KLogTrappedLeave, "UiServiceTab %S(%d): TRAPPED LEAVE, code %d!" );
       
    76 _LIT( KLogExitRet, "UiServiceTab %S: EXIT, Returning " );
       
    77 _LIT( KLogDir,  "UiServiceTab" ); 
       
    78 _LIT( KLogFile, "UiServiceTab.log" );
       
    79 
       
    80 #define TRACER_PARAMS_LENGTH 200
       
    81 
       
    82 // --------------------------------------------------------------------------
       
    83 // Macro to print only function leaves (not entries or exits). 
       
    84 // Example: TRACER_LEAVE( "CMyClass::MyFunction" );
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 #define TRACER_LEAVE(func) TTracer function_tracer( _S(func), _S(""), EFalse )
       
    88 
       
    89 // --------------------------------------------------------------------------
       
    90 // Same as above, except that function name is generated automatically
       
    91 // Example: TRACER_AUTO_LEAVE;
       
    92 // --------------------------------------------------------------------------
       
    93 //
       
    94 #define TRACER_AUTO_LEAVE TTracer \
       
    95     function_tracer( ((const TText8*)__PRETTY_FUNCTION__), _S(""), EFalse )
       
    96 
       
    97 // --------------------------------------------------------------------------
       
    98 // Macro to print function entry, exit and leave. 
       
    99 // Example: TRACER( "CMyClass::MyFunction" );
       
   100 // --------------------------------------------------------------------------
       
   101 //
       
   102 #define TRACER(func) TTracer function_tracer( _S(func), _S(""), ETrue )
       
   103 
       
   104 // --------------------------------------------------------------------------
       
   105 // Same as above, except that function name is generated automatically
       
   106 // Example: TRACER_AUTO;
       
   107 // --------------------------------------------------------------------------
       
   108 //
       
   109 #define TRACER_AUTO \
       
   110     TTracer function_tracer \
       
   111         ( ((const TText8*)__PRETTY_FUNCTION__), _S(""), ETrue )
       
   112 
       
   113 // --------------------------------------------------------------------------
       
   114 // Macro to print function return value in addition to entry, exit 
       
   115 // and leave conditions. Second parameter is a formatting string used 
       
   116 // to print the return value Example to print an integer return value:
       
   117 // TRACER_RET("CMyclass::MyFunction", "%d");
       
   118 // --------------------------------------------------------------------------
       
   119 //
       
   120 #define TRACER_RET(func,format) \
       
   121     TTracer function_tracer( _S(func), _S(format), ETrue )
       
   122 
       
   123 // --------------------------------------------------------------------------
       
   124 // Same as above, except that function name is generated automatically
       
   125 // Example: TRACER_AUTO_RET( "%d" );
       
   126 // --------------------------------------------------------------------------
       
   127 //
       
   128 #define TRACER_AUTO_RET(format) \
       
   129     TTracer function_tracer \
       
   130         ( ((const TText8*)__PRETTY_FUNCTION__), _S(format), ETrue )
       
   131 
       
   132 // --------------------------------------------------------------------------
       
   133 // Macro to print custom msg with any number of parameters
       
   134 // Either TRACER, TRACER_RET or TRACER_LEAVE 
       
   135 // must be called before in same scope
       
   136 // It's is assumed that descriptor (pointer) parameters are 16-bit
       
   137 // NOTE: It is also assumed that parameter values
       
   138 // add no more than TRACE_PARAMS_LENGTH to the traced string
       
   139 // --------------------------------------------------------------------------
       
   140 //
       
   141 #define TRACE( args... ) function_tracer.trace( __LINE__, ## args )
       
   142 
       
   143 // --------------------------------------------------------------------------
       
   144 // Macro to print custom msg with any number of parameters
       
   145 // Either TRACER, TRACER_RET or TRACER_LEAVE 
       
   146 // must be called before in same scope
       
   147 // It's is assumed that descriptor (pointer) parameters are 8-bit
       
   148 // NOTE: It is also assumed that parameter values 
       
   149 // add no more than TRACE_PARAMS_LENGTH to the traced string
       
   150 // --------------------------------------------------------------------------
       
   151 //
       
   152 #define TRACE8( args... ) function_tracer.trace8(__LINE__, ## args )
       
   153 
       
   154 // --------------------------------------------------------------------------
       
   155 // Just to quickly convert descriptors if needed
       
   156 // --------------------------------------------------------------------------
       
   157 //
       
   158 #define DESCRIPTOR_LENGTH 100
       
   159 #define TRACER_TO16( desc8, desc16 ) \
       
   160     TBuf<DESCRIPTOR_LENGTH> desc16;desc16.Copy(desc8) 
       
   161 #define TRACER_TO8( desc16, desc8 ) \
       
   162     TBuf8<DESCRIPTOR_LENGTH> desc8;desc8.Copy(desc16) 
       
   163 
       
   164 #ifdef TRACE_TRAPS
       
   165 #undef TRAP_INSTRUMENTATION_LEAVE
       
   166 #define TRAP_INSTRUMENTATION_LEAVE(aResult) \
       
   167     TTracer::static_trace( ((const TText8*)__PRETTY_FUNCTION__), \
       
   168                            __LINE__, aResult )
       
   169 #endif
       
   170 
       
   171 
       
   172 #if TRACER_LOG_METHOD == 1      // Print to RDebug
       
   173 
       
   174 #include <e32debug.h>
       
   175 
       
   176 #define TRACER_PRINT(a)            RDebug::Print(a,&iFunc);
       
   177 #define TRACER_PRINT_BASE(a,b,c)   RDebug::Print(a,&iFunc, b, c);
       
   178 #define TRACER_PRINT_RET(a,b)      RDebug::Print(a,&iFunc,b);
       
   179 #define TRACER_PRINT_STATIC(a, b, c,d ) \
       
   180     RDebug::Print(a, b, c, d);
       
   181 
       
   182 #elif TRACER_LOG_METHOD == 2    // Print to file
       
   183 
       
   184  // needs flogger.lib as DEBUGLIBRARY
       
   185 
       
   186 // --------------------------------------------------------------------------
       
   187 // Log directory under C:\logs
       
   188 // --------------------------------------------------------------------------
       
   189 //
       
   190 //_LIT( KLogDir,  "MeCo" );      
       
   191 
       
   192 // --------------------------------------------------------------------------
       
   193 // Log filename in log directory
       
   194 // --------------------------------------------------------------------------
       
   195 //
       
   196 //_LIT( KLogFile, "MeCo.log" );
       
   197 #define TRACER_PRINT(a)         RFileLogger::WriteFormat(KLogDir, \
       
   198                             KLogFile,EFileLoggingModeAppend,a,&iFunc);
       
   199 #define TRACER_PRINT_BASE(a,b,c)   RFileLogger::WriteFormat(KLogDir, \
       
   200                             KLogFile,EFileLoggingModeAppend,a,&iFunc,b,c);
       
   201 #define TRACER_PRINT_RET(a,b)   RFileLogger::WriteFormat(KLogDir, \
       
   202                             KLogFile,EFileLoggingModeAppend,a,&iFunc,b);
       
   203 #define TRACER_PRINT_STATIC(a,b,c,d) \
       
   204     RFileLogger::WriteFormat(KLogDir, \
       
   205         KLogFile,EFileLoggingModeAppend,a,b,c,d);
       
   206 
       
   207 #endif
       
   208 
       
   209 
       
   210 /**
       
   211  * Simple tracer class that logs function enter, exit or leave
       
   212  */
       
   213 class TTracer : public CBase
       
   214     {
       
   215 private:
       
   216     
       
   217     /**
       
   218      * Simple helper method to just return if NULL
       
   219      */
       
   220     template<class T> static void ReturnIfNull(T* aNy) 
       
   221         { if ( !aNy ) return; }
       
   222 
       
   223 public:
       
   224     /**
       
   225      * inline constructor to write log of entering a function
       
   226      */
       
   227     TTracer( const TText* aFunc, 
       
   228              const TText* aRetFormat, 
       
   229              TBool aPrintEnter) :
       
   230         iFunc( aFunc), 
       
   231         iRetFormat( aRetFormat ),
       
   232         iPrintEnterAndExit( aPrintEnter)
       
   233         {
       
   234         iFuncBuf = NULL;
       
   235         //iLogger.Connect();
       
   236         //iLogger.CreateLog(KLogDir, KLogFile, mode);
       
   237         if ( iPrintEnterAndExit )
       
   238             {
       
   239             TRACER_PRINT( KLogEnter );
       
   240             }
       
   241         }
       
   242 
       
   243     /**
       
   244      * inline constructor to write log of entering a function (8-bit)
       
   245      */
       
   246     TTracer( const TText8* aFunc, 
       
   247              const TText* aRetFormat, 
       
   248              TBool aPrintEnter) :
       
   249         iRetFormat( aRetFormat ),
       
   250         iPrintEnterAndExit( aPrintEnter )
       
   251         {
       
   252         TInt len = User::StringLength ((const unsigned char*)aFunc );
       
   253         HBufC8* temp = HBufC8::New( len );
       
   254         ReturnIfNull( temp );
       
   255         *temp = (const TUint8*)aFunc;
       
   256         iFuncBuf = HBufC::NewL( len );
       
   257         //iLogger.Connect();
       
   258         //iLogger.CreateLog(KLogDir, KLogFile, EFileLoggingModeAppend);
       
   259         if ( iFuncBuf )
       
   260             {
       
   261             iFuncBuf->Des().Copy( *temp );
       
   262             iFunc.Set( *iFuncBuf );
       
   263             if ( iPrintEnterAndExit )
       
   264                 {
       
   265                 TRACER_PRINT( KLogEnter );
       
   266                 }
       
   267             }
       
   268         delete temp;
       
   269         }
       
   270 
       
   271 
       
   272     /**
       
   273      * inline destructor to write log of exiting a function 
       
   274      * normally or with a leave
       
   275      */
       
   276     ~TTracer( )
       
   277         {
       
   278         if ( std::uncaught_exception ( ) ) // Leave is an exception
       
   279             {
       
   280             // The function exited with a leave
       
   281             TRACER_PRINT( KLogLeave );
       
   282             }
       
   283         else
       
   284             {
       
   285             // The function exited normally
       
   286             if ( iPrintEnterAndExit )
       
   287                 {
       
   288                 if ( iRetFormat.Length ( )== 0 )
       
   289                     {
       
   290                     TRACER_PRINT( KLogExit );
       
   291                     }
       
   292                 else
       
   293                     {
       
   294                     // Log the return value
       
   295 #ifdef __WINS__
       
   296                     TInt32 retVal = 0;
       
   297 
       
   298                     // The assembly bit. This needs to be reimplemented
       
   299                     // for every target. 
       
   300                     _asm( mov retVal, ebx );
       
   301 
       
   302                     TBuf<100> format( KLogExitRet );
       
   303                     format.Append( iRetFormat );
       
   304                     TRACER_PRINT_RET( format, retVal );
       
   305 #else
       
   306                     // ARM assembler to get return value
       
   307                     // could be implemented here..
       
   308                     TRACER_PRINT( KLogExit );
       
   309 #endif
       
   310                     }
       
   311                 }
       
   312             }
       
   313         //iLogger.Close();
       
   314         delete iFuncBuf;
       
   315         }
       
   316 
       
   317 public:
       
   318 
       
   319     /**
       
   320      * Trace method that assumes 16-bit descriptor parameters
       
   321      */
       
   322     void trace( TInt aLine, const char* aMsg, ... )
       
   323         {
       
   324         ReturnIfNull( iFunc.Ptr() );
       
   325         TInt len = User::StringLength ((const unsigned char*)aMsg );
       
   326         TPtrC8 msg( (const unsigned char*)aMsg, len );
       
   327         HBufC* format = HBufC::New( len );
       
   328         ReturnIfNull( format );
       
   329         format->Des().Copy( msg );
       
   330         HBufC* result = HBufC::New( len + TRACER_PARAMS_LENGTH );
       
   331         if ( result )
       
   332             {
       
   333             VA_LIST list;
       
   334             VA_START( list, aMsg );
       
   335             result->Des().FormatList( *format, list );
       
   336             VA_END(list);
       
   337             TRACER_PRINT_BASE(KLogBase, aLine, result );
       
   338             }
       
   339         delete format;
       
   340         delete result;
       
   341         }
       
   342 
       
   343     /**
       
   344      * Trace method that assumes 8-bit descriptor parameters
       
   345      */
       
   346     void trace8(TInt aLine, const char* aMsg, ... )
       
   347         {
       
   348         ReturnIfNull( iFunc.Ptr() );
       
   349         TInt len = User::StringLength ((const unsigned char*)aMsg );
       
   350         TPtrC8 msg( (const unsigned char*)aMsg, len );
       
   351         HBufC8* result8 = HBufC8::New( len + TRACER_PARAMS_LENGTH );
       
   352         ReturnIfNull( result8 );
       
   353         VA_LIST list;
       
   354         VA_START( list, aMsg );
       
   355         result8->Des().FormatList( msg, list );
       
   356         VA_END(list);
       
   357         HBufC* result16 = HBufC::New( result8->Length() );
       
   358         if ( result16 )
       
   359             {
       
   360             result16->Des().Copy( *result8 );
       
   361             TRACER_PRINT_BASE( KLogBase, aLine, result16 );
       
   362             }
       
   363         delete result8;
       
   364         delete result16;
       
   365         }
       
   366     
       
   367     static void static_trace( const TText8* aFunc, 
       
   368                               TInt aLine,
       
   369                               TInt aResult )
       
   370         {
       
   371         TInt len = User::StringLength ((const unsigned char*)aFunc );
       
   372         HBufC8* temp = HBufC8::New( len );
       
   373         ReturnIfNull( temp );
       
   374         *temp = (const TUint8*)aFunc;
       
   375         HBufC* funcBuf = HBufC::NewL( len );
       
   376         if ( funcBuf )
       
   377             {
       
   378             funcBuf->Des().Copy( *temp );
       
   379             TRACER_PRINT_STATIC( 
       
   380                 KLogTrappedLeave, funcBuf, aLine, aResult );
       
   381             }
       
   382         delete temp;
       
   383         delete funcBuf;
       
   384         }
       
   385 public:
       
   386 
       
   387     /** 
       
   388      * Pointer descriptor to function signature that is to be logged.
       
   389      */
       
   390     TPtrC iFunc;
       
   391     //RFileLogger iLogger;
       
   392 
       
   393 private:
       
   394 
       
   395     
       
   396 	/**
       
   397      * Formatting string used to print the function return value
       
   398      */
       
   399     TPtrC iRetFormat;
       
   400     
       
   401     /**
       
   402      * If true, method enters and exits are printed
       
   403      */
       
   404     TBool iPrintEnterAndExit;
       
   405     
       
   406     /**
       
   407      * Buffer to store function name when needed
       
   408      */
       
   409     HBufC* iFuncBuf;
       
   410     };
       
   411 
       
   412 #endif // TRACER_LOG_METHOD != 0 && CHAT_ENABLE_DEBUG_PRINT
       
   413 
       
   414 #endif // UISERVICETABTRACER_H
       
   415 
       
   416 // End of file