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