uifw/AvKon/tsrc/bc/bctestutil/src/bctestlogger.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Write logger.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <eikenv.h>
       
    20 #include <eikappui.h>
       
    21 #include <eikapp.h>
       
    22 
       
    23 #include "bctestlogger.h"
       
    24 
       
    25 _LIT( KPathBase, "c:\\BCTestLog\\" );  // directory for test results in FFS
       
    26 _LIT( KTimeFormat, "%:0%J%:1%T%:2%S%:3%+B" );
       
    27 _LIT( KBCTestLogEnd, "_TestLog.txt" ); // end of auto test log filename
       
    28 _LIT( KDateTimeFormat, "%-B%/0%1%/1%2%/2%3%/3 %:0%J%:1%T%:2%S%:3%+B" );
       
    29 _LIT( KMainLog, "c:\\BCTestLog\\BCTestLog.txt" );  // main log file
       
    30 
       
    31 _LIT( KGeneralLogInfo, "Log file for automated test's results created " );
       
    32 _LIT( KTimeLogStart, " -- " );
       
    33 _LIT( KTimeLogEnd, " --\r\n" );
       
    34 _LIT( KGetTimeFailed, "Getting time failed" );
       
    35 
       
    36 const TInt KTheMaxInterval = 2;
       
    37 const TInt KTempBufferLenth = 128;
       
    38 
       
    39 
       
    40 // ======== MEMBER FUNCTIONS ========
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // static Constructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CBCTestLogger* CBCTestLogger::NewL( CEikonEnv* aEikonEnv )
       
    47     {
       
    48     CBCTestLogger* self = new( ELeave ) CBCTestLogger( aEikonEnv );
       
    49     CleanupStack::PushL( self );
       
    50     self->ConstructL();
       
    51     CleanupStack::Pop( self );
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // Constructor
       
    57 // ---------------------------------------------------------------------------
       
    58 //    
       
    59 CBCTestLogger::CBCTestLogger( CEikonEnv* aEikonEnv ): iEikEnv( aEikonEnv )
       
    60     {    
       
    61     }
       
    62     
       
    63 // ---------------------------------------------------------------------------
       
    64 // private Constructor
       
    65 // ---------------------------------------------------------------------------
       
    66 // 
       
    67 CBCTestLogger::~CBCTestLogger()
       
    68     {
       
    69     iAtLogFile.Close();
       
    70     }
       
    71     
       
    72 // ---------------------------------------------------------------------------
       
    73 // ConstructL, 2nd Constructor
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 void CBCTestLogger::ConstructL()
       
    77     {
       
    78     TInt err = KErrNone;    
       
    79     
       
    80     iEikEnv = CEikonEnv::Static();
       
    81     RFs tempServer = iEikEnv->FsSession();    
       
    82     
       
    83     // \BCTestLog directory created if doesn't exist
       
    84     err = tempServer.MkDir( KPathBase );
       
    85     if ( err == KErrAlreadyExists || err == KErrNone )  
       
    86         {
       
    87         err = KErrNone; // Directory already exists - no error
       
    88         }        
       
    89     else
       
    90         {
       
    91         User::Leave(err);
       
    92         }        
       
    93     
       
    94     // Create autotest results log filename
       
    95     iAtLogFileName = KPathBase;
       
    96     iAtLogFileName.Append( AppCaption() );
       
    97     iAtLogFileName.Append( KBCTestLogEnd );    
       
    98     
       
    99     // Open log file for autotest results.
       
   100     // If the file already exists, replace it.
       
   101     err = iAtLogFile.Replace( tempServer,
       
   102                               iAtLogFileName,
       
   103                               EFileWrite | EFileStreamText );
       
   104 
       
   105     if (err != KErrNone)
       
   106         {
       
   107         User::Leave( err );
       
   108         }
       
   109 
       
   110     iBuf.Zero();
       
   111     iBuf.Append( KGeneralLogInfo );
       
   112     TTime homeTime;
       
   113     homeTime.HomeTime();
       
   114     TBuf<KTempBufferLenth> tempBuf;
       
   115     homeTime.FormatL( tempBuf, KDateTimeFormat );    
       
   116     iBuf.Append( tempBuf );    
       
   117     iBuf.Append( KLogTwoLine );          
       
   118     WriteToFileL( iAtLogFile, iBuf );
       
   119     
       
   120     CreateMainLogL();
       
   121     }
       
   122 
       
   123 // ---------------------------------------------------------------------------
       
   124 // CBCTestLogger::CreateMainLogL
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 void CBCTestLogger::CreateMainLogL()
       
   128     {
       
   129     RFile mainFile;
       
   130     RFs tempServer = iEikEnv->FsSession();
       
   131     TInt err = mainFile.Create( tempServer, KMainLog, 
       
   132         EFileWrite | EFileStreamText );
       
   133     if ( err != KErrNone )
       
   134         {
       
   135         if ( err == KErrAlreadyExists )
       
   136             {
       
   137             TInt errnum = mainFile.Open( tempServer, KMainLog, 
       
   138                 EFileRead | EFileStreamText );
       
   139             if ( errnum != KErrNone )
       
   140                 {
       
   141                 return;
       
   142                 }
       
   143             }
       
   144         }    
       
   145     CleanupClosePushL( mainFile );
       
   146     
       
   147     if ( err == KErrNone )
       
   148         {
       
   149         WriteToFileL( mainFile,  iBuf );
       
   150         }
       
   151     else if ( err == KErrAlreadyExists )
       
   152         {
       
   153         TTime modifiedTime;
       
   154         TTime homeTime;
       
   155         homeTime.HomeTime();
       
   156         err = mainFile.Modified( modifiedTime );
       
   157         if ( err == KErrNone )
       
   158             {
       
   159             TTimeIntervalDays days = homeTime.DaysFrom( modifiedTime );
       
   160             TInt interval = days.Int();
       
   161             if ( interval >= KTheMaxInterval || interval <= -KTheMaxInterval )
       
   162                 {
       
   163                 mainFile.Close();
       
   164                 mainFile.Replace( tempServer, KMainLog,
       
   165                     EFileWrite | EFileStreamText );                
       
   166                 WriteToFileL( mainFile,  iBuf );
       
   167                 }
       
   168             }     
       
   169         }
       
   170     
       
   171     CleanupStack::PopAndDestroy( &mainFile );
       
   172     }
       
   173     
       
   174 // ---------------------------------------------------------------------------
       
   175 // CBCTestLogger::WriteLogL
       
   176 // Write text to log file.
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void CBCTestLogger::WriteLogL( const TDesC& aLogText )
       
   180     {    
       
   181     WriteToFileL( iAtLogFile, aLogText );
       
   182     }
       
   183     
       
   184 // -----------------------------------------------------------------------------
       
   185 // CBCTestLogger::CreateTimeStamp
       
   186 // Creates time stamp.
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 void CBCTestLogger::CreateTimeStamp( TDes& aBuf )
       
   190     {    
       
   191     TTime homeTime;
       
   192     homeTime.HomeTime();
       
   193     
       
   194     aBuf.Append( KTimeLogStart );
       
   195     TBuf<KTempBufferLenth> tempBuf;  
       
   196     TRAPD( err, homeTime.FormatL( tempBuf, KTimeFormat ) );
       
   197     if ( err != KErrNone ) // FormatL failed
       
   198         {
       
   199         tempBuf.Zero();
       
   200         tempBuf.Append( KGetTimeFailed );
       
   201         }        
       
   202     aBuf.Append( tempBuf );
       
   203     aBuf.Append( KTimeLogEnd );
       
   204     }
       
   205     
       
   206 // -----------------------------------------------------------------------------
       
   207 // CBCTestLogger::Buffer
       
   208 // Return reference to iBuf.
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 TDes& CBCTestLogger::Buffer()
       
   212     {
       
   213     return iBuf;
       
   214     }    
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CBCTestLogger::WriteMainLogL
       
   218 // Appends given text to main log file (if exists).
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 void CBCTestLogger::WriteMainLogL(const TDesC& aLogText)
       
   222     {    
       
   223     TBuf<KLogNameLength> mainLogFileName; // main log file
       
   224     mainLogFileName = KMainLog;
       
   225     RFile mainLogFile;
       
   226 
       
   227     // Open log file for writing.
       
   228     TInt err = mainLogFile.Open( iEikEnv->FsSession(),
       
   229                                  mainLogFileName,
       
   230                                  EFileWrite | EFileStreamText | EFileShareAny);
       
   231 
       
   232     if (err != KErrNone)
       
   233         {
       
   234         return; // log file couldn't be opened, do nothing
       
   235         }
       
   236     CleanupClosePushL( mainLogFile );
       
   237     TInt Pos = 0;
       
   238     mainLogFile.Seek( ESeekEnd,Pos );
       
   239 
       
   240     WriteToFileL( mainLogFile, aLogText );
       
   241 
       
   242     mainLogFile.Flush();
       
   243     mainLogFile.Close();
       
   244     CleanupStack::PopAndDestroy( &mainLogFile );
       
   245     }
       
   246     
       
   247 // -----------------------------------------------------------------------------
       
   248 // Get the caption of application
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 const TDesC& CBCTestLogger::AppCaption()
       
   252     {
       
   253     return iEikEnv->EikAppUi()->Application()->AppCaption();;
       
   254     }
       
   255     
       
   256 // -----------------------------------------------------------------------------
       
   257 // Write unicode text to file
       
   258 // -----------------------------------------------------------------------------
       
   259 //
       
   260 void CBCTestLogger::WriteToFileL( RFile& aFile, const TDesC& aText )
       
   261     {
       
   262     TPtrC8 buf( (TUint8*)aText.Ptr(), aText.Size() );
       
   263     aFile.Write( buf );    
       
   264     }