xml/legacyminidomparser/xmlparser/test/t_XMLParserLOG.CPP
changeset 34 c7e9f1c97567
parent 0 e35f40988205
equal deleted inserted replaced
25:417699dc19c9 34:c7e9f1c97567
       
     1 // Copyright (c) 2004-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 //
       
    15 
       
    16 #ifdef _DEBUG
       
    17 #undef _MSG_NO_LOGGING
       
    18 #endif
       
    19 
       
    20 #include "t_XMLParserLOG.H"
       
    21 
       
    22 
       
    23 CImLog::CImLog()
       
    24 	{
       
    25 	__DECLARE_NAME(_S("CImLog"));
       
    26 	}
       
    27 
       
    28 //
       
    29 // NewL - user describes full file path and file name for the log file
       
    30 //
       
    31 CImLog  *CImLog::NewL(const TDesC& aFullFilePath, TImLogOpenMode aImLogOpenMode)
       
    32 	{
       
    33 	CImLog* self = new (ELeave) CImLog();
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL(aFullFilePath,aImLogOpenMode);
       
    36 	CleanupStack::Pop();
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 //
       
    41 // 2nd phase of construction
       
    42 //
       
    43 #ifndef _MSG_NO_LOGGING
       
    44 void CImLog::ConstructL(const TDesC& aFullFilePath, TImLogOpenMode aImLogOpenMode)
       
    45 #else
       
    46 void CImLog::ConstructL(const TDesC& /*aFullFilePath*/, TImLogOpenMode /*aImLogOpenMode*/)
       
    47 #endif
       
    48 	{
       
    49 #ifndef _MSG_NO_LOGGING
       
    50 
       
    51 	User::LeaveIfError(iFs.Connect());
       
    52 	TFileName logFile; 
       
    53 	logFile.Append(aFullFilePath);
       
    54 
       
    55 	if (aImLogOpenMode==EAppend)
       
    56 		{
       
    57 		TInt err=iFile.Open(iFs,logFile,EFileWrite|EFileShareAny);
       
    58 		if (err==KErrNotFound)
       
    59 			{
       
    60 			iFile.Create(iFs,logFile,EFileWrite|EFileShareAny);
       
    61 			}
       
    62 		else if (err != KErrNone)
       
    63 			{	
       
    64 			User::Leave(err);
       
    65 			}
       
    66 		TInt position=1;
       
    67 		User::LeaveIfError(iFile.Seek(ESeekEnd,position));
       
    68 		}
       
    69 	else
       
    70 		{
       
    71 		// If the log directory has not been created, no log will be created. OK
       
    72 		User::LeaveIfError(iFile.Replace(iFs,logFile,EFileWrite|EFileShareAny));
       
    73 		}
       
    74 
       
    75 #endif // _MSG_NO_LOGGING
       
    76 	}
       
    77 
       
    78 CImLog::~CImLog()
       
    79 	{
       
    80 #ifndef _MSG_NO_LOGGING
       
    81 
       
    82 	iFile.Close();
       
    83 	iFs.Close();
       
    84 
       
    85 #endif // _MSG_NO_LOGGING
       
    86 	}
       
    87 
       
    88 //
       
    89 // Log an error message
       
    90 //
       
    91 #ifndef _MSG_NO_LOGGING
       
    92 void CImLog::AppendError(const TDesC8& aErrorMessage, TInt aErrorCode)
       
    93 #else
       
    94 void CImLog::AppendError(const TDesC8& /*aErrorMessage*/, TInt /*aErrorCode*/)
       
    95 #endif
       
    96 	{
       
    97 #ifndef _MSG_NO_LOGGING
       
    98 	WriteToLog(KLogErrorString, &aErrorMessage, aErrorCode);
       
    99 #endif
       
   100 	}
       
   101 
       
   102 //
       
   103 // Log a comment
       
   104 //
       
   105 #ifndef _MSG_NO_LOGGING
       
   106 void CImLog::AppendComment(const TDesC8& aMessage)
       
   107 #else
       
   108 void CImLog::AppendComment(const TDesC8& /*aMessage*/)
       
   109 #endif
       
   110 	{
       
   111 #ifndef _MSG_NO_LOGGING
       
   112 	WriteToLog(KLogCommentString, &aMessage);
       
   113 #endif
       
   114 	}
       
   115 
       
   116 //
       
   117 // Construct a string with date, a message type indicator (out, in, error) sort out formatting
       
   118 //
       
   119 #ifndef _MSG_NO_LOGGING
       
   120 void CImLog::BuildLogString(const TDesC8& aFormat,const TDesC8& aOutPut, TBool aWriteCrLf)
       
   121 #else
       
   122 void CImLog::BuildLogString(const TDesC8& /*aFormat*/,const TDesC8& /*aOutPut*/, TBool /*aWriteCrLf*/)
       
   123 #endif
       
   124 	{
       
   125 #ifndef _MSG_NO_LOGGING
       
   126 
       
   127 	TPtrC8 logString(aOutPut);
       
   128 
       
   129 	for(;;)
       
   130 		{
       
   131 		TInt crLfPos=logString.Find(KLogCR);
       
   132 		if(crLfPos==KErrNotFound)
       
   133 			{
       
   134 			break;
       
   135 			}
       
   136 		else
       
   137 			{
       
   138 			TPtrC8 line;
       
   139 			if (aWriteCrLf && (crLfPos == logString.Size() - 2))
       
   140 				{
       
   141 				line.Set(logString);
       
   142 				}
       
   143 			else
       
   144 				{
       
   145 				line.Set(logString.Left(crLfPos));
       
   146 				}
       
   147 			WriteToLog(TRefByValue<const TDesC8>(aFormat),&line);
       
   148 			logString.Set(logString.Mid(crLfPos+2));
       
   149 			}
       
   150 		}
       
   151 
       
   152 	if(logString.Length())
       
   153 		{
       
   154 		WriteToLog(TRefByValue<const TDesC8>(aFormat),&logString);
       
   155 		}
       
   156 
       
   157 #endif // _MSG_NO_LOGGING
       
   158 	}
       
   159 
       
   160 //
       
   161 // Write information to the log file
       
   162 //
       
   163 #ifndef _MSG_NO_LOGGING
       
   164 void CImLog::WriteToLog(TRefByValue<const TDesC8> aFmt,...)
       
   165 #else
       
   166 void CImLog::WriteToLog(TRefByValue<const TDesC8> /*aFmt*/,...)
       
   167 #endif
       
   168 	{
       
   169 #ifndef _MSG_NO_LOGGING
       
   170 
       
   171 	TBuf8<1024> buf;
       
   172 	TTime date;
       
   173 	date.HomeTime();
       
   174 	TBuf<18> dateString;
       
   175 	TRAPD(error, date.FormatL(dateString,(KLogDateFormatString)));
       
   176 	if(error)
       
   177 	{
       
   178 		dateString.Copy(_L("Invalid Date"));
       
   179 	}
       
   180 	buf.Copy(dateString);
       
   181 	
       
   182 	VA_LIST list;
       
   183 	VA_START(list,aFmt);
       
   184 
       
   185 	buf.AppendFormatList(aFmt,list);
       
   186 	buf.Append(KLogCR);
       
   187 	iFile.Write(buf);
       
   188 
       
   189 #endif // _MSG_NO_LOGGING
       
   190 	}