📄 logstream.h
字号:
/*============================================================================. | Copyright (C) 2006 Gareth Buxton | |----------------------------------------------------------------------------| | LogPlusPlus is free software; you can redistribute it and/or | | modify it under the terms of the GNU Lesser General Public | | License as published by the Free Software Foundation; either | | version 2.1 of the License, or (at your option) any later version. | | | | LogPlusPlus is distributed in the hope that it will be useful, | | but WITHOUT ANY WARRANTY; without even the implied warranty of | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | | Lesser General Public License for more details. | | | | You should have received a copy of the GNU Lesser General Public | | License along with this library; if not, write to the Free Software | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | '============================================================================*/#ifndef LPP_INFOSTREAM_H#define LPP_INFOSTREAM_H#include <liblpp/Types.h>#include <liblpp/LogFlag.h>#include <liblpp/LogLevel.h>#include <ctime>#include <sstream>//=============================================================================LPP_NAMESPACE_BEGIN//=============================================================================/** * An LogStream sends all its output to * a LogWriter. The LogStream class maintains * a LogFlag which it sends to the LogWriter * whenever it requests it to write to its outputs * in order that the LogWriter might determine * whether or not to actually propergate the message * to the actual std::ostream objects. **/class LogStream: public std::ostream, public std::stringbuf{private: std::string name; const std::string logName; class LogWriter& writer; LogLevel level; LogFlag flag; bool isEnabled;public: /** * Override of the std::streambuf::sync() method * used to notify the LogStream that * a buffer is ready for writing to the LogWriter **/ virtual int sync(); /** * Construct a new LogStream. * @param writer The LogWriter object that * will recieve all the output that was sent * to this LogStream. * @param name The name of this LogStream, This is the * 'handle' by which the user of the Log library * identifies and uses this LogStream. * @param logLevel The level at which this stream operates. * This value is internally converted into a bit flag which * will subsequently be logically anded to the LogWriter's * LogMask in order to determine whether or not to propergate * messages from this LogStream to its configured std::ostream * objects. **/ LogStream(const std::string& name, const std::string& logName, class LogWriter& writer, const LogLevel& level = 0); virtual ~LogStream(); /** * Get the name of this LogStream. * @return The name of this LogStream. **/ inline const std::string& getName() const { return name; } /** * Set the name of this LogStream. * @return This LogStream. **/ inline LogStream& setName(const std::string& name) { this->name = name; return *this; } /** * Set the logging level of this LogStream. * @param logLevel The level at which this stream operates. * This value is internally converted into a bit flag which * will subsequently be logically anded to the LogWriter's * LogMask in order to determine whether or not to propergate * messages from this LogStream to its configured std::ostream * objects. **/ void setLogLevel(const LogLevel& level); /** * Get the logging level of this LogStream. * @return The currently configured logging level of * this LogStream. **/ const LogLevel getLogLevel(); /** * Get the logging flag of this LogStream. * @return A flag consisting of a single set bit * determined by the configured logging level * of this LogStream. This flag is used by the * associated LogWriter to determine * whether of not messages from this LogStream * should be propergated its configured std::ostream objects. **/ inline const LogFlag getLogFlag() { return flag; } /** * Write to the associated LogWriter. * @param info The message to be sent to the associated * LogWriter. The LogWriter will only receive this message * if the logging level of this LogStream permits. **/ void write(const std::string& info) const; /** * This operator allows for constructs such * as infoStream() << "message"; * @return The underlying std::ostream. **/ inline std::ostream& operator()() { return *this; } /** * This operator allows for constructs such * as infoStream("message 1") << "message 2"; * @return The underlying std::ostream. **/ inline std::ostream& operator()(const std::string& info) { return (*this) << info; } /** * Turn this LogStream on. **/ inline void on() { flag = (1UL << static_cast<unsigned int>(level)); //flag = level; isEnabled = true; } /** * Turn this LogStream off. **/ inline void off() { isEnabled = false; flag = 0; } /** * Chech if this Infotream is enabled. * @return true if this LogStream is enabled. **/ inline bool isOn() const { return isEnabled; } /*------------------------------------------------------------------------. | Syntactic Sugar | '------------------------------------------------------------------------*/ /** * Chech if this Infotream is disabled. * @return true if this LogStream is disabled. **/ inline bool isOff() const { return !isOn(); } /** * Turn this LogStream on or off. **/ inline void tog() { isOn() ? off() : on(); } /*------------------------------------------------------------------------. | Syntactic Sugar compatability with minisplat Dbg class | '------------------------------------------------------------------------*/ /** * Turn this LogStream on. **/ inline void setEnabled(bool enabled) { enabled ? on() : off(); } /** * Chech if this Infotream is enabled. * @return true if this LogStream is enabled. **/ inline bool getEnabled() const { return isOn(); }};//=============================================================================LPP_NAMESPACE_END//=============================================================================#endif // LPP_INFOSTREAM_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -