Update contrib.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
14 * Description: Contained libc logger class implementation.
22 #ifdef SYMBIAN_FILE_LOGGER
26 #include "libloggerhandler.h"
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"
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
38 #define DUMP_STR_LENGTH 16
40 #ifdef SYMBIAN_FILE_LOGGER
41 _LIT(KLogFileLocation,"libc"); // c:\logs\libc
43 _LIT(KLogFileLocation,"c:\\logs\\libc\\");
44 _LIT8(KDumpSpace," %04x ");
47 const char KMsgType[4][16] =
49 LOG_MESSAGE_TYPE_INFO,
50 LOG_MESSAGE_TYPE_MINOR,
51 LOG_MESSAGE_TYPE_MAJOR,
52 LOG_MESSAGE_TYPE_CRITICAL
55 // ============================ LOCAL FUNCTIONS ===============================
57 // strlen is implemented over here, just to remove the dependancy from libc/estdlib
59 // -----------------------------------------------------------------------------
61 // find the string length.
62 // -----------------------------------------------------------------------------
64 LOCAL_C TInt _lstrlen(const char *str)
68 for (s = str; *s; ++s)
73 // -----------------------------------------------------------------------------
75 // Find the bit position within the number
76 // -----------------------------------------------------------------------------
78 LOCAL_C TInt BitPosition(TInt aNumber)
83 aNumber = (aNumber >> 1);
86 // only first 4 bits are used.
87 if ( pos < 0 || pos >= 4)
89 // crosses the boundary.
96 // -----------------------------------------------------------------------------
97 // CLibLogger::GetLogFilename
98 // Get the log file name.
99 // -----------------------------------------------------------------------------
101 void CLibLogger::GetLogFilename(TDes& aFileName)
103 _LIT(KFileNameFormat, "Mrt_%S.txt");
104 RProcess currentProcess;
105 TName processName = currentProcess.Name();
106 #ifdef SYMBIAN_FILE_LOGGER
107 aFileName.Format(KFileNameFormat, &processName);
109 aFileName.Copy(KLogFileLocation);
110 aFileName.AppendFormat(KFileNameFormat, &processName);
112 currentProcess.Close();
115 // -----------------------------------------------------------------------------
116 // CLibLogger::WriteMessage
117 // Write message to the log file
118 // -----------------------------------------------------------------------------
120 TInt CLibLogger::WriteMessage(const TDesC8& aMessage)
122 TFileName logFileName;
123 GetLogFilename(logFileName);
125 #ifdef SYMBIAN_FILE_LOGGER
126 RFileLogger::Write(KLogFileLocation, logFileName, EFileLoggingModeAppend, aMessage);
128 RFs fsSession; /* file server */
130 err = CLibLogger::CreateFileSession(fsSession, iFile);
133 /* Error : Unable to initialize the file session */
137 /* write the buffer to the file */
138 err = iFile.Write(aMessage);
141 /* Error : Unable to initialize the file session */
147 /* flush the buffer */
148 iFile.Flush(); /* commit the write. */
150 /* close the session and resources */
157 // -----------------------------------------------------------------------------
158 // CLibLogger::LogMessage
159 // Logging the internal messages.
160 // -----------------------------------------------------------------------------
162 int CLibLogger::LogMessage(TLibTraceMessageType aLogMessageType,
166 VA_LIST& aMarkerList)
168 /* checking for error inputs */
171 /* Error : Check the input parameter. */
175 /* trying to fetch the local time */
176 TBuf8<MAX_LOG_STR_LEN> lBuf;
180 #ifdef SYMBIAN_FILE_LOGGER
181 /* formulate the time stamp with log type */
182 _LIT8(KFormat, "(%s : %d) [%s] - ");
186 KMsgType[BitPosition(aLogMessageType)]
191 TDateTime lTimeStamp = lCurTime.DateTime();
193 /* formulate the time stamp with log type */
194 _LIT8(KFormat, "%2d/%2d/%d-%2d:%2d:%2d.%3d - (%s : %d) [%s] - ");
197 (int)(lTimeStamp.Month() + 1),
202 (int)((lTimeStamp.MicroSecond())/1000),
205 KMsgType[BitPosition(aLogMessageType)]
207 #endif // SYMBIAN_FILE_LOGGER
209 TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
210 lBuf.AppendFormatList(lPtr, aMarkerList);
213 _LIT8(KNewLine, "\r\n");
214 lBuf.Append(KNewLine);
216 return lBuf.Length();
219 // -----------------------------------------------------------------------------
220 // CLibLogger::LogMessage
221 // Logging the internal messages.
222 // -----------------------------------------------------------------------------
224 int CLibLogger::LogMessage(char *aFileName, int aLine)
226 /* trying to fetch the local time */
227 TBuf8<MAX_LOG_STR_LEN> lBuf;
231 #ifdef SYMBIAN_FILE_LOGGER
232 /* formulate the time stamp with log type */
233 _LIT8(KFormat, "(%s : %d) ");
240 TDateTime lTimeStamp = lCurTime.DateTime();
242 /* formulate the time stamp with log type */
243 _LIT8(KFormat, "%2d/%2d/%d-%2d:%2d:%2d.%3d - (%s : %d) ");
246 (int)(lTimeStamp.Month() + 1),
251 (int)((lTimeStamp.MicroSecond())/1000),
254 #endif // SYMBIAN_FILE_LOGGER
256 return lBuf.Length();
260 // -----------------------------------------------------------------------------
261 // CLibLogger::LogMessage
262 // Logging the internal messages.
263 // -----------------------------------------------------------------------------
265 int CLibLogger::LogMessage(TLibTraceMessageType aLogMessageType,
267 VA_LIST& aMarkerList)
269 /* checking for error inputs */
272 /* Error : Check the input parameter. */
276 /* trying to fetch the local time */
277 TBuf8<MAX_LOG_STR_LEN> lBuf;
281 /* formulate the time stamp with log type */
282 _LIT8(KFormat, "[%s] - ");
284 KMsgType[BitPosition(aLogMessageType)]
287 TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
288 lBuf.AppendFormatList(lPtr, aMarkerList);
291 _LIT8(KNewLine, "\r\n");
292 lBuf.Append(KNewLine);
294 return lBuf.Length();
298 // -----------------------------------------------------------------------------
299 // CLibLogger::DumpFormatMessage
300 // Dump the internal message in hex format.
301 // -----------------------------------------------------------------------------
303 int CLibLogger::DumpFormatMessage(TLibTraceMessageType aLogMessageType,
308 VA_LIST& aMarkerList)
312 /* Error : Check your input value. */
315 TBuf8<MAX_LOG_STR_LEN> lBuf;
318 TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
319 lBuf.AppendFormatList(lPtr, aMarkerList);
321 return DumpMessage(aLogMessageType, aFileName, aLine, aMessage, (char *)lBuf.PtrZ(), lBuf.Length());
324 // -----------------------------------------------------------------------------
325 // CLibLogger::DumpMessage
326 // Dump the internal message in hex format.
327 // -----------------------------------------------------------------------------
329 int CLibLogger::DumpMessage(TLibTraceMessageType aLogMessageType,
336 int istrLen = (aStrLen == -1) ? _lstrlen(aStr) : aStrLen;
340 /* Error : Check your input value. */
343 /* log the timestamp */
345 return LibTracer(aLogMessageType, aFileName, aLine, aMessage) + DumpMessage(aStr, istrLen);
348 // -----------------------------------------------------------------------------
349 // CLibLogger::DumpFormatMessage
350 // Dump the internal message in hex format.
351 // -----------------------------------------------------------------------------
353 int CLibLogger::DumpFormatMessage(TLibTraceMessageType aLogMessageType,
356 VA_LIST& aMarkerList)
360 /* Error : Check your input value. */
365 /* trying to fetch the local time */
366 TBuf8<MAX_LOG_STR_LEN> lBuf;
370 /* formulate the time stamp with log type */
371 _LIT8(KFormat, "[%s] - %s");
373 KMsgType[BitPosition(aLogMessageType)], aMessage);
376 _LIT8(KNewLine, "\r\n");
377 lBuf.Append(KNewLine);
379 int len = lBuf.Length();
382 TPtrC8 lPtr((TUint8*)aFormat, _lstrlen(aFormat));
383 lBuf.AppendFormatList(lPtr, aMarkerList);
385 return len + DumpMessage((char *)lBuf.PtrZ(), lBuf.Length());
390 #ifdef SYMBIAN_FILE_LOGGER
391 // -----------------------------------------------------------------------------
392 // CLibLogger::DumpMessage
393 // Dump the internal message in hex format.
394 // -----------------------------------------------------------------------------
396 int CLibLogger::DumpMessage(char *aStr, int aStrLen)
398 /* check the inputs */
402 /* Error : Check your input value. */
406 /* Input parameter is okey */
408 TFileName logFileName;
409 GetLogFilename(logFileName);
410 RFileLogger::HexDump(KLogFileLocation, logFileName, EFileLoggingModeAppend, NULL, NULL, (TUint8*)aStr, aStrLen);
417 // -----------------------------------------------------------------------------
418 // CLibLogger::CreateFileSession
419 // Create a file session for the logger.
420 // -----------------------------------------------------------------------------
422 int CLibLogger::CreateFileSession(RFs &aFsSession, RFile &aFile)
425 /* register with the file service session. */
427 lErr = aFsSession.Connect();
430 /* Error : unable to create a session with file server */
434 /* Success : application is connected with the file server */
436 TFileName logFileName;
437 GetLogFilename(logFileName);
439 aFsSession.MkDirAll(logFileName);
444 lErr = aFsSession.GetDir(logFileName,KEntryAttNormal,
445 ESortByName,fileList,dirList);
448 /* Error : unable to retrieve the directory list */
449 aFsSession.Close(); /* close the session with file server */
453 if(0 >= fileList->Count())
455 /* File doesn't exist. create a new file. */
456 lErr = aFile.Replace(aFsSession,
458 EFileWrite|EFileStreamText);
461 /* Error : unable to create the new file. */
464 aFsSession.Close(); /* close the session with file server */
470 /* Shared access for reading and writing. */
471 lErr = aFile.Open(aFsSession,
473 EFileWrite | EFileStreamText);
476 /* Error : unable to open the file. */
479 aFsSession.Close(); /* close the session with file server */
482 int pos = 0; /* since we have pass the 2nd parameter as reference. */
483 lErr = aFile.Seek(ESeekEnd, pos);
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 */
496 /* delete the resources */
504 // -----------------------------------------------------------------------------
505 // CLibLogger::DumpMessage
506 // Dump the internal message in hex format.
507 // -----------------------------------------------------------------------------
509 int CLibLogger::DumpMessage(char *aStr, int aStrLen)
511 /* check the inputs */
515 /* Error : Check your input value. */
519 /* Input parameter is okey */
521 /* local variables */
524 /* store the input string to a local pointer */
525 TPtrC8 istrMesg((TUint8* )aStr, aStrLen);
527 /* local log buffer */
528 TBuf8<MAX_LOG_STR_LEN> lstrLogMesgString;
529 TBuf8<MAX_LOG_STR_LEN> ptempStr;
533 _LIT8(KDblSpace, " ");
534 _LIT8(KFormat, "%2x "); /* hex format */
535 _LIT8(KNewLine, "\r\n");
536 RFs fsSession; /* file server */
538 int lErr = CLibLogger::CreateFileSession(fsSession, file);
541 /* Error : Unable to initialize the file session */
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);
549 for(counter = 0; counter < aStrLen; counter++)
551 if((counter != 0) && ((counter % DUMP_STR_LENGTH) == 0))
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));
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);
565 /* free the buffer, set the length to zero */
566 lstrLogMesgString.FillZ();
567 lstrLogMesgString.SetLength(0);
569 ptempStr.Format(KFormat,(int)istrMesg[counter]);
570 lstrLogMesgString.Append(ptempStr);
573 if(0 < lstrLogMesgString.Length())
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)
580 for(int lliCounter = 0; lliCounter < DUMP_STR_LENGTH - (aStrLen % DUMP_STR_LENGTH); lliCounter++)
582 lstrLogMesgString.Append(KDblSpace); /* filling the blanks */
585 lstrLogMesgString.UpperCase();
586 /* fill the string */
587 lstrLogMesgString.Append(KSpace);
588 /* append the string */
589 if (aStrLen % DUMP_STR_LENGTH != 0)
591 lstrLogMesgString.Append(istrMesg.Mid(((counter / DUMP_STR_LENGTH)) * DUMP_STR_LENGTH, (aStrLen - ((counter / DUMP_STR_LENGTH)) * DUMP_STR_LENGTH)));
595 lstrLogMesgString.Append(istrMesg.Mid(((counter / DUMP_STR_LENGTH) - 1) * DUMP_STR_LENGTH, (aStrLen - ((counter / DUMP_STR_LENGTH) -1) * DUMP_STR_LENGTH)));
597 ptempStr.Format(KDumpSpace, ((aStrLen / DUMP_STR_LENGTH) * DUMP_STR_LENGTH));
598 lstrLogMesgString.Insert(0, ptempStr);
599 file.Write(lstrLogMesgString);
600 file.Write(KNewLine);
603 //CleanupStack::PopAndDestroy(2); // file, fsFession
606 return aStrLen; /* return the total dump message length */
609 #endif // SYMBIAN_FILE_LOGGER
613 GLDEF_C TInt E32Dll(TDllReason aReason)