Logging on the device
Platform: Series 60 (Symbian)
Tested on: Siemens SX1, Nokia 3650
Should also work on: All Series60 devices.
(If you run this game on one of these phones please send me an email so I can mark this phone as supported.)
This is a really very simple example demonstrating the basic logging capability utilizing flogger.lib library. Our project needs to be linked against this library, so lets add this line to .mmp file.
For logging we will use RFileLogger class with some help from a method with variable number of parameters using TRefByValue class. RFileLogger provides both static and non-static versions of access functions to the file logging system. We will use the static one.
void MyLogger::Print(TRefByValue<const TDesC> aFmt, ...)
Clearly in KLoggerLogdir and KLoggerLogfile we have names of the directory and file where we want to log to. The most interesting stuff in the example above is the approach used for being able to get unknown number of parameters to our method.
VA_LIST is typedeffed like this
typedef TInt8* VA_LIST;
and it defines a 'C' style array of pointers to TInt8 types
The first parameter in a function that takes a variable number of parameters must always take the first parameter as a value reference. Therefore our TRefByValue trick.
Usage look like this:
Print(_L("message without params"));
bufLog.Format(_L("one param [%d]"),20);
bufLog.Format(_L("ten params: %b, %c, %d, %o, %u, %x, %d, %d, %d, %d"),65,65,65,65,65,65,7,8,9,10);
And here are the results in the logfile:
30/03/2004 9:47:23 message without params
30/03/2004 9:47:23 one param 
30/03/2004 9:47:23 ten params: 1000001, A, 65, 101, 65, 41, 7, 8, 9, 10
To make it more easy to use lets define set of macros that will be used by our application
#define LOG1(msg, p1)MyLogger::Print((msg), (p1));
#define LOG2(msg, p1, p2)MyLogger::Print((msg), (p1), (p2));
#define LOG3(msg, p1, p2, p3)MyLogger::Print((msg), (p1), (p2), (p3));
#define LOG1(msg, p1)
#define LOG2(msg, p1, p2)
#define LOG3(msg, p1, p2, p3)
Using these macros we can easily switch on/off logging in our application. If __I_WANT_LOGS__ is defined the static logging methods will be called when using LOGX() macros. If it is not defined, dummy macros will be called instead of it.
I'm well aware of some lack of this approach but it will do for small and simple projects. If you were just wondering how to log some information from your application when running on the real device this can be an easy and convenient way to do it.