shortlinkconn_plat/obex_service_utils_api/tsrc/src/testlogger.cpp
branchRCL_3
changeset 39 4096754ee773
parent 38 3dcb815346df
child 40 52a167391590
equal deleted inserted replaced
38:3dcb815346df 39:4096754ee773
     1 /*
       
     2 * Copyright (c) 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:
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 
       
    20 #include "testlogger.h"
       
    21 
       
    22 #include <StifTestModule.h>
       
    23 #include <StifLogger.h>
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <f32file.h>
       
    27 #include <sysutil.h>
       
    28 
       
    29 // RDebug
       
    30 #include <e32debug.h>
       
    31 
       
    32 const TInt CObexTestLogger::KPrefixLength = 8;
       
    33 // Timestamp string length
       
    34 const TInt KTimeStampStrLen = 21;
       
    35 // Extended Timestamp string length
       
    36 const TInt KExtTimeStampStrLen = 40;
       
    37 // Extended time stamp
       
    38 typedef TBuf<KExtTimeStampStrLen> TExtTimeStamp;
       
    39 // Logging path
       
    40 _LIT( KObexServAPITestSystemLogPath, "e:\\logs\\ObexServAPItest\\" );
       
    41 // Log file
       
    42 _LIT( KObexServAPITestSystemLogFile, "ObexServAPItest.html" );
       
    43 // Log file
       
    44 _LIT( KObexServAPITestSystemLogFullName, "\\logs\\ObexServAPItest\\ObexServAPItest%x.html" );
       
    45 // Time stamp format string
       
    46 _LIT( KFormatTimeStamp, "<br/>[%H:%T:%S.%*C4] " );
       
    47 // Date format string
       
    48 _LIT( KFormatDate, "%F%D %N %Y" );
       
    49 // HTML header section
       
    50 _LIT( KHtmlHeadSection, "<html>\n<head>\n<title>Obex Message Handler Test System results</title>\n<style type=\"text/css\">\n.res, .case { display: inline; }\n</style>\n</head>\n\n<body onLoad=\"FormatResults();\">\n\n" );
       
    51 // HTML code which will display the statistics
       
    52 _LIT( KHtmlStatSection, "<p>Logfile created on %S at %S<br />\nS60 version %S</p>\n\n<div id=\"stats\"></div>\n<br />\n\n" );
       
    53 // HTML identifier for test case start
       
    54 _LIT( KHtmlTestCaseStart, "<div class=\"case\">" );
       
    55 // Test case result logging
       
    56 _LIT( KTestCaseResult, "Test completed; function %S, parameter %S, result <div class=\"res\">%d</div></div>" );
       
    57 // JavaScript function for calculating test result
       
    58 #define KJSSECTION _L( "<script type=\"text/javascript\">\nfunction FormatResults() { \
       
    59 \n\tvar tl = document.getElementsByTagName( \"div\" );\n\tvar passed = 0, failed = 0, testcases = 0; \
       
    60 \n\tfor( var i = 0; i < tl.length; i++ ) {\n\t\tif( tl[i].className == \"case\" ) {\n\t\t\ttestcases++;\n\t\t\t} \
       
    61 \n\t\tif( tl[i].className == \"res\" ) {\n\t\t\tif( tl[i].innerHTML == 0 ) {\n\t\t\t\tpassed++;\n\t\t\t\t} \
       
    62 \n\t\t\telse {\n\t\t\t\tfailed++;\n\t\t\t\ttl[i].parentNode.style.color = \"red\";\n\t\t\t\t} } } \
       
    63 \n\tvar stat = document.getElementById( \"stats\" );\n\tvar passrate = Math.round( ( passed / testcases ) * 10000 ) / 100; \
       
    64 \n\tstat.innerHTML = \"Test cases run: \" + testcases + \"<br />Passed: \" + passed \
       
    65 +\"<br />Failed: \" + failed + \"<br /><b>Passrate: \" + passrate + \"%%</b>\";\n\t}\n</script>" )
       
    66 
       
    67 // Construction and destruction.
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CObexTestLogger::NewL
       
    71 // Static constructor to constructor builder objects.
       
    72 // -----------------------------------------------------------------------------
       
    73 //
       
    74 CObexTestLogger* CObexTestLogger::NewL(const CTestModuleIf* aTestModuleIf)
       
    75 	{
       
    76 	CObexTestLogger* self = new (ELeave) CObexTestLogger(aTestModuleIf);
       
    77 
       
    78 	CleanupStack::PushL( self );
       
    79 	self->ConstructL();
       
    80 	CleanupStack::Pop(self);
       
    81 
       
    82 	return self;
       
    83 	}
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CObexTestLogger::CObexTestLogger
       
    87 // Constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CObexTestLogger::CObexTestLogger(const CTestModuleIf* aTestModuleIf)
       
    91 	: CBase(),
       
    92 	  iEnabled(ETrue),
       
    93 	  iTestModuleIf(aTestModuleIf)
       
    94 	{
       
    95 	}
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CTestLogger::ConstructL
       
    99 // 2nd phase constructor.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CObexTestLogger::ConstructL()
       
   103     {
       
   104     TInt err = CheckLogFile();
       
   105     iLog = CStifLogger::NewL( KObexServAPITestSystemLogPath, KObexServAPITestSystemLogFile,
       
   106                               CStifLogger::ETxt, CStifLogger::EFile, EFalse, EFalse );
       
   107     if( err == KErrNotFound )
       
   108         {   // Add info & HTML markup to the beginning of the log file.
       
   109         LogHeadSectionL();
       
   110         }
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // CObexTestLogger::~CObexTestLogger
       
   115 // Destructor.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CObexTestLogger::~CObexTestLogger()
       
   119 	{
       
   120 	delete iLog;
       
   121 	}
       
   122 
       
   123 // Member functions.
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CObexTestLogger::GetLogger
       
   127 // Returns a pointer to current STIF logger.
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 CStifLogger* CObexTestLogger::GetLogger() const
       
   131 	{
       
   132 	return iLog;
       
   133 	}
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CObexTestLogger::GetTestModuleIf
       
   137 // Returns a pointer to current STIF test module interface.
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 const CTestModuleIf* CObexTestLogger::GetTestModuleIf() const
       
   141 	{
       
   142 	return iTestModuleIf;
       
   143 	}
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CObexTestLogger::Log
       
   147 // Logs 8 bit data to screen and file.
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 TInt CObexTestLogger::Log(TLogLevel aLevel, TRefByValue<const TDesC8> aLogText, ...) const
       
   151     {
       
   152     if (!iEnabled)
       
   153         {
       
   154         return KErrNone;
       
   155         }
       
   156 
       
   157     const TDesC8& actualData = aLogText;
       
   158 
       
   159     // Check that user does not try to output too much data.
       
   160     if (actualData.Length() > KMaxLogData - KPrefixLength)
       
   161         {
       
   162         return KErrTooBig;
       
   163         }
       
   164 
       
   165     // Buffer to convert 8 bit data to to 16 bit.
       
   166     TLogInfo8 converter;
       
   167 
       
   168     // Create variable argument list.
       
   169     VA_LIST args;
       
   170     VA_START(args, aLogText);
       
   171 
       
   172     // Format the argument list to the output buffer.
       
   173     converter.FormatList( aLogText, args );
       
   174 
       
   175     // Free the variable argument list
       
   176     VA_END(args);
       
   177 
       
   178     TLogInfo buffer;
       
   179     buffer.Copy( converter );
       
   180 
       
   181     // Append log prefix to the output buffer.
       
   182     buffer.Insert( 0, GetPrefix( aLevel ) );
       
   183 
       
   184     // Log a timestamp
       
   185     TStifLoggerTimeStamp8 time;
       
   186     TRAP_IGNORE( LogTimeStampL( time ) );
       
   187     buffer.Insert( 0, time );
       
   188 
       
   189     // Write log.
       
   190     return WriteLog(aLevel, buffer);
       
   191     }
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CObexTestLogger::Log
       
   195 // Logs 8 bit data to screen and file.
       
   196 // -----------------------------------------------------------------------------
       
   197 //
       
   198 TInt CObexTestLogger::Log(const TDesC& aCategory, TRefByValue<const TDesC8> aLogText, ...) const
       
   199     {
       
   200     if (!iEnabled)
       
   201 	    {
       
   202         return KErrNone;
       
   203         }
       
   204 
       
   205     const TDesC8& actualData = aLogText;
       
   206 
       
   207     // Check that user does not try to output too much data.
       
   208     if (actualData.Length() > KMaxLogData - KPrefixLength)
       
   209         {
       
   210         return KErrTooBig;
       
   211         }
       
   212 
       
   213     // Buffer to convert 8 bit data to to 16 bit.
       
   214     TLogInfo8 converter;
       
   215 
       
   216     // Create variable argument list.
       
   217     VA_LIST args;
       
   218     VA_START(args, aLogText);
       
   219 
       
   220     // Format the argument list to the output buffer.
       
   221     converter.FormatList(aLogText, args);
       
   222 
       
   223     // Free the variable argument list
       
   224     VA_END(args);
       
   225 
       
   226     TLogInfo buffer;
       
   227     buffer.Copy( converter );
       
   228 
       
   229     // Log a timestamp
       
   230     TStifLoggerTimeStamp8 time;
       
   231     TRAP_IGNORE( LogTimeStampL( time ) );
       
   232     buffer.Insert( 0, time );
       
   233 
       
   234     // Write log.
       
   235     return WriteLog(aCategory, buffer);
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CObexTestLogger::Log
       
   240 // Logs 16 bit data to screen and file.
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 TInt CObexTestLogger::Log(TLogLevel aLevel, TRefByValue<const TDesC16> aLogText, ...) const
       
   244 	{
       
   245 	if (!iEnabled)
       
   246 		{
       
   247 		return KErrNone;
       
   248 		}
       
   249 
       
   250 	const TDesC16& actualData = aLogText;
       
   251 	// Check that user does not try to output too much data.
       
   252 	if (actualData.Length() + KPrefixLength > KMaxLogData)
       
   253 		{
       
   254 		return KErrTooBig;
       
   255 		}
       
   256 
       
   257 	TLogInfo buffer;
       
   258 
       
   259 	// Log a timestamp
       
   260 	TRAP_IGNORE( LogTimeStampL( buffer ) );
       
   261 
       
   262 	// Create variable argument list.
       
   263 	VA_LIST args;
       
   264 	VA_START(args, aLogText);
       
   265 
       
   266 	// Format the argument list to the output buffer.
       
   267 	buffer.AppendFormatList(aLogText, args);
       
   268 
       
   269 	// Free the variable argument list
       
   270 	VA_END(args);
       
   271 
       
   272 	// Write log.
       
   273 	return WriteLog(aLevel, buffer);
       
   274 	}
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CObexTestLogger::Log
       
   278 // Logs 16 bit data to screen and file.
       
   279 // -----------------------------------------------------------------------------
       
   280 //
       
   281 TInt CObexTestLogger::Log(const TDesC& aCategory, TRefByValue<const TDesC16> aLogText, ...) const
       
   282 	{
       
   283 	if (!iEnabled)
       
   284 		{
       
   285 		return KErrNone;
       
   286 		}
       
   287 
       
   288 	const TDesC16& actualData = aLogText;
       
   289 	// Check that user does not try to output too much data.
       
   290 	if (actualData.Length() + KPrefixLength > KMaxLogData)
       
   291 		{
       
   292 		return KErrTooBig;
       
   293 		}
       
   294 
       
   295 	TLogInfo buffer;
       
   296 
       
   297 	// Log a timestamp
       
   298 	TRAP_IGNORE( LogTimeStampL( buffer ) );
       
   299 
       
   300 	// Create variable argument list.
       
   301 	VA_LIST args;
       
   302 	VA_START(args, aLogText);
       
   303 
       
   304 	// Format the argument list to the output buffer.
       
   305 	buffer.AppendFormatList(aLogText, args);
       
   306 
       
   307 	// Free the variable argument list
       
   308 	VA_END(args);
       
   309 
       
   310 	// Write log.
       
   311 	return WriteLog(aCategory, buffer);
       
   312 	}
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CObexTestLogger::LogNewLine
       
   316 // Logs an HTML  newline and timestamp.
       
   317 // -----------------------------------------------------------------------------
       
   318 //
       
   319 TInt CObexTestLogger::LogResult( const TDesC8& aFunc, const TDesC& aArg, TInt aRes ) const
       
   320     {
       
   321     TExtTimeStamp func;
       
   322     func.Copy( aFunc ); // Copy 8-bit string to 16-bit buffer
       
   323 
       
   324     Log( CObexTestLogger::ETLResult, KTestCaseResult, &func, &aArg, aRes );
       
   325     return KErrNone;
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CObexTestLogger::LogNewLine
       
   330 // Logs an HTML  newline and timestamp.
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 TInt CObexTestLogger::LogNewLine() const
       
   334     {
       
   335     if (!iEnabled)
       
   336         {
       
   337         return KErrNone;
       
   338         }
       
   339     TExtTimeStamp time;
       
   340     TRAPD( err, LogTimeStampL( time ) );
       
   341     if( !err )
       
   342         {
       
   343         time.Insert( 0, KHtmlTestCaseStart );
       
   344         err = iLog->Log( time );
       
   345         }
       
   346     return err;
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CObexTestLogger::SetEnabled
       
   351 // Returns a pointer to current STIF logger.
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 void CObexTestLogger::SetEnabled(TBool aEnabled)
       
   355 	{
       
   356 	iEnabled = aEnabled;
       
   357 	}
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CObexTestLogger::SetLogger
       
   361 // Sets a pointer to current STIF logger.
       
   362 // -----------------------------------------------------------------------------
       
   363 //
       
   364 void CObexTestLogger::SetLogger(CStifLogger* aLog)
       
   365 	{
       
   366 	iLog = aLog;
       
   367 	}
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // CObexTestLogger::SetTestModuleIf
       
   371 // Sets a pointer to current STIF test module.
       
   372 // -----------------------------------------------------------------------------
       
   373 //
       
   374 void CObexTestLogger::SetTestModuleIf(const CTestModuleIf* aTestModuleIf)
       
   375 	{
       
   376 	iTestModuleIf = aTestModuleIf;
       
   377 	}
       
   378 
       
   379 // -----------------------------------------------------------------------------
       
   380 // CObexTestLogger::WriteLog
       
   381 // Writes log.
       
   382 // -----------------------------------------------------------------------------
       
   383 //
       
   384 TInt CObexTestLogger::WriteLog(TLogLevel aLevel, TDes16& aLog) const
       
   385     {
       
   386     // Write log to file if file logger is available.
       
   387     if (iLog != 0)
       
   388         {
       
   389         iLog->Log(aLog);
       
   390         }
       
   391     // Remove the timestamp from the logging data.
       
   392     TPtrC16 log( aLog.Mid( KTimeStampStrLen ) );
       
   393 
       
   394     // Write log with RDebug.
       
   395     RDebug::Print(log);
       
   396 
       
   397     // Write log to screen if test module interface is available.
       
   398 	if (iTestModuleIf != 0)
       
   399         {
       
   400         CTestModuleIf* nonStaticLogger = const_cast<CTestModuleIf*>(iTestModuleIf);
       
   401         nonStaticLogger->Printf(aLevel, _L(""), log);
       
   402         }
       
   403 
       
   404     return KErrNone;
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CObexTestLogger::WriteLog
       
   409 // Writes log.
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TInt  CObexTestLogger::WriteLog(const TDesC16& aCategory, const TDesC16& aLog) const
       
   413     {
       
   414     // Write log to file if file logger is available.
       
   415     if (iLog != 0)
       
   416         {
       
   417         iLog->Log(aLog);
       
   418         }
       
   419     // Remove the timestamp from the logging data.
       
   420     TPtrC16 log( aLog.Mid( KTimeStampStrLen ) );
       
   421 
       
   422     // Write log with RDebug.
       
   423     RDebug::Print(log);
       
   424 
       
   425     // Write log to screen if test module interface is available.
       
   426     if (iTestModuleIf != 0)
       
   427         {
       
   428         CTestModuleIf* nonStaticLogger = const_cast<CTestModuleIf*>(iTestModuleIf);
       
   429         nonStaticLogger->Printf(0, aCategory, log);
       
   430         }
       
   431 
       
   432     return KErrNone;
       
   433     }
       
   434 
       
   435 // -----------------------------------------------------------------------------
       
   436 // CObexTestLogger::GetPrefix
       
   437 // Returns the prefix for a given logging level.
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 TPtrC CObexTestLogger::GetPrefix(TLogLevel aLevel)
       
   441 	{
       
   442 	// Select a prefix for a log level and return a pointer to it.
       
   443 	switch(aLevel)
       
   444 		{
       
   445 		case ETLError:
       
   446 			{
       
   447 			TPtrC logPrefix = _L("ERROR : ");
       
   448 			return logPrefix;
       
   449 			}
       
   450 		case ETLInfo:
       
   451 		case ETLResult: 
       
   452 			{
       
   453 			TPtrC logPrefix = _L("INFO  : ");
       
   454 			return logPrefix;
       
   455 			}
       
   456 		case ETLDebug:
       
   457 			{
       
   458 			TPtrC logPrefix = _L("DEBUG : ");
       
   459 			return logPrefix;
       
   460 			}
       
   461 		default:
       
   462 			{
       
   463 			// Return a null pointer.
       
   464 			return TPtrC();
       
   465 			}
       
   466 
       
   467 		}
       
   468 	}
       
   469 
       
   470 // -----------------------------------------------------------------------------
       
   471 // CObexTestLogger::CheckLogFile
       
   472 // Check if a log file is already existing, otherwise HTML formatting 
       
   473 // should be added.
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 TInt CObexTestLogger::CheckLogFile()
       
   477     {
       
   478     RThread thread;
       
   479     TInt id = thread.Id();
       
   480     TBuf<KMaxFileName > file;
       
   481     file.Format( KObexServAPITestSystemLogFullName, id );
       
   482     RFs fs;
       
   483     TInt err = fs.Connect();
       
   484     TBool open = EFalse;
       
   485     if( !err )
       
   486         {
       
   487         err = fs.IsFileOpen( file, open );
       
   488         }
       
   489     fs.Close();
       
   490     return err;
       
   491     }
       
   492 
       
   493 // -----------------------------------------------------------------------------
       
   494 // CObexTestLogger::LogTimeStamp
       
   495 // Log HTML head section and general information in a newly created logfile.
       
   496 // -----------------------------------------------------------------------------
       
   497 //
       
   498 void CObexTestLogger::LogHeadSectionL()
       
   499     {
       
   500     iLog->Log( KHtmlHeadSection );
       
   501     iLog->Log( KJSSECTION );
       
   502     TBuf<KSysUtilVersionTextLength> version( _L( "Version unknown!" ) );
       
   503     (void) SysUtil::GetSWVersion( version );
       
   504     TStifLoggerTimeStamp8 date;
       
   505     LogTimeStampL( date, EFalse );
       
   506     TStifLoggerTimeStamp8 time;
       
   507     LogTimeStampL( time );
       
   508     TPtrC timePtr( time.Mid( 6, 13 ) );
       
   509     iLog->Log( KHtmlStatSection, &date, &timePtr, &version );
       
   510     }
       
   511 
       
   512 // -----------------------------------------------------------------------------
       
   513 // CObexTestLogger::LogTimeStamp
       
   514 // Format a timestamp for logging.
       
   515 // If aTime is set to false, the (formatted) date is returned.
       
   516 // -----------------------------------------------------------------------------
       
   517 //
       
   518 void CObexTestLogger::LogTimeStampL(TDes& aBuf, TBool aTime) const
       
   519     {
       
   520     TTime time;
       
   521     time.HomeTime();    // Get time and format it
       
   522     if( aTime )
       
   523         {
       
   524         time.FormatL( aBuf, KFormatTimeStamp );
       
   525         }
       
   526     else
       
   527         {
       
   528         time.FormatL( aBuf, KFormatDate );
       
   529         }
       
   530     }