bref
API 2014 of the Zia HTTP server.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
/Users/giersc_v/projets/zia/api/include/bref/ScopedLogger.h
Go to the documentation of this file.
00001 
00011 #ifndef BREF_API_SCOPEDLOGGER_H
00012 #define BREF_API_SCOPEDLOGGER_H
00013 
00014 #include <sstream>
00015 
00016 #include "ILogger.h"
00017 #include "detail/util/NonCopyable.hpp"
00018 
00034 #define LOG(logger, sev)                                                \
00035   if (sev < logger->severity())                                         \
00036     { }                                                                 \
00037   else                                                                  \
00038     bref::ScopedLogger(logger, sev).log()
00039 
00045 #define LOG_DEBUG(logger) LOG(logger, bref::ILogger::Debug)
00046 
00052 #define LOG_INFO(logger)  LOG(logger, bref::ILogger::Info)
00053 
00059 #define LOG_WARN(logger)  LOG(logger, bref::ILogger::Warning)
00060 
00066 #define LOG_ERROR(logger) LOG(logger, bref::ILogger::Error)
00067 
00073 #define LOG_FATAL(logger) LOG(logger, bref::ILogger::Fatal)
00074 
00075 namespace bref {
00076 
00082 class ScopedLogger : util::NonCopyable
00083 {
00084 private:
00085   std::stringstream  ss_;
00086   ILogger           *logger_;
00087   ILogger::Severity  severity_;
00088 
00089 public:
00090 
00102   inline ScopedLogger(ILogger *logger, ILogger::Severity severity)
00103     : logger_(logger)
00104     , severity_(severity)
00105   { }
00106 
00113   inline ~ScopedLogger()
00114   {
00115     logger_->log(severity_, ss_.str());
00116   }
00117 
00123   inline std::ostream & log()
00124   {
00125     return ss_;
00126   }
00127 };
00128 
00129 } // ! bref
00130 
00131 #endif /* !BREF_API_SCOPEDLOGGER_H */