log: style

This commit is contained in:
Danny Robson 2015-07-23 17:37:02 +10:00
parent 5d6586636e
commit ea953ec024
2 changed files with 34 additions and 15 deletions

29
log.cpp
View File

@ -32,6 +32,8 @@ using namespace util;
using std::string;
using std::map;
///////////////////////////////////////////////////////////////////////////////
void
check_level (level_t l)
{
@ -39,6 +41,7 @@ check_level (level_t l)
CHECK (l >= 0 && l < NUM_LEVELS);
}
//-----------------------------------------------------------------------------
const string&
level_to_string (level_t l) {
check_level (l);
@ -58,6 +61,7 @@ level_to_string (level_t l) {
}
///////////////////////////////////////////////////////////////////////////////
static level_t
string_to_level (string name) {
static const map <string, level_t> NAME_LEVELS = {
@ -83,6 +87,7 @@ string_to_level (string name) {
}
//-----------------------------------------------------------------------------
std::ostream&
util::operator<< (std::ostream& os, level_t l) {
os << level_to_string (l);
@ -90,11 +95,7 @@ util::operator<< (std::ostream& os, level_t l) {
}
void
util::log (level_t l, const std::string &format)
{ detail::log (l, boost::format (format)); }
///////////////////////////////////////////////////////////////////////////////
static level_t
log_level (void) {
const char *env = getenv ("LOG_LEVEL");
@ -109,6 +110,13 @@ log_level (void) {
}
///////////////////////////////////////////////////////////////////////////////
void
util::log (level_t l, const std::string &format)
{ detail::log (l, boost::format (format)); }
//-----------------------------------------------------------------------------
void
util::detail::log (level_t level, boost::format &&format) {
static const level_t LOG_LEVEL = log_level ();
@ -137,13 +145,16 @@ util::detail::log (level_t level, boost::format &&format) {
}
scoped_logger::scoped_logger (const level_t _level,
const std::string &_message):
///////////////////////////////////////////////////////////////////////////////
scoped_logger::scoped_logger (level_t _level,
std::string &&_message):
m_level (_level),
m_message (_message)
m_message (std::move (_message))
{ ; }
scoped_logger::~scoped_logger () {
//-----------------------------------------------------------------------------
scoped_logger::~scoped_logger ()
{
log (m_level, m_message);
}

14
log.hpp
View File

@ -28,6 +28,7 @@
#endif
namespace util {
///////////////////////////////////////////////////////////////////////////
// rfc5424 log levels. It is assumed they are contiguous to simplify array
// indexing in logging code.
//
@ -47,14 +48,19 @@ namespace util {
DEFAULT = INFO
};
//-------------------------------------------------------------------------
std::ostream&
operator<< (std::ostream&, level_t);
///////////////////////////////////////////////////////////////////////////
void log (level_t, const std::string &format);
template <typename ...tail>
void log (level_t, const std::string &format, tail ..._tail);
//-------------------------------------------------------------------------
#define LOG_EMERGENCY(...) do { util::log(util::EMERGENCY, ##__VA_ARGS__); } while (0)
#define LOG_ALERT(...) do { util::log(util::ALERT, ##__VA_ARGS__); } while (0)
#define LOG_CRITICAL(...) do { util::log(util::CRITICAL, ##__VA_ARGS__); } while (0)
@ -70,15 +76,17 @@ namespace util {
#endif
///////////////////////////////////////////////////////////////////////////
class scoped_logger : public nocopy {
public:
scoped_logger (const level_t, const std::string&);
scoped_logger (const level_t, std::string&&);
~scoped_logger ();
protected:
const level_t m_level;
const std::string &m_message;
level_t m_level;
std::string m_message;
};
}
#include "log.ipp"