radioengine/utils/api/cradioenginelogger.h
changeset 13 46974bebc798
child 28 075425b8d9a4
equal deleted inserted replaced
0:f3d95d9c00ab 13:46974bebc798
       
     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 *
       
    16 */
       
    17 
       
    18 #ifndef C_RADIOENGINELOGGER_H
       
    19 #define C_RADIOENGINELOGGER_H
       
    20 
       
    21 // System includes
       
    22 #include <e32base.h>
       
    23 
       
    24 // User includes
       
    25 #include "mradioenginelogger.h"
       
    26 
       
    27 /**
       
    28  * Main logging macro.
       
    29  * When the macro is defined, logging is on
       
    30  */
       
    31 #ifdef _DEBUG
       
    32     #define LOGGING_ENABLED
       
    33 #endif
       
    34 
       
    35 #ifdef LOGGING_ENABLED
       
    36 
       
    37 /**
       
    38  * Log file path.
       
    39  * If the path points to memory card and the card is not present or is read only,
       
    40  * the log is written to C: drive.
       
    41  */
       
    42 _LIT( KLogFile, "c:\\logs\\radio\\radioenginelog.txt" );
       
    43 
       
    44 /**
       
    45  * Logging buffer length. Should be big enough to hold one line of text
       
    46  */
       
    47 const TInt KLogBufferLength = 512;
       
    48 
       
    49 
       
    50 // ===========================================================================
       
    51 // ===========================================================================
       
    52 //
       
    53 // BEGIN LOGGING MACROS. Use these to write log prints
       
    54 //
       
    55 // ===========================================================================
       
    56 // ===========================================================================
       
    57 
       
    58 // All macros except LOG_METHOD and LOG_METHOD_RET are enclosed in do { }while( 0 );
       
    59 // statements to make them form a complete code block that can be used anywhere.
       
    60 
       
    61 /**
       
    62  * Function entry, exit and leave logging.
       
    63  *
       
    64  *  @code
       
    65  *   LOG_METHOD( "CSomeClass::SomeFunctionL" );
       
    66  *  @endcode
       
    67  */
       
    68 #define LOG_METHOD( func ) TRadioMethodLogger ___methodLogger( _S( func ), _S( "" ) )
       
    69 
       
    70 /**
       
    71  * Function entry, exit and leave logging.
       
    72  * Gets the function name automatically
       
    73  *
       
    74  *  @code
       
    75  *   LOG_METHOD_AUTO;
       
    76  *  @endcode
       
    77  */
       
    78 // Substract 1 from MaxLength() because PtrZ() adds the zero terminator
       
    79 #define LOG_METHOD_AUTO \
       
    80     TPtrC8 __ptr8( (const TUint8*)__PRETTY_FUNCTION__ ); \
       
    81     TBuf<150> __buf; \
       
    82     __buf.Copy( __ptr8.Left( __buf.MaxLength() - 1 ) ); \
       
    83     TRadioMethodLogger ___methodLogger( __buf.PtrZ(), _S( "" ) )
       
    84 
       
    85 /**
       
    86  * Function entry, exit and leave logging. Logs also returned value
       
    87  * NOTE! At the moment return value is only logged in emulator builds.
       
    88  *
       
    89  *  @code
       
    90  *   // Function returns an integer value
       
    91  *   LOG_METHOD_RET( "CSomeClass::SomeFunctionL", "Error: %d" );
       
    92  *
       
    93  *   // Function returns an HBufC pointer
       
    94  *   LOG_METHOD_RET( "CSomeClass::SomeFunctionL", "HBufC: %S" );
       
    95  *  @endcode
       
    96  */
       
    97 #define LOG_METHOD_RET( func,fmt ) TRadioMethodLogger ___methodLogger( _S( func ), _S( fmt ) )
       
    98 
       
    99 /**
       
   100  * Function entry, exit and leave logging. Logs also returned value
       
   101  * Gets the function name automatically
       
   102  * NOTE! At the moment return value is only logged in emulator builds.
       
   103  *
       
   104  *  @code
       
   105  *   // Function returns an integer value
       
   106  *   LOG_METHOD_RET( "CSomeClass::SomeFunctionL", "Error: %d" );
       
   107  *
       
   108  *   // Function returns an HBufC pointer
       
   109  *   LOG_METHOD_RET( "CSomeClass::SomeFunctionL", "HBufC: %S" );
       
   110  *  @endcode
       
   111  */
       
   112 // Substract 1 from MaxLength() because PtrZ() adds the zero terminator
       
   113 #define LOG_METHOD_AUTO_RET( fmt ) \
       
   114     TPtrC8 __ptr8( (const TUint8*)__PRETTY_FUNCTION__ ); \
       
   115     TBuf<150> __buf; \
       
   116     __buf.Copy( __ptr8.Left( __buf.MaxLength() - 1 ) ); \
       
   117     TRadioMethodLogger ___methodLogger( __buf.PtrZ(), _S( fmt ) )
       
   118 
       
   119 /**
       
   120  * Simple assert macro that works like normal asserts. The expression ( expr ) is
       
   121  * expected to be true in which case nothing is done. If the expression is false the
       
   122  * statement ( stmt ) is executed.
       
   123  *
       
   124  * Note! As with normal asserts, do not put a function call as the expression because
       
   125  * it will not be executed when logging is off!
       
   126  *
       
   127  *  @code
       
   128  *   // Log error code if it is not KErrNone
       
   129  *   TRAPD( err, SomeFunctionL() );
       
   130  *   MTV_ASSERT( !err, LOG_FORMAT( "Operation failed. error: %d", err ) );
       
   131  *  @endcode
       
   132  */
       
   133 #define LOG_ASSERT( expr, stmt ) do { if ( !( expr )) { stmt; } }while( 0 )
       
   134 
       
   135 #define DEBUGVAR( var ) var
       
   136 
       
   137 /**
       
   138  * Writes a formatted string to log. Accepts variable number of parameters
       
   139  *
       
   140  *  @code
       
   141  *   LOG_FORMAT( "Integer: %d", 1 );
       
   142  *   LOG_FORMAT( "Integer: %d Desc %S", 1, &desc );
       
   143  *   LOG_FORMAT( "Integer: %d Desc %S hex 0x%X", 1, &desc, 0xC0FFEE );
       
   144  *  @endcode
       
   145  */
       
   146 #define LOG_FORMAT( fmt,args...) do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine()).AddFormat( _L( fmt ),args ).Commit();}while( 0 )
       
   147 
       
   148 /**
       
   149  * Writes current time to log. Example: Time: 13.9.2007 13:23:25.383750
       
   150  */
       
   151 #define LOG_CURRENT_TIME() do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine()).AddTimestamp().Commit();}while( 0 )
       
   152 
       
   153 /**
       
   154  * Writes a time to log. Accepts a TTime as parameter
       
   155  *
       
   156  *  @code
       
   157  *   TTime now;
       
   158  *   now.HomeTime();
       
   159  *   LOG_TIME( now );
       
   160  *  @endcode
       
   161  */
       
   162 #define LOG_TIME( x ) do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine()).AddTimestamp( x ).Commit();}while( 0 )
       
   163 
       
   164 /**
       
   165  * Writes a time to log. Accepts a string and a TTime as parameter
       
   166  *
       
   167  *  @code
       
   168  *   TTime now;
       
   169  *   now.HomeTime();
       
   170  *   LOG_TIME2( "Current time", now );
       
   171  *  @endcode
       
   172  */
       
   173 #define LOG_TIME2( desc, time ) do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine()).Add( desc ).Add( ": " ).Add( time ).Commit();}while( 0 )
       
   174 
       
   175 /**
       
   176  * Clear the log contents
       
   177  */
       
   178 #define LOG_CLEAR() do{MRadioEngineLogger::Logger()->ClearLog();}while( 0 )
       
   179 
       
   180 /**
       
   181  * Construct a log line one item at a time. No newline at end
       
   182  */
       
   183 //#define LOG( x ) do{CRadioEngineLogger::Logger()->Add( x );}while( 0 )
       
   184 
       
   185 /**
       
   186  * Write a newline to log
       
   187  */
       
   188 #define LOG_NEWLINE() do{MRadioEngineLogger::Logger()->Commit();}while( 0 )
       
   189 
       
   190 /**
       
   191  * Macro that behaves like User::LeaveIfError except it logs the leave
       
   192  * The line "TInt __error_code = expr;" makes sure the expression is evaluated only once
       
   193  */
       
   194 #define LEAVEIFERROR( expr )                                                \
       
   195     do {                                                                    \
       
   196         TInt __err = expr;                                                  \
       
   197         if ( __err )                                                        \
       
   198             {                                                               \
       
   199             _LIT( KExpression, #expr );                                     \
       
   200             LOG_FORMAT( "%S leaving with code %d!", &KExpression, __err );  \
       
   201             User::Leave( __err );                                           \
       
   202             }                                                               \
       
   203         } while( 0 )
       
   204 
       
   205 /**
       
   206  * Macro that behaves like User::Leave except it logs the leave
       
   207  * The line "TInt __error_code = expr;" makes sure the expression is evaluated only once
       
   208  */
       
   209 #define LEAVE( expr )                                   \
       
   210     do {                                                \
       
   211         TInt __err = expr;                              \
       
   212         LOG_FORMAT( "Leaving with code %d!", __err );   \
       
   213         User::Leave( __err );                           \
       
   214         } while( 0 )
       
   215 
       
   216 /**
       
   217  * Macro that behaves like TRAP except it logs the possible error
       
   218  */
       
   219 #define TRAP_AND_LOG_ERR( err, expr )                               \
       
   220     TRAP( err, expr );                                              \
       
   221     if ( err )                                                      \
       
   222         {                                                           \
       
   223         _LIT( KExpression, #expr );                                 \
       
   224         LOG_FORMAT( "%S failed with err %d", &KExpression, err );   \
       
   225         }                                                           \
       
   226 
       
   227 /**
       
   228  * Macro that behaves like TRAPD except it logs the possible error
       
   229  */
       
   230 #define TRAPD_AND_LOG_ERR( err, expr )  \
       
   231     TInt err = 0;                       \
       
   232     TRAP_AND_LOG_ERR( err, expr )
       
   233 
       
   234 
       
   235 /**
       
   236  * Macro that behaves like TRAP_IGNORE except it logs the possible error
       
   237  */
       
   238 #define TRAP_IGNORE_AND_LOG_ERR( expr )     \
       
   239     do {                                    \
       
   240         TRAPD_AND_LOG_ERR( __err, expr );     \
       
   241         } while ( 0 )
       
   242 
       
   243 /**
       
   244  * Write a single log line.
       
   245  */
       
   246 #define LOGGER_WRITE( x ) do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine ).Add( _L8( x ) ).Commit();}while( 0 )
       
   247 #define LOG( x ) do{MRadioEngineLogger::Logger()->AddIndent( KMarkerEngine ).Add( x ).Commit();}while( 0 )
       
   248 //#define LOG2( x1, x2 ) do{CRadioEngineLogger::Logger()->AddIndent().Add( x1 ).Add( x2 ).Commit();}while( 0 )
       
   249 //#define LOG3( x1, x2, x3 ) do{CRadioEngineLogger::Logger()->AddIndent().Add( x1 ).Add( x2 ).Add( x3 ).Commit();}while( 0 )
       
   250 //#define LOG4( x1, x2, x3, x4 ) do{CRadioEngineLogger::Logger()->AddIndent().Add( x1 ).Add( x2 ).Add( x3 ).Add( x4 ).Commit();}while( 0 )
       
   251 //#define LOG5( x1, x2, x3, x4, x5 ) do{CRadioEngineLogger::Logger()->AddIndent().Add( x1 ).Add( x2 ).Add( x3 ).Add( x4 ).Add( x5 ).Commit();}while( 0 )
       
   252 
       
   253 
       
   254 // ===========================================================================
       
   255 // ===========================================================================
       
   256 //
       
   257 // END LOGGING MACROS. Do not use anything below this line directly!
       
   258 //
       
   259 // ===========================================================================
       
   260 // ===========================================================================
       
   261 
       
   262 
       
   263 #include <f32file.h>
       
   264 #include <e32debug.h>
       
   265 
       
   266 class TRadioMethodLogger;
       
   267 
       
   268 const TInt KTimestampLength = 40;
       
   269 typedef TBuf8<KTimestampLength> TTimestamp;
       
   270 
       
   271 /**
       
   272  * Logger for writing log to a file and RDebug
       
   273  *
       
   274  *  @lib mtvcommon.lib
       
   275  *  @since Live TV UI v1.0
       
   276  */
       
   277 NONSHARABLE_CLASS( CRadioEngineLogger ) : public CBase
       
   278                                         , public MRadioEngineLogger
       
   279                                         , public TDes16Overflow
       
   280     {
       
   281     friend class CRadioEngineTls;
       
   282 public:
       
   283 
       
   284 // from base class MRadioEngineLogger
       
   285 
       
   286     MRadioEngineLogger& ClearLog();
       
   287     MRadioEngineLogger& Add( const TDesC8& aMsg );
       
   288     MRadioEngineLogger& Add( const TDesC& aMsg );
       
   289     MRadioEngineLogger& Add( TInt aInt );
       
   290     MRadioEngineLogger& Add( const TReal& aReal );
       
   291     MRadioEngineLogger& Add( const char* aText );
       
   292     MRadioEngineLogger& Add( const TAny* aPtr );
       
   293     MRadioEngineLogger& Add( const TTime& aTime );
       
   294     MRadioEngineLogger& AddTimestamp();
       
   295     MRadioEngineLogger& AddFormat( TRefByValue<const TDesC> aFmt, ... );
       
   296     MRadioEngineLogger& AddIndent( const TDesC& aMarker );
       
   297     MRadioEngineLogger& AddIndentClear( const TDesC& aMarker );
       
   298     MRadioEngineLogger& IncIndent();
       
   299     MRadioEngineLogger& DecIndent();
       
   300     MRadioEngineLogger& Commit( TBool aNewLine = ETrue );
       
   301 
       
   302 // from base class TDes16Overflow
       
   303 
       
   304     /**
       
   305      * Handles the overflow from AppendFormatList()
       
   306      *
       
   307      * @since Live TV UI v1.0
       
   308      * @param aDes The 16-bit modifiable descriptor whose overflow results in the
       
   309      *             call to this overflow handler.
       
   310      */
       
   311     void Overflow( TDes16 &aDes );
       
   312 
       
   313 private:
       
   314 
       
   315     static CRadioEngineLogger* NewL( RFs& aFs );
       
   316 
       
   317     ~CRadioEngineLogger();
       
   318 
       
   319     CRadioEngineLogger( RFs& aFs );
       
   320 
       
   321     void ConstructL();
       
   322 
       
   323     /**
       
   324      * Returns the amount of characters that still fit in the buffer
       
   325      *
       
   326      * @since Live TV UI v1.0
       
   327      * @return Amount of space available
       
   328      */
       
   329     TInt Available() const;
       
   330 
       
   331     /**
       
   332      * Templated function to add either 8-bit or 16-bit descriptor to buffer
       
   333      *
       
   334      * @since Live TV UI v1.0
       
   335      * @param aDesc Descriptor to add
       
   336      */
       
   337     template<class PTR, class DESC>
       
   338     void AddDesC( const DESC& aDesc );
       
   339 
       
   340     /**
       
   341      * Parses the timestamp from the given TTime
       
   342      *
       
   343      * @since Live TV UI v1.0
       
   344      * @param aTime Time to parse
       
   345      * @param aTimestamp On return, the parsed timestamp
       
   346      */
       
   347     void ParseTimestamp( const TTime& aTime, TTimestamp& aTimestamp );
       
   348 
       
   349 private: // data
       
   350 
       
   351     /**
       
   352      * Reference to file server session.
       
   353      */
       
   354     RFs&    iFs;
       
   355 
       
   356     /**
       
   357      * File handle
       
   358      */
       
   359     RFile   iFile;
       
   360 
       
   361     /**
       
   362      * Name of the log file.
       
   363      * Own.
       
   364      */
       
   365     HBufC*  iFileName;
       
   366 
       
   367     /**
       
   368      * Logging buffer. holds one log line at a time
       
   369      */
       
   370     TBuf8<KLogBufferLength> iBuf8;
       
   371 
       
   372     /**
       
   373      * Indentation
       
   374      */
       
   375     TInt    iIndent;
       
   376 
       
   377     /**
       
   378      * Flag to indicate whether or not timestamp has already been written to the beginning of line
       
   379      */
       
   380     TBool   iTimeStampWritten;
       
   381 
       
   382     };
       
   383 
       
   384 /**
       
   385  * Helper class for method entry, exit and leave logging.
       
   386  * Used as automatic variable declared by LOG_METHOD() macro.
       
   387  */
       
   388 NONSHARABLE_CLASS( TRadioMethodLogger )
       
   389     {
       
   390 public:
       
   391 
       
   392     /**
       
   393      * Constructor. Writes method entry log.
       
   394      */
       
   395     IMPORT_C TRadioMethodLogger( const TText* aFunc, const TText* aRetFormat );
       
   396 
       
   397     /**
       
   398      * Destructor. Writes method exit log.
       
   399      */
       
   400     IMPORT_C ~TRadioMethodLogger();
       
   401 
       
   402 private: // data
       
   403 
       
   404     /**
       
   405      * Pointer descriptor to function signature that is to be logged.
       
   406      */
       
   407     TPtrC iFunc;
       
   408 
       
   409     /**
       
   410      * Formatting string used to print the function return value
       
   411      */
       
   412     TPtrC iRetFormat;
       
   413 
       
   414     };
       
   415 
       
   416 #else // LOGGING_ENABLED
       
   417 
       
   418 // Empty macro definitions for non logging build.
       
   419 
       
   420 #define LOG_METHOD_AUTO
       
   421 #define LOG_METHOD_AUTO_RET( fmt )
       
   422 
       
   423 #define LOG_METHOD( func )
       
   424 #define LOG_METHOD_RET( func,fmt )
       
   425 #define LOG_FORMAT( fmt,args...)
       
   426 #define LOG_ASSERT( expr, stmt )
       
   427 #define DEBUGVAR( var )
       
   428 
       
   429 #define LOG_CURRENT_TIME()
       
   430 #define LOG_TIME( x )
       
   431 #define LOG_TIME2( desc, time )
       
   432 
       
   433 #define LOG_CLEAR()
       
   434 #define LOG( x )
       
   435 #define LOG_NEWLINE()
       
   436 #define LOG1( x )
       
   437 #define LOG2( x1, x2 )
       
   438 #define LOG3( x1, x2, x3 )
       
   439 #define LOG4( x1, x2, x3, x4 )
       
   440 #define LOG5( x1, x2, x3, x4, x5 )
       
   441 
       
   442 // Default implementations for leave macros.
       
   443 // Note! These can NOT be empty macros because the errors would not be checked when logging is off
       
   444 #define LEAVEIFERROR( expr )    User::LeaveIfError( expr )
       
   445 #define LEAVE( expr )           User::Leave( expr )
       
   446 
       
   447 // Default implementation for trap macros
       
   448 // Note! As with leave macros, these can not be empty
       
   449 #define TRAP_AND_LOG_ERR( err,stmt )      TRAP( err, stmt )
       
   450 #define TRAPD_AND_LOG_ERR( err,stmt )     TRAPD( err, stmt )
       
   451 #define TRAP_IGNORE_AND_LOG_ERR( stmt )   TRAP_IGNORE( stmt )
       
   452 
       
   453 #define LOGGER_WRITE( x )
       
   454 
       
   455 // ===========================================================================
       
   456 // Dummy classes to keep the exports unchanged when logging is turned off
       
   457 // These can not be used.
       
   458 // ===========================================================================
       
   459 class RFs;
       
   460 NONSHARABLE_CLASS( CRadioEngineLogger ) : public CBase
       
   461     {
       
   462     friend class CRadioEngineImp;
       
   463 private:
       
   464     static void InitializeL( RFs& aFs );
       
   465     static void Release();
       
   466     };
       
   467 
       
   468 NONSHARABLE_CLASS( TRadioMethodLogger )
       
   469     {
       
   470 public:
       
   471     IMPORT_C TRadioMethodLogger( const TText* /*aFunc*/, const TText* /*aRetFormat*/ );
       
   472     IMPORT_C ~TRadioMethodLogger();
       
   473     };
       
   474 
       
   475 #endif // LOGGING_ENABLED
       
   476 
       
   477 #endif // C_RADIOENGINELOGGER_H