Add string to level conversions

This commit is contained in:
Danny Robson 2012-04-26 18:22:05 +10:00
parent 9f6566812b
commit 2515db00f2

26
log.cpp
View File

@ -25,11 +25,14 @@
#include <ctime> #include <ctime>
#include <cstring> #include <cstring>
#include <map>
#include <string>
#include <boost/format.hpp> #include <boost/format.hpp>
using namespace util; using namespace util;
using std::string; using std::string;
using std::map;
void void
check_level (level_t l) check_level (level_t l)
@ -55,6 +58,29 @@ level_to_string (level_t l) {
} }
level_t
string_to_level (const string &name) {
static const map <string, level_t> NAME_LEVELS = {
{ "EMERGENCY", EMERGENCY },
{ "ALERT", ALERT },
{ "CRITICAL", CRITICAL },
{ "ERROR", ERROR },
{ "WARNING", WARNING },
{ "WARN", WARNING },
{ "NOTICE", NOTICE },
{ "INFO", INFO },
{ "INFORMATIONAL", INFORMATIONAL },
{ "DEBUG", DEBUG }
};
auto pos = NAME_LEVELS.find (name);
if (pos == NAME_LEVELS.end ())
throw std::range_error (name);
return pos->second;
}
std::ostream& std::ostream&
util::operator<< (std::ostream& os, level_t l) { util::operator<< (std::ostream& os, level_t l) {
os << level_to_string (l); os << level_to_string (l);