networkingtestandutils/networkingintegrationtest/IntegrationTestUtils/LogFile.cpp
changeset 0 af10295192d8
equal deleted inserted replaced
-1:000000000000 0:af10295192d8
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file LogFile.cpp
       
    18 */
       
    19 
       
    20 // EPOC includes
       
    21 #include <e32base.h>
       
    22 #include <e32cons.h>
       
    23 
       
    24 // Test system includes
       
    25 #include "../inc/Log.h"
       
    26 
       
    27 EXPORT_C void CFileLogger::CreateLog(const TDesC& aDir, const TDesC& aName)
       
    28 /**
       
    29 Create a log file.
       
    30 
       
    31 @param aDir Directory in which to create the log file.
       
    32 @param aName Log file name.
       
    33 @note If the directory does not exsist then logging is disabled.
       
    34 */
       
    35 	{
       
    36 	// only log if adir exists
       
    37 	TUint aAttValue;
       
    38 	if ( iFs.Att(aDir, aAttValue) ==KErrNotFound) 
       
    39 		{
       
    40 		iEnabled = false;
       
    41 		return;
       
    42 		}
       
    43 	else
       
    44 		iEnabled = true;
       
    45 
       
    46 	// make the correct file name
       
    47 	TFileName LogFile;
       
    48 	LogFile.Format( _L("\\logs\\testresults\\%S"), &aName );
       
    49 
       
    50 	// If the file does not exist - create it
       
    51 	TInt returnCode=iLogfile.Replace(iFs,LogFile,EFileWrite | EFileStreamText); 
       
    52 
       
    53 	// check if open fails 
       
    54 	if (returnCode==KErrNone )
       
    55 		{
       
    56 		//file has opened ok
       
    57 		// add the start to the htm
       
    58 		iLogfile.Write( _L8("<html><body><pre>\n") );
       
    59 		}
       
    60 	else
       
    61 		iEnabled = false;
       
    62 	};
       
    63 
       
    64 EXPORT_C void CFileLogger::CloseLog( void )
       
    65 /**
       
    66 Close log file.
       
    67 */
       
    68 	{
       
    69 
       
    70 	// if logging enabled flush buffers
       
    71 	if ( iEnabled )
       
    72 		{
       
    73 		iLogfile.Flush();
       
    74 		iLogfile.Close();
       
    75 		}
       
    76 
       
    77 	// disconnect from the file server
       
    78 	iFs.Close();
       
    79 	}
       
    80 
       
    81 EXPORT_C void CFileLogger::WriteFormat( TRefByValue<const TDesC16> format, ...  )
       
    82 /**
       
    83 Write formatted output to the log file. 
       
    84 
       
    85 @param format "printf" format string
       
    86 */
       
    87 	{
       
    88 
       
    89 	if ( !iEnabled ) return ;
       
    90 
       
    91     VA_LIST aList;
       
    92 	VA_START( aList, format );
       
    93 
       
    94 	TIntegrationTestLog16Overflow iOverflow16;
       
    95 
       
    96 	// decode formated data for display on console
       
    97 	TBuf <MAX_LOG_LINE_LENGTH> LineBuf;
       
    98 	TBuf8 <MAX_LOG_LINE_LENGTH> LineBuf8;
       
    99 
       
   100 	// get the current time and date
       
   101 	TTime now;
       
   102 	now.HomeTime();
       
   103 	TDateTime dateTime = now.DateTime() ;
       
   104 
       
   105 	// add the current time and date 
       
   106 	LineBuf.AppendFormat(_L("%02d/%02d/%04d\t%02d:%02d:%02d:%03d\t"),
       
   107 		dateTime.Day()+1,
       
   108 		dateTime.Month()+1,
       
   109 		dateTime.Year(),
       
   110 		dateTime.Hour(),
       
   111 		dateTime.Minute(),
       
   112 		dateTime.Second(),
       
   113 		(dateTime.MicroSecond()/1000) ); 
       
   114 
       
   115 	// followed by the formatted data
       
   116 	LineBuf.AppendFormatList( format, aList,  &iOverflow16 );
       
   117 	VA_END( aList ); 
       
   118 
       
   119 	// convert from unicode to 8 bit
       
   120 	for (TInt i = 0; i< LineBuf.Length() ;i++)
       
   121 		LineBuf8.Append( (char)LineBuf[i] );
       
   122 
       
   123 
       
   124 	// write to log file
       
   125 	iLogfile.Write( LineBuf8 );
       
   126 	}
       
   127 
       
   128 EXPORT_C TInt CFileLogger::Connect( void )
       
   129 /**
       
   130 Connect to the file server.
       
   131 */
       
   132 	{
       
   133 	return iFs.Connect();
       
   134 	}