From 2515db00f2b64c69c05f8f9eee1d1a678633b419 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 26 Apr 2012 18:22:05 +1000 Subject: [PATCH] Add string to level conversions --- log.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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);