sdkcreationmw/sdkexamples/cppexamples/S60Ex/helperfunctions/logfile.cpp
changeset 0 b26acd06ea60
equal deleted inserted replaced
-1:000000000000 0:b26acd06ea60
       
     1 /*
       
     2 * Copyright (c) 2004, 2006 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 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <hal.h>
       
    22 #include <charconv.h>
       
    23 
       
    24 #include "logfile.h"
       
    25 #include "logfile.pan"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ==============================
       
    28 
       
    29 // ----------------------------------------------------------------------------
       
    30 // CLogFile::NewL()
       
    31 // Two-phased constructor.
       
    32 // Standard Symbian OS construction sequence.
       
    33 // ----------------------------------------------------------------------------
       
    34 //
       
    35 CLogFile* CLogFile::NewL( const TDesC& aFileName, TBool aInitialiseLog )
       
    36     {
       
    37     CLogFile* self = NewLC( aFileName, aInitialiseLog );
       
    38     CleanupStack::Pop();
       
    39     return( self );
       
    40     }
       
    41 
       
    42 // ----------------------------------------------------------------------------
       
    43 // CLogFile::NewLC()
       
    44 // Two-phased constructor.
       
    45 // Standard Symbian OS construction sequence.
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 CLogFile* CLogFile::NewLC( const TDesC& aFileName, TBool aInitialiseLog )
       
    49     {
       
    50     CLogFile* self = new ( ELeave ) CLogFile();
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( aFileName, aInitialiseLog );
       
    53     return( self );
       
    54     }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // CLogFile::CLogFile()
       
    58 // Constructor.
       
    59 // ----------------------------------------------------------------------------
       
    60 //
       
    61 CLogFile::CLogFile()
       
    62     {
       
    63     // No implementation required
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // CLogFile::CLogFile()
       
    68 // Destructor.
       
    69 // ----------------------------------------------------------------------------
       
    70 //
       
    71 CLogFile::~CLogFile()
       
    72     {
       
    73     iLogFile.Flush();
       
    74     iLogFile.Close();
       
    75     iSession.Close();
       
    76 
       
    77     delete iCharacterConverter;
       
    78     iCharacterConverter = NULL;
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // CLogFile::ConstructL()
       
    83 // Symbian 2nd phase constructor can leave.
       
    84 // ----------------------------------------------------------------------------
       
    85 //
       
    86 void CLogFile::ConstructL( const TDesC& aFileName, TBool aInitialiseLog )
       
    87     {
       
    88     TInt period;
       
    89     User::LeaveIfError( HAL::Get( HALData::ESystemTickPeriod, period ) );
       
    90 
       
    91     iLogMillisecsPerTick = period / 1000;
       
    92 
       
    93     if ( iLogMillisecsPerTick == 0 )
       
    94         {
       
    95         iLogMillisecsPerTick = 1;
       
    96         }
       
    97 
       
    98 
       
    99     iSession.Connect();
       
   100 
       
   101     if ( aInitialiseLog )
       
   102         {
       
   103         User::LeaveIfError( iLogFile.Replace( iSession,
       
   104                                               aFileName,
       
   105                                               EFileShareExclusive ) );
       
   106         }
       
   107     else
       
   108         {
       
   109         TInt err = iLogFile.Open( iSession, aFileName,
       
   110                                   EFileShareExclusive | EFileWrite );
       
   111 
       
   112         switch ( err )
       
   113             {
       
   114             case KErrNone: // Opened ok, so seek to end of file
       
   115                 {
       
   116                 TInt position = 0;
       
   117                 User::LeaveIfError( iLogFile.Seek( ESeekEnd, position ) );
       
   118                 }
       
   119                 break;
       
   120 
       
   121             case KErrNotFound: // File doesn't exist, so create it
       
   122                 User::LeaveIfError( iLogFile.Create( iSession,
       
   123                                                      aFileName,
       
   124                                                      EFileShareExclusive |
       
   125                                                      EFileWrite ) );
       
   126                 break;
       
   127 
       
   128             default: // Unexepected error
       
   129                 User::Leave( err );
       
   130                 break;
       
   131             }
       
   132         }
       
   133 
       
   134     // Create character converter
       
   135     iCharacterConverter = CCnvCharacterSetConverter::NewL();
       
   136     CCnvCharacterSetConverter::TAvailability iConverterAvailability;
       
   137     iConverterAvailability = iCharacterConverter->PrepareToConvertToOrFromL(
       
   138                              KCharacterSetIdentifierAscii, iSession );
       
   139     }
       
   140 
       
   141 // ----------------------------------------------------------------------------
       
   142 // CLogFile::LogTime()
       
   143 // Append a timestamp to the log file.
       
   144 // ----------------------------------------------------------------------------
       
   145 //
       
   146 void CLogFile::LogTime()
       
   147     {
       
   148     StartWrite();
       
   149     LogTimeInternal();
       
   150     EndWrite();
       
   151     }
       
   152 
       
   153 // ----------------------------------------------------------------------------
       
   154 // CLogFile::Log()
       
   155 // Append text to the log file.
       
   156 // ----------------------------------------------------------------------------
       
   157 //
       
   158 void CLogFile::Log( const TDesC8& aText )
       
   159     {
       
   160     StartWrite();
       
   161     LogTextInternal( aText );
       
   162     EndWrite();
       
   163     }
       
   164 
       
   165 // ----------------------------------------------------------------------------
       
   166 // CLogFile::Log()
       
   167 // Append text to the log file.
       
   168 // ----------------------------------------------------------------------------
       
   169 //
       
   170 void CLogFile::Log( const TDesC& aText )
       
   171     {
       
   172     StartWrite();
       
   173 
       
   174     for ( TInt i = 0; i < aText.Length(); i++ )
       
   175         {
       
   176         if ( aText.Mid(i).Find( KCrLf ) == 0 )
       
   177             {
       
   178             LogNewline();
       
   179             i++;
       
   180             }
       
   181         else if ( iConverterAvailability ==
       
   182                   CCnvCharacterSetConverter::EAvailable )
       
   183             {
       
   184             // Convert character from unicode
       
   185             TBuf<1> unicodeBuffer;
       
   186             TBuf8<10> asciiBuffer;
       
   187 
       
   188             unicodeBuffer.Append(aText[i]);
       
   189             TInt status = iCharacterConverter->ConvertFromUnicode(
       
   190                                                asciiBuffer, unicodeBuffer );
       
   191 
       
   192             if ( status >= 0 )
       
   193                 {
       
   194                 LogTextInternal( asciiBuffer );
       
   195                 }
       
   196             }
       
   197         else // character converter not available
       
   198             {
       
   199                 TBuf8<1> asciiBuffer;
       
   200                 asciiBuffer.Append( static_cast<TUint8>( aText[i] ) );
       
   201                 LogTextInternal( asciiBuffer );
       
   202             }
       
   203         }
       
   204 
       
   205     EndWrite();
       
   206     }
       
   207 
       
   208 // ----------------------------------------------------------------------------
       
   209 // CLogFile::Log()
       
   210 // Append the byte to the log file.
       
   211 // ----------------------------------------------------------------------------
       
   212 //
       
   213 void CLogFile::Log( TUint8 aByte )
       
   214     {
       
   215     StartWrite();
       
   216     LogByteInternal( aByte );
       
   217     EndWrite();
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // CLogFile::Log()
       
   222 // Append the integer to the log file.
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 void CLogFile::Log( TUint aNumber )
       
   226     {
       
   227     StartWrite();
       
   228     LogIntInternal( aNumber );
       
   229     EndWrite();
       
   230     }
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // CLogFile::LogBytes()
       
   234 // Append the bytes to the log file.
       
   235 // ----------------------------------------------------------------------------
       
   236 //
       
   237 void CLogFile::LogBytes( const TDesC8& aBuffer )
       
   238     {
       
   239     StartWrite();
       
   240 
       
   241     for ( TInt i = 0; i < aBuffer.Length(); i++ )
       
   242         {
       
   243         LogByteInternal( aBuffer[i] );
       
   244         }
       
   245 
       
   246     EndWrite();
       
   247     }
       
   248 
       
   249 // ----------------------------------------------------------------------------
       
   250 // CLogFile::LogTimeInternal()
       
   251 // Internal function to log time.
       
   252 // ----------------------------------------------------------------------------
       
   253 //
       
   254 void CLogFile::LogTimeInternal()
       
   255     {
       
   256     TBuf8<50> text;
       
   257     TInt timeInMillisecs = User::TickCount() * iLogMillisecsPerTick;
       
   258     TInt secs = timeInMillisecs / 1000;
       
   259     TInt millisecs = timeInMillisecs % 1000;
       
   260     text.Num( secs );
       
   261     text.Append( '.' );
       
   262     Write( text );
       
   263     text.Num( millisecs );
       
   264 
       
   265     while ( text.Length() < KNumberOfDecimalPlaces )
       
   266         {
       
   267         text.Insert( 0, _L8( "0" ) );
       
   268         }
       
   269 
       
   270     text.Append( '-' );
       
   271     Write( text );
       
   272     }
       
   273 
       
   274 // ----------------------------------------------------------------------------
       
   275 // CLogFile::LogTextInternal()
       
   276 // Internal function to log text.
       
   277 // ----------------------------------------------------------------------------
       
   278 //
       
   279 void CLogFile::LogTextInternal( const TDesC8& aText )
       
   280     {
       
   281     TPtrC8 tail( aText.Ptr(), aText.Length() );
       
   282 
       
   283     TInt newLinePosition = tail.Find( KCrLf8 );
       
   284     while ( newLinePosition != KErrNotFound )
       
   285         {
       
   286         if ( newLinePosition > 0 )
       
   287             {
       
   288             Write( tail.Left( newLinePosition ) );
       
   289             tail.Set( aText.Ptr() + newLinePosition, tail.Length() -
       
   290                                                      newLinePosition );
       
   291             }
       
   292         LogNewline();
       
   293         tail.Set( aText.Ptr() + KCrLf8.iTypeLength, tail.Length() -
       
   294                                                     KCrLf8.iTypeLength );
       
   295 
       
   296         newLinePosition = tail.Find( KCrLf8 );
       
   297         }
       
   298 
       
   299     //  No more newlines left so print remainder
       
   300     Write( tail );
       
   301     }
       
   302 
       
   303 // ----------------------------------------------------------------------------
       
   304 // CLogFile::LogByteInternal()
       
   305 // internal function to log a byte.
       
   306 // ----------------------------------------------------------------------------
       
   307 //
       
   308 void CLogFile::LogByteInternal( TUint8 aByte )
       
   309     {
       
   310     if ( ( aByte >= KAsciiStart ) && ( aByte < KAsciiEnd ) )
       
   311         {
       
   312 
       
   313         // Display as ASCII char
       
   314         TBuf8<1> str;
       
   315         str.Append( aByte );
       
   316         Write( str );
       
   317         }
       
   318     else
       
   319         {
       
   320         // Display as hex number
       
   321         TBuf8<4> str;
       
   322         str.Append( KHexCharLeft );
       
   323         str.AppendNum( ( TUint )aByte, EHex );
       
   324         str.Append( KHexCharRight );
       
   325         Write( str );
       
   326         }
       
   327     }
       
   328 
       
   329 // ----------------------------------------------------------------------------
       
   330 // CLogFile::LogIntInternal()
       
   331 // Internal function to log an integer.
       
   332 // ----------------------------------------------------------------------------
       
   333 //
       
   334 void CLogFile::LogIntInternal( TUint aNumber )
       
   335     {
       
   336     // Display as ASCII char
       
   337     TBuf8<20> str;
       
   338     str.Append( KHexCharLeft );
       
   339     str.AppendNum( aNumber, EHex );
       
   340     str.Append( KHexCharRight );
       
   341     Write( str );
       
   342     }
       
   343 
       
   344 // ----------------------------------------------------------------------------
       
   345 // CLogFile::LogNewline()
       
   346 // Start a newline in the log file.
       
   347 // ----------------------------------------------------------------------------
       
   348 //
       
   349 void CLogFile::LogNewline()
       
   350     {
       
   351     Write( KCrLf8 );
       
   352 
       
   353     if ( iAutoTimestamp )
       
   354         {
       
   355         LogTimeInternal();
       
   356         }
       
   357     }
       
   358 
       
   359 // ----------------------------------------------------------------------------
       
   360 // CLogFile::StartWrite()
       
   361 // Perform any initial operation before the main log operation.
       
   362 // ----------------------------------------------------------------------------
       
   363 //
       
   364 void CLogFile::StartWrite()
       
   365     {
       
   366     ASSERT( iCheckNestDepth == 0 );
       
   367     iCheckNestDepth++;
       
   368 
       
   369     if ( iAutoNewline )
       
   370         {
       
   371         LogNewline();
       
   372         }
       
   373     }
       
   374 
       
   375 // ----------------------------------------------------------------------------
       
   376 // CLogFile::EndWrite()
       
   377 // Perform any tidying up operations after the main log operation.
       
   378 // ----------------------------------------------------------------------------
       
   379 //
       
   380 void CLogFile::EndWrite()
       
   381     {
       
   382     if ( iAutoFlush )
       
   383         {
       
   384         iLogFile.Flush();
       
   385         }
       
   386 
       
   387     iCheckNestDepth--;
       
   388     ASSERT( iCheckNestDepth == 0 );
       
   389     }
       
   390 
       
   391 // ----------------------------------------------------------------------------
       
   392 // CLogFile::Write()
       
   393 // Do the actual writing, and associated error checking.
       
   394 // ----------------------------------------------------------------------------
       
   395 //
       
   396 void CLogFile::Write( const TDesC8& aText )
       
   397     {
       
   398 
       
   399     if ( iLogFile.Write( aText ) != KErrNone )
       
   400         {
       
   401         //  As the framework may be trapping User::Panic we need to
       
   402         //  produce the panic at a lower level.
       
   403         RThread().Panic( KLogFilePanic, TLogFileWriteFailed );
       
   404         }
       
   405     }
       
   406 
       
   407 // ----------------------------------------------------------------------------
       
   408 // CLogFile::SetAutoFlush()
       
   409 // Set AutoFlush on.
       
   410 // ----------------------------------------------------------------------------
       
   411 //
       
   412 void CLogFile::SetAutoFlush( TBool aOn )
       
   413     {
       
   414     iAutoFlush = aOn;
       
   415     }
       
   416 
       
   417 // ----------------------------------------------------------------------------
       
   418 // CLogFile::SetAutoTimeStamp()
       
   419 // Set AutoTimeStamp on.
       
   420 // ----------------------------------------------------------------------------
       
   421 //
       
   422 void CLogFile::SetAutoTimeStamp( TBool aOn )
       
   423     {
       
   424     iAutoTimestamp = aOn;
       
   425     }
       
   426 
       
   427 // ----------------------------------------------------------------------------
       
   428 // CLogFile::SetAutoNewline()
       
   429 // Set AutoNewline on.
       
   430 // ----------------------------------------------------------------------------
       
   431 //
       
   432 void CLogFile::SetAutoNewline( TBool aOn )
       
   433     {
       
   434     iAutoNewline = aOn;
       
   435     }
       
   436 
       
   437 // ----------------------------------------------------------------------------
       
   438 // CLogFile::StaticLogL()
       
   439 // Static option to append text to the log file.
       
   440 // ----------------------------------------------------------------------------
       
   441 //
       
   442 void CLogFile::StaticLogL( const TDesC& aFileName, const TDesC8& aText )
       
   443     {
       
   444     CLogFile* logFile = NewLC( aFileName, EFalse );
       
   445     logFile->Log( aText );
       
   446     CleanupStack::Pop( logFile );
       
   447     delete logFile;
       
   448     }
       
   449 
       
   450 // ----------------------------------------------------------------------------
       
   451 // CLogFile::StaticLogL()
       
   452 // Static option to append text to the log file.
       
   453 // ----------------------------------------------------------------------------
       
   454 //
       
   455 void CLogFile::StaticLogL( const TDesC& aFileName, const TDesC& aText )
       
   456     {
       
   457     CLogFile* logFile = NewLC( aFileName, EFalse );
       
   458     logFile->Log( aText );
       
   459     CleanupStack::Pop( logFile );
       
   460     delete logFile;
       
   461     }
       
   462 
       
   463 // End of File