方便、好用、强大的Symbian日志类,请务必收藏~

来源:互联网 发布:caffe 中文教程 编辑:程序博客网 时间:2024/04/28 04:52

Symbian平台上高效调试软件是一个大问题,相对而言,使用文件系统写日志是比较靠得住的方法。但Symbian本身提供的日志类有多种不方便的限制,所以提供以下方便好用、且能直接编译通过的日志类给大家使用(基本没有任何多余的代码,精简高效),希望大家多多收藏使用。

备注:下列代码适合单线程使用,若想使用在多线程工程项目中,请自行替换RFs对象,自己管理该对象。这样就完全可以放心的在多线程中使用,让你快速有效的抓臭虫。(解决不了的可以私下找我。呵呵,写这么多还是因为我没有一点分数,赚点分,想发另一个更深层次的问题)


CTraceLog类头文件:

  1. /*
  2. ============================================================================
  3. Name                : TraceLog.h
  4. Author          : Alex
  5. Version         : 1.0
  6. Copyright   : Your copyright notice
  7. Description : CTraceLog declaration
  8. ============================================================================
  9. */
  10. #ifndef TRACELOG_H
  11. #define TRACELOG_H
  12. // INCLUDES
  13. #include <e32std.h>
  14. #include <e32base.h>
  15. #include <f32file.h>
  16. #include <S32FILE.H>
  17. #include <e32def.h>
  18. // CLASS DECLARATION
  19. class CTraceLog;
  20. extern CTraceLog* g_TraceLog;
  21. #define LOCAL_TRACE
  22. #ifdef LOCAL_TRACE
  23. #define TRACE g_TraceLog->OutPutlog
  24. #else
  25. #define TRACE
  26. #endif
  27. /**
  28. *  CTraceLog
  29. *
  30. */
  31. class CTraceLog : public CBase
  32. {
  33. public:
  34.         // Constructors and destructor
  35.         /**
  36.          * Destructor.
  37.          */
  38.         ~CTraceLog();
  39.         /**
  40.          * Two-phased constructor.
  41.          */
  42.         static void BeginDebugL(const TDesC& aLogFileName);
  43.        
  44.         static void StopDebugL();
  45.        
  46.         void OutPutlog(TRefByValue< const TDesC16 > aFmt,...);
  47.         void OutPutlog(TRefByValue< const TDesC8 > aFmt,...);
  48.         void OutPutlog(const char *aFmt,...);
  49.         void OutPutlog(const char *aFmt, VA_LIST aParamList);
  50. private:
  51.         /**
  52.          * Two-phased constructor.
  53.          */
  54.         static CTraceLog* NewLC(const TDesC& aLogFileName);
  55.        
  56.         /**
  57.          * Constructor for performing 1st stage construction
  58.          */
  59.         CTraceLog();
  60.         /**
  61.          * EPOC default constructor for performing 2nd stage construction
  62.          */
  63.         void ConstructL(const TDesC& aLogFileName);
  64.        
  65. private:
  66.         RFile iFile;
  67.         RFileWriteStream iOutputStream;
  68.         TTime                iTraceTime;
  69.         HBufC*        iLogBuf16;
  70.         HBufC8*        iLogBuf8;
  71. };
  72. #endif // TRACELOG_H
