Log.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Log.h: interface for the CLog class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #pragma once
  5. #include <windows.h>
  6. #include <string>
  7. using namespace std;
  8. enum enmLogLevel{
  9. LogLvlFatal=0,
  10. LogLvlFailed,
  11. LogLvlError,
  12. LogLvlWarn,
  13. LogLvlInfo,
  14. LogLvlTrace,
  15. LogLvlDebug,
  16. LogLvlAll
  17. };
  18. class CLog
  19. {
  20. public:
  21. CLog();
  22. ~CLog();
  23. public:
  24. void SetMinLevel(UINT nLevel){m_nMinLevel=nLevel;};
  25. void Put(wstring Msg, UINT nLevel = LogLvlFatal);
  26. void Put(string Msg, UINT nLevel = LogLvlFatal);
  27. void PutMsg(UINT nLevel,const char *szFmt, ...);
  28. void PutErrMsg(const char *szFmt, ...);
  29. BOOL Init(const char* pszFileName,UINT nMinLevel,int nMaxSize=1*1024*1024);
  30. protected:
  31. UINT m_nMinLevel;
  32. UINT m_nMaxLogSize;
  33. static string logDescripe[];
  34. typedef struct __tagLogIndex{
  35. DWORD dwPos;
  36. DWORD dwLen;
  37. BYTE nLevel;
  38. __tagLogIndex():dwPos(0L),dwLen(0L),nLevel(LogLvlAll)
  39. {
  40. };
  41. }LOGINDEX;
  42. HANDLE m_hFile;
  43. LOGINDEX m_idx;
  44. string m_FileName;
  45. CRITICAL_SECTION m_cs;
  46. char* m_pBuf;
  47. int m_bufSize;
  48. };
  49. class CFunc_trace
  50. {
  51. public:
  52. string funname;
  53. CLog* plog;
  54. UINT loglevel;
  55. string time;
  56. CFunc_trace(string name, CLog* log, UINT level)
  57. {
  58. funname = name;
  59. plog = log;
  60. loglevel = level;
  61. SYSTEMTIME st;
  62. GetLocalTime(&st);
  63. char chBuf[24] = { 0 };
  64. sprintf(chBuf, "%02d:%02d_%02d ",st.wMinute, st.wSecond,st.wMilliseconds);
  65. time = chBuf;
  66. if (plog)
  67. {
  68. plog->PutMsg(loglevel, "+++ %s %s +++", funname.c_str(),time.c_str());
  69. }
  70. }
  71. ~CFunc_trace()
  72. {
  73. if (plog)
  74. {
  75. plog->PutMsg(loglevel, "--- %s %s ---", funname.c_str(), time.c_str());
  76. }
  77. }
  78. };