- 论坛徽章:
- 0
|
本帖最后由 smallest10 于 2010-08-31 19:37 编辑
- // Logger.h: interface for the Logger class.
- //
- //////////////////////////////////////////////////////////////////////
- #if !defined(AFX_LOGGER_H__5C97FAE9_F998_42F3_B797_ADC910889447__INCLUDED_)
- #define AFX_LOGGER_H__5C97FAE9_F998_42F3_B797_ADC910889447__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include <string>
- #include <fstream>
- #include <vector>
- #include <sstream>
- using std::string;
- namespace skgLogger{
- typedef int LogLevelType;
- const LogLevelType INFO_LOG_LEVEL = 0x03; // 0011
- const LogLevelType ERROR_LOG_LEVEL = 0x02; // 0010
- const LogLevelType WARN_LOG_LEVEL= 0x01; // 0001
- const LogLevelType NONE_LOG_LEVEL = 0x00; // 0000
- //const LogLevelType LEVEL_TRACE= 0x04 // 0100 ===== another output way
- typedef std::ios::openmode LOG_OPEN_MODE_TYPE;
- #define LOG_TRACE_MACRO_BODY(logEvent) \
- do { \
- if(Logger::isTraceable()) { \
- std::ostringstream _out_buf; \
- _out_buf << logEvent << "\n"; \
- TRACE((_out_buf.str().append(__FILE__).append(__LINE__)).c_str()); \
- } \
- } while (0)
- // note that the logEvent argument is string type
- #define LOG_TRACE_MACRO_STR_BODY(logEvent) \
- do { \
- if(Logger::isTraceable()) { \
- TRACE((logEvent.append(__FILE__).append(__LINE__).append("\n")).c_str()) \
- } \
- } while (0)
- #define LOG_MACRO_BODY(logger, logEvent, logLevel) \
- do { \
- if((logger)->isEnabledFor(logLevel##_LOG_LEVEL)) { \
- std::ostringstream _out_buf; \
- _out_buf << logEvent; \
- (logger)->WriteLog(logLevel##_LOG_LEVEL, \
- _out_buf.str(), __FILE__, __LINE__); \
- } \
- } while (0)
- // if logEvent is a string, then we could call this MACRO instead of LOG_MACRO_BODY()
- // note that the logEvent argument is string type
- #define LOG_MACRO_STR_BODY(logger, logEvent, logLevel) \
- do { \
- if((logger)->isEnabledFor(logLevel##_LOG_LEVEL)) { \
- (logger)->WriteLog(logLevel##_LOG_LEVEL, \
- logEvent, __FILE__, __LINE__); \
- } \
- } while (0)
- // LOG_TRACE() 与 LOG_TRACE_STR() 有问题.未进行具体调试.
- #if !defined(LOG_DISABLE_TRACE)
- #define LOG_TRACE(logEvent) \
- LOG_TRACE_MACRO_BODY (logEvent)
- #define LOG_TRACE_STR(logEvent) \
- LOG_TRACE_MACRO_STR_BODY (logEvent)
- #else
- #define LOG_TRACE(logEvent) do { } while (0)
- #define LOG_TRACE_STR(logEvent) do { } while (0)
- #endif
- #if !defined(LOG_DISABLE_DEBUG)
- #define LOG_DEBUG(logger, logEvent) \
- LOG_MACRO_BODY (logger, logEvent, DEBUG)
- #define LOG_DEBUG_STR(logger, logEvent) \
- LOG_MACRO_STR_BODY (logger, logEvent, DEBUG)
- #else
- #define LOG_DEBUG(logger, logEvent) do { } while (0)
- #define LOG_DEBUG_STR(logger, logEvent) do { } while (0)
- #endif
- #if !defined(LOG_DISABLE_INFO)
- #define LOG_INFO(logger, logEvent) \
- LOG_MACRO_BODY (logger, logEvent, INFO)
- #define LOG_INFO_STR(logger, logEvent) \
- LOG_MACRO_STR_BODY (logger, logEvent, INFO)
- #else
- #define LOG_INFO(logger, logEvent) do { } while (0)
- #define LOG_INFO_STR(logger, logEvent) do { } while (0)
- #endif
- #if !defined(LOG_DISABLE_WARN)
- #define LOG_WARN(logger, logEvent) \
- LOG_MACRO_BODY (logger, logEvent, WARN)
- #define LOG_WARN_STR(logger, logEvent) \
- LOG_MACRO_STR_BODY (logger, logEvent, WARN)
- #else
- #define LOG_WARN(logger, logEvent) do { } while (0)
- #define LOG_WARN_STR(logger, logEvent) do { } while (0)
- #endif
- #if !defined(LOG_DISABLE_ERROR)
- #define LOG_ERROR(logger, logEvent) \
- LOG_MACRO_BODY (logger, logEvent, ERROR)
- #define LOG_ERROR_STR(logger, logEvent) \
- LOG_MACRO_STR_BODY (logger, logEvent, ERROR)
- #else
- #define LOG_ERROR(logger, logEvent) do { } while (0)
- #define LOG_ERROR_STR(logger, logEvent) do { } while (0)
- #endif
- #if !defined(LOG_DISABLE_FATAL)
- #define LOG_FATAL(logger, logEvent) \
- LOG_MACRO_BODY (logger, logEvent, FATAL)
- #define LOG_FATAL_STR(logger, logEvent) \
- LOG_MACRO_STR_BODY (logger, logEvent, FATAL)
- #else
- #define LOG_FATAL(logger, logEvent) do { } while (0)
- #define LOG_FATAL_STR(logger, logEvent) do { } while (0)
- #endif
- /************************************************************************/
- /* define class Logger */
- /************************************************************************/
- class Logger
- {
- public:
- static BOOL InitLogger();
- static Logger* GetInstance(const string& name); // 获得日志类的实例
- static void RemoveInstance(const string& name);
- static void RemoveInstance(const Logger* logger);
- static BOOL isTraceable();
- static void EnableTrace(BOOL traceable);
-
- // LOG_WANR 等宏会调用此函数
- void WriteLog(LogLevelType level, const string& message, const char* file="unkonw",int line=-1); // 日志打印接口
- virtual void PrintLog(const string& message); // 打印输出信息 --- not implemented yet.
- // LOG_WANR 等宏会调用此函数
- BOOL isEnabledFor(LogLevelType logLevel);
- inline string GetName() const;// the class member 'name' can only be assigned via GetInstance()
- inline LogLevelType GetLogLevel() const;
- inline void SetLogLevel(LogLevelType logLevel);
- BOOL initLogFile(const string& basename,
- long maxFileSize=10*1024*1024, // 10 MB
- BOOL immediateFlush=TRUE,
- LOG_OPEN_MODE_TYPE mode = std::ios::app);// call Open()
- // Logger 级别成员
- private:
- BOOL b_inited;//记录是否已经调用过 initLogFile() 方法
- string name;//name of this logger. 若想控制每个文件只对应一个Logger对象,将此变量的值改为basename的值即可.
- LogLevelType m_LogLevel; //日志输出等级, 默认为 LEVEL_INFO
- CRITICAL_SECTION cs_file; // 对象级别的临界区变量
- static CRITICAL_SECTION cs_loggerClassLevel;// 类级别的临界区变量
- static std::vector<Logger*> loggerList;
- static BOOL b_traceable;
- // 默认按 %y-%m-%d %H:%M:%S 格式来取得时间
- string GetTimeString(const char* timeFormat="%Y-%m-%d %H:%M:%S"); // 按格式获得日期
- // 文件级别成员
- private:
- string openedFilename;// 输出日志文件名 , 文件名格式如 basename_date_seq.log
- string basename;// 用于日志文件更换时,作为基文件名存在. 文件名格式如 basename_date_seq.log .如 srbw_20100901_1.log
- BOOL immediateFlush;
- std::ofstream stream_out;
- char* buffer;
- unsigned long bufferSize;
- long currentFileSize;
- long maxFileSize;
- int currentFileSeq;
- void Open(LOG_OPEN_MODE_TYPE mode);
- BOOL ReOpen();
- BOOL Rollover();// 当前日志文件已记录满,更换日志文件进行存储.
- private:
- // Disallow copying of instances of this class
- Logger(const string& name);
- Logger(const Logger&){} ////////////// need code, to prevent someone call this func in this class
- Logger(Logger*){} ////////////// need code, to prevent someone call this func in this class
- Logger& operator=(const Logger&){}////////////// need code, to prevent someone call this func in this class
- // Disallow deleting of instances of this class
- virtual ~Logger();
- };// end class Logger
- } // end namespace skgLogger
- #endif // !defined(AFX_LOGGER_H__5C97FAE9_F998_42F3_B797_ADC910889447__INCLUDED_)
复制代码 |
|