testexecfw/stf/stfext/testmodules/teftestmod/testexecmdw/filelogger/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 <rfilelogger.h>
       
    25 
       
    26 
       
    27 //_LIT8(KxmlHeader,"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\r\n");
       
    28 
       
    29 
       
    30 // EKA1 requires DLL entry point
       
    31 //
       
    32 // RFileLogger class definition
       
    33 //
       
    34 
       
    35 EXPORT_C RFileFlogger::RFileFlogger() : ilogbody(NULL)
       
    36 /**
       
    37  * Create a new flogger client interface object with an empty body.
       
    38  * @internalTechnology 
       
    39  */
       
    40 	{
       
    41 	iLogfileTag=FALSE;
       
    42 	}
       
    43 
       
    44 EXPORT_C RFileFlogger::~RFileFlogger()
       
    45 /**
       
    46  * Destructor
       
    47  * @internalTechnology 
       
    48  */
       
    49 	{}
       
    50 
       
    51 
       
    52 
       
    53 EXPORT_C TInt RFileFlogger::Connect()
       
    54 /**
       
    55  * @return int - Standard error codes
       
    56  * EKA2 all variants and EKA1 target.
       
    57  * Server is an exe
       
    58  */
       
    59 	{
       
    60 	// Sanity check to make sure it's not been called multiple times
       
    61 	if (ilogbody)
       
    62 		{
       
    63 		return KErrAlreadyExists;
       
    64 		}
       
    65 	ilogbody = new RFileLoggerBody;
       
    66 	if(!ilogbody)
       
    67 		return KErrNoMemory;
       
    68 
       
    69 	TVersion version(KRFileLoggerMajorVersion,KRFileLoggerMinorVersion,KRFileLoggerBuildVersion);
       
    70 	// Assume the server is already running and attempt to create a session
       
    71 	
       
    72 	TInt err = ilogbody->DoCreateSession(KFileLogrerServerName,version,8);
       
    73 	if(err== KErrServerTerminated)
       
    74 	{
       
    75 		User::After(1000000); // OS need time to close previous server properly
       
    76 		err = ilogbody->DoCreateSession(KFileLogrerServerName,version,8);
       
    77 	}
       
    78 
       
    79 	if(err == KErrNotFound)
       
    80 		{
       
    81 		// Server not running
       
    82 		// Construct the server binary name
       
    83 		_LIT(KEmpty,"");
       
    84 		// EKA2 is simple
       
    85 		// No path required
       
    86 		TBuf<32> serverFile;
       
    87 		serverFile.Copy(KFileLogrerServerName);
       
    88 		_LIT(KExe,".exe");
       
    89 		serverFile.Append(KExe);
       
    90 		RProcess server;
       
    91 		err = server.Create(serverFile,KEmpty);
       
    92 		if(err != KErrNone)
       
    93 			return err;
       
    94 		// Synchronise with the server
       
    95 		TRequestStatus reqStatus;
       
    96 		server.Rendezvous(reqStatus);
       
    97 		server.Resume();
       
    98 		// Server will call the reciprocal static synchronise call
       
    99 		User::WaitForRequest(reqStatus);
       
   100 		server.Close();
       
   101 		if(reqStatus.Int() != KErrNone)
       
   102 			return reqStatus.Int();
       
   103 		// Create the root server session
       
   104 		err = ilogbody->DoCreateSession(KFileLogrerServerName,version,8);
       
   105 		}
       
   106 		if (err != KErrNone)
       
   107 		{  // some other problem, kill the logbody and clean the mamory
       
   108 			delete ilogbody;
       
   109 			ilogbody=NULL;
       
   110 		}
       
   111 		else
       
   112 		{   // Makes the session shared among all threads in the process
       
   113 			err = ilogbody->ShareAuto();
       
   114 		}
       
   115 
       
   116 
       
   117 	return err;
       
   118 	}
       
   119 
       
   120 ///////
       
   121 EXPORT_C TInt RFileFlogger::CreateLog(const TDesC& aLogFilePath, TLogMode aMode)
       
   122 /**
       
   123  * @param aLogFilePath - Full path and filename of the log file
       
   124  * @param aMode - Overwrite or Append
       
   125  * Makes synchronous call to the log server to create a log session
       
   126  */
       
   127 	{
       
   128 	iloglevel = ESevrAll; //ELogNone;
       
   129 
       
   130 	if(aLogFilePath.Length() > KMaxLoggerFilePath)
       
   131 		return KErrTooBig;
       
   132 
       
   133 	TIpcArgs args;
       
   134 	args.Set(0,&aLogFilePath);
       
   135 	args.Set(1,aMode);
       
   136     TInt err = ilogbody->DoSendReceive(ECreateLog,args);
       
   137     return err; 
       
   138 
       
   139 	}
       
   140 
       
   141 EXPORT_C void RFileFlogger::Log(const TText8* aFile, TInt aLine, TLogSeverity aSeverity, TRefByValue<const TDesC> aFmt,...)
       
   142 /**
       
   143  * @param aFile - Source file name
       
   144  * @param aLine - Source file line number
       
   145  * @param aSeverity - ERR, WARN, INFO
       
   146  * @param aFmt - UNICODE format string
       
   147  */
       
   148 	{
       
   149 	// Set up a Variable argument list and call private method
       
   150 	if (aSeverity>iloglevel && aSeverity != ESevrTEFUnit) 
       
   151 		{
       
   152 		return;
       
   153 		}
       
   154 	VA_LIST aList;
       
   155 	VA_START(aList, aFmt);
       
   156 	Log(aFile, aLine, aSeverity, aFmt, aList);
       
   157 	VA_END(aList); 
       
   158 
       
   159 	}
       
   160 EXPORT_C void RFileFlogger::Log(const TText8* aFile, TInt aLine, TLogSeverity aSeverity,TInt arraylength, TExtraLogField* aLogFields, TRefByValue<const TDesC> aFmt,...)
       
   161 /**
       
   162  * @param aFile - Source file name
       
   163  * @param aLine - Source file line number
       
   164  * @param aSeverity - ERR, WARN, INFO
       
   165  * @param aFmt - UNICODE format string
       
   166  */
       
   167 	{
       
   168 	// Set up a Variable argument list and call private method
       
   169 	if (aSeverity>iloglevel && aSeverity != ESevrTEFUnit) 
       
   170 		{
       
   171 		return;
       
   172 		}
       
   173 	VA_LIST aList;
       
   174 	VA_START(aList, aFmt);
       
   175 	Log(aFile, aLine, aSeverity, arraylength, aLogFields, aFmt, aList);
       
   176 	VA_END(aList); 
       
   177 	}
       
   178 
       
   179 
       
   180 void RFileFlogger::AddTime(TDes8& aLogBuffer)
       
   181 	{
       
   182 	TTime now;
       
   183 	now.UniversalTime();
       
   184 	TDateTime dateTime = now.DateTime();
       
   185 	_LIT8(KFormat,"%02d:%02d:%02d:%03d");
       
   186 	// add the current time. 
       
   187 /*--------- Maintaince Warning for aLogBuffer -----------------------------------
       
   188 ******* the fomat of below string is sensible to server.  
       
   189 ******* Adding any string to the aLogBuffer has to be checked 
       
   190 ******* in code on server side 
       
   191 --------------------------------------------------------------------------------*/
       
   192 	aLogBuffer.AppendFormat(KFormat,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),(dateTime.MicroSecond()/1000)); 
       
   193 /*--------------- End of Maintaince Warning  ----------------*/
       
   194 	}
       
   195 
       
   196 EXPORT_C void RFileFlogger::Log(const TText8* aFile, TInt aLine, TLogSeverity aSeverity, TRefByValue<const TDesC> aFmt, VA_LIST aList)
       
   197 	{
       
   198 	if (aSeverity>iloglevel && aSeverity != ESevrTEFUnit) 
       
   199 		{
       
   200 		return;
       
   201 		}
       
   202 
       
   203 	TInt arraylength = 0;
       
   204 	TExtraLogField* aLogFields =NULL;	
       
   205 	Log(aFile, aLine, aSeverity, arraylength, aLogFields, aFmt, aList);
       
   206 	}
       
   207 
       
   208 EXPORT_C void RFileFlogger::Log(const TText8* aFile, TInt aLine, TLogSeverity aSeverity,TInt arraylength, TExtraLogField* aLogFields, TRefByValue<const TDesC> aFmt, VA_LIST aList)
       
   209 /**
       
   210  * @param aFile - Source file name
       
   211  * @param aLine - Source file line number
       
   212  * @param aSeverity - ERR, WARN, INFO
       
   213  * @param arraylength
       
   214  * @param aLogFields
       
   215  * @param aFmt - UNICODE format string
       
   216  * @param aList - Variable argument list
       
   217  *
       
   218  * Format a log output line
       
   219  */
       
   220  	{
       
   221 	if (aSeverity>iloglevel && aSeverity != ESevrTEFUnit) 
       
   222 		{
       
   223 		return;
       
   224 		}
       
   225 
       
   226 	if (aSeverity == ESevrTEFUnit)
       
   227 		{
       
   228 		aSeverity = ESevrInfo;
       
   229 		}
       
   230 /*----- Maintaince Warning for this section: -----------------------------------
       
   231 ******* the fomat of below string is very sensible to server Server 
       
   232 ******* defomating these string with the understanding of this 
       
   233 ******* perticular format. Any change made here should be checked 
       
   234 ******* in code on server side 
       
   235 --------------------------------------------------------------------------------*/
       
   236 	// Create a filename string
       
   237 	TBuf16<KMaxFilename> fileName;
       
   238 	GetCPPModuleName(fileName, aFile);  
       
   239 	// Create a buffer for formatting
       
   240 	HBufC* buffer = HBufC::NewLC(KMaxLoggerLineLength*2);
       
   241 	if(!buffer)
       
   242 		return;  // no memory
       
   243 	TPtr ptr(buffer->Des());
       
   244 	_LIT(KEnd,"\r\n");
       
   245 	_LIT(KErr,"ERROR");
       
   246 	_LIT(KHigh,"HIGH");
       
   247 	_LIT(KWarn,"WARN");
       
   248 	_LIT(KMedium,"MEDIUM");
       
   249 	_LIT(KInfo,"INFO");
       
   250 	_LIT(KLow,"LOW");
       
   251 //	ptr.Append(KTypeTagBeging);
       
   252 	ptr.Append(KSeperation);
       
   253 	if(aSeverity == ESevrErr)
       
   254 		ptr.Append(KErr);
       
   255 	else if(aSeverity == ESevrHigh)
       
   256 		ptr.Append(KHigh);
       
   257 	else if(aSeverity == ESevrWarn)
       
   258 		ptr.Append(KWarn);
       
   259 	else if(aSeverity == ESevrMedium)
       
   260 		ptr.Append(KMedium);
       
   261 	else if (aSeverity == ESevrInfo)
       
   262 		ptr.Append(KInfo);
       
   263 	else if(aSeverity == ESevrLow)
       
   264 		ptr.Append(KLow);
       
   265 	else //if(aSeverity == ESevrAll)
       
   266 		ptr.Append(KInfo);
       
   267 	// Add the thread id -------- read CIniData to decide the level of details
       
   268 	ptr.AppendFormat(KMessageFormat,(TInt)(RThread().Id()),&fileName, aLine);
       
   269 	ptr.AppendFormatList(aFmt, aList);
       
   270 	if(arraylength>0)  // trust user providing correct number with actual arrary length
       
   271 	{
       
   272 		_LIT(KTab,"\t");
       
   273 		// presuming the following string is hardly being apart of log message from users
       
   274 		// and no carrige return and line feed in their log field name and field value
       
   275 		ptr.Append(KEnd);
       
   276 		ptr.Append(KTagSeperation);
       
   277 		ptr.Append(KTab);
       
   278 		ptr.AppendNum(TInt64(arraylength));
       
   279 		TInt loopValue(arraylength);
       
   280 		while(loopValue)
       
   281 		{
       
   282 			ptr.Append(KEnd);
       
   283 			ptr.Append(aLogFields->iLogFieldName);
       
   284 			ptr.Append(KTab);
       
   285 			ptr.Append(aLogFields->iLogFieldValue);
       
   286 			loopValue--; // Decrement the looping until all fields are exhausted
       
   287 			aLogFields++; // Increment the pointer address to access sucessive array index values			
       
   288 		}
       
   289 		ptr.Append(KEnd);
       
   290 		ptr.Append(KTagSeperationEnd);
       
   291 
       
   292 	}
       
   293 /*----------------- End of Maintaince warning section --------------------------*/
       
   294 	TRAP_IGNORE(WriteL(ptr));
       
   295 	CleanupStack::PopAndDestroy(buffer);
       
   296 
       
   297 	}
       
   298 
       
   299 
       
   300 void RFileFlogger::WriteL(const TDesC& aLogBuffer)
       
   301 /**
       
   302  * @param aLogBuffer - UNICODE buffer
       
   303  */
       
   304 	{
       
   305 	HBufC8* buffer = HBufC8::NewLC(aLogBuffer.Length()+100);
       
   306 	if(!buffer)
       
   307 		return;  // no memory
       
   308 	TPtr8 ptr(buffer->Des());
       
   309 	AddTime(ptr);
       
   310 	ptr.Append(aLogBuffer);
       
   311 	TRAP_IGNORE(WriteL(ptr));
       
   312 	
       
   313 	CleanupStack::PopAndDestroy(buffer);
       
   314 	}
       
   315 
       
   316 void RFileFlogger::WriteL(TDes8& aLogBuffer)
       
   317 /**
       
   318  * @param aLogBuffer - pre-formatted narrow buffer
       
   319  * 
       
   320  * Synchronous write to the server
       
   321  */
       
   322 	{
       
   323 	_LIT8(KEnd,"\r\n");
       
   324 	// Check to see if there's room to add CRLF
       
   325 	if(aLogBuffer.Length()+2 > aLogBuffer.MaxLength())
       
   326 		{
       
   327 		HBufC8* buffer = HBufC8::NewLC(aLogBuffer.Length()+2);
       
   328 		if(!buffer)
       
   329 			return;  // no memory
       
   330 		TPtr8 ptr(buffer->Des());
       
   331 		ptr.Copy(aLogBuffer);
       
   332 		TBuf8<4> tempBuf(_L8("\r\n"));
       
   333 		if (aLogBuffer.Mid(aLogBuffer.Length()-2,2).CompareF(tempBuf) != 0)
       
   334 			ptr.Append(KEnd);
       
   335 		TIpcArgs args;
       
   336 		args.Set(0,&ptr);
       
   337 		args.Set(1,ptr.Length());
       
   338 		User::LeaveIfError(ilogbody->DoSendReceive(EWriteLog,args));
       
   339 		CleanupStack::PopAndDestroy(buffer);
       
   340 		}
       
   341 	else
       
   342 		{
       
   343 		TBuf8<4> tempBuf(_L8("\r\n"));
       
   344 		if (aLogBuffer.Mid(aLogBuffer.Length()-2,2).CompareF(tempBuf) != 0)
       
   345 			aLogBuffer.Append(KEnd);
       
   346 		TIpcArgs args;
       
   347 		args.Set(0,&aLogBuffer);
       
   348 		args.Set(1,aLogBuffer.Length());
       
   349 		User::LeaveIfError(ilogbody->DoSendReceive(EWriteLog,args));
       
   350 		}
       
   351 	}
       
   352 
       
   353 void RFileFlogger::GetCPPModuleName(TDes& aModuleName, const TText8* aCPPFileName)
       
   354 /**
       
   355  * @return aModuleName - Filename in descriptor
       
   356  * @param aCppFileName - Filename
       
   357  * Borrowed from scheduletest
       
   358  */
       
   359 	{
       
   360 	TPtrC8 fileNamePtrC8(aCPPFileName);
       
   361 	// We do our own filename munging here; TParse can't help us since that's
       
   362 	// expressly for EPOC filepaths and here we've got whatever the build system is
       
   363 	// At present Win32 and Unix directory delimiters are supported
       
   364 	TInt lastDelimiter = Max(fileNamePtrC8.LocateReverse('\\'), fileNamePtrC8.LocateReverse('/'));
       
   365 	if(lastDelimiter >= 0 && lastDelimiter < fileNamePtrC8.Length() - 1)
       
   366 		{
       
   367 		// Found a delimiter which isn't trailing; update the ptr to start at the next char
       
   368 		TInt fileNameLen = Min(KMaxFilename, fileNamePtrC8.Length() - (lastDelimiter + 1));
       
   369 		fileNamePtrC8.Set(aCPPFileName + lastDelimiter + 1, fileNameLen);
       
   370 		}
       
   371 	else
       
   372 		{
       
   373 		// Didn't find a delimiter; take as much of the right-end of the name as fits
       
   374 		fileNamePtrC8.Set(aCPPFileName + Max(0, fileNamePtrC8.Length() - KMaxFilename), Min(fileNamePtrC8.Length(), KMaxFilename));
       
   375 		}
       
   376 	aModuleName.Copy(fileNamePtrC8);
       
   377 	}
       
   378 
       
   379 
       
   380 
       
   381 EXPORT_C void RFileFlogger::SetLogLevel(TLogSeverity aloglevel)
       
   382 	{
       
   383 	iloglevel=aloglevel;
       
   384 	}
       
   385 
       
   386 
       
   387 EXPORT_C void RFileFlogger::Close()
       
   388 	{
       
   389   	if(ilogbody)
       
   390   		{
       
   391 		ilogbody->Close();
       
   392 		delete ilogbody;
       
   393 		ilogbody = NULL;
       
   394   		}
       
   395 	}
       
   396 
       
   397