方便、好用、强大的Symbian日志类,请务必收藏~
来源:互联网 发布:caffe 中文教程 编辑:程序博客网 时间:2024/04/28 04:52
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();
- }
- 方便、好用、强大的Symbian日志类,请务必收藏~
- 收藏一些比较好用的C++学习网站,方便日常使用。比较给力
- 方便好用的selector
- 方便好用的 Beautifulsoup
- 请务必尊重知识产权
- 北科大强大方便的校园网
- Java虚拟机安全性-class文件检验器,看完请务必收藏!
- 开发部发送patch的相关规范,请务必遵守
- ■务必收藏!2008年最有才的股市十大“短信
- 如果你在用socket编程,请务必看看这篇文章
- 方便好用的外键约束
- iOS简单、方便、好用的HZSigmentView
- 好用的控件收藏
- Remoting的一些文章索引,方便阅读 (狂好的,值得收藏的好文啊)
- Symbian好的博客
- 博客站长务必要养成的好习惯
- 一个好用强大的工具regini
- 一个好用的日志工具类
- 联系方式
- 国际网络收款工具Paypal注册图文教程
- 我的第一个JAVA计算器
- 超实用“伪同步”设计代码,用处多多
- 字符串子串无重复字符最长长度的问题
- 方便、好用、强大的Symbian日志类,请务必收藏~
- 喜欢你,即使是暗恋
- 水晶报表中如何动态增加字段
- eclipse 快捷键总结
- Prototype&Production
- Nutch1.2增加插件例子
- 总结:如何查询SQL Server连接数
- 笑话一则 老板今天我想请一天假
- 关于unix shell get、sed、awk用法