log/level: expose enumeration reflection setup

This commit is contained in:
Danny Robson 2020-04-23 07:58:02 +10:00
parent 62660f1146
commit aa4a79aae2
2 changed files with 32 additions and 47 deletions

View File

@ -9,6 +9,7 @@
#include "level.hpp" #include "level.hpp"
#include "../debug/assert.hpp" #include "../debug/assert.hpp"
#include "../parse/enum.hpp"
#include <iostream> #include <iostream>
@ -34,25 +35,11 @@ cruft::log::to_level (std::string_view name)
::toupper ::toupper
); );
static constexpr struct { #define ITEM(NAME) \
char const *name; if (!strcmp (#NAME, upper.data ())) \
cruft::log::level_t value; return NAME;
} VALUES[] = { MAP_LEVEL_T(ITEM)
{ "EMERGENCY", cruft::log::EMERGENCY }, #undef ITEM
{ "ALERT", cruft::log::ALERT },
{ "CRITICAL", cruft::log::CRITICAL },
{ "ERROR", cruft::log::ERROR },
{ "WARN", cruft::log::WARN },
{ "WARNING", cruft::log::WARN },
{ "NOTICE", cruft::log::NOTICE },
{ "INFO", cruft::log::INFO },
{ "INFORMATIONAL", cruft::log::INFO },
{ "DEBUG", cruft::log::DEBUG }
};
for (auto const [key, val]: VALUES)
if (!strcmp (upper.data (), key))
return val;
throw std::invalid_argument (std::string (name)); throw std::invalid_argument (std::string (name));
} }
@ -63,15 +50,13 @@ const std::string&
cruft::log::to_string (level_t l) cruft::log::to_string (level_t l)
{ {
switch (l) { switch (l) {
#define CASE(L) \ #define CASE(L) \
case cruft::log::L: { \ case cruft::log::L: { \
static const std::string STR = #L; \ static const std::string STR = #L; \
return STR; \ return STR; \
} }
MAP_LEVEL_T(CASE)
MAP_LEVEL_T(CASE) #undef CASE
#undef CASE
} }
unreachable (); unreachable ();
@ -142,28 +127,25 @@ cruft::log::log_level (void)
std::size_t std::size_t
cruft::log::level_width (void) cruft::log::level_width (void)
{ {
static constexpr return cruft::max (
cruft::log::level_t #define ITEM(NAME) strlen(#NAME),
ALL_LEVELS[] = { MAP_LEVEL_T(ITEM)
cruft::log::EMERGENCY, #undef ITEM
cruft::log::ALERT,
cruft::log::CRITICAL,
cruft::log::ERROR,
cruft::log::WARN,
cruft::log::NOTICE,
cruft::log::INFO,
cruft::log::DEBUG,
};
0u
static std::size_t width = [] { );
size_t hi = 0; }
for (auto i: ALL_LEVELS)
hi = cruft::max (to_string (i).size (), hi); ///////////////////////////////////////////////////////////////////////////////
cruft::parse::enumeration::cookie
return hi; cruft::log::setup_level_reflection (void)
} (); {
#define ITEM(NAME) { #NAME, NAME },
return width; return parse::enumeration::setup (
std::map<std::string_view, level_t> {
MAP_LEVEL_T(ITEM)
}
);
#undef ITEM
} }

View File

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <cruft/util/preprocessor.hpp> #include <cruft/util/preprocessor.hpp>
#include <cruft/util/parse/fwd.hpp>
#include <string> #include <string>
@ -34,6 +35,8 @@ namespace cruft::log {
DEBUG /** debug-level messages */ DEBUG /** debug-level messages */
}; };
parse::enumeration::cookie setup_level_reflection (void);
#define MAP_LEVEL_T(F) \ #define MAP_LEVEL_T(F) \
MAP0(F, \ MAP0(F, \
EMERGENCY, \ EMERGENCY, \