Symbian中日志文件的使用

来源:互联网 发布:考研数学 概率 知乎 编辑:程序博客网 时间:2024/04/29 22:07

我包装了一下RFileLogger ,个人感觉好用多了,分享一下省得大家为了日志文件发愁。

.h

#ifndef __RLOG_H__
#define __RLOG_H__
#include <e32base.h>
class RLog : public CBase
{
public:
 static void Log(const TDesC &aErrTypeName, const TDesC &aErrMsg);
static  void Log(const TDesC & aErrMsg,const TInt aErrCode);
static void Log(const TDesC& aMsg);
private:
/*!
  @function NewL
  
  @discussion Create a RLog object
  @result a pointer to the created instance of RLog
  */
    static RLog* NewL();
/*!
  @function NewLC
  
  @discussion Create a RLog object
  @result a pointer to the created instance of RLog
  */
    static RLog* NewLC();
/*!
  @function ~RLog
 
  @discussion Destroy the object and release all memory objects
  */
    ~RLog();
private:
/*!
  @function RLog
  @discussion Constructs this object
  */
    RLog();
/*!
  @function ConstructL
  @discussion Performs second phase construction of this object
  */
    void ConstructL();
private:
    // Member variables
};
#endif // __RLOG_H__

 

.cpp

#include "RLog.h"
#include "flogger.h"
RLog* RLog::NewL()
    {
    RLog* self = NewLC();
    CleanupStack::Pop(self);
    return self;
    }
   
RLog* RLog::NewLC()
    {
    RLog* self = new (ELeave) RLog();
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

RLog::RLog()
    {
    }

RLog::~RLog()
    {
    }

void RLog::ConstructL()
    {
    }
 
void RLog::Log(const TDesC &aMsg)
{
 RFileLogger iLog;
 iLog.Connect();
 iLog.CreateLog(_L("mylogDir"),_L("MyLogFile.txt"),EFileLoggingModeAppend);
 
 iLog.Write(aMsg);
 
 // Close the log file and the connection to the server.
 iLog.CloseLog();
 iLog.Close();
}
void RLog::Log(const TDesC &aErrMsg, const TInt aErrCode)
{
 HBufC* buf=HBufC::New(aErrMsg.Length()+20);
 buf->Des().Append(aErrMsg);
 buf->Des().Append(_L(":"));
 buf->Des().AppendNum(aErrCode);
 Log(*buf);
 delete buf;
}
void RLog::Log(const TDesC &aErrTypeName, const TDesC &aErrMsg)
{
 HBufC* buf=HBufC::New(aErrMsg.Length()+2+aErrMsg.Length());
 buf->Des().Append(aErrTypeName);
 buf->Des().Append(_L(":"));
 buf->Des().Append(aErrMsg);
 Log(*buf);
 delete buf;
}

mpp里面添加

LIBRARY flogger.lib//loggerfile

example

#include "RLog.h"

 HBufC * buf=HBufC::New(50);
 buf->Des().Append(_L("MoChEv:"));
 buf->Des().Append(_L("P="));
 buf->Des().AppendNum(aPreviousState);
 buf->Des().Append(_L("C="));
 buf->Des().AppendNum(aCurrentState);
 buf->Des().Append(_L("E="));
 buf->Des().AppendNum(aErrorCode);
 RLog::Log(*buf);//可以直接写消息
delete buf;

 TRAPD(errc,this->CleanAmrFileL(aBarFileName));
 if(errc)
 {
  RLog::Log(_L("CleanAmrFileL:"),errc);//消息+错误码
  User::Leave(errc);
 }
 else{
  RLog::Log(_L("CleanAmrFileL:OK"));
 }


TDesC* aFullFileName=GetFullFileName(aBarFileName);

  RLog::Log(_L("fullfile"),*aFullFileName);//消息+描述符

 

 

 

注意要使用RFileLogger 必须先创建文件夹,因为这个类无法创建新文件夹。

模拟器上使用log

在目录/epoc32/wins/c/logs下创建目录mylogDir,并在下面创建文件MyLogFile.txt

在手机上使用log

1在pkg文件的同级目录创建文件MyLogFile.txt

2需要在pkg文件中加入".MyLogFile.txt"-"c:/logs/mylogDir/MyLogFile.txt"-目的是创建一个文件夹.

你可以用fileman等工具打开查看这个文件。或者将它传到pc上分析。

原创粉丝点击