testexecfw/stf/stfext/testmodules/teftestmod/teftestmodulefw/logger/src/client.cpp
changeset 2 8bb370ba6d1d
equal deleted inserted replaced
1:bbd31066657e 2:8bb370ba6d1d
       
     1 /*
       
     2 * Copyright (c) 2005-2009 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 * Source file for the client api
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  @file Client.cpp
       
    23 */
       
    24 #include <test/testexecutelog.h>
       
    25 
       
    26 
       
    27 const TInt KMaxFilename = 50;
       
    28 const TInt KLowMemBufLength = 128;
       
    29 
       
    30 // EKA1 requires DLL entry point
       
    31 
       
    32 
       
    33 EXPORT_C TInt RTestExecuteLogServ::Connect()
       
    34 /**
       
    35  * @return int - Standard error codes
       
    36  * EKA2 all variants and EKA1 target.
       
    37  * Server is an exe
       
    38  */
       
    39 	{
       
    40 	TVersion version(KTestExecuteLoggerMajorVersion,KTestExecuteLoggerMinorVersion,KTestExecuteLoggerBuildVersion);
       
    41 	// Assume the server is already running and attempt to create a session
       
    42 	TInt err = CreateSession(KTestExecuteLogServerName,version,8);
       
    43 	if(err == KErrNotFound)
       
    44 		{
       
    45 		// Server not running
       
    46 		// Construct the server binary name
       
    47 		_LIT(KEmpty,"");
       
    48 		// EKA2 is simple
       
    49 		// No path required
       
    50 		TBuf<32> serverFile;
       
    51 		serverFile.Copy(KTestExecuteLogServerName);
       
    52 		_LIT(KExe,".exe");
       
    53 		serverFile.Append(KExe);
       
    54 		RProcess server;
       
    55 		err = server.Create(serverFile,KEmpty);
       
    56 		if(err != KErrNone)
       
    57 			return err;
       
    58 		// Synchronise with the server
       
    59 		TRequestStatus reqStatus;
       
    60 		server.Rendezvous(reqStatus);
       
    61 		server.Resume();
       
    62 		// Server will call the reciprocal static synchronise call
       
    63 		User::WaitForRequest(reqStatus);
       
    64 		server.Close();
       
    65 		if(reqStatus.Int() != KErrNone)
       
    66 			return reqStatus.Int();
       
    67 		// Create the root server session
       
    68 		err = CreateSession(KTestExecuteLogServerName,version,8);
       
    69 		}
       
    70 
       
    71 #ifdef TEF_SHARE_AUTO
       
    72 	// Makes the session shared among all threads in the process
       
    73   	if( err == KErrNone )
       
    74   		{
       
    75   		err = ShareAuto();
       
    76   		}
       
    77 #endif
       
    78 
       
    79 	return err;
       
    80 	}
       
    81 
       
    82 ///////
       
    83 EXPORT_C TInt RTestExecuteLogServ::CreateLog(const TDesC& aLogFilePath, TLogMode aMode)
       
    84 /**
       
    85  * @param aLogFilePath - Full path and filename of the log file
       
    86  * @param aMode - Overwrite or Append
       
    87  * Makes synchronous call to the log server to create a log session
       
    88  */
       
    89 	{
       
    90 	if(aLogFilePath.Length() > KMaxTestExecuteLogFilePath)
       
    91 		return KErrTooBig;
       
    92 
       
    93 	iLogLevel = TLogSeverity(ESevrAll);
       
    94 	TIpcArgs args;
       
    95 	args.Set(0,&aLogFilePath);
       
    96 	args.Set(1,aMode);
       
    97 	return SendReceive(ECreateLog,args);
       
    98 
       
    99 	}
       
   100 
       
   101 EXPORT_C void RTestExecuteLogServ::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,TRefByValue<const TDesC> aFmt,...)
       
   102 /**
       
   103  * @param aFile - Source file name
       
   104  * @param aLine - Source file line number
       
   105  * @param aSeverity - ERR, WARN, INFO
       
   106  * @param aFmt - UNICODE format string
       
   107  */
       
   108 	{
       
   109 	if (aSeverity>iLogLevel) 
       
   110 		{
       
   111 		return;
       
   112 		}
       
   113 	// Set up a Variable argument list and call private method
       
   114 	VA_LIST aList;
       
   115 	VA_START(aList, aFmt);
       
   116 	LogExtra(aFile, aLine, aSeverity, aFmt, aList);
       
   117 	VA_END(aList); 
       
   118 	}
       
   119 
       
   120 EXPORT_C void RTestExecuteLogServ::WriteFormat(TRefByValue<const TDesC> aFmt,...)
       
   121 /**
       
   122  * @param aFmt - UNICODE format string
       
   123  */
       
   124 	{
       
   125 	// Set up a Variable argument list and call private method
       
   126 	VA_LIST aList;
       
   127 	VA_START(aList, aFmt);
       
   128 	WriteFormat(aFmt, aList);
       
   129 	VA_END(aList); 
       
   130 	}
       
   131 
       
   132 
       
   133 EXPORT_C void RTestExecuteLogServ::Write(const TDesC& aLogBuffer)
       
   134 /**
       
   135  * @param aLogBuffer - Pre-formatted UNICODE buffer
       
   136  */
       
   137 	{
       
   138 	// Just call private method
       
   139 	// Ignore errors. Could put in ASSERT
       
   140 	TRAP_IGNORE(WriteL(aLogBuffer));
       
   141 	}
       
   142 
       
   143 
       
   144 EXPORT_C void RTestExecuteLogServ::WriteFormat(TRefByValue<const TDesC8> aFmt,...)
       
   145 /**
       
   146  * @param aFmt - Narrow format string
       
   147  */
       
   148 	{
       
   149 	// Set up a Variable argument list and call private method
       
   150 	VA_LIST aList;
       
   151 	VA_START(aList, aFmt);
       
   152 	WriteFormat(aFmt, aList);
       
   153 	VA_END(aList); 
       
   154 	}
       
   155 
       
   156 
       
   157 EXPORT_C void RTestExecuteLogServ::Write(const TDesC8& aLogBuffer)
       
   158 /**
       
   159  * @param aLogBuffer - Pre-formatted Narrow buffer
       
   160  */
       
   161 	{
       
   162 	// Create a larger buffer for adding terminators
       
   163 	// Could do more formatting but just allow for CRLF plus time stamp for the time being
       
   164 	HBufC8* buffer = HBufC8::New(aLogBuffer.Length()+20);
       
   165 	if(buffer)
       
   166 		{
       
   167 		TPtr8 ptr(buffer->Des());
       
   168 		AddTime(ptr);
       
   169 		ptr.Append(aLogBuffer);
       
   170 		// Ignore error for the time being. Could do an ASSERT
       
   171 		TRAP_IGNORE(WriteL(ptr));
       
   172 		delete buffer;
       
   173 		}
       
   174 	}
       
   175 
       
   176 void RTestExecuteLogServ::AddTime(TDes8& aLogBuffer)
       
   177 	{
       
   178 	TTime now;
       
   179 	now.UniversalTime();
       
   180 	TDateTime dateTime = now.DateTime();
       
   181 	_LIT8(KFormat,"%02d:%02d:%02d:%03d ");
       
   182 	// add the current time 
       
   183 	aLogBuffer.AppendFormat(KFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),(dateTime.MicroSecond()/1000)); 
       
   184 	}
       
   185 
       
   186 EXPORT_C void RTestExecuteLogServ::LogExtra(const TText8* aFile, TInt aLine, TInt aSeverity,TRefByValue<const TDesC> aFmt, VA_LIST aList)
       
   187 /**
       
   188  * @param aFile - Source file name
       
   189  * @param aLine - Source file line number
       
   190  * @param aSeverity - ERR, WARN, INFO
       
   191  * @param aFmt - UNICODE format string
       
   192  * @param aList - Variable argument list
       
   193  *
       
   194  * Format a log output line
       
   195  */
       
   196 	{
       
   197 	if (aSeverity>iLogLevel) 
       
   198 		{
       
   199 		return;
       
   200 		}
       
   201 	// Create a filename string
       
   202 	TBuf16<KMaxFilename> fileName;
       
   203 	GetCPPModuleName(fileName, aFile);
       
   204 	// Create a buffer for formatting
       
   205 	HBufC* buffer = HBufC::New(KMaxTestExecuteLogLineLength*2);
       
   206 	if (buffer)
       
   207 		{
       
   208 		TPtr ptr(buffer->Des());
       
   209 		_LIT(KErr,"ERROR - ");
       
   210 		_LIT(KHigh,"HIGH - ");
       
   211 		_LIT(KWarn,"WARN - ");
       
   212 		_LIT(KMedium,"MEDIUM - ");
       
   213 		_LIT(KInfo,"INFO - ");
       
   214 		_LIT(KLow,"LOW - ");
       
   215 		_LIT(KFormat," %d %S %d ");
       
   216 		if(aSeverity == ESevrErr)
       
   217 			ptr.Append(KErr);
       
   218 		else if(aSeverity == ESevrHigh)
       
   219 			ptr.Append(KHigh);
       
   220 		else if(aSeverity == ESevrWarn)
       
   221 			ptr.Append(KWarn);
       
   222 		else if(aSeverity == ESevrMedium)
       
   223 			ptr.Append(KMedium);
       
   224 		else if (aSeverity == ESevrInfo)
       
   225 			ptr.Append(KInfo);
       
   226 		else if(aSeverity == ESevrLow)
       
   227 			ptr.Append(KLow);
       
   228 		else //if(aSeverity == ESevrAll)
       
   229 			ptr.Append(KInfo);
       
   230 		// Add the thread id
       
   231 		ptr.AppendFormat(KFormat,(TInt)RThread().Id(),&fileName, aLine);
       
   232 		TTEFDes16Overflow des16OverflowObject;
       
   233 		ptr.AppendFormatList(aFmt, aList, &des16OverflowObject);
       
   234 		
       
   235 
       
   236 		TRAP_IGNORE(WriteL(ptr));
       
   237 		delete buffer;
       
   238 		}
       
   239 	else
       
   240 		{
       
   241 		_LIT(KWarn,"WARN - ");
       
   242 		TBuf8<KLowMemBufLength> noMemory;
       
   243 		AddTime(noMemory);
       
   244 		noMemory.Append(KWarn);
       
   245 		noMemory.AppendFormat(_L8(" %d "), (TInt)RThread().Id());
       
   246 		noMemory.Append(fileName);
       
   247 		noMemory.AppendFormat(_L8(" %d %S"), aLine, &KLoggerNotEnoughMemory8());
       
   248 		TRAP_IGNORE(WriteL(noMemory));
       
   249 		}
       
   250 	}
       
   251 
       
   252 EXPORT_C void RTestExecuteLogServ::WriteFormat(TRefByValue<const TDesC> aFmt, VA_LIST aList)
       
   253 /**
       
   254  * @param aFmt - UNICODE format string
       
   255  * @param aList - Variable argument list
       
   256  */
       
   257 	{
       
   258 	HBufC* buffer = HBufC::New(KMaxTestExecuteLogLineLength*2);
       
   259 	if (buffer)
       
   260 		{
       
   261 		TPtr ptr(buffer->Des());
       
   262 		TTEFDes16Overflow des16OverflowObject;
       
   263 		ptr.AppendFormatList(aFmt, aList, &des16OverflowObject);
       
   264 
       
   265 		TRAP_IGNORE(WriteL(ptr));
       
   266 		delete buffer;
       
   267 		}
       
   268 	else
       
   269 		{
       
   270 		TRAP_IGNORE(WriteL((TDes8&)KLoggerNotEnoughMemory8()));
       
   271 		}
       
   272 	}
       
   273 
       
   274 EXPORT_C void RTestExecuteLogServ::WriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST aList)
       
   275 /**
       
   276  * @param aFmt - UNICODE format string
       
   277  * @param aList - Variable argument list
       
   278  */
       
   279 	{
       
   280 	HBufC8* buffer = HBufC8::New(KMaxTestExecuteLogLineLength*2);
       
   281 	if (buffer)
       
   282 		{
       
   283 		TPtr8 ptr(buffer->Des());
       
   284 		AddTime(ptr);
       
   285 		TTEFDes8Overflow des8OverflowObject;
       
   286 		ptr.AppendFormatList(aFmt, aList, &des8OverflowObject);
       
   287 
       
   288 		TRAP_IGNORE(WriteL(ptr));
       
   289 		delete buffer;
       
   290 		}
       
   291 	else
       
   292 		{
       
   293 		TRAP_IGNORE(WriteL((TDes8&)KLoggerNotEnoughMemory8()));
       
   294 		}
       
   295 	}
       
   296 
       
   297 void RTestExecuteLogServ::WriteL(const TDesC& aLogBuffer)
       
   298 /**
       
   299  * @param aLogBuffer - UNICODE buffer
       
   300  */
       
   301 	{
       
   302 	HBufC8* buffer = NULL;
       
   303 	TRAPD(err, buffer=HBufC8::NewL(aLogBuffer.Length()+20));
       
   304 	if (KErrNoMemory == err)
       
   305 		{
       
   306 		_LIT8(KWarn,"WARN - ");
       
   307 		TInt startPos = 13; // The place change "INFO" to "WARN"
       
   308 		TInt endPos = 7;
       
   309 		TInt wordCount = 5; // The message before the actual log info
       
   310 		TBuf8<KLowMemBufLength> buffer;
       
   311 		AddTime(buffer);
       
   312 		TLex lex(aLogBuffer);
       
   313 		lex.Mark();
       
   314 		for (TInt i=0; i<wordCount; i++)
       
   315 			{
       
   316 			lex.NextToken();
       
   317 			}
       
   318 		lex.SkipSpace();
       
   319 		buffer.Append(lex.MarkedToken());
       
   320 		buffer.Append(KLoggerNotEnoughMemory8);
       
   321 		buffer.Replace(startPos, endPos, KWarn);
       
   322 		WriteL(buffer);
       
   323 		}
       
   324 	else
       
   325 		{
       
   326 		CleanupStack::PushL(buffer);
       
   327 	  	TPtr8 ptr(buffer->Des());
       
   328 	  	AddTime(ptr);
       
   329 	  	ptr.Append(aLogBuffer);
       
   330 	  	WriteL(ptr);
       
   331 	  
       
   332 	  	CleanupStack::PopAndDestroy(buffer);
       
   333 		}
       
   334 	}
       
   335 
       
   336 void RTestExecuteLogServ::WriteL(TDes8& aLogBuffer)
       
   337 /**
       
   338  * @param aLogBuffer - pre-formatted narrow buffer
       
   339  * 
       
   340  * Synchronous write to the server
       
   341  */
       
   342 	{
       
   343 	_LIT8(KEnd,"\r\n");
       
   344 	// Check to see if there's room to add CRLF
       
   345 	if(aLogBuffer.Length()+2 > aLogBuffer.MaxLength())
       
   346 		{
       
   347 		HBufC8* buffer = NULL;
       
   348 		TRAPD(err, buffer=HBufC8::NewL(aLogBuffer.Length()+2));
       
   349 		if (KErrNoMemory == err)
       
   350 			{
       
   351 			aLogBuffer.Replace(aLogBuffer.Length()-2, 2, KEnd);
       
   352 			TIpcArgs args;
       
   353 			args.Set(0,&aLogBuffer);
       
   354 			args.Set(1,aLogBuffer.Length());
       
   355 			User::LeaveIfError(SendReceive(EWriteLog,args));
       
   356 			}
       
   357 		else
       
   358 			{
       
   359 			CleanupStack::PushL(buffer);
       
   360 			TPtr8 ptr(buffer->Des());
       
   361 			ptr.Copy(aLogBuffer);
       
   362 			ptr.Append(KEnd);
       
   363 			TIpcArgs args;
       
   364 			args.Set(0,&ptr);
       
   365 			args.Set(1,ptr.Length());
       
   366 			User::LeaveIfError(SendReceive(EWriteLog,args));
       
   367 			CleanupStack::PopAndDestroy(buffer);
       
   368 			}
       
   369 		}
       
   370 	else
       
   371 		{
       
   372 		aLogBuffer.Append(KEnd);
       
   373 		TIpcArgs args;
       
   374 		args.Set(0,&aLogBuffer);
       
   375 		args.Set(1,aLogBuffer.Length());
       
   376 		User::LeaveIfError(SendReceive(EWriteLog,args));
       
   377 		}
       
   378 	}
       
   379 
       
   380 void RTestExecuteLogServ::GetCPPModuleName(TDes& aModuleName, const TText8* aCPPFileName)
       
   381 /**
       
   382  * @return aModuleName - Filename in descriptor
       
   383  * @param aCppFileName - Filename
       
   384  * Borrowed from scheduletest
       
   385  */
       
   386 	{
       
   387 	TPtrC8 fileNamePtrC8(aCPPFileName);
       
   388 	// We do our own filename munging here; TParse can't help us since that's
       
   389 	// expressly for EPOC filepaths and here we've got whatever the build system is
       
   390 	// At present Win32 and Unix directory delimiters are supported
       
   391 	TInt lastDelimiter = Max(fileNamePtrC8.LocateReverse('\\'), fileNamePtrC8.LocateReverse('/'));
       
   392 	if(lastDelimiter >= 0 && lastDelimiter < fileNamePtrC8.Length() - 1)
       
   393 		{
       
   394 		// Found a delimiter which isn't trailing; update the ptr to start at the next char
       
   395 		TInt fileNameLen = Min(KMaxFilename, fileNamePtrC8.Length() - (lastDelimiter + 1));
       
   396 		fileNamePtrC8.Set(aCPPFileName + lastDelimiter + 1, fileNameLen);
       
   397 		}
       
   398 	else
       
   399 		{
       
   400 		// Didn't find a delimiter; take as much of the right-end of the name as fits
       
   401 		fileNamePtrC8.Set(aCPPFileName + Max(0, fileNamePtrC8.Length() - KMaxFilename), Min(fileNamePtrC8.Length(), KMaxFilename));
       
   402 		}
       
   403 	aModuleName.Copy(fileNamePtrC8);
       
   404 	}
       
   405 
       
   406 EXPORT_C void RTestExecuteLogServ::SetLogLevel(TLogSeverity aSeverity)
       
   407 	{
       
   408 	iLogLevel = aSeverity;
       
   409 	}