radioengine/utils/src/cradioenginelogger.cpp
changeset 13 46974bebc798
child 14 63aabac4416d
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 #include "cradioenginelogger.h"
       
    19 #include "cradioenginetls.h"
       
    20 
       
    21 // ======== MEMBER FUNCTIONS ========
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // Return the logger instance.
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 EXPORT_C MRadioEngineLogger* MRadioEngineLogger::Logger()
       
    28     {
       
    29     return CRadioEngineTls::Logger();
       
    30     }
       
    31 
       
    32 #ifdef LOGGING_ENABLED
       
    33 
       
    34 #include <bautils.h>
       
    35 
       
    36 // Indentation
       
    37 _LIT( KLogSpace,    "                                " ); // 32 empty spaces
       
    38 const TInt KMaxIndent = 32;
       
    39 
       
    40 /**
       
    41  * Timestamp formatter string
       
    42  */
       
    43 _LIT8( KTimestampFormat, "%02d.%02d.%4d %02d:%02d:%02d.%05d" );
       
    44 
       
    45 /**
       
    46  * Timestamp separator string
       
    47  */
       
    48 _LIT8( KTimestampSeparator, " " );
       
    49 
       
    50 // Memory card path. Has to contain directory ( \\) or BaflUtils::PathExists fails
       
    51 _LIT( KMemoryCard, "E:\\" );
       
    52 
       
    53 _LIT( KLogEnter,     "\\ %S" );
       
    54 _LIT( KLogExit,      "/ %S" );
       
    55 _LIT8( KLogLine,     "| " );
       
    56 _LIT( KLogLeave,     "#+ %S: LEAVE!" );
       
    57 _LIT( KLogExitRet,   "/ %S, Returning " );
       
    58 
       
    59 _LIT8( KNewLine, "\r\n" );
       
    60 _LIT8( KHexFormat, "ptr: 0x%X" );
       
    61 
       
    62 
       
    63 // ======== MEMBER FUNCTIONS ========
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CRadioEngineLogger* CRadioEngineLogger::NewL( RFs& aFs )
       
    70     {
       
    71     CRadioEngineLogger* self = new (ELeave) CRadioEngineLogger( aFs );
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------------------------
       
    79 //
       
    80 // ---------------------------------------------------------------------------
       
    81 //
       
    82 CRadioEngineLogger::CRadioEngineLogger( RFs& aFs )
       
    83     : iFs( aFs )
       
    84     {
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CRadioEngineLogger::ConstructL()
       
    92     {
       
    93     if ( KLogFile().FindF( KMemoryCard ) == 0 )
       
    94         {
       
    95         TBool readOnly = EFalse;
       
    96 
       
    97         // Returns KErrPathNotFound if memory card is not present
       
    98         TInt err = BaflUtils::DiskIsReadOnly( iFs, KMemoryCard, readOnly );
       
    99         if ( err || readOnly )
       
   100             {
       
   101             // Log file path points to the memory card and the card is not
       
   102             // present or is read only => change the drive to C:
       
   103             TFileName fileName( _L( "C:" ) );
       
   104             TParsePtrC parse( KLogFile );
       
   105             fileName.Append( parse.Path() );
       
   106             fileName.Append( parse.NameAndExt() );
       
   107             iFileName = fileName.AllocL();
       
   108             }
       
   109         }
       
   110 
       
   111     if ( !iFileName )
       
   112         {
       
   113         iFileName = KLogFile().AllocL();
       
   114         }
       
   115 
       
   116     BaflUtils::EnsurePathExistsL( iFs, *iFileName );
       
   117 
       
   118     User::LeaveIfError( Dll::SetTls( this ) );
       
   119     }
       
   120 
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 CRadioEngineLogger::~CRadioEngineLogger()
       
   126     {
       
   127     iFile.Close();
       
   128     delete iFileName;
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // Clear the log by deleting the logfile.
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 MRadioEngineLogger& CRadioEngineLogger::ClearLog()
       
   136     {
       
   137     if ( BaflUtils::FileExists( iFs, *iFileName ) )
       
   138         {
       
   139         // Returned error code ignored intentionally because there is no way
       
   140         // to report an error
       
   141         BaflUtils::DeleteFile( iFs, *iFileName );
       
   142         }
       
   143 
       
   144     return *this;
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Adds a 8-bit string to log line
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 MRadioEngineLogger& CRadioEngineLogger::Add( const TDesC8& aMsg )
       
   152     {
       
   153     AddDesC<TPtrC8, TDesC8>( aMsg );
       
   154     return *this;
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Adds a 16-bit string to log line
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 MRadioEngineLogger& CRadioEngineLogger::Add( const TDesC& aMsg )
       
   162     {
       
   163     // Note! No character conversion is performed while going from 16-bit
       
   164     // descriptor to 8-bit descriptor. This is considered acceptable in a
       
   165     // debugging utility
       
   166     AddDesC<TPtrC, TDesC>( aMsg );
       
   167     return *this;
       
   168     }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // Adds a TInt to log line
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 MRadioEngineLogger& CRadioEngineLogger::Add( TInt aInt )
       
   175     {
       
   176     TBuf8<20> buf8;
       
   177     buf8.Num( aInt );
       
   178     Add( buf8 );
       
   179     return *this;
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Adds a TReal to log line
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 MRadioEngineLogger& CRadioEngineLogger::Add( const TReal& aReal )
       
   187     {
       
   188     TBuf8<20> buf8;
       
   189     buf8.Format( _L8( "%f" ), aReal );
       
   190     Add( buf8 );
       
   191     return *this;
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // Adds a c-style string to log line
       
   196 // ---------------------------------------------------------------------------
       
   197 //
       
   198 MRadioEngineLogger& CRadioEngineLogger::Add( const char* aText )
       
   199     {
       
   200     Add( TPtrC8( reinterpret_cast<const TText8*>( aText ) ) );
       
   201     return *this;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // Adds a pointer value to log line
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 MRadioEngineLogger& CRadioEngineLogger::Add( const TAny* aPtr )
       
   209     {
       
   210     TBuf8<20> buf8;
       
   211     buf8.Format( KHexFormat, aPtr );
       
   212     Add( buf8 );
       
   213     return *this;
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // Adds a timestamp of current time to log line
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 MRadioEngineLogger& CRadioEngineLogger::Add( const TTime& aTime )
       
   221     {
       
   222     TTimestamp timestamp;
       
   223     ParseTimestamp( aTime, timestamp );
       
   224     Add( timestamp );
       
   225     return *this;
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // Adds a timestamp of current time to log line
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 MRadioEngineLogger& CRadioEngineLogger::AddTimestamp()
       
   233     {
       
   234     TTime now;
       
   235     now.HomeTime();
       
   236     return Add( now );
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // Adds a formatted string to log line
       
   241 // ---------------------------------------------------------------------------
       
   242 //
       
   243 MRadioEngineLogger& CRadioEngineLogger::AddFormat( TRefByValue<const TDesC> aFmt, ... )
       
   244     {
       
   245     VA_LIST list;
       
   246     VA_START( list, aFmt );
       
   247 
       
   248     TBuf<KLogBufferLength> buf16;
       
   249 
       
   250     // Calls Overflow() if it doesn't fit
       
   251     buf16.AppendFormatList( aFmt, list, this );
       
   252 
       
   253     VA_END( list );
       
   254 
       
   255     Add( buf16 );
       
   256     return *this;
       
   257     }
       
   258 
       
   259 // ---------------------------------------------------------------------------
       
   260 // Adds the line indentation with line prefix to log line
       
   261 // ---------------------------------------------------------------------------
       
   262 //
       
   263 MRadioEngineLogger& CRadioEngineLogger::AddIndent( const TDesC& aMarker )
       
   264     {
       
   265     Add( aMarker );
       
   266     Add( KLogSpace().Mid( 0, iIndent ) );
       
   267     Add( KLogLine() );
       
   268     return *this;
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // Adds the line indentation to log line
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 MRadioEngineLogger& CRadioEngineLogger::AddIndentClear( const TDesC& aMarker )
       
   276     {
       
   277     Add( aMarker );
       
   278     Add( KLogSpace().Mid( 0, iIndent ) );
       
   279     return *this;
       
   280     }
       
   281 
       
   282 // ---------------------------------------------------------------------------
       
   283 // From TDes16Overflow
       
   284 // Handles the overflow from AppendFormatList()
       
   285 // ---------------------------------------------------------------------------
       
   286 //
       
   287 void CRadioEngineLogger::Overflow( TDes16& /*aDes*/ )
       
   288     {
       
   289     // aDes contains the part that did fit in the descriptor, but the part that
       
   290     // didn't is lost. Modifying the descriptor here would modify it in AddFormat(),
       
   291     // but since it gets logged there we only need to add a message about the overflow.
       
   292     Add( _L( "FORMAT OVERFLOW! " ) );
       
   293     }
       
   294 
       
   295 // ---------------------------------------------------------------------------
       
   296 // Increment indentation
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 MRadioEngineLogger& CRadioEngineLogger::IncIndent()
       
   300     {
       
   301     if ( ++iIndent > KMaxIndent )
       
   302         {
       
   303         iIndent = KMaxIndent;
       
   304         }
       
   305 
       
   306     return *this;
       
   307     }
       
   308 
       
   309 // ---------------------------------------------------------------------------
       
   310 // Decrement indentation
       
   311 // ---------------------------------------------------------------------------
       
   312 //
       
   313 MRadioEngineLogger& CRadioEngineLogger::DecIndent()
       
   314     {
       
   315     if ( --iIndent < 0 )
       
   316         {
       
   317         iIndent = 0;
       
   318         }
       
   319 
       
   320     return *this;
       
   321     }
       
   322 
       
   323 // ---------------------------------------------------------------------------
       
   324 // Commits the log line to file and RDebug and resets internal buffer
       
   325 // ---------------------------------------------------------------------------
       
   326 //
       
   327 MRadioEngineLogger& CRadioEngineLogger::Commit( TBool aNewLine )
       
   328     {
       
   329     // Write log buffer to RDebug
       
   330     RDebug::RawPrint( iBuf8 );
       
   331 
       
   332     // Write log buffer to file
       
   333     TInt err = iFile.Open( iFs, *iFileName, EFileWrite );
       
   334     if ( err )
       
   335         {
       
   336         err = iFile.Create( iFs, *iFileName, EFileWrite );
       
   337         }
       
   338 
       
   339     if ( !err )
       
   340         {
       
   341         TInt unused = 0;
       
   342         if ( iFile.Seek( ESeekEnd, unused ) == KErrNone )
       
   343             {
       
   344             if ( !iTimeStampWritten )
       
   345                 {
       
   346                 // First print a timestamp to log
       
   347                 TTimestamp timestamp;
       
   348                 TTime now;
       
   349                 now.HomeTime();
       
   350                 ParseTimestamp( now, timestamp );
       
   351                 iFile.Write( timestamp );
       
   352                 iFile.Write( KTimestampSeparator );
       
   353                 iTimeStampWritten = ETrue;
       
   354                 }
       
   355 
       
   356             iFile.Write( iBuf8 );
       
   357 
       
   358             if ( aNewLine )
       
   359                 {
       
   360                 iFile.Write( KNewLine );
       
   361                 iTimeStampWritten = EFalse;
       
   362                 }
       
   363             }
       
   364 
       
   365         iFile.Close();
       
   366         }
       
   367 
       
   368     iBuf8.Zero();
       
   369     return *this;
       
   370     }
       
   371 
       
   372 // ---------------------------------------------------------------------------
       
   373 // Returns the amount of characters that still fit in the buffer
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 TInt CRadioEngineLogger::Available() const
       
   377     {
       
   378     return iBuf8.MaxLength() - iBuf8.Length();
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // Templated function to add either 8-bit or 16-bit descriptor to buffer
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 template<class PTR, class DESC>
       
   386 void CRadioEngineLogger::AddDesC( const DESC& aDesc )
       
   387     {
       
   388     PTR ptr( aDesc );
       
   389     while ( ptr.Length() > Available() )
       
   390         {
       
   391         PTR writePtr = ptr.Left( Available() );
       
   392         iBuf8.Append( writePtr );
       
   393 
       
   394         ptr.Set( ptr.Mid( writePtr.Length() ) );
       
   395         Commit( EFalse );
       
   396         }
       
   397 
       
   398     iBuf8.Append( ptr );
       
   399     }
       
   400 
       
   401 // ---------------------------------------------------------------------------
       
   402 // Parses the timestamp from the given TTime
       
   403 // ---------------------------------------------------------------------------
       
   404 //
       
   405 void CRadioEngineLogger::ParseTimestamp( const TTime& aTime, TTimestamp& aTimestamp )
       
   406     {
       
   407     TDateTime dateTime = aTime.DateTime();
       
   408     aTimestamp.Zero();
       
   409     aTimestamp.Format( KTimestampFormat, dateTime.Day() + 1,
       
   410                                          dateTime.Month() + 1,
       
   411                                          dateTime.Year(),
       
   412                                          dateTime.Hour(),
       
   413                                          dateTime.Minute(),
       
   414                                          dateTime.Second(),
       
   415                                          dateTime.MicroSecond() );
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------------------------
       
   419 // Constructor. Log method entry
       
   420 // ---------------------------------------------------------------------------
       
   421 //
       
   422 EXPORT_C TRadioMethodLogger::TRadioMethodLogger( const TText* aFunc,
       
   423                                                  const TText* aRetFormat )
       
   424     : iFunc( aFunc )
       
   425     , iRetFormat( aRetFormat )
       
   426     {
       
   427     MRadioEngineLogger* logger = MRadioEngineLogger::Logger();
       
   428     logger->AddIndentClear( KMarkerEngine ).AddFormat( KLogEnter, &iFunc ).Commit();
       
   429     logger->IncIndent();
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // Destructor. Log method exit
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 EXPORT_C TRadioMethodLogger::~TRadioMethodLogger()
       
   437     {
       
   438     MRadioEngineLogger::Logger()->DecIndent();
       
   439 
       
   440     if ( std::uncaught_exception() ) // Leave is an exception
       
   441         {
       
   442         // The function exited with a leave
       
   443         MRadioEngineLogger::Logger()->AddIndentClear( KMarkerEngine ).AddFormat( KLogLeave, &iFunc ).Commit();
       
   444         }
       
   445     else
       
   446         {
       
   447         // The function exited normally
       
   448         if ( iRetFormat.Length() == 0 )
       
   449             {
       
   450             MRadioEngineLogger::Logger()->AddIndentClear( KMarkerEngine ).AddFormat( KLogExit, &iFunc ).Commit();
       
   451             }
       
   452         else
       
   453             {
       
   454             TBuf<100> format( KLogExitRet );
       
   455             format.Append( iRetFormat );
       
   456             #if defined( __WINS__ )
       
   457 
       
   458                 TInt32 retVal = 0;
       
   459                 _asm( mov retVal, ebx );
       
   460                 MRadioEngineLogger::Logger()->AddIndentClear( KMarkerEngine ).AddFormat( format, &iFunc, retVal ).Commit();
       
   461 
       
   462             #else
       
   463 
       
   464                 MRadioEngineLogger::Logger()->AddIndentClear( KMarkerEngine ).AddFormat( KLogExit, &iFunc ).Commit();
       
   465 
       
   466             #endif
       
   467             }
       
   468         }
       
   469     }
       
   470 
       
   471 #else // #ifdef LOGGING_ENABLED
       
   472 
       
   473 // ===========================================================================
       
   474 // Dummy function definitions to keep the exports unchanged when logging is turned off
       
   475 // These can not be used.
       
   476 // ===========================================================================
       
   477 EXPORT_C TRadioMethodLogger::TRadioMethodLogger( const TText*, const TText* ) {}
       
   478 EXPORT_C TRadioMethodLogger::~TRadioMethodLogger() {}
       
   479 
       
   480 #endif // #ifdef LOGGING_ENABLED