resourcemgmt/hwresourcesmgr/test/multiclient/common/log.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-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 // includes
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 #include "tc_log.h"
       
    20 #include <e32base.h>
       
    21 #include <e32cons.h>
       
    22 #include <stdio.h>
       
    23 
       
    24 #if (defined USE_TF_LOGGING)
       
    25 
       
    26 //#include <tflogger.h>
       
    27 #include <flogger.h>
       
    28 _LIT(KTfLogFolder,"TF");
       
    29 _LIT(KTfLogFile,"TFLOG.TXT");
       
    30 #define TFLOGTEXT(x) RFileLogger::Write(KTfLogFolder(),KTfLogFile(),EFileLoggingModeAppend, (x))
       
    31 
       
    32 #endif
       
    33 
       
    34 _LIT(KFailed,   "Failed  ");
       
    35 _LIT(KPassed,   "Passed  ");
       
    36 _LIT(KWarnings, "Warnings");
       
    37 _LIT(KComments, "Comments");
       
    38 
       
    39 /*
       
    40 ================================================================================= 
       
    41 Function:  Constructor
       
    42 
       
    43 Description: Constructor initialises line counters for middle summary report and
       
    44 			 for total summary report. Input arguments have been initialized also.
       
    45 			 Constructor opens log file, sets file pointer to beginning of the log file
       
    46 			 and prints starting text to log file.
       
    47 
       
    48 Inputs: CConsoleBase *aConsole  (pointer of CConsoleBase -class),
       
    49 		and next command lien switches:
       
    50 		TDesC &aFileName		(file name of log file (-f<file name>)), 
       
    51 		bool echoOn				(state of echoing (-n)),
       
    52 		Priority priorityFilter (verbosity level (-v<value>)). 
       
    53 
       
    54 Outputs: No return values
       
    55 			 
       
    56 =================================================================================
       
    57 */
       
    58 
       
    59 
       
    60 Log::Log()
       
    61 :iPassed(KPassed),
       
    62  iFailed(KFailed),
       
    63  iWarnings(KWarnings),
       
    64  iComments(KComments)
       
    65 {
       
    66 	iConsole = 0;
       
    67 	iBatch = EFalse;
       
    68 	iFile = 0;
       
    69 }
       
    70 
       
    71 void Log::OpenL(const TDesC &aFileName, TBool aBatch, TBool aEchoOn, TBool aTimestamp, Priority aPriorityFilter)
       
    72 {
       
    73 	// initialisation of arguments
       
    74     iPriorityFilter  = aPriorityFilter;
       
    75 	iWriteTimestamp  = aTimestamp;
       
    76 	iBatch           = aBatch;
       
    77 
       
    78 	// open console, but only if echo is on...
       
    79 	if (aEchoOn)
       
    80 	{
       
    81 		iConsole = Console::NewL(_L("TClient"), TSize(KConsFullScreen, KConsFullScreen));
       
    82 	}
       
    83 	
       
    84 	// open file
       
    85     TBuf8<MAX_LEN> fname;   // descriptor for the file name
       
    86     fname.Copy(aFileName);	// conversion from TDesC type to TBuf8<>  
       
    87     iFile = (FILE*)fopen((const char*) fname.PtrZ(), "a");
       
    88 	if (!iFile)
       
    89 	{
       
    90 		if (iConsole)
       
    91 		{
       
    92 			iConsole->Printf(_L("Can't open file %S\n"), &aFileName);
       
    93 			//iConsole->Getch();
       
    94 		}
       
    95 		User::Leave(KErrNotFound);
       
    96 	}
       
    97 
       
    98 	// seek to start of file
       
    99 	fseek((FILE*)iFile, 0, SEEK_END);		// sets file pointer to end of log file
       
   100 
       
   101 	// mark session as started..
       
   102 	Printf(Log::SESSION_MARK, _L("*******************************************\nBEGIN LOG\n"));	// starting the session text
       
   103 }
       
   104 
       
   105 /*
       
   106 ================================================================================= 
       
   107 Function:  Destructor
       
   108 
       
   109 Description: Destructor prints total summary report to log file and console if 
       
   110 			 allowed. Finally destructor close the log file and console.
       
   111 			
       
   112 Inputs:  No inputs
       
   113 Outputs: No return values
       
   114 
       
   115 =================================================================================
       
   116 */
       
   117 
       
   118 Log::~Log()
       
   119 {
       
   120     if (iFile) 
       
   121 	{
       
   122 		// print total summary report 
       
   123 		Printf(SUMMARY, _L("SESSION TOTAL"));
       
   124 		iFailed.Total(*this);
       
   125 		iPassed.Total(*this);
       
   126 		iWarnings.Total(*this);
       
   127 		iComments.Total(*this);
       
   128 		Printf(SESSION_MARK, _L("END LOG"));
       
   129 		fclose((FILE*)iFile);
       
   130 	}
       
   131 
       
   132 	if (iConsole)
       
   133 	{
       
   134 		// if printing to console is allowed. then next line will be printed and console window will
       
   135 		// closed only after pressing return.
       
   136 		if (!iBatch)
       
   137 		{
       
   138 			iConsole->Printf(_L("[press return to continue]\n"));
       
   139 			iConsole->Getch();
       
   140 		}
       
   141 		delete iConsole;
       
   142 	}
       
   143 }
       
   144 
       
   145 
       
   146 /*
       
   147 ================================================================================= 
       
   148 Function:  SummaryReport
       
   149 
       
   150 Description: Prints middle summary report. Summary lines includes number of 
       
   151 			 lines between script file commands begin and end   
       
   152 
       
   153 Inputs: TBuf<MAX_LEN> aTestCaseName Name of test case; 
       
   154 Output: No return values	
       
   155 
       
   156 
       
   157 =================================================================================
       
   158 */
       
   159 
       
   160 void Log::SummaryReport(const TDesC& aTestCaseName)
       
   161 {
       
   162 	if (iFile)
       
   163     {
       
   164 		Write(SUMMARY, aTestCaseName);
       
   165 		iFailed.Summary(*this);
       
   166 		iPassed.Summary(*this);
       
   167 		iWarnings.Summary(*this);
       
   168 		iComments.Summary(*this);
       
   169     }
       
   170 
       
   171 }
       
   172 
       
   173 
       
   174 
       
   175 void Log::RequestKeyPress()
       
   176 {
       
   177 	_LIT(KPressReturn, "[press return to contine]\n");
       
   178 
       
   179 	if (iConsole && !iBatch)
       
   180 	{
       
   181 		iConsole->Write(KPressReturn);
       
   182 		iConsole->Read(iConsoleKeyRequest); // request key press
       
   183 	}
       
   184 }
       
   185 
       
   186 
       
   187 TBool Log::ReceiveKeyPress()
       
   188 {
       
   189 	TBool received = EFalse;
       
   190 	if (iConsole && !iBatch)
       
   191 	{
       
   192 		if (iConsoleKeyRequest == KRequestPending)
       
   193 		{
       
   194 			received = EFalse; // still waiting
       
   195 		}
       
   196 		else
       
   197 		{
       
   198 			User::WaitForRequest(iConsoleKeyRequest);
       
   199 			if (iConsole->KeyCode() == '\r')
       
   200 			{
       
   201 				received = ETrue; // got the right key
       
   202 			}
       
   203 			else
       
   204 			{
       
   205 				iConsole->Read(iConsoleKeyRequest);
       
   206 				received = EFalse; // wait some more (wrong key)
       
   207 			}
       
   208 		}
       
   209 	} else
       
   210 	{
       
   211 		received = ETrue; // simulate received
       
   212 	}
       
   213 	return received;
       
   214 }
       
   215 
       
   216 void Log::WaitKeyPress()
       
   217 {
       
   218 	if (iConsole && !iBatch)
       
   219 	{
       
   220 		TBool waiting = ETrue;
       
   221 		while (waiting)
       
   222 		{
       
   223 			User::WaitForRequest(iConsoleKeyRequest);
       
   224 			if (iConsole->KeyCode() == '\r')
       
   225 			{
       
   226 				waiting = EFalse;
       
   227 			}
       
   228 			else
       
   229 			{
       
   230 				iConsole->Read(iConsoleKeyRequest);
       
   231 			}
       
   232 		}
       
   233 	}
       
   234 }
       
   235 
       
   236 TInt Log::MultipleChoice(TInt aMin, TInt aMax)
       
   237 {
       
   238 	TInt choice = aMin;
       
   239 	if (iConsole && !iBatch)
       
   240 	{
       
   241 		do
       
   242 		{
       
   243 			iConsole->Printf(_L("[Press key %d...%d]\n"), aMin, aMax);
       
   244 			iConsole->Read(iConsoleKeyRequest);
       
   245 			User::WaitForRequest(iConsoleKeyRequest);
       
   246 			choice = TInt(iConsole->KeyCode()) - TInt('0');
       
   247 		} while (choice < aMin || choice > aMax);
       
   248 	}
       
   249 	return choice;
       
   250 }
       
   251 
       
   252 void Log::SetTitle(const TDesC& aTitle)
       
   253 {
       
   254 	if (iConsole)
       
   255 	{
       
   256 		iConsole->SetTitle(aTitle);
       
   257 	}
       
   258 }
       
   259 
       
   260 
       
   261 /*
       
   262 ================================================================================= 
       
   263 Function:  Printf
       
   264 
       
   265 Description: This method adds line counters prints,  C-string format information to logfile.
       
   266 
       
   267 Inputs: Priority priority, const char *fmt This method can have variant number 
       
   268 		or arguments prints only allowed lines to buffer. Content of buffer will
       
   269 		be printed to log file and to console if allowed.
       
   270 
       
   271 Ouputs	No return values:
       
   272 
       
   273 
       
   274 =================================================================================
       
   275 */
       
   276 
       
   277 void Log::Printf(Priority priority, const char *fmt, ...)
       
   278 {
       
   279 	// route to another printf..
       
   280 	TInt   len = 0;
       
   281 
       
   282     __e32_va_list list;
       
   283     va_start(list, fmt);
       
   284     len = vsprintf((char*)iUint8, fmt, list);
       
   285 	va_end(list);
       
   286 
       
   287 	iStr.Copy(TPtrC8(iUint8,len) );
       
   288 	Write(priority, iStr);
       
   289 
       
   290 }
       
   291 
       
   292 
       
   293 
       
   294 
       
   295 
       
   296 /*
       
   297 ================================================================================= 
       
   298 Function:  Printf
       
   299 
       
   300 Description: This method prints TDesC -format information to logfile.
       
   301 
       
   302 Inputs: TRefByValue<const TDesC> aFmt. This method can 
       
   303 		have variant number of arguments.
       
   304 
       
   305 
       
   306 =================================================================================
       
   307 */
       
   308 void Log::Printf(TRefByValue<const TDesC> aFmt, ...)
       
   309 {
       
   310 	// rest of printf...
       
   311 	VA_LIST list;
       
   312     VA_START(list,aFmt);
       
   313 	iStr.FormatList(aFmt, list);
       
   314 	VA_END(list);
       
   315 	Write(Log::COMMENT, iStr);
       
   316 }
       
   317 
       
   318 /*
       
   319 ================================================================================= 
       
   320 Function:  Printf
       
   321 
       
   322 Description: This method prints TDesC -format information to logfile.
       
   323 
       
   324 Inputs: Priority priority, TRefByValue<const TDesC> aFmt. This method can 
       
   325 		have variant number of arguments.
       
   326 
       
   327 
       
   328 =================================================================================
       
   329 */
       
   330 void Log::Printf(Priority priority, TRefByValue<const TDesC> aFmt, ...)
       
   331 {
       
   332 	// rest of printf...
       
   333 	VA_LIST list;
       
   334     VA_START(list,aFmt);
       
   335 	iStr.FormatList(aFmt, list);
       
   336 	VA_END(list);
       
   337 	Write(priority, iStr);
       
   338 }
       
   339 
       
   340 /*
       
   341 ================================================================================= 
       
   342 Function:  Write
       
   343 
       
   344 Description: This method writes TDesC -format information to logfile.
       
   345 
       
   346 Inputs: Priority priority, const TDesC& aFmt.
       
   347 
       
   348 
       
   349 =================================================================================
       
   350 */
       
   351 
       
   352 void Log::Write(Priority priority, const TDesC& aStr)
       
   353 {
       
   354 	// names of priorities
       
   355   //don't need these (paivheik)
       
   356 	/* static TText *priorityNames[] = {
       
   357         (TText*)L"ZERO    ",
       
   358         (TText*)L"PASS    ",
       
   359         (TText*)L"NOTIFY  ",
       
   360 		(TText*)L"SCHEDUL ",
       
   361 		(TText*)L"MONITOR ",
       
   362         (TText*)L"WARNING ",
       
   363         (TText*)L"FAIL    ",
       
   364         (TText*)L"COMMENT ",
       
   365         (TText*)L"SUMMARY ",
       
   366         (TText*)L"SESSION ",
       
   367 		(TText*)L"BEGIN   ",
       
   368 		(TText*)L"END     "
       
   369     };
       
   370    
       
   371 	*/
       
   372 
       
   373    // add line counters
       
   374     switch (priority)
       
   375     {
       
   376     case PASS:    iPassed++; break;
       
   377 	case FAIL:    iFailed++; break;
       
   378 	case WARNING: iWarnings++; break;
       
   379     case COMMENT: iComments++; break;
       
   380 	default: break;
       
   381     }
       
   382 
       
   383     // filter out unnecessary stuff
       
   384    if ((int)iPriorityFilter > (int)priority)
       
   385         return;
       
   386 
       
   387 
       
   388    // get current time time and format it to string
       
   389 	TBuf<20> time_str;
       
   390     iTime.UniversalTime();
       
   391     TRAPD(error, iTime.FormatL(time_str, _L("%H:%T:%S:%*C3")) );
       
   392     if (error != KErrNone)
       
   393 	{
       
   394 		time_str.Copy(_L("TIMER-ERROR"));
       
   395 	}
       
   396 
       
   397 
       
   398 	// cleaning iLog_bufout
       
   399 	iLog_bufout.Delete(0,MAX_LEN*2);
       
   400 	iLog_bufout.SetLength(0);
       
   401 
       
   402 	// priority name and line number
       
   403 
       
   404 	//don't need in these tests,
       
   405 /*	TPtrC priName(priorityNames[priority]);
       
   406 	iLog_bufout.AppendFormat(_L(" %S (%3d)\t"), &priName, Lex::GetLineNo());*/
       
   407 
       
   408 	// rest of printf...
       
   409 	iLog_bufout.Append(aStr);
       
   410 
       
   411 
       
   412 	// Log can ouput to three diffrent destinations
       
   413 	// (at the same time). Console window,
       
   414 	// text file, or flogger.
       
   415 	
       
   416 	// output to console
       
   417 	if (iConsole)
       
   418 	{
       
   419 		iConsole->Write(time_str);
       
   420 		iConsole->Write(iLog_bufout);
       
   421 		iConsole->Write(_L("\n"));
       
   422 	}
       
   423 
       
   424 	// output to file
       
   425 	if (iFile)
       
   426 	{
       
   427 		// cleaning iOut8
       
   428 		iOut8.Delete(0,MAX_LEN*2);
       
   429 		iOut8.SetLength(0);
       
   430 		if (iWriteTimestamp)
       
   431 		{
       
   432 			iOut8.Copy(time_str);
       
   433 			fputs((char*)iOut8.PtrZ(), (FILE*)iFile);
       
   434 		}
       
   435 		iOut8.Copy(iLog_bufout);
       
   436 		fputs((char*)iOut8.PtrZ(), (FILE*)iFile);
       
   437 		fputs("\r\n", (FILE*)iFile);
       
   438 		fflush((FILE*)iFile);
       
   439 	}
       
   440 
       
   441 #if (defined USE_TF_LOGGING)
       
   442 	// output to flogger
       
   443 	{
       
   444 		_LIT(KTClient, "TC:");
       
   445 		iLog_bufout.Insert(0, KTClient);
       
   446 		TFLOGTEXT(iLog_bufout);
       
   447 	}
       
   448 #endif
       
   449 
       
   450 }
       
   451 
       
   452