分类:
2007-03-05 10:02:06
Need to debug some code on a mobile or track the execution flow ? The RFileLogger may help you. As a matter of fact this class is really powerful and easy to use.
The first step is to declare a connection to the file logger server and create the log file:
The CreateLog function takes three parameter:
the base name of the log directory (relative to C:\Logs and without trailing ’/’)
the name of the log file
the logging mode (EFileLoggingModeOverwrite or EFileLoggingModeAppend)
In the exemple code above, the full path to my log file (on the phone file system) would be C:/Logs/MyLoggingDirectory/MyLogFile.
As far as I am concerned, I like to put the opening code in the ConstructL() method of the class I want to trace, and the closing code in the destructor. Also note that in real production code, you would better check that Connect() and CreateLog() does return KErrNone.
Once you have done this, you can output text and data to the log file. The basic primitives are:
Primitive | Output in the log file |
---|---|
iLog.Write(_L("Hello World")) | 11/07/2003 4:00:13 Hello World |
iLog.WriteFormat(_L("Result=%d"),err) | 11/07/2003 4:00:13 Result=0 |
iLog.HexDump(aHeader,aHeader,myPtr,4) | 11/07/2003 4:00:13 myBuf:0000: 41 42 00 44 AB.D |
If you don’t want to have date or time logged, you can turn it off by using:
iLog.SetDateAndTime(TBool aUseDate, TBool aUseTime).
Don’t forget to add the flogger.lib in your MMP file and to include the flogger.h file, and that’s it. You can compile, execute and get your log file.
No ? The log file was not created! You’re right. This is not (yet) an issue with your code. But another specificity of how Symbian handles log file: nothing will be logged unless you manually create the C:/Logs/MyLoggingDirectory by yourself. No need to recompile anything to enable/disable the log feature. Just create or not logging directory [1]. I like this (but it has a cost in term of code size).