email/pop3andsmtpmtm/clientmtms/src/MIUTLOG.CPP
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1998-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 "MIUTLOG.H"
       
    21 
       
    22 
       
    23 CImLog::CImLog()
       
    24 	{
       
    25 	__DECLARE_NAME(_S("CImLog"));
       
    26 	}
       
    27 
       
    28 EXPORT_C CImLog  *CImLog::NewL(TInt aPortNum)
       
    29 	{
       
    30 	CImLog* self = new (ELeave) CImLog();
       
    31 	CleanupStack::PushL(self);
       
    32 	self->ConstructL(aPortNum);
       
    33 	CleanupStack::Pop();
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 //
       
    38 // 2nd phase of construction
       
    39 //
       
    40 #ifndef _MSG_NO_LOGGING
       
    41 void CImLog::ConstructL(TInt aPortNum)
       
    42 #else
       
    43 void CImLog::ConstructL(TInt /*aPortNum*/)
       
    44 #endif
       
    45 	{
       
    46 #ifndef _MSG_NO_LOGGING
       
    47 
       
    48 	User::LeaveIfError(iFs.Connect());
       
    49 	TFileName logFileStem; 
       
    50 	logFileStem.Format(KMiutLogFile,aPortNum);
       
    51 
       
    52 	TBool fileOpen = TRUE;
       
    53 	TFileName logFile = logFileStem;
       
    54 	logFile.Append(KMiutLogFileExtension);
       
    55 	TUint fileCounter = 1;
       
    56 	TInt err;
       
    57 
       
    58 // Keep trying until a suitable file name is found that isn't already open.
       
    59 	while (fileOpen)
       
    60 		{
       
    61 		err = iFs.IsFileOpen(logFile, fileOpen);
       
    62 		if (err == KErrNotFound)
       
    63 			{
       
    64 			err = KErrNone;
       
    65 			fileOpen = FALSE;
       
    66 			}
       
    67 
       
    68 		User::LeaveIfError(err);
       
    69 		
       
    70 		if (fileOpen)
       
    71 			{
       
    72 			++fileCounter;
       
    73 			logFile.Format(KMiutLogUnopenedFileFormatString, &logFileStem, fileCounter);
       
    74 			}
       
    75 		}
       
    76 
       
    77 // If the log directory has not been created, no log will be created. OK
       
    78 		User::LeaveIfError(iFile.Replace(iFs,logFile,EFileWrite|EFileShareAny));
       
    79 
       
    80 #endif // _MSG_NO_LOGGING
       
    81 	}
       
    82 
       
    83 //
       
    84 // Alternate NewL - user describes full file path and file name for the log file
       
    85 //
       
    86 EXPORT_C CImLog  *CImLog::NewL(const TDesC& aFullFilePath, TImLogOpenMode aImLogOpenMode)
       
    87 	{
       
    88 	CImLog* self = new (ELeave) CImLog();
       
    89 	CleanupStack::PushL(self);
       
    90 	self->ConstructL(aFullFilePath,aImLogOpenMode);
       
    91 	CleanupStack::Pop();
       
    92 	return self;
       
    93 	}
       
    94 
       
    95 //
       
    96 // 2nd phase of construction
       
    97 //
       
    98 #ifndef _MSG_NO_LOGGING
       
    99 void CImLog::ConstructL(const TDesC& aFullFilePath, TImLogOpenMode aImLogOpenMode)
       
   100 #else
       
   101 void CImLog::ConstructL(const TDesC& /*aFullFilePath*/, TImLogOpenMode /*aImLogOpenMode*/)
       
   102 #endif
       
   103 	{
       
   104 #ifndef _MSG_NO_LOGGING
       
   105 
       
   106 	User::LeaveIfError(iFs.Connect());
       
   107 	TFileName logFile; 
       
   108 	logFile.Append(aFullFilePath);
       
   109 
       
   110 	if (aImLogOpenMode==EAppend)
       
   111 		{
       
   112 		TInt err=iFile.Open(iFs,logFile,EFileWrite|EFileShareAny);
       
   113 		if (err==KErrNotFound)
       
   114 			{
       
   115 			User::LeaveIfError(iFile.Create(iFs,logFile,EFileWrite|EFileShareAny));
       
   116 			}
       
   117 		else if (err != KErrNone)
       
   118 			{
       
   119 			User::Leave(err);
       
   120 			}			
       
   121 		TInt position=1;
       
   122 		User::LeaveIfError(iFile.Seek(ESeekEnd,position));
       
   123 		}
       
   124 	else
       
   125 		{
       
   126 		// If the log directory has not been created, no log will be created. OK
       
   127 		User::LeaveIfError(iFile.Replace(iFs,logFile,EFileWrite|EFileShareAny));
       
   128 		}
       
   129 
       
   130 #endif // _MSG_NO_LOGGING
       
   131 	}
       
   132 
       
   133 EXPORT_C CImLog::~CImLog()
       
   134 	{
       
   135 #ifndef _MSG_NO_LOGGING
       
   136 
       
   137 	iFile.Close();
       
   138 	iFs.Close();
       
   139 
       
   140 #endif // _MSG_NO_LOGGING
       
   141 	}
       
   142 
       
   143 //
       
   144 // Log message sent to the socket
       
   145 //
       
   146 #ifndef _MSG_NO_LOGGING
       
   147 EXPORT_C void CImLog::AppendOut(const TDesC8& outPut)
       
   148 #else
       
   149 EXPORT_C void CImLog::AppendOut(const TDesC8& /*outPut*/)
       
   150 #endif
       
   151 	{
       
   152 #ifndef _MSG_NO_LOGGING
       
   153 	BuildLogString(KMiutLogOutputString, outPut, ETrue);
       
   154 #endif
       
   155 	}
       
   156 
       
   157 //
       
   158 // Log message received from socket
       
   159 //
       
   160 #ifndef _MSG_NO_LOGGING
       
   161 EXPORT_C void CImLog::AppendResponse(const TDesC8& theResponse)
       
   162 #else
       
   163 EXPORT_C void CImLog::AppendResponse(const TDesC8& /*theResponse*/)
       
   164 #endif
       
   165 	{
       
   166 #ifndef _MSG_NO_LOGGING
       
   167 	BuildLogString(KMiutLogResponseString, theResponse, ETrue);
       
   168 #endif
       
   169 	}
       
   170 
       
   171 //
       
   172 // Log an error message
       
   173 //
       
   174 #ifndef _MSG_NO_LOGGING
       
   175 EXPORT_C void CImLog::AppendError(const TDesC8& theError, TInt anErrorCode)
       
   176 #else
       
   177 EXPORT_C void CImLog::AppendError(const TDesC8& /*theError*/, TInt /*anErrorCode*/)
       
   178 #endif
       
   179 	{
       
   180 #ifndef _MSG_NO_LOGGING
       
   181 	WriteToLog(KMiutLogErrorString, &theError, anErrorCode);
       
   182 #endif
       
   183 	}
       
   184 
       
   185 //
       
   186 // Log a comment
       
   187 //
       
   188 #ifndef _MSG_NO_LOGGING
       
   189 EXPORT_C void CImLog::AppendComment(const TDesC8& theError)
       
   190 #else
       
   191 EXPORT_C void CImLog::AppendComment(const TDesC8& /*theError*/)
       
   192 #endif
       
   193 	{
       
   194 #ifndef _MSG_NO_LOGGING
       
   195 	WriteToLog(KMiutLogCommentString, &theError);
       
   196 #endif
       
   197 	}
       
   198 
       
   199 //
       
   200 // Construct a string with date, a message type indicator (out, in, error) sort out formatting
       
   201 //
       
   202 #ifndef _MSG_NO_LOGGING
       
   203 void CImLog::BuildLogString(const TDesC8& aFormat,const TDesC8& theOutPut, TBool aWriteCrLf)
       
   204 #else
       
   205 void CImLog::BuildLogString(const TDesC8& /*aFormat*/,const TDesC8& /*theOutPut*/, TBool /*aWriteCrLf*/)
       
   206 #endif
       
   207 	{
       
   208 #ifndef _MSG_NO_LOGGING
       
   209 
       
   210 	TPtrC8 logString(theOutPut);
       
   211 
       
   212 	for(;;)
       
   213 		{
       
   214 		TInt crLfPos=logString.Find(KMiutLogCR);
       
   215 		if(crLfPos==KErrNotFound)
       
   216 			break;
       
   217 		else
       
   218 			{
       
   219 			TPtrC8 line;
       
   220 			if (aWriteCrLf && (crLfPos == logString.Size() - 2))
       
   221 				{
       
   222 				line.Set(logString);
       
   223 				}
       
   224 			else
       
   225 				{
       
   226 				line.Set(logString.Left(crLfPos));
       
   227 				}
       
   228 			WriteToLog(TRefByValue<const TDesC8>(aFormat),&line);
       
   229 			logString.Set(logString.Mid(crLfPos+2));
       
   230 			}
       
   231 		}
       
   232 
       
   233 	if(logString.Length())
       
   234 		WriteToLog(TRefByValue<const TDesC8>(aFormat),&logString);
       
   235 
       
   236 #endif // _MSG_NO_LOGGING
       
   237 	}
       
   238 
       
   239 //
       
   240 // Write information to the log file
       
   241 //
       
   242 #ifndef _MSG_NO_LOGGING
       
   243 void CImLog::WriteToLog(TRefByValue<const TDesC8> aFmt,...)
       
   244 #else
       
   245 void CImLog::WriteToLog(TRefByValue<const TDesC8> /*aFmt*/,...)
       
   246 #endif
       
   247 	{
       
   248 #ifndef _MSG_NO_LOGGING
       
   249 
       
   250 	TBuf8<2560> buf;
       
   251 	TTime date;
       
   252 	date.UniversalTime();
       
   253 	TBuf<18> dateString;
       
   254 	TRAP_IGNORE(date.FormatL(dateString,(KMiutLogDateFormatString)));
       
   255 	buf.Copy(dateString);
       
   256 	
       
   257 	VA_LIST list;
       
   258 	VA_START(list,aFmt);
       
   259 
       
   260 	buf.AppendFormatList(aFmt,list);
       
   261 	buf.Append(KMiutLogCR);
       
   262 	iFile.Write(buf);
       
   263 
       
   264 #endif // _MSG_NO_LOGGING
       
   265 	}