diff --git a/log.cpp b/log.cpp index d01c460c..6a6b77b0 100644 --- a/log.cpp +++ b/log.cpp @@ -25,11 +25,14 @@ #include #include +#include +#include #include using namespace util; using std::string; +using std::map; void 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 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& util::operator<< (std::ostream& os, level_t l) { os << level_to_string (l);