2012-04-24 17:38:35 +10:00
|
|
|
/*
|
|
|
|
* This file is part of libgim.
|
|
|
|
*
|
|
|
|
* libgim is free software: you can redistribute it and/or modify it under the
|
|
|
|
* terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Copyright 2012 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "log.hpp"
|
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
#include "types.hpp"
|
|
|
|
|
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
|
|
|
|
|
|
|
void
|
|
|
|
check_level (level_t l)
|
2012-05-11 12:34:21 +10:00
|
|
|
{ CHECK (l >= 0 && l < NUM_LEVELS); }
|
2012-04-24 17:38:35 +10:00
|
|
|
|
|
|
|
|
|
|
|
const string&
|
|
|
|
level_to_string (level_t l) {
|
|
|
|
check_level (l);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-26 18:22:05 +10:00
|
|
|
level_t
|
|
|
|
string_to_level (const string &name) {
|
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
auto pos = NAME_LEVELS.find (name);
|
|
|
|
if (pos == NAME_LEVELS.end ())
|
|
|
|
throw std::range_error (name);
|
|
|
|
|
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
std::ostream&
|
|
|
|
util::operator<< (std::ostream& os, level_t l) {
|
|
|
|
os << level_to_string (l);
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
util::log (level_t l, const std::string &format)
|
|
|
|
{ detail::log (l, boost::format (format)); }
|
|
|
|
|
|
|
|
|
2012-04-26 18:22:24 +10:00
|
|
|
static level_t
|
|
|
|
log_level (void) {
|
|
|
|
const char *env = getenv ("LOG_LEVEL");
|
|
|
|
if (!env)
|
|
|
|
return DEFAULT;
|
|
|
|
|
|
|
|
try {
|
|
|
|
return string_to_level (env);
|
|
|
|
} catch (...) {
|
|
|
|
return DEFAULT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 17:38:35 +10:00
|
|
|
void
|
|
|
|
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] ");
|
|
|
|
|
2012-05-03 15:55:09 +10:00
|
|
|
char time_string[strlen("YYYY-mm-dd HHMMhSS") + 1];
|
2012-04-24 17:38:35 +10:00
|
|
|
time_t unix_time = time (nullptr);
|
|
|
|
if (0 == strftime (time_string,
|
|
|
|
elems (time_string),
|
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
|
|
|
}
|