Simple-to-use Symbian C++ macros for writing debug log files
Article Metadata
Compatibility
Article
Description
The RFileLogger class provides both static and non-static methods for file logging. By writing a few simple macros, file logging can be simplified even further.
Solution
The following header file must be included in each source file that needs to write to the log file. The KLogsDir constant must be customized to identify the directory under c:\logs\ that will contain the log file.
The log file will be created only if the directory specified by KLogsDir exists under c:\logs\. This way, logging can be turned on or off without having to recompile by simply removing the directory.
#ifndef __FILELOGGER_H__
#define __FILELOGGER_H__
#include <e32std.h>
#include <flogger.h> // link against flogger.lib
_LIT( KLogsDir, "MYAPP");
_LIT( KLogFileName, "log.txt");
#define __LOGSTR_TOFILE(S) { _LIT(KTmpStr, S); RFileLogger::WriteFormat(KLogsDir(), KLogFileName(), EFileLoggingModeAppend, KTmpStr()); }
#define __LOGSTR_TOFILE1(S, P0) { _LIT(KTmpStr, S); RFileLogger::WriteFormat(KLogsDir(), KLogFileName(), EFileLoggingModeAppend, TRefByValue<const TDesC>(KTmpStr()),P0); }
#define __LOGSTR_TOFILE2(S, P0, P1) { _LIT(KTmpStr, S); RFileLogger::WriteFormat(KLogsDir(), KLogFileName(), EFileLoggingModeAppend, TRefByValue<const TDesC>(KTmpStr()),P0,P1); }
#endif /* __FILELOGGER_H__ */
Usage:
Note that these these macros cannot be used with variable argument lists. Instead, the macro to be used is selected based on the number of arguments in the logged text string.
// Log a single line of text without arguments
__LOGSTR_TOFILE("Init complete.")
// Log a single integer value
__LOGSTR_TOFILE1("Magic number: %d", intValue)
// Log a string (descriptor) + integer value
__LOGSTR_TOFILE2("Message: \"%S\", self-destructs in %d sec", &msgDes, timeleft)
Macros taking more arguments can be created as needed.


Improvement suggestion
ltomuta 16:17, 31 May 2009 (EEST)