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