Symbian平台上高效调试软件是一个大问题,相对而言,使用文件系统写日志是比较靠得住的方法。但Symbian本身提供的日志类有多种不方便的限制,所以提供以下方便好用、且能直接编译通过的日志类给大家使用(基本没有任何多余的代码,精简高效),希望大家多多收藏使用。
备注:下列代码适合单线程使用,若想使用在多线程工程项目中,请自行替换RFs对象,自己管理该对象。这样就完全可以放心的在多线程中使用,让你快速有效的抓臭虫。(解决不了的可以私下找我。呵呵,写这么多还是因为我没有一点分数,赚点分,想发另一个更深层次的问题)
CTraceLog类头文件:
- /*
- ============================================================================
- Name : TraceLog.h
- Author : Alex
- Version : 1.0
- Copyright : Your copyright notice
- Description : CTraceLog declaration
- ============================================================================
- */
- #ifndef TRACELOG_H
- #define TRACELOG_H
- // INCLUDES
- #include <e32std.h>
- #include <e32base.h>
- #include <f32file.h>
- #include <S32FILE.H>
- #include <e32def.h>
- // CLASS DECLARATION
- class CTraceLog;
- extern CTraceLog* g_TraceLog;
- #define LOCAL_TRACE
- #ifdef LOCAL_TRACE
- #define TRACE g_TraceLog->OutPutlog
- #else
- #define TRACE
- #endif
- /**
- * CTraceLog
- *
- */
- class CTraceLog : public CBase
- {
- public:
- // Constructors and destructor
- /**
- * Destructor.
- */
- ~CTraceLog();
- /**
- * Two-phased constructor.
- */
- static void BeginDebugL(const TDesC& aLogFileName);
- static void StopDebugL();
- void OutPutlog(TRefByValue< const TDesC16 > aFmt,...);
- void OutPutlog(TRefByValue< const TDesC8 > aFmt,...);
- void OutPutlog(const char *aFmt,...);
- void OutPutlog(const char *aFmt, VA_LIST aParamList);
- private:
- /**
- * Two-phased constructor.
- */
- static CTraceLog* NewLC(const TDesC& aLogFileName);
- /**
- * Constructor for performing 1st stage construction
- */
- CTraceLog();
- /**
- * EPOC default constructor for performing 2nd stage construction
- */
- void ConstructL(const TDesC& aLogFileName);
- private:
- RFile iFile;
- RFileWriteStream iOutputStream;
- TTime iTraceTime;
- HBufC* iLogBuf16;
- HBufC8* iLogBuf8;
- };
- #endif // TRACELOG_H
- /*
- ============================================================================
- Name : TraceLog.cpp
- Author : Alex
- Version : 1.0
- Copyright : Your copyright notice
- Description : CTraceLog implementation
- ============================================================================
- */
- #include "TraceLog.h"
- #include <COEMAIN.H>
- _LIT(KTimeFormat,"[%Y%M%D%1-%2-%3 %H:%T:%S] ");
- _LIT(KLogFileFormat,"C://data//%S_log.txt");
- _LIT(K16LogLevel0," <Trace >: ");
- _LIT(K16LogLevel1," <Error>: ");
- _LIT(K16LogEOF,"/r/n");
- _LIT8(K8LogLevel0," <Info >: ");
- _LIT8(K8LogLevel1," <Error>: ");
- _LIT8(K8LogEOF,"/r/n");
- const TInt KMaxLogSize = 1024;
- CTraceLog* g_TraceLog = NULL;
- CTraceLog::CTraceLog()
- {
- // No implementation required
- }
- CTraceLog::~CTraceLog()
- {
- delete iLogBuf16;
- iLogBuf16 = NULL;
- delete iLogBuf8;
- iLogBuf8 = NULL;
- iOutputStream.Close();
- iFile.Close();
- }
- CTraceLog* CTraceLog::NewLC(const TDesC& aLogFileName)
- {
- CTraceLog* self = new (ELeave) CTraceLog();
- CleanupStack::PushL(self);
- self->ConstructL(aLogFileName);
- return self;
- }
- void CTraceLog::BeginDebugL(const TDesC& aLogFileName)
- {
- #ifdef LOCAL_TRACE
- g_TraceLog = CTraceLog::NewLC(aLogFileName);
- CleanupStack::Pop();
- #endif
- }
- void CTraceLog::StopDebugL()
- {
- if(g_TraceLog != NULL)
- {
- delete g_TraceLog;
- g_TraceLog = NULL;
- }
- }
- void CTraceLog::ConstructL(const TDesC& aLogFileName)
- {
- TInt ret;
- TFileName fileName;
- fileName.Format(KLogFileFormat,&aLogFileName);
- ret = CCoeEnv::Static()->FsSession().MkDirAll(fileName);
- if( (KErrNone!=ret) && (KErrAlreadyExists!=ret) )
- {
- ASSERT(0);
- }
- User::LeaveIfError(iFile.Replace(CCoeEnv::Static()->FsSession(),
- fileName,
- EFileWrite|EFileRead|EFileShareAny) );
- iOutputStream.Attach(iFile);
- iLogBuf16 = HBufC16::NewL(KMaxLogSize);
- iLogBuf8 = HBufC8::NewL(KMaxLogSize);
- }
- void CTraceLog::OutPutlog(TRefByValue< const TDesC16 > aFmt,...)
- {
- TPtr logBufPtr = iLogBuf16->Des();
- iTraceTime.HomeTime();
- iTraceTime.FormatL(logBufPtr,KTimeFormat);
- VA_LIST ap;
- VA_START(ap,aFmt);
- logBufPtr.AppendFormatList(aFmt,ap);
- VA_END(ap);
- logBufPtr.Append(K16LogEOF);
- iOutputStream.WriteL(logBufPtr);
- iOutputStream.CommitL();
- }
- void CTraceLog::OutPutlog(TRefByValue< const TDesC8 > aFmt,...)
- {
- TBuf<30> tmpTimeBuf;
- iTraceTime.HomeTime();
- iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
- TPtr logBufPtr = iLogBuf16->Des();
- TPtr8 logBuf8Ptr = iLogBuf8->Des();
- VA_LIST ap;
- VA_START(ap,aFmt);
- logBuf8Ptr.FormatList(aFmt,ap);
- VA_END(ap);
- logBuf8Ptr.Append(K8LogEOF);
- logBufPtr.Copy(logBuf8Ptr);
- logBufPtr.Insert(0,tmpTimeBuf);
- iOutputStream.WriteL(logBufPtr);
- iOutputStream.CommitL();
- }
- void CTraceLog::OutPutlog(const char *aFmt,...)
- {
- TBuf<30> tmpTimeBuf;
- iTraceTime.HomeTime();
- iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
- TPtr logBufPtr = iLogBuf16->Des();
- TPtr8 logBuf8Ptr = iLogBuf8->Des();
- TPtrC8 fmt((TUint8*)aFmt);
- VA_LIST ap;
- VA_START(ap,aFmt);
- logBuf8Ptr.FormatList(fmt,ap);
- VA_END(ap);
- logBuf8Ptr.Append(K8LogEOF);
- logBufPtr.Copy(logBuf8Ptr);
- logBufPtr.Insert(0,tmpTimeBuf);
- iOutputStream.WriteL(logBufPtr);
- iOutputStream.CommitL();
- }
- void CTraceLog::OutPutlog(const char *aFmt, VA_LIST aParamList)
- {
- TBuf<30> tmpTimeBuf;
- iTraceTime.HomeTime();
- iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
- TPtr logBufPtr = iLogBuf16->Des();
- TPtr8 logBuf8Ptr = iLogBuf8->Des();
- TPtrC8 fmt((TUint8*)aFmt);
- logBuf8Ptr.FormatList(fmt,aParamList);
- logBuf8Ptr.Append(K8LogEOF);
- logBufPtr.Copy(logBuf8Ptr);
- logBufPtr.Insert(0,tmpTimeBuf);
- iOutputStream.WriteL(logBufPtr);
- iOutputStream.CommitL();
- }