lokinet/llarp/util/logging/loglevel.cpp

68 lines
1.7 KiB
C++
Raw Normal View History

2019-09-01 12:10:49 +00:00
#include <util/logging/loglevel.hpp>
2020-01-20 21:00:08 +00:00
#include <unordered_map>
2020-02-20 15:49:18 +00:00
#include <algorithm>
2019-06-13 13:26:34 +00:00
namespace llarp
{
std::string
LogLevelToString(LogLevel lvl)
{
switch (lvl)
2019-06-13 13:26:34 +00:00
{
case eLogTrace:
return "TRC";
2019-06-13 13:26:34 +00:00
case eLogDebug:
return "DBG";
case eLogInfo:
return "NFO";
case eLogWarn:
return "WRN";
case eLogError:
return "ERR";
default:
return "???";
}
}
2020-01-20 21:00:08 +00:00
std::string
LogLevelToName(LogLevel lvl)
{
switch (lvl)
2020-01-20 21:00:08 +00:00
{
case eLogTrace:
return "Trace";
case eLogDebug:
return "Debug";
case eLogInfo:
return "Info";
case eLogWarn:
return "Warn";
case eLogError:
return "Error";
case eLogNone:
2020-01-20 21:12:19 +00:00
return "None";
2020-01-20 21:00:08 +00:00
default:
return "???";
}
}
std::optional<LogLevel>
2020-01-20 21:00:08 +00:00
LogLevelFromString(std::string level)
{
std::transform(level.begin(), level.end(), level.begin(), [](const unsigned char ch) -> char {
return std::tolower(ch);
});
static const std::unordered_map<std::string, LogLevel> levels = {{"trace", eLogTrace},
{"debug", eLogDebug},
{"info", eLogInfo},
{"warn", eLogWarn},
{"error", eLogError},
{"none", eLogNone}};
2020-01-20 21:00:08 +00:00
const auto itr = levels.find(level);
if (itr == levels.end())
2020-01-20 21:00:08 +00:00
return {};
return itr->second;
}
2019-09-01 12:10:49 +00:00
} // namespace llarp