Logger.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Copyright @ 2014 Hangzhou Topzen Ltd.
  3. // Author: Tang (tang@hztopzen.com) @ 2014-12
  4. //
  5. #ifndef __LOGGER_H
  6. #define __LOGGER_H
  7. #include "BaseTypes.h"
  8. #include "BaseHeaders.h"
  9. #include "BaseCalls.h"
  10. #include "Locks.h"
  11. #include "Thread.h"
  12. #include "Semaphore.h"
  13. namespace tzc {
  14. class DECLDLL Logger : public OSThread {
  15. public:
  16. static Logger * Instance();
  17. static void DestroyInstance();
  18. public:
  19. void Initialize(const std::string & sConfig = "./logger.config");
  20. void Initialize(const std::string & sModule,
  21. const std::string & sLogFile,
  22. const TZ_Uint32 iMaxMBytes,
  23. const TZ_Uint32 iMaxLines,
  24. const TZ_Uint32 iDebugLevel,
  25. const TZ_BOOL bForeground,
  26. const TZ_BOOL bSyncWrite);
  27. const std::string & LogFilePath() const;
  28. TZ_Uint32 TotalLines();
  29. void SetLogDebugLevel(TZ_Uint32 level);
  30. void LogInfo(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  31. const TZ_CHAR * format, ...);
  32. void LogWarn(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  33. const TZ_CHAR * format, ...);
  34. void LogError(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  35. const TZ_CHAR * format, ...);
  36. void LogAlert(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  37. const TZ_CHAR * format, ...);
  38. void LogDebug(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  39. const TZ_UINT iLevel, const TZ_CHAR * format, ...);
  40. protected:
  41. void doLog(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  42. const TZ_CHAR * prefix, const TZ_CHAR * format, va_list ap);
  43. void doLogAsync(const TZ_CHAR * file, const TZ_CHAR * func, const TZ_INT line,
  44. const TZ_CHAR * prefix, const TZ_CHAR * format, va_list ap);
  45. void check4Seperate();
  46. TZ_INT touchFilePath(const std::string & sFilePath);
  47. TZ_INT getFileLineCount(const std::string & sFilePath);
  48. void outputLogItem(const std::string & item);
  49. virtual void Entry();
  50. protected:
  51. Logger();
  52. virtual ~Logger();
  53. private:
  54. TZ_BOOL m_bSyncWrite;
  55. TZ_BOOL m_bForeground;
  56. TZ_BOOL m_bInitialized;
  57. volatile TZ_Uint32 m_iDebugLevel;
  58. TZ_Uint32 m_iCurrLineIdx;
  59. TZ_Uint32 m_iCurrFileIdx;
  60. TZ_Uint32 m_iMaxBytes;
  61. TZ_Uint32 m_iMaxLines;
  62. TZ_Uint32 m_iMaxFiles;
  63. std::string m_sFilePath;
  64. std::string m_sModuleName;
  65. Mutex m_mutex;
  66. FILE * m_fileStream;
  67. Semaphore m_sem;
  68. Mutex m_queueMutex;
  69. std::list<std::string> m_itemQueue;
  70. private:
  71. static const std::string PREFIX_INFO;
  72. static const std::string PREFIX_WARN;
  73. static const std::string PREFIX_ERROR;
  74. static const std::string PREFIX_ALERT;
  75. static const std::string PREFIX_DEBUG;
  76. static const TZ_UINT MAX_LENGTH = 1000;
  77. static Mutex _mutex;
  78. static Logger * _instance;
  79. };
  80. }; // namespace tzc
  81. #define POSITION __FILE__, __FUNCTION__, __LINE__
  82. #define INITIALIZE_LOGGER_CONFIG(configurefile) \
  83. tzc::Logger::Instance()->Initialize(configurefile)
  84. //
  85. // Notice: @level should be larger than 5 while 1 ~ 5 levels are used for
  86. // Common inner implementation; otherwise there will be much more logs
  87. // output in the application.
  88. //
  89. #define INITIALIZE_LOGGER_NORMAL(module, file, mbytes, lines, level, forground, sync) \
  90. tzc::Logger::Instance()->Initialize(module, file, mbytes, lines, level, forground, sync)
  91. #define DESTROY_Logger() tzc::Logger::DestroyInstance()
  92. #define TotalLogLines() \
  93. tzc::Logger::Instance()->TotalLines()
  94. #define TZLogSetDebugLevel(level) \
  95. tzc::Logger::Instance()->SetLogDebugLevel(level)
  96. #if defined(WIN32) || defined(WINDOWS)
  97. #define TZLogInfo(format, ...) \
  98. tzc::Logger::Instance()->LogInfo(POSITION, format, __VA_ARGS__)
  99. #define TZLogWarn(format, ...) \
  100. tzc::Logger::Instance()->LogWarn(POSITION, format, __VA_ARGS__)
  101. #define TZLogError(format, ...) \
  102. tzc::Logger::Instance()->LogError(POSITION, format, __VA_ARGS__)
  103. #define TZLogAlert(format, ...) \
  104. tzc::Logger::Instance()->LogAlert(POSITION, format, __VA_ARGS__)
  105. #define TZLogDebug(level, format, ...) \
  106. tzc::Logger::Instance()->LogDebug(POSITION, level, format, __VA_ARGS__)
  107. #elif defined(LINUX) // Linux
  108. #define TZLogInfo(format, args...) \
  109. tzc::Logger::Instance()->LogInfo(POSITION, format, ##args)
  110. #define TZLogWarn(format, args...) \
  111. tzc::Logger::Instance()->LogWarn(POSITION, format, ##args)
  112. #define TZLogError(format, args...) \
  113. tzc::Logger::Instance()->LogError(POSITION, format, ##args)
  114. #define TZLogAlert(format, args...) \
  115. tzc::Logger::Instance()->LogAlert(POSITION, format, ##args)
  116. #define TZLogDebug(level, format, args...) \
  117. tzc::Logger::Instance()->LogDebug(POSITION, level, format, ##args)
  118. #endif
  119. #endif /* ----- #ifndef __LOGGER_H ----- */