log: move symbols into the 'log' namespace
This commit is contained in:
parent
9cd6a60134
commit
8f7d4adef7
118
log.cpp
118
log.cpp
@ -21,17 +21,17 @@
|
||||
#include <string>
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
static
|
||||
const cruft::level_t
|
||||
static constexpr
|
||||
cruft::log::level_t
|
||||
ALL_LEVELS[] = {
|
||||
cruft::EMERGENCY,
|
||||
cruft::ALERT,
|
||||
cruft::CRITICAL,
|
||||
cruft::ERROR,
|
||||
cruft::WARN,
|
||||
cruft::NOTICE,
|
||||
cruft::INFO,
|
||||
cruft::DEBUG,
|
||||
cruft::log::EMERGENCY,
|
||||
cruft::log::ALERT,
|
||||
cruft::log::CRITICAL,
|
||||
cruft::log::ERROR,
|
||||
cruft::log::WARN,
|
||||
cruft::log::NOTICE,
|
||||
cruft::log::INFO,
|
||||
cruft::log::DEBUG,
|
||||
};
|
||||
|
||||
|
||||
@ -40,23 +40,23 @@ ALL_LEVELS[] = {
|
||||
///
|
||||
/// conversion is case insensitive
|
||||
/// throws std::range_error if unable to convert
|
||||
static cruft::level_t
|
||||
static cruft::log::level_t
|
||||
to_level (std::string name)
|
||||
{
|
||||
if (std::empty (name))
|
||||
return cruft::EMERGENCY;
|
||||
return cruft::log::EMERGENCY;
|
||||
|
||||
static const std::map<std::string, cruft::level_t> NAME_LEVELS = {
|
||||
{ "EMERGENCY", cruft::EMERGENCY },
|
||||
{ "ALERT", cruft::ALERT },
|
||||
{ "CRITICAL", cruft::CRITICAL },
|
||||
{ "ERROR", cruft::ERROR },
|
||||
{ "WARN", cruft::WARN },
|
||||
{ "WARNING", cruft::WARN },
|
||||
{ "NOTICE", cruft::NOTICE },
|
||||
{ "INFO", cruft::INFO },
|
||||
{ "INFORMATIONAL", cruft::INFO },
|
||||
{ "DEBUG", cruft::DEBUG }
|
||||
static const std::map<std::string, cruft::log::level_t> NAME_LEVELS = {
|
||||
{ "EMERGENCY", cruft::log::EMERGENCY },
|
||||
{ "ALERT", cruft::log::ALERT },
|
||||
{ "CRITICAL", cruft::log::CRITICAL },
|
||||
{ "ERROR", cruft::log::ERROR },
|
||||
{ "WARN", cruft::log::WARN },
|
||||
{ "WARNING", cruft::log::WARN },
|
||||
{ "NOTICE", cruft::log::NOTICE },
|
||||
{ "INFO", cruft::log::INFO },
|
||||
{ "INFORMATIONAL", cruft::log::INFO },
|
||||
{ "DEBUG", cruft::log::DEBUG }
|
||||
};
|
||||
|
||||
std::transform (name.cbegin (), name.cend (), name.begin (), ::toupper);
|
||||
@ -71,11 +71,11 @@ to_level (std::string name)
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
const std::string&
|
||||
cruft::to_string (level_t l)
|
||||
cruft::log::to_string (level_t l)
|
||||
{
|
||||
switch (l) {
|
||||
#define CASE(L) \
|
||||
case cruft::L: { \
|
||||
case cruft::log::L: { \
|
||||
static const std::string STR = #L; \
|
||||
return STR; \
|
||||
}
|
||||
@ -91,7 +91,7 @@ cruft::to_string (level_t l)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
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);
|
||||
}
|
||||
@ -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
|
||||
// will likely result in infinite recursion.
|
||||
static
|
||||
cruft::level_t
|
||||
static cruft::log::level_t
|
||||
initial_log_level (void)
|
||||
{
|
||||
const char *env = getenv ("LOG_LEVEL");
|
||||
if (!env)
|
||||
return cruft::DEFAULT_LOG_LEVEL;
|
||||
return cruft::log::DEFAULT_LOG_LEVEL;
|
||||
|
||||
try {
|
||||
return to_level (env);
|
||||
} catch (...) {
|
||||
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.
|
||||
|
||||
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 (level_t _level)
|
||||
cruft::log::level_t
|
||||
cruft::log::log_level (level_t _level)
|
||||
{
|
||||
s_log_level_value = _level;
|
||||
s_log_level_done = true;
|
||||
@ -138,8 +137,8 @@ cruft::log_level (level_t _level)
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
cruft::level_t
|
||||
cruft::log_level (void)
|
||||
cruft::log::level_t
|
||||
cruft::log::log_level (void)
|
||||
{
|
||||
if (!s_log_level_done) {
|
||||
s_log_level_value = initial_log_level ();
|
||||
@ -152,9 +151,9 @@ cruft::log_level (void)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
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) {
|
||||
const char *env = getenv ("BREAK_LEVEL");
|
||||
if (!env)
|
||||
@ -175,25 +174,25 @@ needs_break (cruft::level_t level)
|
||||
//-----------------------------------------------------------------------------
|
||||
static
|
||||
cruft::term::csi::graphics
|
||||
level_colour (cruft::level_t level)
|
||||
level_colour (cruft::log::level_t level)
|
||||
{
|
||||
using cruft::term::csi::graphics;
|
||||
|
||||
switch (level) {
|
||||
case cruft::EMERGENCY:
|
||||
case cruft::ALERT:
|
||||
case cruft::CRITICAL:
|
||||
case cruft::ERROR:
|
||||
case cruft::log::EMERGENCY:
|
||||
case cruft::log::ALERT:
|
||||
case cruft::log::CRITICAL:
|
||||
case cruft::log::ERROR:
|
||||
return graphics (graphics::FOREGROUND, graphics::RED);
|
||||
|
||||
case cruft::WARNING:
|
||||
case cruft::log::WARNING:
|
||||
return graphics (graphics::FOREGROUND, graphics::YELLOW);
|
||||
|
||||
case cruft::NOTICE:
|
||||
case cruft::INFORMATIONAL:
|
||||
case cruft::log::NOTICE:
|
||||
case cruft::log::INFORMATIONAL:
|
||||
return graphics (graphics::FOREGROUND, graphics::GREEN);
|
||||
|
||||
case cruft::DEBUG:
|
||||
case cruft::log::DEBUG:
|
||||
return graphics (graphics::FOREGROUND, graphics::WHITE);
|
||||
}
|
||||
|
||||
@ -221,7 +220,7 @@ level_width (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 ()) {
|
||||
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,
|
||||
std::string _message):
|
||||
cruft::log::scoped_logger::scoped_logger (
|
||||
level_t _level,
|
||||
std::string _message
|
||||
):
|
||||
m_level (_level),
|
||||
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,
|
||||
std::string _message):
|
||||
cruft::log::scoped_timer::scoped_timer (
|
||||
cruft::log::level_t _level,
|
||||
std::string _message
|
||||
):
|
||||
m_level (_level),
|
||||
m_message (std::move (_message)),
|
||||
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 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
34
log.hpp
@ -3,11 +3,10 @@
|
||||
* 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/.
|
||||
*
|
||||
* Copyright 2012-2018 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2012-2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef CRUFT_UTIL_LOG_HPP
|
||||
#define CRUFT_UTIL_LOG_HPP
|
||||
#pragma once
|
||||
|
||||
#include "format.hpp"
|
||||
|
||||
@ -21,7 +20,7 @@
|
||||
#undef ERROR
|
||||
#endif
|
||||
|
||||
namespace cruft {
|
||||
namespace cruft::log {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// rfc5424 log levels. It is assumed they are contiguous to simplify array
|
||||
// indexing in logging code.
|
||||
@ -68,14 +67,14 @@ namespace cruft {
|
||||
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>
|
||||
void
|
||||
log (level_t l, const char (&fmt)[N], Args &&...args)
|
||||
write (level_t l, const char (&fmt)[N], Args &&...args)
|
||||
{
|
||||
log (
|
||||
write (
|
||||
l,
|
||||
to_string (
|
||||
format::printf (fmt) (
|
||||
@ -93,16 +92,16 @@ namespace cruft {
|
||||
// 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
|
||||
// speed.
|
||||
#define LOG_EMERGENCY(...) do { cruft::log(cruft::EMERGENCY, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_ALERT(...) do { cruft::log(cruft::ALERT, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_CRITICAL(...) do { cruft::log(cruft::CRITICAL, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_ERROR(...) do { cruft::log(cruft::ERROR, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_WARNING(...) do { cruft::log(cruft::WARNING, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_WARN(...) do { cruft::log(cruft::WARN, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_NOTICE(...) do { cruft::log(cruft::NOTICE, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_INFO(...) do { cruft::log(cruft::INFO, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_EMERGENCY(...) do { cruft::log::write (cruft::log::EMERGENCY, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_ALERT(...) do { cruft::log::write (cruft::log::ALERT, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_CRITICAL(...) do { cruft::log::write (cruft::log::CRITICAL, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_ERROR(...) do { cruft::log::write (cruft::log::ERROR, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_WARNING(...) do { cruft::log::write (cruft::log::WARNING, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_WARN(...) do { cruft::log::write (cruft::log::WARN, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_NOTICE(...) do { cruft::log::write (cruft::log::NOTICE, ##__VA_ARGS__); } while (0)
|
||||
#define LOG_INFO(...) do { cruft::log::write (cruft::log::INFO, ##__VA_ARGS__); } while (0)
|
||||
#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
|
||||
#define LOG_DEBUG(...) do { ; } while (0)
|
||||
#endif
|
||||
@ -144,6 +143,3 @@ namespace cruft {
|
||||
uint64_t m_start;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user