loggingservices/filelogger/SCLI/FLOGFMT.CPP
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 1997-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 /**
       
    17  @file
       
    18 */
       
    19 
       
    20 #include <utf.h>
       
    21 #include <flogger.h>
       
    22 #include "FLOGSTD.H"
       
    23 
       
    24 /**
       
    25 16-bit tab char width for symbian OS build.
       
    26 */
       
    27 const TText KTabChar='\t';
       
    28 
       
    29 /**
       
    30 8-bit FullStop char for non-unicode
       
    31 */
       
    32 const TText8 KFullStopChar8='.';
       
    33 
       
    34 /**
       
    35 8-bit tab char for unicode
       
    36 */
       
    37 const TText8 KTabChar8='\t';
       
    38 
       
    39 /**
       
    40 Max length for a TBuf date or time variable.
       
    41 */
       
    42 const TInt KDateOrTimeMaxLength=30;
       
    43 
       
    44 /**
       
    45 Constructs a 8 bit constant literal descriptor for
       
    46 end of line.
       
    47 */
       
    48 _LIT8(KEndOfLineCharacters8,"\r\n");
       
    49 
       
    50 /**
       
    51 Constructs a 16 bit constant literal descriptor for
       
    52 end of line.
       
    53 */
       
    54 _LIT(KEndOfLineCharacters,"\r\n");
       
    55 
       
    56 /**
       
    57 Date format for European style
       
    58 */
       
    59 _LIT(KDateFormat,"%1%/1%2%/2%3\t");
       
    60 
       
    61 /**
       
    62 24 hour time format
       
    63 */
       
    64 _LIT(KTimeFormat,"%J%:1%T%:2%S\t");
       
    65 
       
    66 
       
    67 /**
       
    68 TLogFile class definition
       
    69 */
       
    70 
       
    71 TLogFile::TLogFile()
       
    72 /**
       
    73 Sets initial values for iValid and iMode.
       
    74 */
       
    75 	{
       
    76 
       
    77 	iValid=EFalse;
       
    78 	iMode=EFileLoggingModeUnknown;
       
    79 	}
       
    80 
       
    81 TLogFile::TLogFile(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode)
       
    82 /**
       
    83 Sets initial values for iValid,iDirectory,iName and iMode.
       
    84 
       
    85 @param aDir Full Path of the log file.
       
    86 @param aName Name of the log file.
       
    87 @param aMode Specifies whether data is appended or file is truncated.
       
    88 */
       
    89 	{
       
    90 
       
    91 	iValid=EFalse;
       
    92 	iDirectory=aDir;
       
    93 	iName=aName;
       
    94 	iMode=aMode;
       
    95 	}
       
    96 
       
    97 TBool TLogFile::operator==(const TLogFile& aLogFile) const
       
    98 /**
       
    99 iValid members need not be equal
       
   100 
       
   101 @param aLogFile log file.
       
   102 */
       
   103 	{
       
   104 
       
   105 	if (iDirectory!=aLogFile.iDirectory)
       
   106 		return EFalse;
       
   107 	if (iName!=aLogFile.iName)
       
   108 		return EFalse;
       
   109 	if (iMode!=aLogFile.iMode)
       
   110 		return EFalse;
       
   111 	return ETrue;
       
   112 	}
       
   113 
       
   114 void TLogFile::Set(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode)
       
   115 /**
       
   116 Sets values for iValid,iDirectory,iName and iMode.
       
   117 
       
   118 @param aDir Full Path of the log file.
       
   119 @param aName Name of the log file.
       
   120 @param aMode Specifies whether data is appended or file is truncated.
       
   121 */
       
   122 	{
       
   123 
       
   124 	iValid=EFalse;
       
   125 	iDirectory=aDir;
       
   126 	iName=aName;
       
   127 	iMode=aMode;
       
   128 	}
       
   129 
       
   130 /**
       
   131 TLogFormatterOverflow class definition
       
   132 */
       
   133 
       
   134 void TLogFormatter16Overflow::Overflow(TDes16& /*aDes*/)
       
   135 /** 
       
   136 TDes16Overflow pure virtual 
       
   137 This member is internal and not intended for use.
       
   138 */	
       
   139 	{
       
   140 	}
       
   141 
       
   142 void TLogFormatter8Overflow::Overflow(TDes8& /*aDes*/)
       
   143 /** 
       
   144 TDes16Overflow pure virtual 
       
   145 This member is internal and not intended for use.
       
   146 */
       
   147 	{
       
   148 	}
       
   149 
       
   150 /**
       
   151 TLogFormat class definition
       
   152 */
       
   153 
       
   154 TLogFormatter::TLogFormatter()
       
   155 	: iUseDate(ETrue), iUseTime(ETrue)
       
   156 /**
       
   157 Sets iUseDate and iUseTime to ETrue.
       
   158 */
       
   159 	{}
       
   160 
       
   161 void TLogFormatter::SetDateAndTime(TBool aUseDate, TBool aUseTime)
       
   162 /**
       
   163 Sets whether to use date and/or time
       
   164 */
       
   165 	{
       
   166 
       
   167 	iUseDate=aUseDate;
       
   168 	iUseTime=aUseTime;
       
   169 	}
       
   170 
       
   171 TInt TLogFormatter::FormatTextToWritableBuffer(TDes8& aBuf, const TDesC16& aText) const
       
   172 /**
       
   173 Returns result in aBuf
       
   174 */
       
   175 	{
       
   176 
       
   177 	TRAPD(ret,WriteL(aBuf,aText));
       
   178 	return ret;
       
   179 	}
       
   180 
       
   181 TInt TLogFormatter::FormatTextToWritableBuffer(TDes8& aBuf, const TDesC8& aText) const
       
   182 /**
       
   183 Returns result in aBuf
       
   184 */
       
   185 	{
       
   186 
       
   187 	TRAPD(ret,WriteL(aBuf,aText));
       
   188 	return ret;
       
   189 	}
       
   190 
       
   191 TInt TLogFormatter::ConvertToWritableBuffer(TDes8& aBuf, TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
       
   192 /**
       
   193 Formats string to aBuf
       
   194 */
       
   195 	{
       
   196 
       
   197 	TBuf16<KLogBufferSize> buf;
       
   198 	buf.AppendFormatList(aFmt,aList,&iOverflow16);
       
   199 	TRAPD(ret,WriteL(aBuf,buf));
       
   200 	return ret;
       
   201 	}
       
   202 
       
   203 TInt TLogFormatter::ConvertToWritableBuffer(TDes8& aBuf, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
       
   204 /**
       
   205 Formats string to aBuf
       
   206 */
       
   207 	{
       
   208 
       
   209 	TBuf8<KLogBufferSize> buf;
       
   210 	buf.AppendFormatList(aFmt,aList,&iOverflow8);
       
   211 	TRAPD(ret,WriteL(aBuf,buf));
       
   212 	return ret;
       
   213 	}
       
   214 
       
   215 void TLogFormatter::GetDateAndTimeL(TDes& aDate, TDes& aTime) const
       
   216 /**
       
   217 Gets date and time according to flags to buffer aBuf
       
   218 */
       
   219 	{
       
   220 
       
   221 	aDate.SetLength(0);
       
   222 	aTime.SetLength(0);
       
   223 		
       
   224 	if (!iUseTime && !iUseDate)
       
   225 		return;
       
   226 
       
   227 	TTime t;
       
   228 	t.HomeTime();
       
   229 	if (iUseDate)
       
   230 		t.FormatL(aDate,KDateFormat);
       
   231 	
       
   232 	if (iUseTime)
       
   233 		t.FormatL(aTime,KTimeFormat);
       
   234 	}
       
   235 
       
   236 void TLogFormatter::WriteL(TDes8& aTrg, const TDesC16& aSrc) const
       
   237 /**
       
   238 Appends date/time as specified and truncate aSrc and convert 
       
   239 unprintable characters to '.'.  Convert unicode to UTF8 and 
       
   240 return the result in aTrg
       
   241 */
       
   242 	{	
       
   243 
       
   244 	aTrg.SetLength(0);
       
   245 	TBuf16<KDateOrTimeMaxLength> dateBuf;
       
   246 	TBuf16<KDateOrTimeMaxLength> timeBuf;
       
   247 	GetDateAndTimeL(dateBuf,timeBuf);
       
   248 	TBuf16<KLogBufferSize> buf;
       
   249 	__ASSERT_DEBUG(buf.MaxLength()>=dateBuf.Length(), User::Invariant());
       
   250 	buf.Append(dateBuf);
       
   251 	__ASSERT_DEBUG((buf.MaxLength()-buf.Length())>=timeBuf.Length(), User::Invariant());
       
   252 	buf.Append(timeBuf);
       
   253 	buf.Append(aSrc.Left(Min(aSrc.Length(),(KLogBufferSize-buf.Length()-2))));	// -2 to allow for CRLF
       
   254 	TChar ch;
       
   255 	for (TInt i=0; i<buf.Length(); i++)
       
   256 		{
       
   257 		ch=buf[i];
       
   258 		if(!((ch.IsPrint()) || (ch==KTabChar)))
       
   259 			buf[i]=KFullStopChar;
       
   260 		}
       
   261 	buf.Append(KEndOfLineCharacters);
       
   262 
       
   263 	TBuf8<KLogBufferSize> utfBuf;
       
   264 	CnvUtfConverter::ConvertFromUnicodeToUtf8(utfBuf,buf);
       
   265 	aTrg.Copy(utfBuf.Left(Min(utfBuf.Length(),aTrg.MaxLength())));
       
   266 	}
       
   267 
       
   268 void TLogFormatter::WriteL(TDes8& aTrg, const TDesC8& aSrc) const
       
   269 /**
       
   270 Append date/time as specified and truncate aSrc and convert 
       
   271 unprintable characters to '.'.
       
   272 */
       
   273 	{	
       
   274 
       
   275 	aTrg.SetLength(0);
       
   276 	TBuf16<KDateOrTimeMaxLength> dateBuf;
       
   277 	TBuf16<KDateOrTimeMaxLength> timeBuf;
       
   278 	GetDateAndTimeL(dateBuf,timeBuf);
       
   279 	TBuf8<KDateOrTimeMaxLength> eightBitDateBuf;
       
   280 	eightBitDateBuf.Copy(dateBuf);
       
   281 	TBuf8<KDateOrTimeMaxLength> eightBitTimeBuf;
       
   282 	eightBitTimeBuf.Copy(timeBuf);
       
   283 	TBuf8<KLogBufferSize> buf;
       
   284 	__ASSERT_DEBUG(buf.MaxLength()>=dateBuf.Length(), User::Invariant());
       
   285 	buf.Append(eightBitDateBuf);
       
   286 	__ASSERT_DEBUG((buf.MaxLength()-buf.Length())>=timeBuf.Length(), User::Invariant());
       
   287 	buf.Append(eightBitTimeBuf);
       
   288 	buf.Append(aSrc.Left(Min(aSrc.Length(),(KLogBufferSize-buf.Length()-2))));	// -2 to allow for CRLF
       
   289 	TChar ch;
       
   290 	for (TInt i=0; i<buf.Length(); i++)
       
   291 		{
       
   292 		ch=buf[i];
       
   293 		if(!((ch.IsPrint()) || (ch==KTabChar8)))
       
   294 			buf[i]=KFullStopChar8;
       
   295 		}
       
   296 	buf.Append(KEndOfLineCharacters8);
       
   297 	aTrg.Copy(buf.Left(Min(buf.Length(),aTrg.MaxLength())));
       
   298 	}