log/level: expose enumeration reflection setup
This commit is contained in:
parent
62660f1146
commit
aa4a79aae2
@ -9,6 +9,7 @@
|
||||
#include "level.hpp"
|
||||
|
||||
#include "../debug/assert.hpp"
|
||||
#include "../parse/enum.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
@ -34,25 +35,11 @@ cruft::log::to_level (std::string_view name)
|
||||
::toupper
|
||||
);
|
||||
|
||||
static constexpr struct {
|
||||
char const *name;
|
||||
cruft::log::level_t value;
|
||||
} VALUES[] = {
|
||||
{ "EMERGENCY", cruft::log::EMERGENCY },
|
||||
{ "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;
|
||||
#define ITEM(NAME) \
|
||||
if (!strcmp (#NAME, upper.data ())) \
|
||||
return NAME;
|
||||
MAP_LEVEL_T(ITEM)
|
||||
#undef ITEM
|
||||
|
||||
throw std::invalid_argument (std::string (name));
|
||||
}
|
||||
@ -63,15 +50,13 @@ const std::string&
|
||||
cruft::log::to_string (level_t l)
|
||||
{
|
||||
switch (l) {
|
||||
#define CASE(L) \
|
||||
#define CASE(L) \
|
||||
case cruft::log::L: { \
|
||||
static const std::string STR = #L; \
|
||||
return STR; \
|
||||
}
|
||||
|
||||
MAP_LEVEL_T(CASE)
|
||||
|
||||
#undef CASE
|
||||
MAP_LEVEL_T(CASE)
|
||||
#undef CASE
|
||||
}
|
||||
|
||||
unreachable ();
|
||||
@ -142,28 +127,25 @@ cruft::log::log_level (void)
|
||||
std::size_t
|
||||
cruft::log::level_width (void)
|
||||
{
|
||||
static constexpr
|
||||
cruft::log::level_t
|
||||
ALL_LEVELS[] = {
|
||||
cruft::log::EMERGENCY,
|
||||
cruft::log::ALERT,
|
||||
cruft::log::CRITICAL,
|
||||
cruft::log::ERROR,
|
||||
cruft::log::WARN,
|
||||
cruft::log::NOTICE,
|
||||
cruft::log::INFO,
|
||||
cruft::log::DEBUG,
|
||||
};
|
||||
return cruft::max (
|
||||
#define ITEM(NAME) strlen(#NAME),
|
||||
MAP_LEVEL_T(ITEM)
|
||||
#undef ITEM
|
||||
|
||||
|
||||
static std::size_t width = [] {
|
||||
size_t hi = 0;
|
||||
|
||||
for (auto i: ALL_LEVELS)
|
||||
hi = cruft::max (to_string (i).size (), hi);
|
||||
|
||||
return hi;
|
||||
} ();
|
||||
|
||||
return width;
|
||||
0u
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
cruft::parse::enumeration::cookie
|
||||
cruft::log::setup_level_reflection (void)
|
||||
{
|
||||
#define ITEM(NAME) { #NAME, NAME },
|
||||
return parse::enumeration::setup (
|
||||
std::map<std::string_view, level_t> {
|
||||
MAP_LEVEL_T(ITEM)
|
||||
}
|
||||
);
|
||||
#undef ITEM
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cruft/util/preprocessor.hpp>
|
||||
#include <cruft/util/parse/fwd.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
@ -34,6 +35,8 @@ namespace cruft::log {
|
||||
DEBUG /** debug-level messages */
|
||||
};
|
||||
|
||||
parse::enumeration::cookie setup_level_reflection (void);
|
||||
|
||||
#define MAP_LEVEL_T(F) \
|
||||
MAP0(F, \
|
||||
EMERGENCY, \
|
||||
|
Loading…
Reference in New Issue
Block a user