123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- // logfile.h :
- //
- #if !defined(AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_)
- #define AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_
- #include <stdio.h>
- #include <stdarg.h>
- #ifdef __cplusplus
- #include <string>
- /////////////////////////////////////////////////////////////////////////////
- // logfile.h - Declarations:
- #define _MAX_LOGFILE_SIZE (0x1ul << 20) // 1 MiB
- #define _MAX_BACKUP_FILES 1
- /////////////////////////////////////////////////////////////////////////////
- #ifdef _DEBUG
- #define _DEF_VERBOSITY VB_Dbg
- #else // _DEBUG
- #define _DEF_VERBOSITY VB_Inf
- #endif // _DEBUG
- class CLogfile // thread safe
- {
- public:
- enum Verbosity
- {
- VB_Off, // 0, do not log anything
- VB_Err, // 1, log errors only
- VB_War, // 2, log errors and warnings
- VB_Inf, // 3, log errors, warnings and infos
- VB_Dbg // 4, log errors, warnings, infos and debug info
- };
- /////////////////////////////////////////////////////////////////////////
- public:
- CLogfile(void);
- CLogfile(FILE *fd, bool bClose = false, int verb = _DEF_VERBOSITY);
- CLogfile(const char *pszFilename, bool bAppend = true, int verb = _DEF_VERBOSITY, size_t nMaxFileSize = _MAX_LOGFILE_SIZE, unsigned int nMaxBackupFiles = _MAX_BACKUP_FILES);
- virtual ~CLogfile(void);
- bool Open(const char *pszFilename, bool bAppend = true, int verb = _DEF_VERBOSITY, size_t nMaxFileSize = _MAX_LOGFILE_SIZE, unsigned int nMaxBackupFiles = _MAX_BACKUP_FILES);
- void Close(void);
- void Flush(void);
- bool Attach(FILE *fd, bool bClose = false, int verb = _DEF_VERBOSITY);
- void Detach(void);
- void Lock(void) {
- CsLock();}
- void Unlock(void) {
- CsUnlock();}
- void SetVerbosity(int verb);
- int GetVerbosity(void) const {
- return m_vb;}
- void Debug(const char *pszFormat, ...); // output = date time - DEBUG: message
- void Info(const char *pszFormat, ...); // output = date time - INFO: message
- void Warning(const char *pszFormat, ...); // output = date time - WARNING: message
- void Error(const char *pszFormat, ...); // output = date time - ERROR: message
- void Log(const char *pszFormat, ...); // plain message
- static void StdOut(const char *pszFormat, ...); // not thread safe
- static void StdErr(const char *pszFormat, ...); // not thread safe
-
- size_t GetFileSize(void);
- /////////////////////////////////////////////////////////////////////////
- protected:
- virtual const char* CreateFormatString(Verbosity verb, const char *pszFormat, std::string &ret) const;
- /////////////////////////////////////////////////////////////////////////
- private:
- void Log(FILE *fd, const char *pszFormat, va_list args);
- void CsLock(void);
- void CsUnlock(void);
- bool ProcessFileSizeLimit(FILE *fd);
- /////////////////////////////////////////////////////////////////////////
- private:
- FILE *m_pf;
- std::string m_strFilePath;
- bool m_bAttached;
- bool m_bClose;
- Verbosity m_vb;
- pthread_mutex_t m_lsync;
- size_t m_nFileSize;
- size_t m_nMaxFileSize;
- unsigned int m_nMaxBackupFiles;
- };
- /////////////////////////////////////////////////////////////////////////////
- #endif // __cplusplus
- #endif // !defined(AGD_LOGFILE_H__4DFE59C7_5C38_431E_AD27_9E88C67490A8__INCLUDED_)
|