📄 logger.h
字号:
/************************************************************************* * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************/#ifndef _LOGGER_H__#define _LOGGER_H__#include "global.h"#include <fstream>#include <iostream>#include <sstream>#include <iomanip>#define LOG_ERROR (*this) << Logger::ERROR#define LOG_NORMAL (*this) << Logger::NORMAL#define LOG_VERBOSE (*this) << Logger::VERBOSE#define LOG_VVERBOSE (*this) << Logger::VVERBOSEusing namespace std;/** * The callback method definition. * * @param str The log message. * @param level The level of the log message. */typedef void (*funcPtr)(string str, int level);/** * This interface should be implemented if you want * to get the log messages and process them * programatically rather than dumping them to stdout. * For any message the method callback(string str,int level) will * be called. */class LoggerInterface{ public: /** * This method will be called whenever a log message * is to be processed. * * @param str The log message. * @param level The level of the log message. */ virtual inline void callback (string str, int level) = 0;}; /** * A Stream that can be used to control log messages. * The log messages are either dumped to stdout, resp. the streambuf * object given to the constructor but can also be intercepted by * specifying a callback method, resp. object. * This class should be inheritated and the macros defined above * should be used log log messages. */class Logger : public ostream{ public: /** * The log levels. */ enum VERBOSITY { QUIET = 0, ERROR = 1, NORMAL = 2, VERBOSE = 3, VVERBOSE = 4}; /** * Constructor. */ Logger (); /** * Constructor. * * @param sbuf The streambuf object that should be used * for output. */ Logger (streambuf* sbuf); /** * Set the verbosity level. * Only messages that have a loglevel equal to or less * than verbosity will be dumped. * * @param verbosity The ne log level that should be set. */ void setVerbosity (VERBOSITY verbosity); /** * Set the callback function. This function will be called * whenever a new log messages will appear instead of * dumping them to stdout. * * @param callback_function The callback function. */ void setCallBackFunction (funcPtr callback_function); /** * Set the callback object. This object will be called * whenever a new log messages will appear instead of * dumping them to stdout. * * @param callback_object The callback function. */ void setCallBackObject (LoggerInterface *callback_object); /** * Set another Logger object as this Logger's parent. That * means if you change the loglevel of parent, the loglevel * of this object will be set accordingly. This is true * for the following methods: * Logger.setVerbosity () * Logger.setCallbackFunction () * Logger.setCallBackObject () */ void setParentLogger (Logger * parent); /** * Returns the verbosity level. * * @returns The verbosity level. */ VERBOSITY getVerbosity (); /** * Returns the callback object. * * @returns The callback_object. */ LoggerInterface *getCallBackObject (); /** * Returns the callback function. * * @returns The callback_function. */ funcPtr getCallBackFunction (); /** * Set the verbosity level of the following log * message to verbosity. * * @param verbosity The log level for the following log message. */ Logger& operator<< (VERBOSITY verbosity); // Anything below are the normal ostream operators. Logger& operator<< (bool val); Logger& operator<< (short val); Logger& operator<< (unsigned short val); Logger& operator<< (int val); Logger& operator<< (unsigned int val); Logger& operator<< (long val); Logger& operator<< (unsigned long val); Logger& operator<< (float val); Logger& operator<< (double val); Logger& operator<< (long double val); Logger& operator<< (const void* val); Logger& operator<< (char ch); Logger& operator<< (signed char ch); Logger& operator<< (unsigned char ch); Logger& operator<< (const char* str); Logger& operator<< (const signed char* str); Logger& operator<< (const unsigned char* str); Logger& operator<< (streambuf *sb); Logger& operator<< (ostream& (*pf)(ostream&)); Logger& operator<< (ios& (*pf)(ios&)); Logger& operator<< (string str); protected: /** * Synchronize this Logger object with it's parent. If * This Logger objects does not has any parent, nothing * will be done. */ void sync (); /** * The callback object. */ LoggerInterface * callback_object; /** * The callback function. */ funcPtr callback_function; /** * The verbosity level. */ VERBOSITY verbosity; /** * The log level for the following log messages. */ VERBOSITY lvl; /** * The parent Logger object. */ Logger * parent;};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -