Log.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. BOOL Init(const char* pszFileName,UINT nMinLevel,int nMaxSize=1*1024*1024);
  29. protected:
  30. UINT m_nMinLevel;
  31. UINT m_nMaxLogSize;
  32. static string logDescripe[];
  33. typedef struct __tagLogIndex{
  34. DWORD dwPos;
  35. DWORD dwLen;
  36. BYTE nLevel;
  37. __tagLogIndex():dwPos(0L),dwLen(0L),nLevel(LogLvlAll)
  38. {
  39. };
  40. }LOGINDEX;
  41. HANDLE m_hFile;
  42. LOGINDEX m_idx;
  43. string m_FileName;
  44. CRITICAL_SECTION m_cs;
  45. char* m_pBuf;
  46. int m_bufSize;
  47. };
  48. class CFunc_trace
  49. {
  50. public:
  51. string funname;
  52. CLog* plog;
  53. UINT loglevel;
  54. string time;
  55. CFunc_trace(string name, CLog* log, UINT level)
  56. {
  57. funname = name;
  58. plog = log;
  59. loglevel = level;
  60. SYSTEMTIME st;
  61. GetLocalTime(&st);
  62. char chBuf[24] = { 0 };
  63. sprintf(chBuf, "%02d:%02d_%02d ",st.wMinute, st.wSecond,st.wMilliseconds);
  64. time = chBuf;
  65. if (plog)
  66. {
  67. plog->PutMsg(loglevel, "+++ %s %s +++", funname.c_str(),time.c_str());
  68. }
  69. }
  70. ~CFunc_trace()
  71. {
  72. if (plog)
  73. {
  74. plog->PutMsg(loglevel, "--- %s %s ---", funname.c_str(), time.c_str());
  75. }
  76. }
  77. };