systemsettings/GSAccessoryPlugin/inc/trace.h
changeset 0 2e3d3ce01487
equal deleted inserted replaced
-1:000000000000 0:2e3d3ce01487
       
     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 macros for GSAccessoryPlugin module.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef TRACE_H
       
    20 #define TRACE_H
       
    21 
       
    22 #include "traceconfiguration.hrh"
       
    23 
       
    24 #ifdef TRACE_INTO_FILE
       
    25 #include <flogger.h> // RFileLogger
       
    26 #else
       
    27 #include <e32debug.h> // RDebug
       
    28 #endif
       
    29 
       
    30 //-----------------------------------------------------------------------------
       
    31 // Constants
       
    32 //-----------------------------------------------------------------------------
       
    33 //
       
    34 
       
    35 /**
       
    36 * Prefix trace macro to complete tracing with component name.
       
    37 * Returns TDesC which can be used directly with RDebug or RFileLogger.
       
    38 */
       
    39 #define _PREFIX_TRACE( aMsg ) TPtrC( (const TText*)L"[GSACCDLG]: " L##aMsg )
       
    40 
       
    41 /**
       
    42 * Prefix error trace
       
    43 */
       
    44 #define _PREFIX_ERROR( aMsg ) _PREFIX_TRACE( "[ERROR: %d]: " L##aMsg )
       
    45 
       
    46 /**
       
    47 * Prefix info trace.
       
    48 */
       
    49 #define _PREFIX_INFO( aMsg ) _PREFIX_TRACE( "[INFO]: " L##aMsg )
       
    50 
       
    51 /**
       
    52 * Prefix macro for strings
       
    53 */
       
    54 #define _PREFIX_CHAR( aMsg ) (const char*)"[GSACCDLG]: " ##aMsg
       
    55 
       
    56 /**
       
    57 * Define needed directories if TRACE_INTO_FILE macro in use
       
    58 */
       
    59 #ifdef TRACE_INTO_FILE
       
    60 
       
    61     _LIT( KDir, "gsaccdlg" );
       
    62     _LIT( KFile, "gsaccdlg_log.txt" );
       
    63     _LIT( KFullPath, "c:\\logs\\gsaccdlg\\" );
       
    64 
       
    65 #endif
       
    66 
       
    67 //----------------------------------------------------------------------------
       
    68 // Error trace macros
       
    69 //----------------------------------------------------------------------------
       
    70 //
       
    71 #ifdef ERROR_TRACE
       
    72 
       
    73     /**
       
    74     * Error trace definitions.
       
    75     */
       
    76     #ifdef TRACE_INTO_FILE
       
    77 
       
    78         #define ERROR( aErr, aMsg )\
       
    79             {\
       
    80             if( aErr < KErrNone )\
       
    81                 {\
       
    82                 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
    83                     _PREFIX_ERROR( aMsg ), aErr );\
       
    84                 }\
       
    85             }
       
    86         #define ERROR_1( aErr, aMsg, aP1 )\
       
    87             {\
       
    88             if( aErr < KErrNone )\
       
    89                 {\
       
    90                 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
    91                     _PREFIX_ERROR( aMsg ), aErr, aP1 );\
       
    92                 }\
       
    93             }
       
    94         #define ERROR_2( aErr, aMsg, aP1, aP2 )\
       
    95             {\
       
    96             if( aErr < KErrNone )\
       
    97                 {\
       
    98                 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
    99                     _PREFIX_ERROR( aMsg ), aErr, aP1, aP2 );\
       
   100                 }\
       
   101             }
       
   102         #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\
       
   103             {\
       
   104             if( aErr < KErrNone )\
       
   105                 {\
       
   106                 RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
   107                     _PREFIX_ERROR( aMsg ), aErr, aP1, aP2, aP3 );\
       
   108                 }\
       
   109             }
       
   110 
       
   111     #else//TRACE_INTO_FILE not defined
       
   112 
       
   113         #define ERROR( aErr, aMsg )\
       
   114             {\
       
   115             if( aErr < KErrNone )\
       
   116                 {\
       
   117                 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr );\
       
   118                 }\
       
   119             }
       
   120         #define ERROR_1( aErr, aMsg, aP1 )\
       
   121             {\
       
   122             if( aErr < KErrNone )\
       
   123                 {\
       
   124                 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1 );\
       
   125                 }\
       
   126             }
       
   127         #define ERROR_2( aErr, aMsg, aP1, aP2 )\
       
   128             {\
       
   129             if( aErr < KErrNone )\
       
   130                 {\
       
   131                 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1, aP2 );\
       
   132                 }\
       
   133             }
       
   134         #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )\
       
   135             {\
       
   136             if( aErr < KErrNone )\
       
   137                 {\
       
   138                 RDebug::Print( _PREFIX_ERROR( aMsg ), aErr, aP1, aP2, aP3 );\
       
   139                 }\
       
   140             }
       
   141 
       
   142     #endif//TRACE_INTO_FILE
       
   143 
       
   144     #define ERROR_GEN( aMsg ) ERROR( KErrGeneral, aMsg )
       
   145     #define ERROR_GEN_1( aMsg, aP1 ) ERROR_1( KErrGeneral, aMsg, aP1 )
       
   146     #define ERROR_GEN_2( aMsg, aP1, aP2 ) ERROR_2( KErrGeneral, aMsg, aP1, aP2 )
       
   147     #define ERROR_GEN_3( aMsg, aP1, aP2, aP3 ) ERROR_3( KErrGeneral, aMsg, aP1, aP3 )
       
   148 
       
   149 #else//ERROR_TRACE not defined
       
   150 
       
   151     #define ERROR( aErr, aMsg )
       
   152     #define ERROR_1( aErr, aMsg, aP1 )
       
   153     #define ERROR_2( aErr, aMsg, aP1, aP2 )
       
   154     #define ERROR_3( aErr, aMsg, aP1, aP2, aP3 )
       
   155     #define ERROR_GEN( aMsg )
       
   156     #define ERROR_GEN_1( aMsg, aP1 )
       
   157     #define ERROR_GEN_2( aMsg, aP1, aP2 )
       
   158     #define ERROR_GEN_3( aMsg, aP1, aP2, aP3 )
       
   159 
       
   160 #endif//ERROR_TRACE
       
   161 
       
   162 //----------------------------------------------------------------------------
       
   163 // Info trace macros
       
   164 //----------------------------------------------------------------------------
       
   165 //
       
   166 #ifdef INFO_TRACE
       
   167 
       
   168     /**
       
   169     * Info log message definitions.
       
   170     */
       
   171     #ifdef TRACE_INTO_FILE
       
   172 
       
   173         #define INFO( aMsg )\
       
   174             {\
       
   175             RFileLogger::Write( KDir, KFile, EFileLoggingModeAppend, \
       
   176                 _PREFIX_INFO( aMsg ) );\
       
   177             }
       
   178         #define INFO_1( aMsg, aP1 )\
       
   179             {\
       
   180             RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
   181                 _PREFIX_INFO( aMsg ), aP1 );\
       
   182             }
       
   183         #define INFO_2( aMsg, aP1, aP2 )\
       
   184             {\
       
   185             RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
   186                 _PREFIX_INFO( aMsg ), aP1, aP2 );\
       
   187             }
       
   188         #define INFO_3( aMsg, aP1, aP2, aP3 )\
       
   189             {\
       
   190             RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, \
       
   191                 _PREFIX_INFO( aMsg ), aP1, aP2, aP3 );\
       
   192             }
       
   193 
       
   194     #else//TRACE_INTO_FILE not defined
       
   195 
       
   196         #define INFO( aMsg )\
       
   197             {\
       
   198             RDebug::Print( _PREFIX_INFO( aMsg ) );\
       
   199             }
       
   200         #define INFO_1( aMsg, aP1 )\
       
   201             {\
       
   202             RDebug::Print( _PREFIX_INFO( aMsg ), aP1 );\
       
   203             }
       
   204         #define INFO_2( aMsg, aP1, aP2 )\
       
   205             {\
       
   206             RDebug::Print( _PREFIX_INFO( aMsg ), aP1, aP2 );\
       
   207             }
       
   208         #define INFO_3( aMsg, aP1, aP2, aP3 )\
       
   209             {\
       
   210             RDebug::Print( _PREFIX_INFO( aMsg ), aP1, aP2, aP3 );\
       
   211             }
       
   212 
       
   213     #endif//TRACE_INTO_FILE
       
   214 
       
   215 #else//INFO_TRACE not defined
       
   216 
       
   217     #define INFO( aMsg )
       
   218     #define INFO_1( aMsg, aP1 )
       
   219     #define INFO_2( aMsg, aP1, aP2 )
       
   220     #define INFO_3( aMsg, aP1, aP2, aP3 )
       
   221 
       
   222 #endif//INFO_TRACE
       
   223 
       
   224 //----------------------------------------------------------------------------
       
   225 // Function trace macros
       
   226 //----------------------------------------------------------------------------
       
   227 //
       
   228 #ifdef FUNC_TRACE
       
   229 
       
   230     /**
       
   231     * Function logging definitions.
       
   232     */
       
   233     #ifdef TRACE_INTO_FILE
       
   234 
       
   235         #define FUNC( aMsg, aP1 )\
       
   236             {\
       
   237             TPtrC8 trace( _S8( aMsg ) );\
       
   238             RFileLogger::WriteFormat( KDir, KFile, EFileLoggingModeAppend, trace, aP1 );\
       
   239             }\
       
   240 
       
   241     #else//TRACE_INTO_FILE not defined
       
   242 
       
   243         #define FUNC( aMsg, aP1 )\
       
   244             {\
       
   245             RDebug::Printf( aMsg, aP1 );\
       
   246             }\
       
   247 
       
   248     #endif//TRACE_INTO_FILE
       
   249 
       
   250     /**
       
   251     * Function trace helper class.
       
   252     *
       
   253     * NOTE:
       
   254     * LC -methods cannot be trapped. Therefore if LC -method leaves
       
   255     * END trace is used instead of LEAVE trace.
       
   256     * If you have an idea how to round this problem please tell.
       
   257     */
       
   258     _LIT8( KFuncNameTerminator, "(" );
       
   259     _LIT8( KFuncLeavePatternL, "L" );
       
   260     class TFuncLog
       
   261         {
       
   262         public:
       
   263             static void Cleanup( TAny* aPtr )
       
   264                 {
       
   265                 TFuncLog* self = static_cast< TFuncLog* >( aPtr );
       
   266                 self->iLeft = ETrue;
       
   267                 FUNC( _PREFIX_CHAR("%S-LEAVE"), &self->iFunc ); // Leave detected
       
   268                 }
       
   269             inline TFuncLog( const char* aFunc ) :
       
   270                     iFunc( aFunc ? _S8( aFunc ) : _S8("") ),
       
   271                     iLeft( EFalse ),
       
   272                     iCleanupItem( Cleanup, this ),
       
   273                     iCanLeave( EFalse )
       
   274                 {
       
   275                 TInt pos( iFunc.Find( KFuncNameTerminator ) );
       
   276                 if( pos != KErrNotFound )
       
   277                     {
       
   278                     iFunc.Set( iFunc.Left( pos ) );
       
   279                     iCanLeave = !iFunc.Right(
       
   280                         KFuncLeavePatternL().Length() ).Compare( KFuncLeavePatternL );
       
   281                     if ( iCanLeave )
       
   282                         {
       
   283                         CleanupStack::PushL( iCleanupItem ); // Ignore warnings
       
   284                         }
       
   285                     }
       
   286                 FUNC( _PREFIX_CHAR("%S-START"), &iFunc );
       
   287                 }
       
   288 
       
   289             inline ~TFuncLog()
       
   290                 {
       
   291                 if ( !iLeft )
       
   292                     {
       
   293                     if ( iCanLeave )
       
   294                         {
       
   295                         CleanupStack::Pop( this ); // Pop the cleanup item
       
   296                         }
       
   297                     FUNC( _PREFIX_CHAR("%S-END"), &iFunc ); // Normally finished
       
   298                     }
       
   299                 }
       
   300 
       
   301         private: // Data
       
   302             TPtrC8 iFunc;
       
   303             TBool iLeft;
       
   304             TCleanupItem iCleanupItem;
       
   305             TBool iCanLeave;
       
   306         };
       
   307     #define FUNC_LOG TFuncLog _fl( __PRETTY_FUNCTION__ );
       
   308 
       
   309 #else //FUNC_TRACE not defined
       
   310 
       
   311     #define FUNC_LOG
       
   312 
       
   313 #endif //FUNC_TRACE
       
   314 
       
   315 #ifdef _DEBUG
       
   316 
       
   317     static void Panic( const TDesC8& aFileName, const TInt aLineNum )
       
   318         {
       
   319         TPath name;
       
   320         name.Copy( aFileName );
       
   321         RDebug::Print( _L( "Assertion failed in file=%S, line=%d" ), &name, aLineNum );
       
   322         User::Invariant();
       
   323         }
       
   324 
       
   325     #define ASSERT_ALWAYS_TRACE Panic( _L8(__FILE__), __LINE__ );
       
   326     #define ASSERT_TRACE( _s ) if ( !( _s ) ) { ASSERT_ALWAYS_TRACE; }
       
   327 
       
   328 #else // _DEBUG
       
   329 
       
   330     #define ASSERT_ALWAYS_TRACE
       
   331     #define ASSERT_TRACE( _s )
       
   332 
       
   333 #endif // _DEBUG
       
   334 
       
   335 #endif // TRACE_H
       
   336