Subject: [基础] 创建Log文件调试Symbian程序
Lee
Administrator
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 0
Credits 18567
Posts 133
Money 189
Reading Access 200
Registered 13-3-2007
Status Offline
Post at 15-3-2007 20:33  Profile | Blog | P.M. 
创建Log文件调试Symbian程序

需要在手机上调试代码或跟踪执行过程?RFileLogger也许可以帮助你.这个class的功能非常强大而且很容易使用._

首先声明一个文件日志的连接并且创建一个log文件

//打开日志文件服务的连接
RFileLogger iLog;
iLog.Connect();
iLog.CreateLog(_L("MyLoggingDirectory"),_L("MyLogFile"),EFileLoggingModeOverwrite);

...
//关闭log文件和服务器连接
iLog.CloseLog();#,lFb9
iLog.Close();

第3行的CreateLog函数有三个参数:%

Log路径

Log文件名

记录模式(EFileLoggingModeOverwrite|EFileLoggingModeAppend)

例如下列代码,是我的log文件的full path(手机上的文件系统)

C:/Logs/MyLoggingDirectory/MyLogFile.

对我来说,我习惯把opening代码放到我希望跟踪的class的ConstructL()函数里面,然后在析构函数里closing.在真正编码时,你最好检测一下Connect和CreateLog是否返回了KErrNone

一旦你完成了这些,你就可以向log文件中输入文本和数据了。基本的命令为:

命令Log文件

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

如果不想记录日期和时间,你可以使用这条语句来关闭它们。

iLog.SetDateAndTime(TBool aUseDate, TBool aUseTime)

别忘了在MMP文件上加入flogger.lib,还有包含flogger.h头文件。现在可以编译执行你的程序了。

No!log文件还没建立呢!正确!这没办法在代码里完成,我们只能手工在C:/Logs/MyLoggingDirectory目录中创建log文件,否则什么日志也不会记录。不需要重新编译。

注意:如果你不知道怎么做,你可以使用 FExplorer 将文件通过PC传到手机上。




有其他问题请加入Symbian开发群参与讨论:群 ①:623041已满,群②:36865776已满 请加群③:76404484
Top
Lee
Administrator
Rank: 9Rank: 9Rank: 9



UID 2
Digest Posts 0
Credits 18567
Posts 133
Money 189
Reading Access 200
Registered 13-3-2007
Status Offline
Post at 15-3-2007 20:35  Profile | Blog | P.M. 
Creating log files

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:

// Open a connection to the File logger server
RFileLogger iLog;
iLog.Connect();
iLog.CreateLog(_L("MyLoggingDirectory"),_L("MyLogFile"),EFileLoggingModeOverwrite);

...

// Close the log file and the connection to the server.
iLog.CloseLog();
iLog.Close();

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).

If you don’y know how to do this, just check any file explorer application like FExplorer. You will need this kind of app to read or send the log file back to your PC


原文地址




有其他问题请加入Symbian开发群参与讨论:群 ①:623041已满,群②:36865776已满 请加群③:76404484
Top
 


All times are GMT+8, the time now is 5-9-2010 14:33

CopyRight © Symbianx.cn 2007 Powered By Discuz! 5
Clear Cookies - Contact Us - Symbian OS系统[S60,UIQ]开发中文翻译论坛 - Archiver

本站原文版权归原文作者所有,本站译文版权归本站所有,如需转载请注明原文和译文出处,否则追究法律责任