复制代码
CTraceLog类实现代码:
  1. /*
  2. ============================================================================
  3. Name                : TraceLog.cpp
  4. Author          : Alex
  5. Version         : 1.0
  6. Copyright   : Your copyright notice
  7. Description : CTraceLog implementation
  8. ============================================================================
  9. */
  10. #include "TraceLog.h"
  11. #include <COEMAIN.H>
  12. _LIT(KTimeFormat,"[%Y%M%D%1-%2-%3 %H:%T:%S] ");
  13. _LIT(KLogFileFormat,"C://data//%S_log.txt");
  14. _LIT(K16LogLevel0," <Trace >: ");
  15. _LIT(K16LogLevel1," <Error>: ");
  16. _LIT(K16LogEOF,"/r/n");
  17. _LIT8(K8LogLevel0," <Info >: ");
  18. _LIT8(K8LogLevel1," <Error>: ");
  19. _LIT8(K8LogEOF,"/r/n");
  20. const TInt KMaxLogSize        = 1024;
  21. CTraceLog* g_TraceLog = NULL;
  22. CTraceLog::CTraceLog()
  23. {
  24.         // No implementation required
  25. }
  26. CTraceLog::~CTraceLog()
  27. {
  28.         delete iLogBuf16;
  29.         iLogBuf16 = NULL;
  30.        
  31.         delete iLogBuf8;
  32.         iLogBuf8 = NULL;
  33.        
  34.         iOutputStream.Close();
  35.         iFile.Close();
  36. }
  37. CTraceLog* CTraceLog::NewLC(const TDesC& aLogFileName)
  38. {
  39.         CTraceLog* self = new (ELeave) CTraceLog();
  40.         CleanupStack::PushL(self);
  41.         self->ConstructL(aLogFileName);
  42.         return self;
  43. }
  44. void CTraceLog::BeginDebugL(const TDesC& aLogFileName)
  45. {
  46. #ifdef LOCAL_TRACE
  47.         g_TraceLog = CTraceLog::NewLC(aLogFileName);
  48.         CleanupStack::Pop();
  49. #endif
  50. }
  51. void CTraceLog::StopDebugL()
  52. {
  53.         if(g_TraceLog != NULL)
  54.         {
  55.                 delete g_TraceLog;
  56.                 g_TraceLog = NULL;
  57.         }
  58. }
  59. void CTraceLog::ConstructL(const TDesC& aLogFileName)
  60. {
  61.         TInt ret;
  62.         TFileName fileName;
  63.         fileName.Format(KLogFileFormat,&aLogFileName);
  64.         ret = CCoeEnv::Static()->FsSession().MkDirAll(fileName);
  65.         if( (KErrNone!=ret) && (KErrAlreadyExists!=ret) )
  66.         {
  67.                 ASSERT(0);
  68.         }
  69.        
  70.         User::LeaveIfError(iFile.Replace(CCoeEnv::Static()->FsSession(),
  71.                         fileName,
  72.                         EFileWrite|EFileRead|EFileShareAny) );
  73.        
  74.         iOutputStream.Attach(iFile);
  75.        
  76.         iLogBuf16 = HBufC16::NewL(KMaxLogSize);
  77.         iLogBuf8 = HBufC8::NewL(KMaxLogSize);
  78. }
  79. void CTraceLog::OutPutlog(TRefByValue< const TDesC16 > aFmt,...)
  80. {
  81.         TPtr logBufPtr = iLogBuf16->Des();
  82.         iTraceTime.HomeTime();
  83.         iTraceTime.FormatL(logBufPtr,KTimeFormat);
  84.        
  85.         VA_LIST ap;
  86.         VA_START(ap,aFmt);
  87.         logBufPtr.AppendFormatList(aFmt,ap);
  88.         VA_END(ap);
  89.         logBufPtr.Append(K16LogEOF);
  90.         iOutputStream.WriteL(logBufPtr);
  91.         iOutputStream.CommitL();
  92. }
  93. void CTraceLog::OutPutlog(TRefByValue< const TDesC8 > aFmt,...)
  94. {
  95.         TBuf<30> tmpTimeBuf;
  96.         iTraceTime.HomeTime();
  97.         iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
  98.        
  99.         TPtr logBufPtr = iLogBuf16->Des();
  100.         TPtr8 logBuf8Ptr = iLogBuf8->Des();
  101.         VA_LIST ap;
  102.         VA_START(ap,aFmt);
  103.         logBuf8Ptr.FormatList(aFmt,ap);
  104.         VA_END(ap);
  105.         logBuf8Ptr.Append(K8LogEOF);
  106.         logBufPtr.Copy(logBuf8Ptr);
  107.         logBufPtr.Insert(0,tmpTimeBuf);
  108.        
  109.         iOutputStream.WriteL(logBufPtr);
  110.         iOutputStream.CommitL();
  111. }
  112. void CTraceLog::OutPutlog(const char *aFmt,...)
  113. {
  114.         TBuf<30> tmpTimeBuf;
  115.         iTraceTime.HomeTime();
  116.         iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
  117.        
  118.         TPtr logBufPtr = iLogBuf16->Des();
  119.         TPtr8 logBuf8Ptr = iLogBuf8->Des();
  120.         TPtrC8 fmt((TUint8*)aFmt);
  121.         VA_LIST ap;
  122.         VA_START(ap,aFmt);
  123.         logBuf8Ptr.FormatList(fmt,ap);
  124.         VA_END(ap);
  125.         logBuf8Ptr.Append(K8LogEOF);
  126.         logBufPtr.Copy(logBuf8Ptr);
  127.         logBufPtr.Insert(0,tmpTimeBuf);
  128.        
  129.         iOutputStream.WriteL(logBufPtr);
  130.         iOutputStream.CommitL();
  131. }
  132. void CTraceLog::OutPutlog(const char *aFmt, VA_LIST aParamList)
  133. {
  134.         TBuf<30> tmpTimeBuf;
  135.         iTraceTime.HomeTime();
  136.         iTraceTime.FormatL(tmpTimeBuf,KTimeFormat);
  137.         TPtr logBufPtr = iLogBuf16->Des();
  138.         TPtr8 logBuf8Ptr = iLogBuf8->Des();
  139.         TPtrC8 fmt((TUint8*)aFmt);
  140.         logBuf8Ptr.FormatList(fmt,aParamList);
  141.         logBuf8Ptr.Append(K8LogEOF);
  142.         logBufPtr.Copy(logBuf8Ptr);
  143.         logBufPtr.Insert(0,tmpTimeBuf);
  144.        
  145.         iOutputStream.WriteL(logBufPtr);
  146.         iOutputStream.CommitL();
  147. }

原创粉丝点击