libcruft-util/log.cpp

260 lines
6.5 KiB
C++
Raw Normal View History

2012-04-24 17:38:35 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2012-04-24 17:38:35 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2012-04-24 17:38:35 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2012-04-24 17:38:35 +10:00
*
2016-01-20 15:37:52 +11:00
* Copyright 2012-2016 Danny Robson <danny@nerdcruft.net>
2012-04-24 17:38:35 +10:00
*/
#include "log.hpp"
#include "debug.hpp"
2016-03-21 14:21:14 +11:00
#include "term.hpp"
2015-07-23 17:37:43 +10:00
#include "time.hpp"
2016-03-21 14:21:14 +11:00
#include "types.hpp"
#include "cast.hpp"
2012-04-24 17:38:35 +10:00
#include <array>
2012-04-24 17:38:35 +10:00
#include <ctime>
#include <cstring>
2012-04-26 18:22:05 +10:00
#include <map>
#include <string>
2016-03-21 14:21:14 +11:00
#include <iomanip>
///////////////////////////////////////////////////////////////////////////////
static
const util::level_t
ALL_LEVELS[] = {
util::EMERGENCY,
util::ALERT,
util::CRITICAL,
util::ERROR,
util::WARN,
util::WARN,
util::NOTICE,
util::INFO,
util::INFO,
util::DEBUG,
};
2012-04-24 17:38:35 +10:00
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////////
/// convert a string representation of a log-level into an enumeration value.
///
/// conversion is case insensitive
/// throws std::range_error if unable to convert
2016-01-20 15:44:01 +11:00
static util::level_t
to_level (std::string name)
2016-01-20 15:36:32 +11:00
{
2016-01-20 15:44:01 +11:00
static const std::map<std::string, util::level_t> NAME_LEVELS = {
{ "EMERGENCY", util::EMERGENCY },
{ "ALERT", util::ALERT },
{ "CRITICAL", util::CRITICAL },
{ "ERROR", util::ERROR },
{ "WARN", util::WARN },
{ "WARNING", util::WARN },
{ "NOTICE", util::NOTICE },
{ "INFO", util::INFO },
{ "INFORMATIONAL", util::INFO },
{ "DEBUG", util::DEBUG }
2012-04-26 18:22:05 +10:00
};
2014-09-11 15:33:46 +10:00
std::transform (name.cbegin (), name.cend (), name.begin (), ::toupper);
2012-04-26 18:22:05 +10:00
auto pos = NAME_LEVELS.find (name);
if (pos == NAME_LEVELS.end ())
throw std::range_error (name);
return pos->second;
}
2016-03-21 14:21:14 +11:00
///////////////////////////////////////////////////////////////////////////////
const std::string&
util::to_string (level_t l)
{
switch (l) {
#define CASE(L) \
case util::L: { \
static const std::string STR = #L; \
return STR; \
}
MAP_LEVEL_T(CASE)
#undef CASE
}
unreachable ();
}
2015-07-23 17:37:02 +10:00
//-----------------------------------------------------------------------------
2012-04-24 17:38:35 +10:00
std::ostream&
2016-01-20 15:44:01 +11:00
util::operator<< (std::ostream& os, util::level_t l)
2016-01-20 15:36:32 +11:00
{
2016-03-21 14:21:14 +11:00
return os << to_string (l);
2012-04-24 17:38:35 +10:00
}
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////////
2016-01-20 15:44:01 +11:00
static util::level_t
2016-01-20 15:36:32 +11:00
log_level (void)
{
const char *env = getenv ("LOG_LEVEL");
if (!env)
return util::DEFAULT_LOG_LEVEL;
try {
return to_level (env);
} catch (...) {
return util::DEFAULT_LOG_LEVEL;
}
}
//-----------------------------------------------------------------------------
static bool
needs_break (util::level_t level)
{
static util::level_t break_level;
static bool has_level = [&] (void) {
const char *env = getenv ("BREAK_LEVEL");
if (!env)
return false;
try {
break_level = to_level (env);
return true;
} catch (...) {
return false;
}
} ();
return has_level && level <= break_level;
}
2016-03-21 14:21:14 +11:00
//-----------------------------------------------------------------------------
static
util::term::csi::graphics
level_colour (util::level_t level)
{
using util::term::csi::graphics;
switch (level) {
case util::EMERGENCY:
case util::ALERT:
case util::CRITICAL:
case util::ERROR:
return graphics (graphics::FOREGROUND, graphics::RED);
case util::WARNING:
return graphics (graphics::FOREGROUND, graphics::YELLOW);
case util::NOTICE:
case util::INFORMATIONAL:
return graphics (graphics::FOREGROUND, graphics::GREEN);
case util::DEBUG:
return graphics (graphics::FOREGROUND, graphics::WHITE);
}
unreachable ();
}
//-----------------------------------------------------------------------------
static
size_t
level_width (void)
{
static size_t width = [] {
size_t hi = 0;
for (auto i: ALL_LEVELS)
hi = util::max (to_string (i).size (), hi);
return hi;
} ();
return width;
}
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////////
void
util::log (util::level_t level, const std::string &msg)
2016-01-20 15:36:32 +11:00
{
static const util::level_t LOG_LEVEL = log_level ();
if (level <= LOG_LEVEL) {
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
std::string time_string (time_len - 1, '\0');
time_t unix_time = time (nullptr);
if (0 == strftime (&time_string[0],
time_len,
"%Y-%m-%d %H%Mh%S",
localtime (&unix_time))) {
warn ("failed to log time");
return;
}
std::cerr << time_string << " ["
<< level_colour (level)
<< std::setw (trunc_cast<int> (level_width ()))
<< std::left
<< level
<< std::setw (0)
<< util::term::csi::graphics::RESET
<< "] " << msg << std::endl;
}
if (needs_break (level))
breakpoint ();
2012-04-24 17:38:35 +10:00
}
2013-03-05 21:38:51 +11:00
2015-07-23 17:37:02 +10:00
///////////////////////////////////////////////////////////////////////////////
util::scoped_logger::scoped_logger (util::level_t _level,
std::string _message):
2013-03-05 21:38:51 +11:00
m_level (_level),
2015-07-23 17:37:02 +10:00
m_message (std::move (_message))
2013-03-05 21:38:51 +11:00
{ ; }
2015-07-23 17:37:02 +10:00
//-----------------------------------------------------------------------------
2016-01-20 15:44:01 +11:00
util::scoped_logger::~scoped_logger ()
2015-07-23 17:37:02 +10:00
{
2013-03-05 21:38:51 +11:00
log (m_level, m_message);
}
2015-07-23 17:37:43 +10:00
///////////////////////////////////////////////////////////////////////////////
2016-01-20 15:44:01 +11:00
util::scoped_timer::scoped_timer (util::level_t _level,
std::string _message):
2015-07-23 17:37:43 +10:00
m_level (_level),
m_message (std::move (_message)),
2015-07-23 17:37:43 +10:00
m_start (util::nanoseconds ())
{ ; }
//-----------------------------------------------------------------------------
2016-01-20 15:44:01 +11:00
util::scoped_timer::~scoped_timer ()
2015-07-23 17:37:43 +10:00
{
auto finish = util::nanoseconds ();
auto duration = finish - m_start;
log (m_level, "%fs, %s", float (duration) / 1'000'000'000.f, m_message);
2015-07-23 17:37:43 +10:00
}