📄 logger.cpp
字号:
/************************************************************************* * * * 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. * * * *************************************************************************/#include "logger.h"/** * Constructor. */Logger::Logger () : ostream (cout.rdbuf ()){ this->callback_function = NULL; this->callback_object = NULL; this->verbosity = QUIET; this->lvl = NORMAL; this->parent = NULL;}/** * Constructor. * * @param sbuf The streambuf object that should be used * for output. */Logger::Logger (streambuf* sbuf) : ostream (sbuf){ this->callback_function = NULL; this->callback_object = NULL; this->verbosity = QUIET; this->lvl = NORMAL; this->parent = NULL;}/** * 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 Logger::setParentLogger (Logger * parent){ this->parent = parent;}/** * Synchronize this Logger object with it's parent. If * This Logger objects does not has any parent, nothing * will be done. */void Logger::sync (){ if (this->parent != NULL) { this->verbosity = parent->getVerbosity (); this->callback_function = parent->getCallBackFunction (); this->callback_object = parent->getCallBackObject (); }}/** * Returns the verbosity level. * * @returns The verbosity level. */Logger::VERBOSITY Logger::getVerbosity (){ sync (); return (this->verbosity);}/** * Returns the callback function. * * @returns The callback_function. */funcPtr Logger::getCallBackFunction (){ sync (); return (this->callback_function);}/** * Returns the callback object. * * @returns The callback_object. */LoggerInterface *Logger::getCallBackObject (){ sync (); return (this->callback_object);}/** * 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 Logger::setVerbosity (VERBOSITY verbosity){ this->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 Logger::setCallBackFunction (funcPtr callback_function){ this->callback_function = 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 Logger::setCallBackObject (LoggerInterface *callback_object){ this->callback_object = callback_object;}Logger& Logger::operator<< (bool val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (short val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (unsigned short val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (int val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (unsigned int val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (long val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (unsigned long val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (float val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (double val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (long double val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (const void* val){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << val; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << val; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (val); } } return (*this);}Logger& Logger::operator<< (char ch){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << ch; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << ch; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (ch); } } return (*this);} Logger& Logger::operator<< (signed char ch){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << ch; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << ch; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (ch); } } return (*this);} Logger& Logger::operator<< (unsigned char ch){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << ch; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << ch; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (ch); } } return (*this);} Logger& Logger::operator<< (const char* str){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << str; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << str; this->callback_object->callback (osstream.str (), this->lvl); } else { std::operator<< ((*this), string (str)); } } return (*this);} Logger& Logger::operator<< (const signed char* str){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << str; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << str; this->callback_object->callback (osstream.str (), this->lvl); } else { std::operator<< ((*this), string ((char*)str)); } } return (*this);} Logger& Logger::operator<< (const unsigned char* str){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << str; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << str; this->callback_object->callback (osstream.str (), this->lvl); } else { std::operator<< ((*this), string ((char*)str)); } } return (*this);} Logger& Logger::operator<< (streambuf *sb){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << sb; this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << sb; this->callback_object->callback (osstream.str (), this->lvl); } else { this->ostream::operator<< (sb); } } return (*this);} Logger& Logger::operator<< (ostream& (*pf)(ostream&)){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; (*pf) (osstream); this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; (*pf) (osstream); this->callback_object->callback (osstream.str (), this->lvl); } else { (*pf) (*this); } } return (*this);} Logger& Logger::operator<< (ios& (*pf)(ios&)){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; (*pf) (osstream); this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; (*pf) (osstream); this->callback_object->callback (osstream.str (), this->lvl); } else { (*pf) (*this); } } return (*this);} Logger& Logger::operator<< (string str){ sync (); if (this->lvl <= this->verbosity) { if (this->callback_function != NULL) { ostringstream osstream; osstream << str.c_str (); this->callback_function (osstream.str (), this->lvl); } else if (this->callback_object != NULL) { ostringstream osstream; osstream << str.c_str (); this->callback_object->callback (osstream.str (), this->lvl); } else { std::operator<< ((*this), str); } } return (*this);}/** * Set the verbosity level of the following log * message to verbosity. * * @param verbosity The log level for the following log message. */Logger& Logger::operator<< (VERBOSITY verbosity){ sync (); if (verbosity != QUIET) { this->lvl = verbosity; } return (*this);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -