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
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "log.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "types.hpp"
|
2015-07-23 17:37:43 +10:00
|
|
|
#include "time.hpp"
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2012-05-08 16:37:38 +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>
|
2012-04-24 17:38:35 +10:00
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
|
|
using namespace util;
|
|
|
|
using std::string;
|
2012-04-26 18:22:05 +10:00
|
|
|
using std::map;
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-01-20 15:37:31 +11:00
|
|
|
static void
|
|
|
|
sanity (level_t l)
|
2015-01-21 23:37:15 +11:00
|
|
|
{
|
|
|
|
(void)l; // Consume even in release mode
|
|
|
|
CHECK (l >= 0 && l < NUM_LEVELS);
|
|
|
|
}
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-24 17:38:35 +10:00
|
|
|
const string&
|
2016-01-20 15:36:32 +11:00
|
|
|
level_to_string (level_t l)
|
|
|
|
{
|
2016-01-20 15:37:31 +11:00
|
|
|
sanity (l);
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2012-05-08 16:37:38 +10:00
|
|
|
static const std::array <std::string, NUM_LEVELS> LEVEL_NAMES ({{
|
2012-04-24 17:38:35 +10:00
|
|
|
"EMERGENCY",
|
|
|
|
"ALERT",
|
|
|
|
"CRITICAL",
|
|
|
|
"ERROR",
|
2012-04-26 18:21:23 +10:00
|
|
|
"WARN",
|
2012-04-24 17:38:35 +10:00
|
|
|
"NOTICE",
|
2012-04-26 18:21:23 +10:00
|
|
|
"INFO",
|
2012-04-24 17:38:35 +10:00
|
|
|
"DEBUG"
|
2012-05-08 16:37:38 +10:00
|
|
|
}});
|
2012-04-24 17:38:35 +10:00
|
|
|
|
2012-05-08 16:37:38 +10:00
|
|
|
return LEVEL_NAMES[l];
|
2012-04-24 17:38:35 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-09-11 15:33:46 +10:00
|
|
|
static level_t
|
2016-01-20 15:37:31 +11:00
|
|
|
to_level (string name)
|
2016-01-20 15:36:32 +11:00
|
|
|
{
|
2012-04-26 18:22:05 +10:00
|
|
|
static const map <string, level_t> NAME_LEVELS = {
|
|
|
|
{ "EMERGENCY", EMERGENCY },
|
|
|
|
{ "ALERT", ALERT },
|
|
|
|
{ "CRITICAL", CRITICAL },
|
|
|
|
{ "ERROR", ERROR },
|
2012-05-11 12:18:08 +10:00
|
|
|
{ "WARN", WARN },
|
|
|
|
{ "WARNING", WARN },
|
2012-04-26 18:22:05 +10:00
|
|
|
{ "NOTICE", NOTICE },
|
|
|
|
{ "INFO", INFO },
|
2012-05-11 12:18:08 +10:00
|
|
|
{ "INFORMATIONAL", INFO },
|
2012-04-26 18:22:05 +10:00
|
|
|
{ "DEBUG", DEBUG }
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-24 17:38:35 +10:00
|
|
|
std::ostream&
|
2016-01-20 15:36:32 +11:00
|
|
|
util::operator<< (std::ostream& os, level_t l)
|
|
|
|
{
|
2012-04-24 17:38:35 +10:00
|
|
|
os << level_to_string (l);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2012-04-26 18:22:24 +10:00
|
|
|
static level_t
|
2016-01-20 15:36:32 +11:00
|
|
|
log_level (void)
|
|
|
|
{
|
2012-04-26 18:22:24 +10:00
|
|
|
const char *env = getenv ("LOG_LEVEL");
|
|
|
|
if (!env)
|
|
|
|
return DEFAULT;
|
|
|
|
|
|
|
|
try {
|
2016-01-20 15:37:31 +11:00
|
|
|
return to_level (env);
|
2012-04-26 18:22:24 +10:00
|
|
|
} catch (...) {
|
|
|
|
return DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
|
|
|
util::log (level_t l, const std::string &format)
|
2016-01-20 15:36:32 +11:00
|
|
|
{
|
|
|
|
detail::log (l, boost::format (format));
|
|
|
|
}
|
2015-07-23 17:37:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2012-04-24 17:38:35 +10:00
|
|
|
void
|
2016-01-20 15:36:32 +11:00
|
|
|
util::detail::log (level_t level, boost::format &&format)
|
|
|
|
{
|
2012-04-26 18:22:24 +10:00
|
|
|
static const level_t LOG_LEVEL = log_level ();
|
|
|
|
if (level > LOG_LEVEL)
|
|
|
|
return;
|
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
static const boost::format LEVEL_FORMAT ("%s [%s] ");
|
2015-04-13 18:06:08 +10:00
|
|
|
|
2012-06-13 15:49:22 +10:00
|
|
|
static const size_t time_len = strlen("YYYY-mm-dd HHMMhSS") + 1;
|
2012-06-14 18:26:46 +10:00
|
|
|
std::string time_string (time_len - 1, '\0');
|
2012-04-24 17:38:35 +10:00
|
|
|
time_t unix_time = time (nullptr);
|
2012-06-13 15:49:22 +10:00
|
|
|
|
|
|
|
if (0 == strftime (&time_string[0],
|
|
|
|
time_len,
|
2012-05-03 15:55:09 +10:00
|
|
|
"%Y-%m-%d %H%Mh%S",
|
2012-04-24 17:38:35 +10:00
|
|
|
localtime (&unix_time))) {
|
|
|
|
unusual ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << boost::format (LEVEL_FORMAT)
|
|
|
|
% time_string
|
|
|
|
% level
|
|
|
|
<< format
|
2012-05-11 12:18:27 +10:00
|
|
|
<< std::endl;
|
2012-04-24 17:38:35 +10:00
|
|
|
}
|
2013-03-05 21:38:51 +11:00
|
|
|
|
|
|
|
|
2015-07-23 17:37:02 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
scoped_logger::scoped_logger (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
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
scoped_logger::~scoped_logger ()
|
|
|
|
{
|
2013-03-05 21:38:51 +11:00
|
|
|
log (m_level, m_message);
|
|
|
|
}
|
2015-07-23 17:37:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
scoped_timer::scoped_timer (level_t _level,
|
|
|
|
std::string &&_message):
|
|
|
|
m_level (_level),
|
|
|
|
m_message (_message),
|
|
|
|
m_start (util::nanoseconds ())
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
scoped_timer::~scoped_timer ()
|
|
|
|
{
|
|
|
|
auto finish = util::nanoseconds ();
|
|
|
|
auto duration = finish - m_start;
|
|
|
|
|
|
|
|
log (m_level, "%fs, %s", duration / 1'000'000'000.f, m_message);
|
|
|
|
}
|