networkingtestandutils/networkingintegrationtest/IntegrationTestUtils/Log.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 Log.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 CLog * CLog::NewL( CConsoleBase * console )
       
    28 /**
       
    29 Epoc style constuctor
       
    30 
       
    31 @param console A pointer to the current console object.
       
    32 @return A new CLog object.
       
    33 */
       
    34 
       
    35 	{
       
    36 	CLog * self = new(ELeave) CLog;
       
    37 	CleanupStack::PushL(self);
       
    38 	self->Construct( console );
       
    39 	CleanupStack::Pop();
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 EXPORT_C void CLog::Construct( CConsoleBase * console )
       
    44 /**
       
    45 Epoc style 2nd phase constructor. 
       
    46 Initialise the Utils.
       
    47 
       
    48 @param console A pointer to the current console object.
       
    49 @return A new CLog object.
       
    50 */
       
    51 	{
       
    52 	// the log system needs the current console
       
    53 	// for the console log display
       
    54 	iConsole = console; 
       
    55 	iSeverity = ESevrAll;
       
    56 	//Do we need to put information about source file & #line?
       
    57 	//Default is yes.
       
    58 	iPutSrcInfo = ETrue;
       
    59 	iHtmlLogMode = ETrue ;
       
    60 	}
       
    61 
       
    62 EXPORT_C CLog::~CLog()
       
    63 /**
       
    64 destructor
       
    65 */
       
    66 	{
       
    67 	}
       
    68 
       
    69 EXPORT_C void CLog::OpenLogFileL(const TFileName &aScriptFileName)
       
    70 /**
       
    71 Open the test log file.
       
    72 
       
    73 @param aScriptFileName The script file name which is used to generate the log file name.
       
    74 */
       
    75 	{
       
    76 
       
    77 	// get the full pathname default drive name and extension
       
    78 	_LIT(Kdefault,"c:\\xx.htm"); 
       
    79 	TParse ScriptFileName;
       
    80 
       
    81 	// combine C:\\xx.htm and the whole parameter supplied
       
    82 	TInt returnCode = ScriptFileName.Set( aScriptFileName, &Kdefault, NULL );
       
    83 
       
    84 	// overright extension if supplied with .htm
       
    85 	returnCode = ScriptFileName.Set( ScriptFileName.Name(), &Kdefault, NULL );
       
    86 	if (returnCode != KErrNone )
       
    87 		return;
       
    88 
       
    89 	// connect to the server
       
    90 	TInt ret = iFileLogger.Connect();
       
    91 	if (ret == KErrNone )
       
    92 		{
       
    93 		// create the log file in fixed directory c:\logs\Testresults
       
    94 		iFileLogger.CreateLog(_L("C:\\Logs\\TestResults"), ScriptFileName.NameAndExt());
       
    95 		}
       
    96 	}
       
    97 
       
    98 EXPORT_C void CLog::Log( TRefByValue<const TDesC16> format, ... )
       
    99 /**
       
   100 Display the log data on the console. 
       
   101 If there is a log file open write to it.
       
   102 
       
   103 @param format "printf" format string
       
   104 */
       
   105 	{
       
   106     VA_LIST aList;
       
   107 	VA_START( aList, format );
       
   108 
       
   109 	TIntegrationTestLog16Overflow iOverflow16;
       
   110 
       
   111 	// decode formated data for display on console
       
   112 	TBuf <MAX_LOG_LINE_LENGTH> LineBuf;
       
   113 	LineBuf.AppendFormatList( format, aList, &iOverflow16 );
       
   114 
       
   115 	// write to the console
       
   116 	iConsole->Printf(_L("%S\n"),&LineBuf );
       
   117 
       
   118 	// write to log file
       
   119 	iFileLogger.WriteFormat(_L("%S\n"),&LineBuf);
       
   120 
       
   121 	VA_END( aList ); 
       
   122 	}
       
   123 
       
   124 EXPORT_C void CLog::SetPutSrcInfo( TBool aPutSrcInfo )
       
   125 /**
       
   126 Enable or disable souve information in the log file.
       
   127 
       
   128 @param aPutSrcInfo The error code.
       
   129 */
       
   130 	{
       
   131 	iPutSrcInfo=aPutSrcInfo;
       
   132 	}
       
   133 	
       
   134 EXPORT_C void CLog::Log( TInt aSeverity, TRefByValue<const TDesC16> format, ... )
       
   135 /**
       
   136 Display the log data on the console. 
       
   137 If there is a log file open write to it.
       
   138 
       
   139 @param aSeverity severity level
       
   140 @param format "printf" format string
       
   141 */
       
   142 {
       
   143     VA_LIST aList;
       
   144 	VA_START( aList, format );
       
   145 
       
   146 	if( aSeverity & Severity()) 
       
   147 		{
       
   148 		Log( format, aList);
       
   149 		}
       
   150 
       
   151 	VA_END( aList ); 
       
   152 }
       
   153 
       
   154 EXPORT_C void CLog::Log( TRefByValue<const TDesC16> format, VA_LIST aList )
       
   155 /**
       
   156 Display the log data on the console 
       
   157 If there is a log file open write to it.
       
   158 
       
   159 @param format "printf" format string.
       
   160 @param aList Variable length argument list.
       
   161 */
       
   162 	{
       
   163 	// decode formated data for display on console
       
   164 	TBuf <MAX_LOG_LINE_LENGTH> LineBuf;
       
   165 	TIntegrationTestLog16Overflow iOverflow16;
       
   166 
       
   167 	LineBuf.AppendFormatList( format, aList, &iOverflow16 );
       
   168 
       
   169 
       
   170 	// write to log file
       
   171 	iFileLogger.WriteFormat(_L("%S\n"),&LineBuf);
       
   172 
       
   173 	// write to the console
       
   174 	iConsole->Printf(_L("%S\n"),&LineBuf );
       
   175 	}
       
   176 
       
   177 
       
   178 
       
   179 EXPORT_C void CLog::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,
       
   180 		TRefByValue<const TDesC> aFmt, VA_LIST aList)
       
   181 /**
       
   182 Display the log data on the console 
       
   183 if there is a log file open write to it
       
   184 
       
   185 @param aFile source file name.
       
   186 @param aLine line number.
       
   187 @param aSeverity severity level.
       
   188 @param aFmt "printf" format string.
       
   189 @param aList List of printf params.
       
   190 @note should be used for macros only
       
   191 */
       
   192 {
       
   193 	if( aSeverity & Severity()) 
       
   194 	{
       
   195 
       
   196 		TIntegrationTestLog16Overflow iOverflow16;
       
   197 
       
   198 		// decode formated data for display on console
       
   199 		TBuf <MAX_LOG_LINE_LENGTH> LineBuf;
       
   200 
       
   201 		LineBuf.AppendFormatList( aFmt, aList, &iOverflow16 );
       
   202 		// write to the console
       
   203 		iConsole->Printf(_L("%S\n"),&LineBuf );
       
   204 		//Do we need to put information about source file & #line?
       
   205 		if(iPutSrcInfo)
       
   206 		{		// Braces used to scope lifetime of TBuf objects
       
   207 			TPtrC8 fileName8(aFile);
       
   208 			TBuf<128> fileName;
       
   209 			TParse printFileName ;
       
   210 			fileName.Copy(fileName8);  //TText8->TBuf16
       
   211 			//We don't need full file name.
       
   212 			printFileName.Set(fileName, NULL, NULL) ;
       
   213 			fileName.Copy(printFileName.NameAndExt()) ;
       
   214 			// write to log file
       
   215 			iFileLogger.WriteFormat(_L("%S\t%d\t%S\n"),&fileName, aLine, &LineBuf);
       
   216 		}
       
   217 		else
       
   218 		{
       
   219 	// write to log file
       
   220 	iFileLogger.WriteFormat(_L("%S\n"),&LineBuf);
       
   221 		}
       
   222 
       
   223 
       
   224 
       
   225 	}
       
   226 
       
   227 }
       
   228 
       
   229 EXPORT_C void CLog::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,
       
   230 		TRefByValue<const TDesC> aFmt,...)
       
   231 /**
       
   232 Display the log data on the console 
       
   233 if there is a log file open write to it.
       
   234 
       
   235 @param aFile source file name
       
   236 @param aLine line number
       
   237 @param aSeverity severity level
       
   238 @param aFmt "printf" format string
       
   239 @note should be used for macros only
       
   240 */
       
   241 	{
       
   242 	VA_LIST aList;
       
   243 	VA_START( aList, aFmt );
       
   244 	LogExtra(aFile, aLine, aSeverity, aFmt, aList);
       
   245 	VA_END( aList ); 
       
   246 	}
       
   247 
       
   248 
       
   249 EXPORT_C void CLog::LogResult( TVerdict ver, TRefByValue<const TDesC16> format, ... )
       
   250 /**
       
   251 Display highlighted results on the console. 
       
   252 If there is a log file open write to it.
       
   253 
       
   254 @param ver The test result.
       
   255 @param format A printf style format string.
       
   256 */
       
   257 	{
       
   258     VA_LIST aList;
       
   259 	VA_START( aList, format );
       
   260 
       
   261 	TIntegrationTestLog16Overflow iOverflow16;
       
   262 
       
   263 	// decode formated data for display on console
       
   264 	TBuf <MAX_LOG_LINE_LENGTH> LineBuf;
       
   265 	LineBuf.AppendFormatList( format, aList, &iOverflow16 );
       
   266 
       
   267 	// write to the console
       
   268 	iConsole->Printf(_L("%S\n"),&LineBuf );
       
   269 
       
   270 	// write to log file
       
   271 	if( iHtmlLogMode)
       
   272 		{ 
       
   273 	switch( ver)
       
   274 		{
       
   275 	case EPass:
       
   276 			ERR_PRINTF2(_L("<font size=4 color=00AF00>%S</font>\n"),&LineBuf );
       
   277 		break;
       
   278 	case EFail:
       
   279 			ERR_PRINTF2(_L("<font size=4 color=FF0000>%S</font>\n"),&LineBuf );
       
   280 		break;
       
   281 	case EInconclusive:
       
   282 			ERR_PRINTF2(_L("<font size=4 color=0000FF>%S</font>\n"),&LineBuf );
       
   283 		break;
       
   284 	case ETestSuiteError:
       
   285 			ERR_PRINTF2(_L("<font size=4 color=0000FF>%S</font>\n"),&LineBuf );
       
   286 		break;
       
   287 	case EAbort:
       
   288 			ERR_PRINTF2(_L("<font size=4 color=0000FF>%S</font>\n"),&LineBuf );
       
   289 		}
       
   290 	}
       
   291 	else
       
   292 	{
       
   293 			ERR_PRINTF2(_L("%S\n"),&LineBuf );
       
   294 		}	
       
   295 	VA_END( aList ); 
       
   296 	}
       
   297 
       
   298 EXPORT_C TPtrC CLog::EpocErrorToText(TInt aError)
       
   299 /**
       
   300 Convert a SymbianOS error code to text.
       
   301 
       
   302 @param aError The error code.
       
   303 @return Text describing the error code.
       
   304 */
       
   305 	{
       
   306 	switch (aError)
       
   307 		{
       
   308 	case KErrNone:
       
   309 		return _L("KErrNone");
       
   310 	case KErrNotFound:
       
   311 		return _L("KErrNotFound");
       
   312 	case KErrGeneral:
       
   313 		return _L("KErrGeneral");
       
   314 	case KErrCancel:
       
   315 		return _L("KErrCancel");
       
   316 	case KErrNoMemory:
       
   317 		return _L("KErrNoMemory");
       
   318 	case KErrNotSupported:
       
   319 		return _L("KErrNotSupported");
       
   320 	case KErrArgument:
       
   321 		return _L("KErrArgument");
       
   322 	case KErrTotalLossOfPrecision:
       
   323 		return _L("KErrTotalLossOfPrecision");
       
   324 	case KErrBadHandle:
       
   325 		return _L("KErrBadHandle");
       
   326 	case KErrOverflow:
       
   327 		return _L("KErrOverflow");
       
   328 	case KErrUnderflow:
       
   329 		return _L("KErrUnderflow");
       
   330 	case KErrAlreadyExists:
       
   331 		return _L("KErrAlreadyExists");
       
   332 	case KErrPathNotFound:
       
   333 		return _L("KErrPathNotFound");
       
   334 	case KErrDied:
       
   335 		return _L("KErrDied");
       
   336 	case KErrInUse:
       
   337 		return _L("KErrInUse");
       
   338 	case KErrServerTerminated:
       
   339 		return _L("KErrServerTerminated");
       
   340 	case KErrServerBusy:
       
   341 		return _L("KErrServerBusy");
       
   342 	case KErrCompletion:
       
   343 		return _L("KErrCompletion");
       
   344 	case KErrNotReady:
       
   345 		return _L("KErrNotReady");
       
   346 	case KErrUnknown:
       
   347 		return _L("KErrUnknown");
       
   348 	case KErrCorrupt:
       
   349 		return _L("KErrCorrupt");
       
   350 	case KErrAccessDenied:
       
   351 		return _L("KErrAccessDenied");
       
   352 	case KErrLocked:
       
   353 		return _L("KErrLocked");
       
   354 	case KErrWrite:
       
   355 		return _L("KErrWrite");
       
   356 	case KErrDisMounted:
       
   357 		return _L("KErrDisMounted");
       
   358 	case KErrEof:
       
   359 		return _L("KErrEof");
       
   360 	case KErrDiskFull:
       
   361 		return _L("KErrDiskFull");
       
   362 	case KErrBadDriver:
       
   363 		return _L("KErrBadDriver");
       
   364 	case KErrBadName:
       
   365 		return _L("KErrBadName");
       
   366 	case KErrCommsLineFail:
       
   367 		return _L("KErrCommsLineFail");
       
   368 	case KErrCommsFrame:
       
   369 		return _L("KErrCommsFrame");
       
   370 	case KErrCommsOverrun:
       
   371 		return _L("KErrCommsOverrun");
       
   372 	case KErrCommsParity:
       
   373 		return _L("KErrCommsParity");
       
   374 	case KErrTimedOut:
       
   375 		return _L("KErrTimedOut");
       
   376 	case KErrCouldNotConnect:
       
   377 		return _L("KErrCouldNotConnect");
       
   378 	case KErrCouldNotDisconnect:
       
   379 		return _L("KErrCouldNotDisconnect");
       
   380 	case KErrDisconnected:
       
   381 		return _L("KErrDisconnected");
       
   382 	case KErrBadLibraryEntryPoint:
       
   383 		return _L("KErrBadLibraryEntryPoint");
       
   384 	case KErrBadDescriptor:
       
   385 		return _L("KErrBadDescriptor");
       
   386 	case KErrAbort:
       
   387 		return _L("KErrAbort");
       
   388 	case KErrTooBig:
       
   389 		return _L("KErrTooBig");
       
   390 	default:
       
   391 		return _L("Unknown");
       
   392 		}
       
   393 	}
       
   394 
       
   395 
       
   396 EXPORT_C const TText* CLog::TestResultText( enum TVerdict aTestVerdict )
       
   397 /**
       
   398 Turn a test verdict into text.
       
   399 
       
   400 @param aTestVerdict The test result.
       
   401 @return Text describing the current verdict.
       
   402 */
       
   403 	{
       
   404 	switch ( aTestVerdict )
       
   405 		{
       
   406 	case EPass:
       
   407 		return _S("PASS");
       
   408 	case EFail:
       
   409 		return _S("FAIL");
       
   410 	case EInconclusive:
       
   411 		return _S("INCONCLUSIVE");
       
   412 	case ETestSuiteError:
       
   413 		return _S("TEST_SUITE_ERROR");
       
   414 	case EAbort:
       
   415 		return _S("ABORT");
       
   416 	default:
       
   417 		return _S("What");
       
   418 
       
   419 		}
       
   420 	}
       
   421 
       
   422 EXPORT_C void CLog::LogBlankLine( TInt number )
       
   423 /**
       
   424 Add a number of blank lines.
       
   425 
       
   426 @param number The number of blank lines to add to the Log.
       
   427 */
       
   428 	{
       
   429 	for (TInt i =0; i< number; i++)
       
   430 		Log(_L(" "));
       
   431 	}
       
   432 
       
   433 EXPORT_C void CLog::CloseLogFile()
       
   434 /**
       
   435 Close the log file.
       
   436 */
       
   437 	{
       
   438 	// add the htm end
       
   439 	iFileLogger.WriteFormat(_L("</end>"));
       
   440 	
       
   441 	// close flogger
       
   442 	iFileLogger.CloseLog();
       
   443 	}
       
   444 
       
   445 void TIntegrationTestLog16Overflow::Overflow(TDes16& /*aDes*/)
       
   446 /**
       
   447 This function is called if the format text overflows the internal buffer.
       
   448 */
       
   449 	{
       
   450 
       
   451 	User::Panic(_L("Log output buffer overflow"),1);
       
   452 
       
   453 	}
       
   454 
       
   455 EXPORT_C void CLog::SetSeverity( TInt aSeverity )
       
   456 /**
       
   457 Set the current severity level.
       
   458 
       
   459 @param aSeverity The new severity level.
       
   460 */
       
   461 {
       
   462 	iSeverity = aSeverity;
       
   463 }
       
   464 
       
   465 EXPORT_C TInt CLog::Severity()
       
   466 /**
       
   467 Get the current severity level.
       
   468 
       
   469 @return The current severity level.
       
   470 */
       
   471 {
       
   472 	return iSeverity;
       
   473 }
       
   474 
       
   475