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