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