log: style

This commit is contained in:
Danny Robson 2016-01-20 15:36:32 +11:00
parent 8cb5029dca
commit ecbedf6420

19
log.cpp
View File

@ -44,7 +44,8 @@ check_level (level_t l)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const string& const string&
level_to_string (level_t l) { level_to_string (level_t l)
{
check_level (l); check_level (l);
static const std::array <std::string, NUM_LEVELS> LEVEL_NAMES ({{ static const std::array <std::string, NUM_LEVELS> LEVEL_NAMES ({{
@ -64,7 +65,8 @@ level_to_string (level_t l) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static level_t static level_t
string_to_level (string name) { string_to_level (string name)
{
static const map <string, level_t> NAME_LEVELS = { static const map <string, level_t> NAME_LEVELS = {
{ "EMERGENCY", EMERGENCY }, { "EMERGENCY", EMERGENCY },
{ "ALERT", ALERT }, { "ALERT", ALERT },
@ -90,7 +92,8 @@ string_to_level (string name) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::ostream& std::ostream&
util::operator<< (std::ostream& os, level_t l) { util::operator<< (std::ostream& os, level_t l)
{
os << level_to_string (l); os << level_to_string (l);
return os; return os;
} }
@ -98,7 +101,8 @@ util::operator<< (std::ostream& os, level_t l) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static level_t static level_t
log_level (void) { log_level (void)
{
const char *env = getenv ("LOG_LEVEL"); const char *env = getenv ("LOG_LEVEL");
if (!env) if (!env)
return DEFAULT; return DEFAULT;
@ -114,12 +118,15 @@ log_level (void) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void
util::log (level_t l, const std::string &format) util::log (level_t l, const std::string &format)
{ detail::log (l, boost::format (format)); } {
detail::log (l, boost::format (format));
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void void
util::detail::log (level_t level, boost::format &&format) { util::detail::log (level_t level, boost::format &&format)
{
static const level_t LOG_LEVEL = log_level (); static const level_t LOG_LEVEL = log_level ();
if (level > LOG_LEVEL) if (level > LOG_LEVEL)
return; return;