sl@0: // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // inulogger.cpp
sl@0: // 
sl@0: //
sl@0: 
sl@0: #include <f32file.h> // for TParse, in Flogger stuff
sl@0: 
sl@0: 
sl@0: #include "InuLogger.h"
sl@0: 
sl@0: #define KLogsDir				_L("c:\\logs\\")
sl@0: #define KInuLogsDirName			_L("InetUtils")
sl@0: #define KTestHeader				_L("Inet Utils Log")
sl@0: #define KTestCommentPrepend		_L("\t")
sl@0: 
sl@0: const TInt KMaxLogLineLength = 128;
sl@0: 
sl@0: EXPORT_C
sl@0: TInuLogger::~TInuLogger()
sl@0: 	{
sl@0: #if defined (_DEBUG)
sl@0: 	iLogger.Close();
sl@0: #endif
sl@0: 	}
sl@0: 
sl@0: EXPORT_C
sl@0: #if defined (_DEBUG)
sl@0: void TInuLogger::CreateFlogger(const TDesC& aFileName, TInt aShowDate, TInt aShowTime)
sl@0: //
sl@0: //	Create log file in directory KLogsdir\KWapLogsDirName - Note: ingore Drive and Path of aFilename
sl@0: 	{
sl@0: 	iLogger.Connect();
sl@0: 	TParse p;
sl@0: 	p.Set(aFileName, NULL, NULL);
sl@0: 	iLogger.CreateLog(KInuLogsDirName, p.NameAndExt(), EFileLoggingModeOverwrite);
sl@0: 	iLogger.SetDateAndTime(aShowDate, aShowTime);
sl@0: 	iLogger.Write(KTestHeader);
sl@0: 	}
sl@0: #else if !defined (_DEBUG)
sl@0: void TInuLogger::CreateFlogger(const TDesC& , TInt , TInt ) {}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C
sl@0: #if defined (_DEBUG)
sl@0: void TInuLogger::LogIt(TRefByValue<const TDesC> aFmt, ...)
sl@0: //
sl@0: //	Messages to the front end emulator and to the log file
sl@0: 	{
sl@0: 	VA_LIST list;
sl@0: 	VA_START(list,aFmt);
sl@0: 
sl@0: 	TBuf<KMaxFileName> buf;
sl@0: 	buf.Append(KTestCommentPrepend);
sl@0: 	buf.AppendFormatList(aFmt,list);
sl@0: 	VA_END(list);
sl@0: 
sl@0: 	WriteComment(buf);
sl@0: 	}
sl@0: #else if !defined (_DEBUG)
sl@0: void TInuLogger::LogIt(TRefByValue<const TDesC> , ...) {}
sl@0: #endif
sl@0: 
sl@0: 
sl@0: 
sl@0: #if defined (_DEBUG)
sl@0: EXPORT_C void TInuLogger::WriteComment(const TDesC& aComment)
sl@0: //
sl@0: //	Writes aComment to test log file, logging file and test harness
sl@0: 	{
sl@0: 	TPtrC line;
sl@0: 	line.Set(aComment);
sl@0: 
sl@0: 	while (line.Length() > KMaxLogLineLength)
sl@0: 		{
sl@0: 		iLogger.Write(line.Left(KMaxLogLineLength));
sl@0: 		line.Set(line.Right(line.Length() - KMaxLogLineLength));
sl@0: 		}
sl@0: 	
sl@0: 	iLogger.Write(line.Left(line.Length()));
sl@0: 	
sl@0: 	}
sl@0: 
sl@0: #else if !defined (_DEBUG)
sl@0: EXPORT_C void TInuLogger::WriteComment(const TDesC& ) {}
sl@0: #endif
sl@0: 
sl@0: 
sl@0: 
sl@0: EXPORT_C
sl@0: #if defined (_DEBUG)
sl@0: void TInuLogger::DumpIt(const TDesC8& aData)
sl@0: //Do a formatted dump of binary data
sl@0: 	{
sl@0: 	// Iterate the supplied block of data in blocks of cols=80 bytes
sl@0: 	const TInt cols=16;
sl@0: 	TInt pos = 0;
sl@0: 	TBuf<KMaxLogLineLength> logLine;
sl@0: 	TBuf<KMaxLogLineLength> anEntry;
sl@0: 	while (pos < aData.Length())
sl@0: 		{
sl@0: 		//start-line exadecimal( a 4 digit number)
sl@0: 		anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos);
sl@0: 		logLine.Append(anEntry.Left(KMaxLogLineLength));
sl@0: 
sl@0: 		// Hex output
sl@0: 		TInt offset;
sl@0: 		for (offset = 0; offset < cols; offset++)
sl@0: 			{
sl@0: 			if (pos + offset < aData.Length())
sl@0: 				{
sl@0: 				TInt nextByte = aData[pos + offset];
sl@0: 				anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte);
sl@0: 				logLine.Append(anEntry);
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				//fill the remaining spaces with blanks untill the cols-th Hex number 
sl@0: 				anEntry.Format(TRefByValue<const TDesC>_L("   "));
sl@0: 				logLine.Append(anEntry);
sl@0: 				}
sl@0: 			}
sl@0: 			anEntry.Format(TRefByValue<const TDesC>_L(": "));
sl@0: 			logLine.Append(anEntry);
sl@0: 
sl@0: 		// Char output
sl@0: 		for (offset = 0; offset < cols; offset++)
sl@0: 			{
sl@0: 			if (pos + offset < aData.Length())
sl@0: 				{
sl@0: 				TInt nextByte = aData[pos + offset];
sl@0: 				if ((nextByte >= 32) && (nextByte <= 127))
sl@0: 					{
sl@0: 					anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte);
sl@0: 					logLine.Append(anEntry);
sl@0: 					}
sl@0: 				else
sl@0: 					{
sl@0: 					anEntry.Format(TRefByValue<const TDesC>_L("."));
sl@0: 					logLine.Append(anEntry);
sl@0: 					}
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				anEntry.Format(TRefByValue<const TDesC>_L(" "));
sl@0: 				logLine.Append(anEntry);
sl@0: 				}
sl@0: 			}
sl@0: 			LogIt(TRefByValue<const TDesC>_L("%S\n"), &logLine);	
sl@0: 			logLine.Zero();
sl@0: 
sl@0: 		// Advance to next  byte segment (1 seg= cols)
sl@0: 		pos += cols;
sl@0: 		}
sl@0: 	}
sl@0: #else if !defined (_DEBUG)
sl@0: void TInuLogger::DumpIt(const TDesC8& ) {}
sl@0: #endif
sl@0: 
sl@0: EXPORT_C
sl@0: #if defined (_DEBUG)
sl@0: void TInuLogger::WriteComment(const TDesC8& aData)
sl@0: //Do a write of the supplied data, literally where possible
sl@0: 	{
sl@0: 	TPtrC8 line;
sl@0: 	line.Set(aData);
sl@0: 	while (line.Length() > KMaxLogLineLength)
sl@0: 		{
sl@0: 		iLogger.Write(line.Left(KMaxLogLineLength));
sl@0: 		line.Set(line.Right(line.Length() - KMaxLogLineLength));
sl@0: 		}
sl@0: 	
sl@0: 	iLogger.Write(line.Left(line.Length()));
sl@0: 	}
sl@0: #else if !defined (_DEBUG)
sl@0: void TInuLogger::WriteComment(const TDesC8& ) {}
sl@0: #endif
sl@0: