os/ossrv/genericopenlibs/openenvcore/liblogger/src/libloggerhandler.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-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:  Contained libc logger class implementation.
    15  *
    16 */
    17 
    18 
    19 // INCLUDE FILES
    20 #include <f32file.h>
    21 
    22 #ifdef SYMBIAN_FILE_LOGGER
    23 #include <flogger.h>
    24 #endif
    25 
    26 #include "libloggerhandler.h"
    27 
    28 // CONSTANTS
    29 #define LOG_MESSAGE_TYPE_INFO "INFO"
    30 #define LOG_MESSAGE_TYPE_MINOR "MINOR"
    31 #define LOG_MESSAGE_TYPE_MAJOR "MAJOR"
    32 #define LOG_MESSAGE_TYPE_CRITICAL "CRITICAL"
    33 
    34 
    35 #define MAX_LOG_STR_LEN 512 /* Overall size of any logging string size, including timestamp and others */
    36 #define MAX_DATE_TIME_LEN 32
    37 
    38 #define DUMP_STR_LENGTH 16
    39 
    40 #ifdef SYMBIAN_FILE_LOGGER
    41 _LIT(KLogFileLocation,"libc"); // c:\logs\libc
    42 #else
    43 _LIT(KLogFileLocation,"c:\\logs\\libc\\");
    44 _LIT8(KDumpSpace,"                   %04x ");
    45 #endif
    46 
    47 const char KMsgType[4][16] =
    48 	{
    49 		LOG_MESSAGE_TYPE_INFO,
    50 		LOG_MESSAGE_TYPE_MINOR,
    51 		LOG_MESSAGE_TYPE_MAJOR,
    52 		LOG_MESSAGE_TYPE_CRITICAL
    53 	};
    54 
    55 // ============================ LOCAL FUNCTIONS ===============================
    56 
    57 // strlen is implemented over here, just to remove the dependancy from libc/estdlib
    58 
    59 // -----------------------------------------------------------------------------
    60 // _lstrlen
    61 // find the string length.
    62 // -----------------------------------------------------------------------------
    63 //
    64 LOCAL_C TInt _lstrlen(const char *str)
    65 
    66     {
    67 	const char *s;
    68 	for (s = str; *s; ++s)
    69 	    {
    70 	    }
    71 	return(s - str);
    72     }
    73 // -----------------------------------------------------------------------------
    74 // BitPosition
    75 // Find the bit position within the number
    76 // -----------------------------------------------------------------------------
    77 //
    78 LOCAL_C TInt BitPosition(TInt aNumber)
    79     {
    80     TInt pos = 0;
    81     while(!(aNumber & 1))
    82         {
    83         aNumber = (aNumber >> 1);
    84         pos++;
    85         }
    86     // only first 4 bits are used.
    87     if ( pos < 0 || pos >= 4)
    88         {
    89         // crosses the boundary.
    90         pos = 0;
    91         }
    92     return pos;
    93     }
    94 
    95 
    96 // -----------------------------------------------------------------------------
    97 // CLibLogger::GetLogFilename
    98 // Get the log file name.
    99 // -----------------------------------------------------------------------------
   100 //
   101 void CLibLogger::GetLogFilename(TDes& aFileName)
   102 {
   103 	_LIT(KFileNameFormat, "Mrt_%S.txt");
   104     RProcess currentProcess;
   105     TName processName = currentProcess.Name();
   106 #ifdef SYMBIAN_FILE_LOGGER
   107     aFileName.Format(KFileNameFormat, &processName);
   108 #else
   109     aFileName.Copy(KLogFileLocation);
   110     aFileName.AppendFormat(KFileNameFormat, &processName);
   111 #endif
   112     currentProcess.Close();
   113 }
   114 
   115 // -----------------------------------------------------------------------------
   116 // CLibLogger::WriteMessage
   117 // Write message to the log file
   118 // -----------------------------------------------------------------------------
   119 //
   120 TInt CLibLogger::WriteMessage(const TDesC8& aMessage)
   121 {
   122 	TFileName logFileName;
   123 	GetLogFilename(logFileName);
   124 	TInt err = KErrNone;
   125 #ifdef SYMBIAN_FILE_LOGGER
   126     RFileLogger::Write(KLogFileLocation, logFileName, EFileLoggingModeAppend, aMessage);
   127 #else
   128 	RFs fsSession; /* file server */
   129 	RFile iFile;
   130 	err = CLibLogger::CreateFileSession(fsSession, iFile);
   131 	if(KErrNone != err)
   132 	{
   133 		/* Error : Unable to initialize the file session */
   134 		return err;
   135 	}
   136 
   137 	/* write the buffer to the file */
   138 	err = iFile.Write(aMessage);
   139 	if(KErrNone != err)
   140 	{
   141 		/* Error : Unable to initialize the file session */
   142 		iFile.Close();
   143 		fsSession.Close();
   144 		return err;
   145 	}
   146 
   147 	/* flush the buffer */
   148 	iFile.Flush(); /* commit the write. */
   149 
   150 	/* close the session and resources */
   151 	iFile.Close();
   152 	fsSession.Close();
   153 #endif
   154     return err;
   155 }
   156 
   157 // -----------------------------------------------------------------------------
   158 // CLibLogger::LogMessage
   159 // Logging the internal messages.
   160 // -----------------------------------------------------------------------------
   161 //
   162 int CLibLogger::LogMessage(TLibTraceMessageType aLogMessageType,
   163 							char *aFileName,
   164 							int aLine,
   165 							char *aFormat,
   166 							VA_LIST& aMarkerList)
   167 {
   168 	/* checking for error inputs */
   169 	if(NULL == aFormat)
   170 	{
   171 		/* Error : Check the input parameter. */
   172 		return -1;
   173 	}
   174 
   175 	/* trying to fetch the local time */
   176 	TBuf8<MAX_LOG_STR_LEN> lBuf;
   177 	lBuf.FillZ();
   178 	lBuf.SetLength(0);
   179 
   180 #ifdef SYMBIAN_FILE_LOGGER
   181 	/* formulate the time stamp with log type */
   182 	_LIT8(KFormat, "(%s : %d) [%s] - ");
   183 	lBuf.Format(KFormat,
   184 		aFileName,
   185 		aLine,
   186 		KMsgType[BitPosition(aLogMessageType)]
   187 		);
   188 #else
   189 	TTime lCurTime;
   190 	lCurTime.HomeTime();
   191 	TDateTime lTimeStamp = lCurTime.DateTime();
   192 
   193 	/* formulate the time stamp with log type */
   194 	_LIT8(KFormat, "%2d/%2d/%d-%2d:%2d:%2d.%3d - (%s : %d) [%s] - ");
   195 	lBuf.Format(KFormat,
   196 		lTimeStamp.Day(),
   197 		(int)(lTimeStamp.Month() + 1),
   198 		lTimeStamp.Year(),
   199 		lTimeStamp.Hour(),
   200 		lTimeStamp.Minute(),
   201 		lTimeStamp.Second(),
   202 		(int)((lTimeStamp.MicroSecond())/1000),
   203 		aFileName,
   204 		aLine,
   205 		KMsgType[BitPosition(aLogMessageType)]
   206 		);
   207 #endif // SYMBIAN_FILE_LOGGER
   208 
   209 	TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
   210 	lBuf.AppendFormatList(lPtr, aMarkerList);
   211 
   212 	/* new line */
   213 	_LIT8(KNewLine, "\r\n");
   214 	lBuf.Append(KNewLine);
   215     WriteMessage(lBuf);
   216 	return lBuf.Length();
   217 }
   218 
   219 // -----------------------------------------------------------------------------
   220 // CLibLogger::LogMessage
   221 // Logging the internal messages.
   222 // -----------------------------------------------------------------------------
   223 //
   224 int CLibLogger::LogMessage(char *aFileName, int aLine)
   225 {
   226 	/* trying to fetch the local time */
   227 	TBuf8<MAX_LOG_STR_LEN> lBuf;
   228 	lBuf.FillZ();
   229 	lBuf.SetLength(0);
   230 
   231 #ifdef SYMBIAN_FILE_LOGGER
   232 	/* formulate the time stamp with log type */
   233 	_LIT8(KFormat, "(%s : %d) ");
   234 	lBuf.Format(KFormat,
   235 		aFileName,
   236 		aLine);
   237 #else
   238 	TTime lCurTime;
   239 	lCurTime.HomeTime();
   240 	TDateTime lTimeStamp = lCurTime.DateTime();
   241 
   242 	/* formulate the time stamp with log type */
   243 	_LIT8(KFormat, "%2d/%2d/%d-%2d:%2d:%2d.%3d - (%s : %d) ");
   244 	lBuf.Format(KFormat,
   245 		lTimeStamp.Day(),
   246 		(int)(lTimeStamp.Month() + 1),
   247 		lTimeStamp.Year(),
   248 		lTimeStamp.Hour(),
   249 		lTimeStamp.Minute(),
   250 		lTimeStamp.Second(),
   251 		(int)((lTimeStamp.MicroSecond())/1000),
   252 		aFileName,
   253 		aLine);
   254 #endif // SYMBIAN_FILE_LOGGER
   255     WriteMessage(lBuf);
   256 	return lBuf.Length();
   257 }
   258 
   259 
   260 // -----------------------------------------------------------------------------
   261 // CLibLogger::LogMessage
   262 // Logging the internal messages.
   263 // -----------------------------------------------------------------------------
   264 //
   265 int CLibLogger::LogMessage(TLibTraceMessageType aLogMessageType,
   266 							char *aFormat, 
   267 							VA_LIST& aMarkerList)
   268 {
   269 	/* checking for error inputs */
   270 	if(NULL == aFormat)
   271 	{
   272 		/* Error : Check the input parameter. */
   273 		return -1;
   274 	}
   275 
   276 	/* trying to fetch the local time */
   277 	TBuf8<MAX_LOG_STR_LEN> lBuf;
   278 	lBuf.FillZ();
   279 	lBuf.SetLength(0);
   280 
   281 	/* formulate the time stamp with log type */
   282 	_LIT8(KFormat, "[%s] - ");
   283 	lBuf.Format(KFormat, 
   284 		KMsgType[BitPosition(aLogMessageType)]
   285 		);
   286 
   287 	TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
   288 	lBuf.AppendFormatList(lPtr, aMarkerList);
   289 
   290 	/* new line */
   291 	_LIT8(KNewLine, "\r\n");
   292 	lBuf.Append(KNewLine);
   293     WriteMessage(lBuf);
   294 	return lBuf.Length();
   295 }
   296 
   297 
   298 // -----------------------------------------------------------------------------
   299 // CLibLogger::DumpFormatMessage
   300 // Dump the internal message in hex format.
   301 // -----------------------------------------------------------------------------
   302 //
   303 int CLibLogger::DumpFormatMessage(TLibTraceMessageType aLogMessageType,
   304                             char *aFileName,
   305 							int aLine,
   306 							char *aMessage,
   307 							char *aFormat,
   308 							VA_LIST& aMarkerList)
   309 {
   310 	if(NULL == aFormat)
   311 	{
   312 		/* Error : Check your input value. */
   313 		return 0;
   314 	}
   315 	TBuf8<MAX_LOG_STR_LEN> lBuf;
   316 	lBuf.FillZ();
   317 	lBuf.SetLength(0);
   318 	TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
   319 	lBuf.AppendFormatList(lPtr, aMarkerList);
   320 
   321 	return DumpMessage(aLogMessageType, aFileName, aLine, aMessage, (char *)lBuf.PtrZ(), lBuf.Length());
   322 }
   323 
   324 // -----------------------------------------------------------------------------
   325 // CLibLogger::DumpMessage
   326 // Dump the internal message in hex format.
   327 // -----------------------------------------------------------------------------
   328 //
   329 int CLibLogger::DumpMessage(TLibTraceMessageType aLogMessageType,
   330                             char *aFileName,
   331 							int aLine,
   332 							char *aMessage,
   333 							char *aStr,
   334 							int aStrLen)
   335 {
   336 	int istrLen = (aStrLen == -1) ? _lstrlen(aStr) : aStrLen;
   337 	if((NULL == aStr)
   338 		|| (istrLen <= 0))
   339 	{
   340 		/* Error : Check your input value. */
   341 		return 0;
   342 	}
   343 	/* log the timestamp */
   344 
   345 	return LibTracer(aLogMessageType, aFileName, aLine, aMessage) + DumpMessage(aStr, istrLen);
   346 }
   347 
   348 // -----------------------------------------------------------------------------
   349 // CLibLogger::DumpFormatMessage
   350 // Dump the internal message in hex format.
   351 // -----------------------------------------------------------------------------
   352 //
   353 int CLibLogger::DumpFormatMessage(TLibTraceMessageType aLogMessageType,
   354 							char *aMessage,
   355 							char *aFormat,
   356 							VA_LIST& aMarkerList)
   357 {
   358 	if(NULL == aFormat)
   359 	{
   360 		/* Error : Check your input value. */
   361 		return 0;
   362 	}
   363 
   364 
   365 	/* trying to fetch the local time */
   366 	TBuf8<MAX_LOG_STR_LEN> lBuf;
   367 	lBuf.FillZ();
   368 	lBuf.SetLength(0);
   369 
   370 	/* formulate the time stamp with log type */
   371 	_LIT8(KFormat, "[%s] - %s");
   372 	lBuf.Format(KFormat,
   373 		KMsgType[BitPosition(aLogMessageType)], aMessage);
   374 
   375 
   376 	_LIT8(KNewLine, "\r\n");
   377 	lBuf.Append(KNewLine);
   378     WriteMessage(lBuf);
   379     int len = lBuf.Length();
   380 	lBuf.FillZ();
   381 	lBuf.SetLength(0);
   382 	TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
   383 	lBuf.AppendFormatList(lPtr, aMarkerList);
   384 
   385 	return len + DumpMessage((char *)lBuf.PtrZ(), lBuf.Length());
   386 }
   387 
   388 
   389 
   390 #ifdef SYMBIAN_FILE_LOGGER
   391 // -----------------------------------------------------------------------------
   392 // CLibLogger::DumpMessage
   393 // Dump the internal message in hex format.
   394 // -----------------------------------------------------------------------------
   395 //
   396 int CLibLogger::DumpMessage(char *aStr, int aStrLen)
   397 {
   398 	/* check the inputs */
   399 	if((NULL == aStr)
   400 		|| (aStrLen <= 0))
   401 	{
   402 		/* Error : Check your input value. */
   403 		return 0;
   404 	}
   405 
   406 	/* Input parameter is okey */
   407 
   408 	TFileName logFileName;
   409 	GetLogFilename(logFileName);
   410     RFileLogger::HexDump(KLogFileLocation, logFileName, EFileLoggingModeAppend, NULL, NULL, (TUint8*)aStr, aStrLen);
   411     return aStrLen;
   412 }
   413 
   414 
   415 #else
   416 
   417 // -----------------------------------------------------------------------------
   418 // CLibLogger::CreateFileSession
   419 // Create a file session for the logger.
   420 // -----------------------------------------------------------------------------
   421 //
   422 int CLibLogger::CreateFileSession(RFs &aFsSession, RFile &aFile)
   423 {
   424 	int lErr;
   425 	/* register with the file service session. */
   426 
   427 	lErr = aFsSession.Connect();
   428 	if(lErr != KErrNone)
   429 	{
   430 		/* Error : unable to create a session with file server */
   431 		return lErr;
   432 	}
   433 
   434 	/* Success : application is connected with the file server */
   435 
   436 	TFileName logFileName;
   437 	GetLogFilename(logFileName);
   438 
   439 	aFsSession.MkDirAll(logFileName);
   440 	CDir* fileList;
   441 	CDir* dirList;
   442 	
   443 	//coverity[alloc_fn]
   444 	lErr = aFsSession.GetDir(logFileName,KEntryAttNormal,
   445 		ESortByName,fileList,dirList);
   446 	if(lErr != KErrNone)
   447 	{
   448 		/* Error : unable to retrieve the directory list */
   449 		aFsSession.Close(); /* close the session with file server */
   450 		return lErr;
   451 	}
   452 
   453 	if(0 >= fileList->Count())
   454 	{
   455 		/* File doesn't exist. create a new file. */
   456 		lErr = aFile.Replace(aFsSession,
   457 								logFileName,
   458 								EFileWrite|EFileStreamText);
   459 		if(lErr != KErrNone)
   460 		{
   461 			/* Error : unable to create the new file. */
   462 			delete fileList;
   463 			delete dirList;
   464 			aFsSession.Close(); /* close the session with file server */
   465 			return lErr;
   466 		}
   467 	}
   468 	else
   469 	{
   470 		/* Shared access for reading and writing. */
   471 		lErr = aFile.Open(aFsSession,
   472 								logFileName,
   473 								EFileWrite | EFileStreamText);
   474 		if(lErr != KErrNone)
   475 		{
   476 			/* Error : unable to open the file. */
   477 			delete fileList;
   478 			delete dirList;
   479 			aFsSession.Close(); /* close the session with file server */
   480 			return lErr;
   481 		}
   482 		int pos = 0; /* since we have pass the 2nd parameter as reference. */
   483 		lErr = aFile.Seek(ESeekEnd, pos);
   484 		if(lErr != KErrNone)
   485 		{
   486 			/* Error : unable to move the file pointer. */
   487 			//coverity[leave_without_push]
   488 			aFile.Close(); /* close the file */
   489 			//coverity[leave_without_push]
   490 			aFsSession.Close(); /* close the session with file server */
   491 			delete fileList;
   492 			delete dirList;
   493 			return lErr;
   494 		}
   495 	}
   496 	/* delete the resources */
   497 
   498 	delete fileList;
   499 	delete dirList;
   500 	return lErr;
   501 }
   502 
   503 
   504 // -----------------------------------------------------------------------------
   505 // CLibLogger::DumpMessage
   506 // Dump the internal message in hex format.
   507 // -----------------------------------------------------------------------------
   508 //
   509 int CLibLogger::DumpMessage(char *aStr, int aStrLen)
   510 {
   511 	/* check the inputs */
   512 	if((NULL == aStr)
   513 		|| (aStrLen <= 0))
   514 	{
   515 		/* Error : Check your input value. */
   516 		return 0;
   517 	}
   518 
   519 	/* Input parameter is okey */
   520 
   521 	/* local variables */
   522 	int counter = 0;
   523 
   524 	/* store the input string to a local pointer */
   525 	TPtrC8 istrMesg((TUint8* )aStr, aStrLen);
   526 
   527 	/* local log buffer */
   528 	TBuf8<MAX_LOG_STR_LEN> lstrLogMesgString;
   529 	TBuf8<MAX_LOG_STR_LEN> ptempStr;
   530 
   531 	/* few literals */
   532 	_LIT8(KSpace, " ");
   533 	_LIT8(KDblSpace, "   ");
   534 	_LIT8(KFormat, "%2x "); /* hex format */
   535     _LIT8(KNewLine, "\r\n");
   536 	RFs fsSession; /* file server */
   537 	RFile file;
   538 	int lErr = CLibLogger::CreateFileSession(fsSession, file);
   539 	if(KErrNone != lErr)
   540 	{
   541 		/* Error : Unable to initialize the file session */
   542 		return lErr;
   543 	}
   544 	// since these method can be called in from thread context and
   545 	// thread may not call this method within TRAPD
   546     //CleanupClosePushL(fsSession);
   547     //CleanupClosePushL(file);
   548 
   549 	for(counter = 0; counter < aStrLen; counter++)
   550 	{
   551 		if((counter != 0) && ((counter % DUMP_STR_LENGTH) == 0))
   552 		{
   553 			/* sring reaches with the multiple to 16 (DUMP_STR_LENGTH) */
   554 			lstrLogMesgString.UpperCase();
   555 			lstrLogMesgString.Append(KSpace);
   556 			/* append 16 (DUMP_STR_LENGTH) characters */
   557 			lstrLogMesgString.Append(istrMesg.Mid(((counter / DUMP_STR_LENGTH) - 1) * DUMP_STR_LENGTH, DUMP_STR_LENGTH));
   558 
   559 			/* dump the buffer */
   560 			ptempStr.Format(KDumpSpace, counter - DUMP_STR_LENGTH);
   561 			lstrLogMesgString.Insert(0, ptempStr);
   562             file.Write(lstrLogMesgString);
   563             file.Write(KNewLine);
   564             file.Flush();
   565 			/* free the buffer, set the length to zero */
   566 			lstrLogMesgString.FillZ();
   567 			lstrLogMesgString.SetLength(0);
   568 		} /* end of if */
   569 		ptempStr.Format(KFormat,(int)istrMesg[counter]);
   570 		lstrLogMesgString.Append(ptempStr);
   571 	} /*  end of for */
   572 
   573 	if(0 < lstrLogMesgString.Length())
   574 	{
   575 		/* few character remains */
   576 		/* it's not multiple of 16 (DUMP_STR_LENGTH) */
   577 		/* adjust the dump and the character portion */
   578 		if (aStrLen % DUMP_STR_LENGTH != 0)
   579 		{
   580     		for(int lliCounter = 0; lliCounter < DUMP_STR_LENGTH - (aStrLen % DUMP_STR_LENGTH); lliCounter++)
   581     		{
   582     			lstrLogMesgString.Append(KDblSpace); /* filling the blanks */
   583     		} /* end of for */
   584         }
   585 		lstrLogMesgString.UpperCase();
   586 		/* fill the string */
   587 		lstrLogMesgString.Append(KSpace);
   588 		/* append the string */
   589 		if (aStrLen % DUMP_STR_LENGTH != 0)
   590 		{
   591 		    lstrLogMesgString.Append(istrMesg.Mid(((counter / DUMP_STR_LENGTH)) * DUMP_STR_LENGTH, (aStrLen - ((counter / DUMP_STR_LENGTH)) * DUMP_STR_LENGTH)));
   592 		}
   593 		else
   594 		{
   595 		    lstrLogMesgString.Append(istrMesg.Mid(((counter / DUMP_STR_LENGTH) - 1) * DUMP_STR_LENGTH, (aStrLen - ((counter / DUMP_STR_LENGTH) -1) * DUMP_STR_LENGTH)));
   596 		}
   597 		ptempStr.Format(KDumpSpace, ((aStrLen / DUMP_STR_LENGTH) * DUMP_STR_LENGTH));
   598 		lstrLogMesgString.Insert(0, ptempStr);
   599         file.Write(lstrLogMesgString);
   600         file.Write(KNewLine);
   601         file.Flush();
   602 	} /* end of if */
   603 	//CleanupStack::PopAndDestroy(2); // file, fsFession
   604 	file.Close();
   605 	fsSession.Close();
   606 	return aStrLen; /* return the total dump message length */
   607 }
   608 
   609 #endif // SYMBIAN_FILE_LOGGER
   610 
   611 #ifndef EKA2
   612 
   613 GLDEF_C TInt E32Dll(TDllReason aReason)
   614 	{
   615 	switch (aReason)
   616 		{
   617 	default:
   618 		break;
   619 		}
   620 	return KErrNone;
   621 	}
   622 
   623 #endif
   624 
   625 // End of file