/
level.cpp
99 lines (87 loc) · 2.52 KB
/
level.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdexcept>
#include <iostream>
#include "logging/level.h"
#include "util/util.h"
namespace mckeys {
using namespace std;
static ValueMap valueMap;
#define ADD_TO_VALUE_MAP(level) valueMap.insert(ValueMap::value_type(level.getValue(), level))
static NameMap nameMap;
#define ADD_TO_NAME_MAP(level) nameMap.insert(NameMap::value_type(level.getName(), level))
// protected constructor
Level::Level(const string &name, const uint32_t value)
: name(name),
value(value)
{}
// static values
Level Level::TRACE = Level("TRACE", 0);
Level Level::DEBUG = Level("DEBUG", 1);
Level Level::INFO = Level("INFO", 2);
Level Level::WARNING = Level("WARNING", 3);
Level Level::ERROR = Level("ERROR", 4);
Level Level::FATAL = Level("FATAL", 5);
Level Level::OFF = Level("OFF", USIZE_BITS(uint32_t));
Level Level::fromName(const string &name)
{
if (nameMap.empty()) {
ADD_TO_NAME_MAP(Level::TRACE);
ADD_TO_NAME_MAP(Level::DEBUG);
ADD_TO_NAME_MAP(Level::INFO);
ADD_TO_NAME_MAP(Level::WARNING);
ADD_TO_NAME_MAP(Level::ERROR);
ADD_TO_NAME_MAP(Level::FATAL);
ADD_TO_NAME_MAP(Level::OFF);
}
NameMap::iterator it = nameMap.find(name);
if (it != nameMap.end()) {
return it->second;
} else {
throw range_error("No such level with name " + name);
}
}
Level Level::fromValue(const uint32_t value)
{
if (valueMap.empty()) {
ADD_TO_VALUE_MAP(Level::TRACE);
ADD_TO_VALUE_MAP(Level::DEBUG);
ADD_TO_VALUE_MAP(Level::INFO);
ADD_TO_VALUE_MAP(Level::WARNING);
ADD_TO_VALUE_MAP(Level::ERROR);
ADD_TO_VALUE_MAP(Level::FATAL);
ADD_TO_VALUE_MAP(Level::OFF);
}
ValueMap::iterator it = valueMap.find(value);
if (it != valueMap.end()) {
return it->second;
} else {
throw range_error("No such level with value " + std::to_string(value));
}
}
string Level::getName() const
{
return name;
}
uint32_t Level::getValue() const
{
return value;
}
/* Comparison Operators */
bool operator==(const Level& lhs, const Level& rhs) {
return (lhs.getValue() == rhs.getValue());
}
bool operator<(const Level& lhs, const Level& rhs) {
return (lhs.getValue() < rhs.getValue());
}
bool operator<=(const Level& lhs, const Level& rhs) {
return (lhs.getValue() <= rhs.getValue());
}
bool operator>(const Level& lhs, const Level& rhs) {
return (lhs.getValue() >= rhs.getValue());
}
bool operator>=(const Level& lhs, const Level& rhs) {
return (lhs.getValue() >= rhs.getValue());
}
bool operator!=(const Level& lhs, const Level& rhs) {
return (lhs.getValue() != rhs.getValue());
}
} // end namespace mckeys