log: move symbols into the 'log' namespace

This commit is contained in:
Danny Robson 2019-10-10 12:32:03 +11:00
parent 9cd6a60134
commit 8f7d4adef7
2 changed files with 79 additions and 75 deletions

118
log.cpp
View File

@ -21,17 +21,17 @@
#include <string> #include <string>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static static constexpr
const cruft::level_t cruft::log::level_t
ALL_LEVELS[] = { ALL_LEVELS[] = {
cruft::EMERGENCY, cruft::log::EMERGENCY,
cruft::ALERT, cruft::log::ALERT,
cruft::CRITICAL, cruft::log::CRITICAL,
cruft::ERROR, cruft::log::ERROR,
cruft::WARN, cruft::log::WARN,
cruft::NOTICE, cruft::log::NOTICE,
cruft::INFO, cruft::log::INFO,
cruft::DEBUG, cruft::log::DEBUG,
}; };
@ -40,23 +40,23 @@ ALL_LEVELS[] = {
/// ///
/// conversion is case insensitive /// conversion is case insensitive
/// throws std::range_error if unable to convert /// throws std::range_error if unable to convert
static cruft::level_t static cruft::log::level_t
to_level (std::string name) to_level (std::string name)
{ {
if (std::empty (name)) if (std::empty (name))
return cruft::EMERGENCY; return cruft::log::EMERGENCY;
static const std::map<std::string, cruft::level_t> NAME_LEVELS = { static const std::map<std::string, cruft::log::level_t> NAME_LEVELS = {
{ "EMERGENCY", cruft::EMERGENCY }, { "EMERGENCY", cruft::log::EMERGENCY },
{ "ALERT", cruft::ALERT }, { "ALERT", cruft::log::ALERT },
{ "CRITICAL", cruft::CRITICAL }, { "CRITICAL", cruft::log::CRITICAL },
{ "ERROR", cruft::ERROR }, { "ERROR", cruft::log::ERROR },
{ "WARN", cruft::WARN }, { "WARN", cruft::log::WARN },
{ "WARNING", cruft::WARN }, { "WARNING", cruft::log::WARN },
{ "NOTICE", cruft::NOTICE }, { "NOTICE", cruft::log::NOTICE },
{ "INFO", cruft::INFO }, { "INFO", cruft::log::INFO },
{ "INFORMATIONAL", cruft::INFO }, { "INFORMATIONAL", cruft::log::INFO },
{ "DEBUG", cruft::DEBUG } { "DEBUG", cruft::log::DEBUG }
}; };
std::transform (name.cbegin (), name.cend (), name.begin (), ::toupper); std::transform (name.cbegin (), name.cend (), name.begin (), ::toupper);
@ -71,11 +71,11 @@ to_level (std::string name)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
const std::string& const std::string&
cruft::to_string (level_t l) cruft::log::to_string (level_t l)
{ {
switch (l) { switch (l) {
#define CASE(L) \ #define CASE(L) \
case cruft::L: { \ case cruft::log::L: { \
static const std::string STR = #L; \ static const std::string STR = #L; \
return STR; \ return STR; \
} }
@ -91,7 +91,7 @@ cruft::to_string (level_t l)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
std::ostream& std::ostream&
cruft::operator<< (std::ostream& os, cruft::level_t l) cruft::log::operator<< (std::ostream& os, level_t l)
{ {
return os << to_string (l); return os << to_string (l);
} }
@ -103,19 +103,18 @@ cruft::operator<< (std::ostream& os, cruft::level_t l)
// //
// Note that the LOG macros _cannot_ be used from within this function as it // Note that the LOG macros _cannot_ be used from within this function as it
// will likely result in infinite recursion. // will likely result in infinite recursion.
static static cruft::log::level_t
cruft::level_t
initial_log_level (void) initial_log_level (void)
{ {
const char *env = getenv ("LOG_LEVEL"); const char *env = getenv ("LOG_LEVEL");
if (!env) if (!env)
return cruft::DEFAULT_LOG_LEVEL; return cruft::log::DEFAULT_LOG_LEVEL;
try { try {
return to_level (env); return to_level (env);
} catch (...) { } catch (...) {
std::clog << "Invalid environment LOG_LEVEL: '" << env << "'\n"; std::clog << "Invalid environment LOG_LEVEL: '" << env << "'\n";
return cruft::DEFAULT_LOG_LEVEL; return cruft::log::DEFAULT_LOG_LEVEL;
} }
} }
@ -125,11 +124,11 @@ initial_log_level (void)
// the most verbose option just in case we've made a mistake elsewhere. // the most verbose option just in case we've made a mistake elsewhere.
static bool s_log_level_done; static bool s_log_level_done;
static cruft::level_t s_log_level_value; static cruft::log::level_t s_log_level_value;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
cruft::level_t cruft::log::level_t
cruft::log_level (level_t _level) cruft::log::log_level (level_t _level)
{ {
s_log_level_value = _level; s_log_level_value = _level;
s_log_level_done = true; s_log_level_done = true;
@ -138,8 +137,8 @@ cruft::log_level (level_t _level)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
cruft::level_t cruft::log::level_t
cruft::log_level (void) cruft::log::log_level (void)
{ {
if (!s_log_level_done) { if (!s_log_level_done) {
s_log_level_value = initial_log_level (); s_log_level_value = initial_log_level ();
@ -152,9 +151,9 @@ cruft::log_level (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static bool static bool
needs_break (cruft::level_t level) needs_break (cruft::log::level_t level)
{ {
static cruft::level_t break_level; static cruft::log::level_t break_level;
static bool has_level = [&] (void) { static bool has_level = [&] (void) {
const char *env = getenv ("BREAK_LEVEL"); const char *env = getenv ("BREAK_LEVEL");
if (!env) if (!env)
@ -175,25 +174,25 @@ needs_break (cruft::level_t level)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static static
cruft::term::csi::graphics cruft::term::csi::graphics
level_colour (cruft::level_t level) level_colour (cruft::log::level_t level)
{ {
using cruft::term::csi::graphics; using cruft::term::csi::graphics;
switch (level) { switch (level) {
case cruft::EMERGENCY: case cruft::log::EMERGENCY:
case cruft::ALERT: case cruft::log::ALERT:
case cruft::CRITICAL: case cruft::log::CRITICAL:
case cruft::ERROR: case cruft::log::ERROR:
return graphics (graphics::FOREGROUND, graphics::RED); return graphics (graphics::FOREGROUND, graphics::RED);
case cruft::WARNING: case cruft::log::WARNING:
return graphics (graphics::FOREGROUND, graphics::YELLOW); return graphics (graphics::FOREGROUND, graphics::YELLOW);
case cruft::NOTICE: case cruft::log::NOTICE:
case cruft::INFORMATIONAL: case cruft::log::INFORMATIONAL:
return graphics (graphics::FOREGROUND, graphics::GREEN); return graphics (graphics::FOREGROUND, graphics::GREEN);
case cruft::DEBUG: case cruft::log::DEBUG:
return graphics (graphics::FOREGROUND, graphics::WHITE); return graphics (graphics::FOREGROUND, graphics::WHITE);
} }
@ -221,7 +220,7 @@ level_width (void)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void
cruft::log (cruft::level_t level, const std::string &msg) cruft::log::write (level_t level, const std::string &msg)
{ {
if (level <= log_level ()) { if (level <= log_level ()) {
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1; static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
@ -252,23 +251,27 @@ cruft::log (cruft::level_t level, const std::string &msg)
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
cruft::scoped_logger::scoped_logger (cruft::level_t _level, cruft::log::scoped_logger::scoped_logger (
std::string _message): level_t _level,
std::string _message
):
m_level (_level), m_level (_level),
m_message (std::move (_message)) m_message (std::move (_message))
{ ; } { ; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
cruft::scoped_logger::~scoped_logger () cruft::log::scoped_logger::~scoped_logger ()
{ {
log (m_level, m_message); write (m_level, m_message);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
cruft::scoped_timer::scoped_timer (cruft::level_t _level, cruft::log::scoped_timer::scoped_timer (
std::string _message): cruft::log::level_t _level,
std::string _message
):
m_level (_level), m_level (_level),
m_message (std::move (_message)), m_message (std::move (_message)),
m_start (cruft::nanoseconds ()) m_start (cruft::nanoseconds ())
@ -276,10 +279,15 @@ cruft::scoped_timer::scoped_timer (cruft::level_t _level,
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
cruft::scoped_timer::~scoped_timer () cruft::log::scoped_timer::~scoped_timer ()
{ {
auto finish = cruft::nanoseconds (); auto finish = cruft::nanoseconds ();
auto duration = finish - m_start; auto duration = finish - m_start;
log (m_level, "%fs, %s", float (duration) / 1'000'000'000.f, m_message); write (
m_level,
"%fs, %s",
float (duration) / 1'000'000'000.f,
m_message
);
} }

34
log.hpp
View File

@ -3,11 +3,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2012-2018 Danny Robson <danny@nerdcruft.net> * Copyright 2012-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_LOG_HPP #pragma once
#define CRUFT_UTIL_LOG_HPP
#include "format.hpp" #include "format.hpp"
@ -21,7 +20,7 @@
#undef ERROR #undef ERROR
#endif #endif
namespace cruft { namespace cruft::log {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// rfc5424 log levels. It is assumed they are contiguous to simplify array // rfc5424 log levels. It is assumed they are contiguous to simplify array
// indexing in logging code. // indexing in logging code.
@ -68,14 +67,14 @@ namespace cruft {
level_t log_level (level_t); level_t log_level (level_t);
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
void log (level_t, const std::string &msg); void write (level_t, const std::string &msg);
template <typename ...Args, size_t N> template <typename ...Args, size_t N>
void void
log (level_t l, const char (&fmt)[N], Args &&...args) write (level_t l, const char (&fmt)[N], Args &&...args)
{ {
log ( write (
l, l,
to_string ( to_string (
format::printf (fmt) ( format::printf (fmt) (
@ -93,16 +92,16 @@ namespace cruft {
// LOG_DEBUG is treated similarly to assert; if NDEBUG is defined then we // LOG_DEBUG is treated similarly to assert; if NDEBUG is defined then we
// compile out the statement so as to gain a little runtime efficiency // compile out the statement so as to gain a little runtime efficiency
// speed. // speed.
#define LOG_EMERGENCY(...) do { cruft::log(cruft::EMERGENCY, ##__VA_ARGS__); } while (0) #define LOG_EMERGENCY(...) do { cruft::log::write (cruft::log::EMERGENCY, ##__VA_ARGS__); } while (0)
#define LOG_ALERT(...) do { cruft::log(cruft::ALERT, ##__VA_ARGS__); } while (0) #define LOG_ALERT(...) do { cruft::log::write (cruft::log::ALERT, ##__VA_ARGS__); } while (0)
#define LOG_CRITICAL(...) do { cruft::log(cruft::CRITICAL, ##__VA_ARGS__); } while (0) #define LOG_CRITICAL(...) do { cruft::log::write (cruft::log::CRITICAL, ##__VA_ARGS__); } while (0)
#define LOG_ERROR(...) do { cruft::log(cruft::ERROR, ##__VA_ARGS__); } while (0) #define LOG_ERROR(...) do { cruft::log::write (cruft::log::ERROR, ##__VA_ARGS__); } while (0)
#define LOG_WARNING(...) do { cruft::log(cruft::WARNING, ##__VA_ARGS__); } while (0) #define LOG_WARNING(...) do { cruft::log::write (cruft::log::WARNING, ##__VA_ARGS__); } while (0)
#define LOG_WARN(...) do { cruft::log(cruft::WARN, ##__VA_ARGS__); } while (0) #define LOG_WARN(...) do { cruft::log::write (cruft::log::WARN, ##__VA_ARGS__); } while (0)
#define LOG_NOTICE(...) do { cruft::log(cruft::NOTICE, ##__VA_ARGS__); } while (0) #define LOG_NOTICE(...) do { cruft::log::write (cruft::log::NOTICE, ##__VA_ARGS__); } while (0)
#define LOG_INFO(...) do { cruft::log(cruft::INFO, ##__VA_ARGS__); } while (0) #define LOG_INFO(...) do { cruft::log::write (cruft::log::INFO, ##__VA_ARGS__); } while (0)
#if !defined(NDEBUG) #if !defined(NDEBUG)
#define LOG_DEBUG(...) do { cruft::log(cruft::DEBUG, ##__VA_ARGS__); } while (0) #define LOG_DEBUG(...) do { cruft::log::write (cruft::log::DEBUG, ##__VA_ARGS__); } while (0)
#else #else
#define LOG_DEBUG(...) do { ; } while (0) #define LOG_DEBUG(...) do { ; } while (0)
#endif #endif
@ -144,6 +143,3 @@ namespace cruft {
uint64_t m_start; uint64_t m_start;
}; };
} }
#endif