loggingservices/rfilelogger/Logger/Src/Server.Cpp
branchRCL_3
changeset 24 cc28652e0254
parent 23 26645d81f48d
equal deleted inserted replaced
23:26645d81f48d 24:cc28652e0254
   380 		TPtrC8 SearchBuf;
   380 		TPtrC8 SearchBuf;
   381 		TInt aCount[8]; 
   381 		TInt aCount[8]; 
   382 		aCount[0]=0;
   382 		aCount[0]=0;
   383 		TInt posI(0);
   383 		TInt posI(0);
   384 
   384 
   385         // retrive log message:
   385 // retrive log message:
   386 		// Retrive common part of log message:
   386 		// Retrive common part of log message:
   387 		TInt i=0;
   387 		for(TInt i = 1; i<6; i++)
   388 		for(i=1; i<6; i++)
       
   389 			{
   388 			{
   390 			SearchBuf.Set(aPtr.Mid(posI));
   389 			SearchBuf.Set(aPtr.Mid(posI));
   391 			aCount[i]=SearchBuf.Find(KSeperation8)+posI;
   390 			aCount[i]=SearchBuf.Find(KSeperation8)+posI;
   392 			posI=aCount[i]+3;
   391 			posI=aCount[i]+3;
   393 			if(aCount[i]<aCount[i-1])	
   392 			if(aCount[i]<aCount[i-1])	
   394                 {
   393         {
   395                 delete pBuf1;
   394         delete pBuf1;
   396                 return; // wrong format string from client
   395         return; // wrong format string from client
   397                 }
   396         }
   398 			}
   397 			}
   399 		// seperating common log message and extra log fields will be easy for future maintaince.
   398 		// seperating common log message and extra log fields will be easy for future maintaince.
   400 		TLogField8* alogField = new TLogField8[6];  // the common part of log message for both 
   399 		TLogField8* alogField = new TLogField8[6];  // the common part of log message for both 
   401 								  					// with and without extra log fields
   400 								  					// with and without extra log fields
   402 		if(!alogField) 
   401 		if(!alogField) 
   403             {
   402       {
   404             delete pBuf1;
   403       delete pBuf1;
   405             return; // no memory
   404       return; // no memory
   406             }
   405       }
   407 
   406 
   408 		TLogField8* extralogField=NULL; // only applied to extra log fields
   407 		TLogField8* extralogField=NULL; // only applied to extra log fields
   409 
   408 
   410 		TInt alength=0;  // a length of array of extra log fields
   409 		TInt alength=0;  // a length of array of extra log fields
   411 
   410 
   415 		alogField[3].iLogTag8.Copy(_L8("FILENAME"));
   414 		alogField[3].iLogTag8.Copy(_L8("FILENAME"));
   416 		alogField[4].iLogTag8.Copy(_L8("LINENUMBER"));
   415 		alogField[4].iLogTag8.Copy(_L8("LINENUMBER"));
   417 		alogField[5].iLogTag8.Copy(_L8("TEXT"));
   416 		alogField[5].iLogTag8.Copy(_L8("TEXT"));
   418 			
   417 			
   419 		alogField[0].iLogValue8.Copy(aPtr.Mid(aCount[0],aCount[1]-aCount[0]));
   418 		alogField[0].iLogValue8.Copy(aPtr.Mid(aCount[0],aCount[1]-aCount[0]));
   420 		for(i=1; i<5; i++)
   419 		for(TInt i=1; i<5; i++)
   421 			{
   420 			{
   422 			alogField[i].iLogValue8.Copy(aPtr.Mid(aCount[i]+3,aCount[i+1]-aCount[i]-3));				
   421 			alogField[i].iLogValue8.Copy(aPtr.Mid(aCount[i]+3,aCount[i+1]-aCount[i]-3));				
   423 			}
   422 			}
   424 
   423 
   425 		SearchBuf.Set(aPtr.Mid(posI));
   424 		SearchBuf.Set(aPtr.Mid(posI));
   427 		if(aCount[6]<posI)  // no addtional fields. Find return value is KErrNotFound or >0
   426 		if(aCount[6]<posI)  // no addtional fields. Find return value is KErrNotFound or >0
   428 			{
   427 			{
   429 			alogField[5].iLogValue8.Copy(aPtr.Mid(aCount[5]+3,aDes.Length()-aCount[5]-5));
   428 			alogField[5].iLogValue8.Copy(aPtr.Mid(aCount[5]+3,aDes.Length()-aCount[5]-5));
   430 			}
   429 			}
   431 		else
   430 		else
   432             {
   431 		{
   433 			alogField[5].iLogValue8.Copy(aPtr.Mid(aCount[5]+3,aCount[6]-aCount[5]-5));
   432 			alogField[5].iLogValue8.Copy(aPtr.Mid(aCount[5]+3,aCount[6]-aCount[5]-5));
   434 			posI=aCount[6]+45;  //45 is from the length of long string and a tab
   433 			posI=aCount[6]+45;  //45 is from the length of long string and a tab
   435 			SearchBuf.Set(aPtr.Mid(posI));
   434 			SearchBuf.Set(aPtr.Mid(posI));
   436 			aCount[7]=SearchBuf.Find(_L8("\r\n"))+posI;
   435 			aCount[7]=SearchBuf.Find(_L8("\r\n"))+posI;
   437 			TLex8 lex(aPtr.Mid(posI,aCount[7]-posI));  // get the length
   436 			TLex8 lex(aPtr.Mid(posI,aCount[7]-posI));  // get the length
   438 			TInt err=lex.Val(alength); 
   437 			TInt err=lex.Val(alength); 
   439 			if (err)
   438 			if (err)	alength=0; // ignor the extra log fields. Let the log go
   440                 alength=0; // ignor the extra log fields. Let the log go
       
   441 
   439 
   442 			// Retrive the extra log fields
   440 			// Retrive the extra log fields
   443 			extralogField = new TLogField8[alength];
   441 			extralogField = new TLogField8[alength];
   444 			if(!extralogField)
   442 			if(!extralogField)
   445                 {
   443         {
   446                 delete pBuf1; 
   444         delete pBuf1; 
   447                 return; // no memory
   445         return; // no memory
   448                 }
   446         }
   449 			for(TInt i=0; i<alength; i++)
   447 			for(TInt i=0; i<alength; i++)
   450 				{
   448 				{
   451 				aCount[6]=aCount[7]+2;
   449 				aCount[6]=aCount[7]+2;
   452 				SearchBuf.Set(aPtr.Mid(aCount[6]));
   450 				SearchBuf.Set(aPtr.Mid(aCount[6]));
   453 				aCount[7]=SearchBuf.Find(_L8("\t"))+aCount[6];
   451 				aCount[7]=SearchBuf.Find(_L8("\t"))+aCount[6];
   455 				aCount[6]=aCount[7]+1;
   453 				aCount[6]=aCount[7]+1;
   456 				SearchBuf.Set(aPtr.Mid(aCount[6]));
   454 				SearchBuf.Set(aPtr.Mid(aCount[6]));
   457 				aCount[7]=SearchBuf.Find(_L8("\r\n"))+aCount[6];
   455 				aCount[7]=SearchBuf.Find(_L8("\r\n"))+aCount[6];
   458 				extralogField[i].iLogValue8.Copy(aPtr.Mid(aCount[6],aCount[7]-aCount[6]));
   456 				extralogField[i].iLogValue8.Copy(aPtr.Mid(aCount[6],aCount[7]-aCount[6]));
   459 				}
   457 				}
   460             }
   458 		}
   461 		// Start to organize an XML format:
   459 			
   462 		TInt afileSize;
   460 			// Start to organize an XML format: 			
   463 		_LIT(KLogMutex, "LoggingServerMutex");
   461 			TInt afileSize;
   464 		RMutex mutex;
   462 			_LIT(KLogMutex, "LoggingServerMutex");
   465 		TInt r = mutex.CreateGlobal(KLogMutex);
   463 			RMutex mutex;
   466 		if(r==KErrAlreadyExists)
   464 			TInt r = mutex.CreateGlobal(KLogMutex);
   467 			r = mutex.OpenGlobal(KLogMutex);  
   465 			if(r==KErrAlreadyExists)
   468 		
   466 				r = mutex.OpenGlobal(KLogMutex);  
   469 		if(!r)
   467 			
   470             mutex.Wait(); // If still failed, let logging go without bother the mutex.
   468 			if(!r)	mutex.Wait();  // if still failed, let logging go 
   471 		iLogFile.Size(afileSize);
   469 									//without bother the mutex
   472 		if(afileSize<12) // 12 is from charters of "\r\n</LOGFILE>" 
   470 			  
   473 					//It shoud happened once at the beginning of the file
   471 			iLogFile.Size(afileSize);
   474 					// such as overwrite mode
   472 			if(afileSize<12) // 12 is from charters of "\r\n</LOGFILE>" 
   475             {
   473 						//It shoud happened once at the beginning of the file
   476 			afileSize=12; // used for lock position
   474 						// such as overwrite mode
   477 			alogbuf.Copy(KxmlHeader);
   475 				{
   478 			}
   476 				afileSize=12; // used for lock position
   479 		alogbuf.Append(_L8("\r\n<MESSAGE>\r\n"));
   477     			alogbuf.Copy(KxmlHeader);
   480 		for(TInt i=0; i<6; i++)
   478 				}
   481 			{
   479 			alogbuf.Append(_L8("\r\n<MESSAGE>\r\n"));
   482 			alogbuf.Append(_L8("  <"));
   480 			for(TInt i=0; i<6; i++)
   483 			alogbuf.Append(alogField[i].iLogTag8);
   481 				{
   484 			alogbuf.Append(_L8(">"));
   482 				alogbuf.Append(_L8("  <"));
   485 			alogbuf.Append(alogField[i].iLogValue8);
   483 				alogbuf.Append(alogField[i].iLogTag8);
   486 			alogbuf.Append(_L8("</"));
   484 				alogbuf.Append(_L8(">"));
   487 			alogbuf.Append(alogField[i].iLogTag8);
   485 				alogbuf.Append(alogField[i].iLogValue8);
   488 			alogbuf.Append(_L8(">\r\n"));				
   486 				alogbuf.Append(_L8("</"));
   489 			}
   487 				alogbuf.Append(alogField[i].iLogTag8);
   490 		for(TInt i=0; i<alength; i++)  
   488 				alogbuf.Append(_L8(">\r\n"));				
   491 			{
   489 				}
   492 			alogbuf.Append(_L8("  <"));
   490 			for(TInt i=0; i<alength; i++)  
   493 			alogbuf.Append(extralogField[i].iLogTag8);
   491 				{
   494 			alogbuf.Append(_L8(">"));
   492 				alogbuf.Append(_L8("  <"));
   495 			alogbuf.Append(extralogField[i].iLogValue8);
   493 				alogbuf.Append(extralogField[i].iLogTag8);
   496 			alogbuf.Append(_L8("</"));
   494 				alogbuf.Append(_L8(">"));
   497 			alogbuf.Append(extralogField[i].iLogTag8);
   495 				alogbuf.Append(extralogField[i].iLogValue8);
   498 			alogbuf.Append(_L8(">\r\n"));				
   496 				alogbuf.Append(_L8("</"));
   499 			}
   497 				alogbuf.Append(extralogField[i].iLogTag8);
   500 		
   498 				alogbuf.Append(_L8(">\r\n"));				
   501 		alogbuf.Append(_L8("</MESSAGE>"));
   499 				}
   502 		alogbuf.Append(_L8("\r\n</LOGFILE>"));
   500 			
   503 		
   501     		alogbuf.Append(_L8("</MESSAGE>"));
   504 		iLogFile.Write(afileSize-12, alogbuf,iStatus);
   502     		alogbuf.Append(_L8("\r\n</LOGFILE>"));
   505 													
   503     		
   506 		if(!r)	
   504     		iLogFile.Write(afileSize-12, alogbuf,iStatus);
   507 			{
   505     										
   508 			mutex.Signal();
   506 			
   509 			mutex.Close();				
   507 			if(!r)	
   510 			} 
   508 				{
   511 
   509 				mutex.Signal();
   512 		if(extralogField)
   510 				mutex.Close();				
   513             delete[] extralogField;
   511 				} 
   514 
   512 				
       
   513 		if(extralogField) delete[] extralogField;
   515 		delete[] alogField;	
   514 		delete[] alogField;	
   516 		delete pBuf1;
   515 		delete pBuf1;
       
   516 
   517 	}
   517 	}
   518 
   518 
   519 void CLogFileControl::WriteTxt(const TDesC8 &aDes)
   519 void CLogFileControl::WriteTxt(const TDesC8 &aDes)
   520 /**
   520 /**
   521  * @param aDes - send a aDes string in xml format to a log file
   521  * @param aDes - send a aDes string in xml format to a log file