applayerprotocols/httptransportfw/utils/httplogger.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-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 // System includes
       
    17 #include <f32file.h> // for TParse, in Flogger stuff
       
    18 #include <http/framework/httplogger.h> // Class signature
       
    19 
       
    20 // Local includes
       
    21 
       
    22 #define KLogsDir				_L("c:\\logs\\")
       
    23 #define KHttpLogsDirName			_L("http")
       
    24 #define KTestHeader				_L("HTTP Log")
       
    25 #define KTestCommentPrepend		_L("\t")
       
    26 
       
    27 #define KMaxLogLineLength 128
       
    28 
       
    29 EXPORT_C
       
    30 THttpLogger::~THttpLogger()
       
    31 	{
       
    32 #if defined (_DEBUG)
       
    33 	if(iLogger)
       
    34 		{
       
    35 		iLogger->Close();
       
    36 		delete iLogger;
       
    37 		}
       
    38 #endif
       
    39 	}
       
    40 
       
    41 EXPORT_C
       
    42 #if defined (_DEBUG)
       
    43 /**
       
    44 Function to create log file
       
    45 @param aFileName The descriptor that holds the name of the log file to be created.
       
    46 @param aShowDate If set to 1, the date of creation of the log file is recorded.
       
    47 @param aShowTime If set to 1, the time of creation of the log file is recorded.
       
    48 */
       
    49 void THttpLogger::CreateFlogger(const TDesC& aFileName, TInt aShowDate, TInt aShowTime)
       
    50 //
       
    51 //	Create log file in directory KLogsdir\KWapLogsDirName - Note: ingore Drive and Path of aFilename
       
    52 	{
       
    53 	if(!iLogger)
       
    54 		{
       
    55 		iLogger = new RFileLogger;
       
    56 		}
       
    57 
       
    58 	if(iLogger)
       
    59 		{
       
    60 		TInt error = iLogger->Connect();
       
    61 		if(error == KErrNone)
       
    62 			{
       
    63 			TParse p;
       
    64 			p.Set(aFileName, NULL, NULL);
       
    65 			iLogger->CreateLog(KHttpLogsDirName, p.NameAndExt(), EFileLoggingModeOverwrite);
       
    66 			iLogger->SetDateAndTime(aShowDate, aShowTime);
       
    67 			iLogger->Write(KTestHeader);
       
    68 			}
       
    69 		else
       
    70 			User::InfoPrint(_L("Flogger connect failed"));
       
    71 		}
       
    72 	else
       
    73 		User::InfoPrint(_L("Flogger create failed"));
       
    74 	}
       
    75 #elif !defined (_DEBUG)
       
    76 void THttpLogger::CreateFlogger(const TDesC& , TInt , TInt ) {}
       
    77 #endif
       
    78 
       
    79 EXPORT_C
       
    80 #if defined (_DEBUG)
       
    81 /**
       
    82 Function to print inputed log string on front end emulator and to write it to the log file.
       
    83 @param aFmt The descriptor containing the format string. The TRefByValue class provides a constructor which takes a TDesC type.
       
    84 @see TRefByValue
       
    85 @see TDesC
       
    86 */
       
    87 void THttpLogger::LogIt(TRefByValue<const TDesC> aFmt, ...)
       
    88 //
       
    89 //	Messages to the front end emulator and to the log file
       
    90 	{
       
    91 	VA_LIST list;
       
    92 	VA_START(list,aFmt);
       
    93 
       
    94 	TBuf<KMaxFileName> buf;
       
    95 	buf.AppendFormatList(aFmt,list,this);
       
    96 	VA_END(list);
       
    97 
       
    98 	WriteComment(buf);
       
    99 	}
       
   100 #elif !defined (_DEBUG)
       
   101 void THttpLogger::LogIt(TRefByValue<const TDesC> , ...) {}
       
   102 #endif
       
   103 
       
   104 
       
   105 
       
   106 #if defined (_DEBUG)
       
   107 /**
       
   108 Function to write a comment string to test log file, logging file and test harness.
       
   109 @param aComment The descriptor containing the comment string
       
   110 */
       
   111 EXPORT_C void THttpLogger::WriteComment(const TDesC& aComment)
       
   112 //
       
   113 //	Writes aComment to test log file, logging file and test harness
       
   114 	{
       
   115 	if(iLogger)
       
   116 		{
       
   117 		// If connection to flogger was made
       
   118 		if(iLogger->Handle() != 0)
       
   119 			{
       
   120 			TPtrC line;
       
   121 			line.Set(aComment);
       
   122 
       
   123 			while (line.Length() > KMaxLogLineLength)
       
   124 				{
       
   125 				iLogger->Write(line.Left(KMaxLogLineLength));
       
   126 				line.Set(line.Right(line.Length() - KMaxLogLineLength));
       
   127 				}
       
   128 			
       
   129 			iLogger->Write(line.Left(line.Length()));
       
   130 			}
       
   131 		}
       
   132 	}
       
   133 
       
   134 #elif !defined (_DEBUG)
       
   135 EXPORT_C void THttpLogger::WriteComment(const TDesC& ) {}
       
   136 #endif
       
   137 
       
   138 
       
   139 
       
   140 EXPORT_C
       
   141 #if defined (_DEBUG)
       
   142 /**
       
   143 Function to do a formatted dump of binary data.
       
   144 @param aData The descriptor that holds the binary data
       
   145 */
       
   146 void THttpLogger::DumpIt(const TDesC8& aData)
       
   147 //Do a formatted dump of binary data
       
   148 	{
       
   149 	// Iterate the supplied block of data in blocks of cols=80 bytes
       
   150 	const TInt cols=16;
       
   151 	TInt pos = 0;
       
   152 	TBuf<KMaxLogLineLength> logLine;
       
   153 	TBuf<KMaxLogLineLength> anEntry;
       
   154 	while (pos < aData.Length())
       
   155 		{
       
   156 		//start-line exadecimal( a 4 digit number)
       
   157 		anEntry.Format(TRefByValue<const TDesC>_L("%04x : "), pos);
       
   158 		logLine.Append(anEntry.Left(KMaxLogLineLength));
       
   159 
       
   160 		// Hex output
       
   161 		TInt offset;
       
   162 		for (offset = 0; offset < cols; offset++)
       
   163 			{
       
   164 			if (pos + offset < aData.Length())
       
   165 				{
       
   166 				TInt nextByte = aData[pos + offset];
       
   167 				anEntry.Format(TRefByValue<const TDesC>_L("%02x "), nextByte);
       
   168 				logLine.Append(anEntry);
       
   169 				}
       
   170 			else
       
   171 				{
       
   172 				//fill the remaining spaces with blanks untill the cols-th Hex number 
       
   173 				anEntry.Format(TRefByValue<const TDesC>_L("   "));
       
   174 				logLine.Append(anEntry);
       
   175 				}
       
   176 			}
       
   177 			anEntry.Format(TRefByValue<const TDesC>_L(": "));
       
   178 			logLine.Append(anEntry);
       
   179 
       
   180 		// Char output
       
   181 		for (offset = 0; offset < cols; offset++)
       
   182 			{
       
   183 			if (pos + offset < aData.Length())
       
   184 				{
       
   185 				TInt nextByte = aData[pos + offset];
       
   186 				if ((nextByte >= 32) && (nextByte <= 127))
       
   187 					{
       
   188 					anEntry.Format(TRefByValue<const TDesC>_L("%c"), nextByte);
       
   189 					logLine.Append(anEntry);
       
   190 					}
       
   191 				else
       
   192 					{
       
   193 					anEntry.Format(TRefByValue<const TDesC>_L("."));
       
   194 					logLine.Append(anEntry);
       
   195 					}
       
   196 				}
       
   197 			else
       
   198 				{
       
   199 				anEntry.Format(TRefByValue<const TDesC>_L(" "));
       
   200 				logLine.Append(anEntry);
       
   201 				}
       
   202 			}
       
   203 			LogIt(TRefByValue<const TDesC>_L("%S\n"), &logLine);	
       
   204 			logLine.Zero();
       
   205 
       
   206 		// Advance to next  byte segment (1 seg= cols)
       
   207 		pos += cols;
       
   208 		}
       
   209 	}
       
   210 #elif !defined (_DEBUG)
       
   211 void THttpLogger::DumpIt(const TDesC8& ) {}
       
   212 #endif
       
   213 
       
   214 EXPORT_C
       
   215 #if defined (_DEBUG)
       
   216 /**
       
   217 Function to do a write of the supplied data, literally where possible.
       
   218 @param aData The descriptor that holds the supplied data.
       
   219 */
       
   220 void THttpLogger::WriteComment(const TDesC8& aData)
       
   221 //Do a write of the supplied data, literally where possible
       
   222 	{
       
   223 	if(iLogger)
       
   224 		{
       
   225 		// If the connection to flogger was made
       
   226 		if(iLogger->Handle() != 0)
       
   227 			{
       
   228 			TPtrC8 line;
       
   229 			line.Set(aData);
       
   230 			while (line.Length() > KMaxLogLineLength)
       
   231 				{
       
   232 				iLogger->Write(line.Left(KMaxLogLineLength));
       
   233 				line.Set(line.Right(line.Length() - KMaxLogLineLength));
       
   234 				}
       
   235 			
       
   236 			iLogger->Write(line.Left(line.Length()));
       
   237 			}
       
   238 		}
       
   239 	}
       
   240 #elif !defined (_DEBUG)
       
   241 void THttpLogger::WriteComment(const TDesC8& ) {}
       
   242 #endif
       
   243 
       
   244 EXPORT_C
       
   245 #ifdef _DEBUG
       
   246 void THttpLogger::Overflow(TDes& aDes)
       
   247 	{
       
   248 	// Overflow has occured - end log line with '...'
       
   249 	_LIT(KErrOverflowMsg, "...");
       
   250 	if( aDes.MaxLength() >= KErrOverflowMsg().Length() + aDes.Length() )
       
   251 		aDes.Append(KErrOverflowMsg);
       
   252 	}
       
   253 #else
       
   254 void THttpLogger::Overflow(TDes& /*aDes*/)
       
   255 	{
       
   256 	}
       
   257 #endif