1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/loggingservices/filelogger/SCLI/FLOGCLI.CPP Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,813 @@
1.4 +// Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +/**
1.20 + @file
1.21 +*/
1.22 +
1.23 +#include <f32file.h>
1.24 +#include <flogger.h>
1.25 +#include "FLOGSTD.H"
1.26 +
1.27 +/**
1.28 +Macro for Single blank space.
1.29 +
1.30 +*/
1.31 +#define BLANK _S("")
1.32 +
1.33 +const TInt KHexDumpWidth=16;
1.34 +
1.35 +/**
1.36 +Literal constant that hold a string and unicode hex.
1.37 +
1.38 +*/
1.39 +_LIT(KFirstFormatString,"%s%04x : ");
1.40 +
1.41 +/**
1.42 +Literal constant that hold a string and unicode hex.
1.43 +
1.44 +*/
1.45 +_LIT(KSecondFormatString,"%02x ");
1.46 +
1.47 +/**
1.48 +Character Constant.
1.49 +
1.50 +*/
1.51 +_LIT(KThirdFormatString,"%c");
1.52 +
1.53 +/**
1.54 +Literal Constant for 3 blank spaces.
1.55 +
1.56 +*/
1.57 +_LIT(KThreeSpaces," ");
1.58 +
1.59 +/**
1.60 +Literal Constant for 2 blank spaces.
1.61 +
1.62 +*/
1.63 +_LIT(KTwoSpaces," ");
1.64 +
1.65 +
1.66 +IMPORT_C extern const TBool KFloggerSTI;
1.67 +
1.68 +/*
1.69 +RFileLogger class definition
1.70 +*/
1.71 +
1.72 +EXPORT_C RFileLogger::RFileLogger()
1.73 + : RSessionBase(), iFormatter(), iLastError(KErrNone), iLogSTI(KFloggerSTI)
1.74 +/** Creates a default RFileLogger object. */
1.75 + {}
1.76 +
1.77 +EXPORT_C RFileLogger::~RFileLogger()
1.78 +/** Empty destructor.
1.79 +Clients with open sessions must end the session by calling Close() beforehand.
1.80 +Note that it does not delete the log file.*/
1.81 + {}
1.82 +
1.83 +EXPORT_C TVersion RFileLogger::Version() const
1.84 +/** Returns the version number.
1.85 +
1.86 +@return The version number. */
1.87 + {
1.88 +
1.89 + return(TVersion(KFLogSrvMajorVersionNumber,KFLogSrvMinorVersionNumber,KFLogSrvBuildVersionNumber));
1.90 + }
1.91 +
1.92 +EXPORT_C TInt RFileLogger::Connect()
1.93 +/** Connects to the file logger server with the default number of message slots, which is 8.
1.94 +
1.95 +This function does not need to be called if you are using the static versions
1.96 +of Write(), WriteFormat() or HexDump().
1.97 +
1.98 +@return KErrNone if successful, otherwise one of the system-wide error codes. */
1.99 + {
1.100 +
1.101 + TInt ret=DoConnect();
1.102 + if (ret==KErrNotFound)
1.103 + {
1.104 + ret=FLogger::Start();
1.105 + if (ret==KErrNone || ret==KErrAlreadyExists)
1.106 + ret=DoConnect();
1.107 + }
1.108 + return ret;
1.109 + }
1.110 +
1.111 +EXPORT_C void RFileLogger::SetDateAndTime(TBool aUseDate,TBool aUseTime)
1.112 +/** Specifies whether time and/or date should be appended to log data.
1.113 +
1.114 +Appending of the time and date to log entries can be switched on and off
1.115 +at anytime between creation and destruction of RFileLogger.
1.116 +
1.117 +This function does not need to be called if you are using the static versions
1.118 +of Write(), WriteFormat() or HexDump().
1.119 +
1.120 +@param aUseDate Use ETrue to log the date, otherwise EFalse.
1.121 +@param aUseTime Use ETrue to log the time, otherwise EFalse. */
1.122 + {
1.123 +
1.124 + iFormatter.SetDateAndTime(aUseDate,aUseTime);
1.125 + }
1.126 +
1.127 +EXPORT_C void RFileLogger::CreateLog(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode)
1.128 +/** Creates or opens a file for logging.
1.129 +
1.130 +When specifying a directory for logging only specify the relative path to
1.131 +'C:\\Logs\\' and do not append a '\\' to the end of the path either. CreateLog()
1.132 +only creates the specified log file if the directory exists. This means that
1.133 +switching logging on and off can be achieved without having to re-compile,
1.134 +by just removing or renaming the log directory.
1.135 +
1.136 +aLogFile.iValid is set according to whether logging will actually occur or not.
1.137 +
1.138 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides
1.139 +or is to be created.
1.140 +@param aName The name of the log file.
1.141 +@param aMode The mode in which the log file should be opened, either EAppend
1.142 +or EOverwrite. */
1.143 + {
1.144 +
1.145 + iLogFile.Set(aDir,aName,aMode);
1.146 + TPckg<TLogFile> logFilePckg(iLogFile);
1.147 + iLastError=SendReceive(ECreateLog, TIpcArgs( &logFilePckg));
1.148 +
1.149 + if (iLastError!=KErrNone && !LogSTI())
1.150 + iLogFile.SetValid(EFalse);
1.151 + }
1.152 +
1.153 +EXPORT_C void RFileLogger::CloseLog()
1.154 +/** Closes the log file, iLogFile.
1.155 +
1.156 +This function closes the log file previously created or opened by CreateLog(). */
1.157 + {
1.158 + TPckg<TLogFile> logFilePckg(iLogFile);
1.159 + iLastError=SendReceive(ECloseLog,TIpcArgs( &logFilePckg));
1.160 + }
1.161 +
1.162 +EXPORT_C TBool RFileLogger::LogValid() const
1.163 +/** Returns the status of the log.
1.164 +
1.165 +@return ETrue if the log file is valid; otherwise EFalse. */
1.166 + {
1.167 +
1.168 + return iLogFile.Valid();
1.169 + }
1.170 +
1.171 +EXPORT_C TInt RFileLogger::LastError() const
1.172 +/** Returns the last error status that has been set.
1.173 +
1.174 +@return This value can be set to any of the valid error codes from any of
1.175 +the functions in this class. */
1.176 + {
1.177 +
1.178 + return iLastError;
1.179 + }
1.180 +
1.181 +EXPORT_C TBool RFileLogger::LogSTI() const
1.182 +/** Returns patchable constant value.
1.183 +
1.184 +@return This value specify the logging output. The default value EFalse
1.185 +cause that the logs are stored to filesystem. ETrue intorduced as in CR 1688
1.186 +is used for logging using RDebug::Print */
1.187 + {
1.188 +
1.189 + return iLogSTI;
1.190 + }
1.191 +
1.192 +//
1.193 +// 16-bit non-static writes
1.194 +//
1.195 +
1.196 +EXPORT_C void RFileLogger::Write(const TDesC16& aText)
1.197 +/** Writes a string of Unicode characters to an open log, iLogFile, if it is a valid file.
1.198 +
1.199 +Note that the text will be converted to an 8 bit format for the log file.
1.200 +
1.201 +@param aText The Unicode string to write to the open log. */
1.202 + {
1.203 +
1.204 + if (iLogFile.Valid() || LogSTI())
1.205 + {
1.206 + TBuf8<KLogBufferSize> buf;
1.207 + iLastError=iFormatter.FormatTextToWritableBuffer(buf,aText);
1.208 + if (iLastError==KErrNone)
1.209 + DoWrite(buf);
1.210 + }
1.211 + }
1.212 +
1.213 +EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC16> aFmt,...)
1.214 +/** Formats the remaining arguments of the function according to aFmt, and writes the
1.215 +result to the log, iLogFile, if it is a valid file.
1.216 +
1.217 +The format string aFmt contains literal text, embedded with directives, for converting
1.218 +the trailing list of arguments into text. The number and type of arguments is dictated
1.219 +by the structure of the directives in aFmt.
1.220 +
1.221 +Note that the text will be converted to an 8 bit format for the log file.
1.222 +
1.223 +@param aFmt The 16-bit non modifiable descriptor containing the format string.
1.224 +The TRefByValue class provides a constructor which takes a TDesC16 type. */
1.225 + {
1.226 +
1.227 + VA_LIST list;
1.228 + VA_START(list,aFmt);
1.229 + DoWriteFormat(aFmt,list);
1.230 + }
1.231 +
1.232 +EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
1.233 +/** Formats the arguments pointed to by aList according to aFmt, and writes the
1.234 +result to the log, iLogFile, if it is a valid file.
1.235 +
1.236 +The format string aFmt contains literal text, embedded with directives,
1.237 +for converting the trailing list of arguments into text. The number and type
1.238 +of arguments pointed to by aList is dictated by the structure of the directives
1.239 +in aFmt.
1.240 +
1.241 +Note that the text will be converted to an 8 bit format for the log file.
1.242 +
1.243 +@param aFmt The 16-bit non modifiable descriptor containing the format string.
1.244 +The TRefByValue class provides a constructor which takes a TDesC16 type.
1.245 +@param aList A pointer to an argument list. */
1.246 + {
1.247 +
1.248 + DoWriteFormat(aFmt,aList);
1.249 + }
1.250 +
1.251 +//
1.252 +// 8-bit non-static writes
1.253 +//
1.254 +
1.255 +EXPORT_C void RFileLogger::Write(const TDesC8& aText)
1.256 +/** Writes a string of 8-bit characters to an open log, iLogFile, if it is a valid file.
1.257 +
1.258 +@param aText The 8-bit character string to write to the open log. */
1.259 + {
1.260 +
1.261 + if (iLogFile.Valid() || LogSTI())
1.262 + {
1.263 + TBuf8<KLogBufferSize> buf;
1.264 + iLastError=iFormatter.FormatTextToWritableBuffer(buf,aText);
1.265 + if (iLastError==KErrNone)
1.266 + DoWrite(buf);
1.267 + }
1.268 + }
1.269 +
1.270 +EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC8> aFmt,...)
1.271 +/** Formats the remaining arguments of the function according to aFmt, and writes the
1.272 +result to the log, iLogFile, if it is a valid file.
1.273 +
1.274 +The format string aFmt contains literal text, embedded with directives,
1.275 +for converting the trailing list of arguments into text. The number and type
1.276 +of arguments is dictated by the structure of the directives in aFmt.
1.277 +
1.278 +@param aFmt The 8 bit non modifiable descriptor containing the format string.
1.279 +The TRefByValue class provides a constructor which takes a TDesC8 type.*/
1.280 + {
1.281 +
1.282 + VA_LIST list;
1.283 + VA_START(list,aFmt);
1.284 + DoWriteFormat(aFmt,list);
1.285 + }
1.286 +
1.287 +EXPORT_C void RFileLogger::WriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.288 +/** Formats the arguments pointed to by aList according to aFmt, and writes the result
1.289 +to the log, iLogFile, if it is a valid file.
1.290 +
1.291 +The format string aFmt contains literal text, embedded with directives,
1.292 +for converting the trailing list of arguments into text. The number and type
1.293 +of arguments pointed to by aList is dictated by the structure of the directives
1.294 +in aFmt.
1.295 +
1.296 +@param aFmt The 8 bit non modifiable descriptor containing the format string.
1.297 +The TRefByValue class provides a constructor which takes a TDesC8 type.
1.298 +@param aList A pointer to an argument list. */
1.299 + {
1.300 +
1.301 + DoWriteFormat(aFmt,aList);
1.302 + }
1.303 +
1.304 +//
1.305 +// 16-bit static writes
1.306 +//
1.307 +
1.308 +EXPORT_C void RFileLogger::Write(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TDesC16& aText)
1.309 +/** Writes a string of 16-bit characters to an open log.
1.310 +
1.311 +Connects to the logging server, creates/opens the log file and write aText to it.
1.312 +
1.313 +This is a "static write".
1.314 +
1.315 +Note that the text will be converted to an 8 bit format for the log file.
1.316 +
1.317 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.318 +@param aName The name of the log file.
1.319 +@param aMode The mode in which the log file should be opened: either EAppend or EOverwrite.
1.320 +@param aText The Unicode string to write to the log. */
1.321 + {
1.322 +
1.323 + RFileLogger logger;
1.324 + TInt ret=logger.Connect();
1.325 + if (ret==KErrNone)
1.326 + {
1.327 + logger.SetDateAndTime(ETrue,ETrue);
1.328 + logger.iLogFile.Set(aDir,aName,aMode);
1.329 + TBuf8<KLogBufferSize> buf;
1.330 + ret=logger.iFormatter.FormatTextToWritableBuffer(buf,aText);
1.331 + if (ret==KErrNone)
1.332 + {
1.333 + if (logger.LogSTI())
1.334 + {
1.335 + logger.DoStaticWrite(buf);
1.336 + } else
1.337 + {
1.338 + TPckg<TLogFile> logFilePckg(logger.iLogFile);
1.339 + logger.SendReceive(ECreateWriteAndCloseLog, TIpcArgs ( &logFilePckg, &buf)); // ignore error
1.340 + }
1.341 + }
1.342 + }
1.343 + logger.Close();
1.344 + }
1.345 +
1.346 +EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC16> aFmt,...)
1.347 +/** Formats the remaining arguments of the function according to aFmt and writes the
1.348 +result to the log.
1.349 +
1.350 +The format string aFmt contains literal text, embedded with directives,
1.351 +for converting the trailing list of arguments into text. The number and type
1.352 +of arguments is dictated by the structure of the directives in aFmt.
1.353 +
1.354 +Connects to the logging server, creates/opens the log file and writes the text arguments to it.
1.355 +
1.356 +This is a "static write".
1.357 +
1.358 +Note that the text will be converted to an 8 bit format for the log file.
1.359 +
1.360 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.361 +@param aName The name of the log file.
1.362 +@param aMode The mode in which the log file should be opened: either EAppend
1.363 +or EOverwrite.
1.364 +@param aFmt The 16 bit non modifiable descriptor containing the format string.
1.365 +The TRefByValue class provides a constructor which takes a TDesC16 type. */
1.366 + {
1.367 +
1.368 + VA_LIST list;
1.369 + VA_START(list,aFmt);
1.370 + DoStaticWriteFormat(aDir,aName,aMode,aFmt,list);
1.371 + }
1.372 +
1.373 +EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
1.374 +/** Formats the arguments pointed to by aList according to aFmt, and writes the result
1.375 +to the log.
1.376 +
1.377 +The format string aFmt contains literal text, embedded with directives,
1.378 +for converting the trailing list of arguments into text. The number and type
1.379 +of arguments pointed to by aList is dictated by the structure of the directives
1.380 +in aFmt.
1.381 +
1.382 +Connects to the logging server, creates/opens the log file and writes the text arguments to it.
1.383 +
1.384 +This is a "static write".
1.385 +
1.386 +Note that the text will be converted to an 8 bit format for the log file.
1.387 +
1.388 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.389 +@param aName The name of the log file.
1.390 +@param aMode The mode in which the log file should be opened: either EAppend
1.391 +or EOverwrite.
1.392 +@param aFmt The 16 bit non modifiable descriptor containing the format string.
1.393 +The TRefByValue class provides a constructor which takes a TDesC16 type.
1.394 +@param aList A pointer to an argument list. */
1.395 + {
1.396 +
1.397 + DoStaticWriteFormat(aDir,aName,aMode,aFmt,aList);
1.398 + }
1.399 +
1.400 +//
1.401 +// 8-bit static writes
1.402 +//
1.403 +
1.404 +EXPORT_C void RFileLogger::Write(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TDesC8& aText)
1.405 +/** Writes a string of 8-bit characters to an open log.
1.406 +
1.407 +Connects to the logging server, creates/opens the log file and writes aText to it.
1.408 +
1.409 +This is a "static write".
1.410 +
1.411 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.412 +@param aName The name of the log file.
1.413 +@param aMode The mode in which the log file should be opened: either EAppend
1.414 +or EOverwrite.
1.415 +@param aText The 8-bit string to write to the log. */
1.416 +
1.417 + {
1.418 +
1.419 + RFileLogger logger;
1.420 + TInt ret=logger.Connect();
1.421 + if (ret==KErrNone)
1.422 + {
1.423 + logger.SetDateAndTime(ETrue,ETrue);
1.424 + logger.iLogFile.Set(aDir,aName,aMode);
1.425 +// TBuf8<KLogBufferSize> buf;
1.426 + TBuf8<1600> buf; //Want at least an mtu sized buffer
1.427 + //PG 14/08/2002 - If mode is set to *Raw, Don't change format of client buffer
1.428 + if(aMode == EFileLoggingModeAppendRaw || aMode == EFileLoggingModeOverwriteRaw)
1.429 + {
1.430 + TPtrC8 ptr8;
1.431 + if (logger.LogSTI())
1.432 + {
1.433 + ptr8.Set(aText.Left(KLogBufferSize)); //in LogSTI mode, truncate to KLogBufferSize
1.434 + }
1.435 + else
1.436 + {
1.437 + ptr8.Set(aText.Left(1600)); // truncate to 1600
1.438 + }
1.439 + buf.Copy(ptr8);
1.440 + }
1.441 + else
1.442 + {
1.443 + ret=logger.iFormatter.FormatTextToWritableBuffer(buf,aText);
1.444 + }
1.445 + if (ret==KErrNone)
1.446 + {
1.447 + logger.DoStaticWrite(buf);
1.448 + }
1.449 + }
1.450 + logger.Close();
1.451 + }
1.452 +
1.453 +EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt,...)
1.454 +/** Formats the remaining arguments of the function according to aFmt and writes the
1.455 +result to the log.
1.456 +
1.457 +The format string aFmt contains literal text, embedded with directives,
1.458 +for converting the trailing list of arguments into text. The number and type
1.459 +of arguments is dictated by the structure of the directives in aFmt.
1.460 +
1.461 +Connects to the logging server, creates/opens the log file and writes the text arguments to it.
1.462 +
1.463 +This is a "static write".
1.464 +
1.465 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.466 +@param aName The name of the log file.
1.467 +@param aMode The mode in which the log file should be opened: either EAppend
1.468 +or EOverwrite.
1.469 +@param aFmt The 8 bit non modifiable descriptor containing the format string.
1.470 +The TRefByValue class provides a constructor which takes a TDesC8 type. */
1.471 + {
1.472 +
1.473 + VA_LIST list;
1.474 + VA_START(list,aFmt);
1.475 + DoStaticWriteFormat(aDir,aName,aMode,aFmt,list);
1.476 + }
1.477 +
1.478 +EXPORT_C void RFileLogger::WriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.479 +/** Formats the arguments pointed to by aList according to aFmt, and writes the
1.480 +result to the log.
1.481 +
1.482 +The format string aFmt contains literal text, embedded with directives,
1.483 +for converting the trailing list of arguments into text. The number and type
1.484 +of arguments pointed to by aList is dictated by the structure of the directives
1.485 +in aFmt.
1.486 +
1.487 +Connects to the logging server, creates/opens the log file and writes the text arguments to it.
1.488 +
1.489 +This is a "static write".
1.490 +
1.491 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.492 +@param aName The name of the log file.
1.493 +@param aMode The mode in which the log file should be opened: either EAppend
1.494 +or EOverwrite.
1.495 +@param aFmt The 8 bit non modifiable descriptor containing the format string.
1.496 +The TRefByValue class provides a constructor which takes a TDesC8 type.
1.497 +@param aList A pointer to an argument list. */
1.498 + {
1.499 +
1.500 + DoStaticWriteFormat(aDir,aName,aMode,aFmt,aList);
1.501 + }
1.502 +
1.503 +//
1.504 +// Hex Dumps
1.505 +//
1.506 +
1.507 +EXPORT_C void RFileLogger::HexDump(const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen)
1.508 +/** Writes a hex dump of the specified data to the log.
1.509 +
1.510 +The format of the hex dump entry is a header followed by the hex string of
1.511 +the data followed by any printable characters (non printable characters are
1.512 +substituted with '.'). For example,
1.513 +
1.514 +@code
1.515 +RLog_Ex0000 : 41 42 6C 6B 0A 0A 45 46 20 20 78 7A ABlk..EF
1.516 + xz
1.517 +RL_cont0001 : 43 44 6C 6B 0A 0A 45 46 20 20 78 7A CDlk..EF
1.518 + xz
1.519 +RL_cont0002 : 45 46 6C 6B 0A 0A 47 48 20 20 78 7A EFlk..GH
1.520 + xz
1.521 +.
1.522 +.
1.523 +.
1.524 +@endcode
1.525 +@param aHeader A label for the hex dump entry. The label has a sequence number
1.526 +appended to it automatically.
1.527 +@param aMargin A continuation label if the hex dump exceeds one line. This
1.528 +label is displayed on all subsequent lines after line one and also has a sequence
1.529 +number appended to it.
1.530 +@param aPtr The data that is to be converted to a hex string.
1.531 +@param aLen How many of the characters in aPtr are to be converted. Conversion
1.532 +always starts from position 0 within aPtr. */
1.533 + {
1.534 +
1.535 + if (iLogFile.Valid() || LogSTI())
1.536 + DoHexDump(aHeader,aMargin,aPtr,aLen);
1.537 + }
1.538 +
1.539 +EXPORT_C void RFileLogger::HexDump(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen)
1.540 +/** Writes a hex dump of the specified data to the log.
1.541 +
1.542 +The format of the hex dump entry is a header followed by the hex string of
1.543 +the data followed by any printable characters (non printable characters are
1.544 +substituted with '.'). For example,
1.545 +
1.546 +@code
1.547 +RLog_Ex0000 : 41 42 6C 6B 0A 0A 45 46 20 20 78 7A ABlk..EF
1.548 + xz
1.549 +RL_cont0001 : 43 44 6C 6B 0A 0A 45 46 20 20 78 7A CDlk..EF
1.550 + xz
1.551 +RL_cont0002 : 45 46 6C 6B 0A 0A 47 48 20 20 78 7A EFlk..GH
1.552 + xz
1.553 +.
1.554 +.
1.555 +.
1.556 +@endcode
1.557 +
1.558 +
1.559 +@param aDir The directory relative to 'C:\\Logs\\' where the log file resides.
1.560 +@param aName The name of the log file.
1.561 +@param aMode The mode in which the log file should be opened: either EAppend
1.562 +or EOverwrite.
1.563 +@param aHeader A label for the hex dump entry. The label has a sequence number
1.564 +appended to it automatically.
1.565 +@param aMargin A continuation label if the hex dump exceeds one line. This
1.566 +label is displayed on all subsequent lines after line one and also has a sequence
1.567 +number appended to it.
1.568 +@param aPtr The data that is to be converted to a hex string.
1.569 +@param aLen How many of the characters in aPtr are to be converted. Conversion
1.570 +always starts from position 0 within aPtr. */
1.571 + {
1.572 +
1.573 + RFileLogger logger;
1.574 + TInt ret=logger.Connect();
1.575 + if (ret==KErrNone)
1.576 + {
1.577 + logger.CreateLog(aDir,aName,aMode);
1.578 + if (logger.iLogFile.Valid())
1.579 + {
1.580 + logger.SetDateAndTime(ETrue,ETrue);
1.581 + logger.DoHexDump(aHeader,aMargin,aPtr,aLen);
1.582 + }
1.583 + logger.CloseLog();
1.584 + }
1.585 + logger.Close();
1.586 + }
1.587 +
1.588 +//
1.589 +// Private functions
1.590 +//
1.591 +
1.592 +TInt RFileLogger::DoConnect()
1.593 +/**
1.594 +Connects to the flogger server - default number of message slots = 8
1.595 +
1.596 +@return TInt indicating success code (KErrNone) or an error code.
1.597 +*/
1.598 + {
1.599 +
1.600 + return CreateSession(KFLoggerServerName,Version(),0); // no async IPC
1.601 + }
1.602 +
1.603 +void RFileLogger::DoWrite(const TDesC8& aBuf)
1.604 +/**
1.605 +Sends the pre-formatted write string to the flogger server.
1.606 +
1.607 +@pre
1.608 +session is already open.
1.609 +@param aBuf 8-bit text to be written.
1.610 +*/
1.611 + {
1.612 + if (LogSTI())
1.613 + {
1.614 + TBuf<KLogBufferSize> n;
1.615 + n.Copy(aBuf);
1.616 + TFileName logFileName = iLogFile.Name();
1.617 + RDebug::Print(_L("FLG %S %S"),&logFileName,&n);
1.618 + }
1.619 + else
1.620 + {
1.621 + TPckg<TLogFile> logFilePckg(iLogFile);
1.622 + iLastError=SendReceive(EWriteLog,TIpcArgs (&logFilePckg, &aBuf));
1.623 + }
1.624 + }
1.625 +
1.626 +void RFileLogger::DoStaticWrite(const TDesC8& aBuf)
1.627 +/**
1.628 +Sends the pre-formatted write string to the flogger server.
1.629 +
1.630 +@pre
1.631 +session is already open. aText is not longer than KLogBufferSize
1.632 +@param aBuf text to write
1.633 +@post
1.634 +The text is only written if the original connection was successful.
1.635 +No text is ever written if the directory specified in the original connection request does not exist.
1.636 +Each line is preceded in the date and time.
1.637 +*/
1.638 + {
1.639 + TPckg<TLogFile> logFilePckg(iLogFile);
1.640 + if (LogSTI())
1.641 + {
1.642 + TBuf<KLogBufferSize> n;
1.643 + n.Copy(aBuf);
1.644 + TFileName logFileName = iLogFile.Name();
1.645 + RDebug::Print(_L("FLG %S %S"),&logFileName,&n);
1.646 + }
1.647 + else
1.648 + {
1.649 + SendReceive(ECreateWriteAndCloseLog, TIpcArgs( &logFilePckg, &aBuf)); // ignore error
1.650 + }
1.651 + }
1.652 +
1.653 +void RFileLogger::DoWriteFormat(TRefByValue<const TDesC16> aFmt, VA_LIST& aList)
1.654 +/**
1.655 +Trim format string before sending to the flogger server.
1.656 +
1.657 +@pre
1.658 +session is already open.
1.659 +@param aFmt c-style formatted text to be written.
1.660 +@param aList any variables required by the format.
1.661 +@post
1.662 +The final string is truncated to KLogBufferSize.
1.663 +@see void RFileLogger::DoWriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.664 +*/
1.665 + {
1.666 +
1.667 + if (iLogFile.Valid() || LogSTI())
1.668 + {
1.669 + TBuf8<KLogBufferSize> buf;
1.670 + iLastError=iFormatter.ConvertToWritableBuffer(buf,aFmt,aList);
1.671 + if (iLastError==KErrNone)
1.672 + DoWrite(buf);
1.673 + }
1.674 + }
1.675 +
1.676 +void RFileLogger::DoWriteFormat(TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.677 +/**
1.678 +Trim format string before sending to the flogger server.
1.679 +
1.680 +@pre
1.681 +session is already open.
1.682 +@param aFmt c-style formatted text to be written.
1.683 +@param aList any variables required by the format.
1.684 +@post
1.685 +The final string is truncated to KLogBufferSize.
1.686 +*/
1.687 + {
1.688 +
1.689 + if (iLogFile.Valid() || LogSTI())
1.690 + {
1.691 + TBuf8<KLogBufferSize> buf;
1.692 + iLastError=iFormatter.ConvertToWritableBuffer(buf,aFmt,aList);
1.693 + if (iLastError==KErrNone)
1.694 + DoWrite(buf);
1.695 + }
1.696 + }
1.697 +
1.698 +void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC> aFmt, VA_LIST& aList)
1.699 +/**
1.700 +Sends a format write string to the flogger server to write to the specified file.
1.701 +
1.702 +@param aDir Path of the log file.
1.703 +@param aName Name of the log file.
1.704 +@param aFmt c-style formatted text to be written.
1.705 +@param aList any variables required by the format.
1.706 +@post
1.707 +The text is only written if the original connection was successful.
1.708 +No text is ever written if the directory specified in the original connection request does not exist.
1.709 +Each line is preceded in the date and time.
1.710 +@see void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.711 +*/
1.712 + {
1.713 +
1.714 + RFileLogger logger;
1.715 + TInt ret=logger.Connect();
1.716 + if (ret==KErrNone)
1.717 + {
1.718 + logger.SetDateAndTime(ETrue,ETrue);
1.719 + logger.iLogFile.Set(aDir,aName,aMode);
1.720 + TBuf8<KLogBufferSize> buf;
1.721 + ret=logger.iFormatter.ConvertToWritableBuffer(buf,aFmt,aList);
1.722 + if (ret==KErrNone)
1.723 + logger.DoStaticWrite(buf);
1.724 + }
1.725 + logger.Close();
1.726 + }
1.727 +
1.728 +void RFileLogger::DoStaticWriteFormat(const TDesC& aDir, const TDesC& aName, TFileLoggingMode aMode, TRefByValue<const TDesC8> aFmt, VA_LIST& aList)
1.729 +/**
1.730 +Sends a format write string to the flogger server to write to the specified file.
1.731 +There is no pre-condition.
1.732 +
1.733 +@param aDir Path of the log file.
1.734 +@param aName Name of the log file.
1.735 +@param aFmt c-style formatted text to be written.
1.736 +@param aList any variables required by the format.
1.737 +@post
1.738 +The text is only written if the original connection was successful.
1.739 +No text is ever written if the directory specified in the original
1.740 +connection request does not exist. Each line is preceded in the date and time.
1.741 +*/
1.742 + {
1.743 +
1.744 + RFileLogger logger;
1.745 + TInt ret=logger.Connect();
1.746 + if (ret==KErrNone)
1.747 + {
1.748 + logger.SetDateAndTime(ETrue,ETrue);
1.749 + logger.iLogFile.Set(aDir,aName,aMode);
1.750 + TBuf8<KLogBufferSize> buf;
1.751 + ret=logger.iFormatter.ConvertToWritableBuffer(buf,aFmt,aList);
1.752 + if (ret==KErrNone)
1.753 + logger.DoStaticWrite(buf);
1.754 + }
1.755 + logger.Close();
1.756 + }
1.757 +
1.758 +void RFileLogger::DoHexDump(const TText* aHeader, const TText* aMargin, const TUint8* aPtr, TInt aLen)
1.759 +/**
1.760 +Static Write. Dumps arbitrary data to the log file as a standard hex dump.
1.761 +
1.762 +@pre
1.763 +session is already open.
1.764 +@param aHeader Specify a string to be printed before the first hex line.
1.765 +Leave as null or an empty string for no header.
1.766 +@param aMargin Specify a string to be printed before each subsequent line.
1.767 +Leave as null or an empty string for no Margin.
1.768 +@param aPtr pointer to the data being dumped.
1.769 +@param aLen the number of bytes to dump
1.770 +@post
1.771 +The text is only written if the original connection was successful.
1.772 +No text is ever written if the directory specified in the original connection
1.773 +request does not exist. Each line is preceded in the date and time.
1.774 +@note This function has poor performance since it performs a full connection and
1.775 +disconnection to the flogger server. Example of aHeader/aMargin.
1.776 +If "aHeader" is set to "Fn Output:" and "aMargin" is set to " ", then output
1.777 +would look like (for a print of the alphabet):
1.778 +14/11/2002 12:32:24 Fn Output:0000 : 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 abcdefghijklmnop
1.779 +14/11/2002 12:32:24 0010 : 71 72 73 74 75 76 77 78 79 7a qrstuvwxyz
1.780 +*/
1.781 + {
1.782 +
1.783 + if (aPtr==NULL) // nothing to do
1.784 + return;
1.785 +
1.786 + TBuf<KLogBufferSize> buf;
1.787 + TBuf8<KLogBufferSize> temp;
1.788 + TInt i=0;
1.789 + const TText* p=aHeader;
1.790 + while (aLen>0)
1.791 + {
1.792 + if (p==NULL)
1.793 + p=BLANK; // if NULL set p to a blank string
1.794 + TInt n=(aLen>KHexDumpWidth ? KHexDumpWidth : aLen);
1.795 + buf.AppendFormat(KFirstFormatString,p,i);
1.796 + TInt j;
1.797 + for (j=0; j<n; j++)
1.798 + buf.AppendFormat(KSecondFormatString,aPtr[i+j]);
1.799 + while (j++<KHexDumpWidth)
1.800 + buf.Append(KThreeSpaces);
1.801 + buf.Append(KTwoSpaces);
1.802 + for (j=0; j<n; j++)
1.803 + buf.AppendFormat(KThirdFormatString,(aPtr[i+j]<32 || aPtr[i+j]>126) ? KFullStopChar : aPtr[i+j]);
1.804 +
1.805 + iLastError=iFormatter.FormatTextToWritableBuffer(temp,buf);
1.806 + if (iLastError==KErrNone)
1.807 + DoWrite(temp);
1.808 +
1.809 + buf.SetLength(0);
1.810 + temp.SetLength(0);
1.811 + aLen-=n;
1.812 + i+=n;
1.813 + p=aMargin;
1.814 + }
1.815 + }
1.816 +