logfile.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // logfile.h :
  2. //
  3. #if !defined(AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_)
  4. #define AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_
  5. #include <stdio.h>
  6. #include <stdarg.h>
  7. #ifdef __cplusplus
  8. #include <string>
  9. /////////////////////////////////////////////////////////////////////////////
  10. // logfile.h - Declarations:
  11. #define _MAX_LOGFILE_SIZE (0x1ul << 20) // 1 MiB
  12. #define _MAX_BACKUP_FILES 1
  13. /////////////////////////////////////////////////////////////////////////////
  14. #ifdef _DEBUG
  15. #define _DEF_VERBOSITY VB_Dbg
  16. #else // _DEBUG
  17. #define _DEF_VERBOSITY VB_Inf
  18. #endif // _DEBUG
  19. class CLogfile // thread safe
  20. {
  21. public:
  22. enum Verbosity
  23. {
  24. VB_Off, // 0, do not log anything
  25. VB_Err, // 1, log errors only
  26. VB_War, // 2, log errors and warnings
  27. VB_Inf, // 3, log errors, warnings and infos
  28. VB_Dbg // 4, log errors, warnings, infos and debug info
  29. };
  30. /////////////////////////////////////////////////////////////////////////
  31. public:
  32. CLogfile(void);
  33. CLogfile(FILE *fd, bool bClose = false, int verb = _DEF_VERBOSITY);
  34. CLogfile(const char *pszFilename, bool bAppend = true, int verb = _DEF_VERBOSITY, size_t nMaxFileSize = _MAX_LOGFILE_SIZE, unsigned int nMaxBackupFiles = _MAX_BACKUP_FILES);
  35. virtual ~CLogfile(void);
  36. bool Open(const char *pszFilename, bool bAppend = true, int verb = _DEF_VERBOSITY, size_t nMaxFileSize = _MAX_LOGFILE_SIZE, unsigned int nMaxBackupFiles = _MAX_BACKUP_FILES);
  37. void Close(void);
  38. void Flush(void);
  39. bool Attach(FILE *fd, bool bClose = false, int verb = _DEF_VERBOSITY);
  40. void Detach(void);
  41. void Lock(void) {
  42. CsLock();}
  43. void Unlock(void) {
  44. CsUnlock();}
  45. void SetVerbosity(int verb);
  46. int GetVerbosity(void) const {
  47. return m_vb;}
  48. void Debug(const char *pszFormat, ...); // output = date time - DEBUG: message
  49. void Info(const char *pszFormat, ...); // output = date time - INFO: message
  50. void Warning(const char *pszFormat, ...); // output = date time - WARNING: message
  51. void Error(const char *pszFormat, ...); // output = date time - ERROR: message
  52. void Log(const char *pszFormat, ...); // plain message
  53. static void StdOut(const char *pszFormat, ...); // not thread safe
  54. static void StdErr(const char *pszFormat, ...); // not thread safe
  55. size_t GetFileSize(void);
  56. /////////////////////////////////////////////////////////////////////////
  57. protected:
  58. virtual const char* CreateFormatString(Verbosity verb, const char *pszFormat, std::string &ret) const;
  59. /////////////////////////////////////////////////////////////////////////
  60. private:
  61. void Log(FILE *fd, const char *pszFormat, va_list args);
  62. void CsLock(void);
  63. void CsUnlock(void);
  64. bool ProcessFileSizeLimit(FILE *fd);
  65. /////////////////////////////////////////////////////////////////////////
  66. private:
  67. FILE *m_pf;
  68. std::string m_strFilePath;
  69. bool m_bAttached;
  70. bool m_bClose;
  71. Verbosity m_vb;
  72. pthread_mutex_t m_lsync;
  73. size_t m_nFileSize;
  74. size_t m_nMaxFileSize;
  75. unsigned int m_nMaxBackupFiles;
  76. };
  77. /////////////////////////////////////////////////////////////////////////////
  78. #endif // __cplusplus
  79. #endif // !defined(AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_)