Details
-
New Feature
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
0.10.0
-
None
-
None
-
n/a
Description
A stream which provides a nice abstracted way to only do formatting when your current log level is enabled. See list archives for details.
#ifndef LOGGER_STREAM_HPP__INCLUDE
#define LOGGER_STREAM_HPP__INCLUDE
#include <log4cxx/logger.h>
#ifndef DISABLE_IOSTREAMS
#include <sstream>
#endif
#include <cassert>
template<typename B>
class LoggerStream {
public:
typedef enum
modifiers;
typedef B buffer_type;
typedef LoggerStream<buffer_type> self_type;
inline LoggerStream(log4cxx::LoggerPtr& aLogger,
const log4cxx::LevelPtr& aLevel,
const char* aFile = 0, int aLine = -1)
: logger(aLogger),
level(aLevel),
file(aFile),
line(aLine),
buffer((aLogger->isEnabledFor(aLevel)) ? (new buffer_type()) : 0) {
}
inline ~LoggerStream()
{ delete(buffer); //no need to check for null }inline bool isEnabled() const
{ return buffer != 0; } template<typename T>
inline LoggerStream& operator<<(const T& aValue) {
if (isEnabled())
return *this;
}
inline LoggerStream& operator<<(const modifiers& aValue)
{ assert(aValue == endl); flush(); } inline LoggerStream& flush() {
if (isEnabled())
return *this;
}
private:
log4cxx::LoggerPtr& logger;
const log4cxx::LevelPtr& level;
const char* file;
const int line;
buffer_type* const buffer;
};
//helpful macros that use STL IOSTREAMS
#define LOG_STREAM_EX(stream_type, logger, level) LoggerStream<stream_type>(logger, level, _FILE, __LINE_)
#define LOG_STREAM(logger, level) LOG_STREAM_EX(std::ostringstream, logger, level)
#define WLOG_STREAM(logger, level) LOG_STREAM_EX(std::wostringstream, logger, level)
#define ENDL LoggerStream<std::ostringstream>::endl
#define WENDL LoggerStream<std::wostringstream>::endl
#endif