forked from KjellKod/g3log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloglevels.cpp
77 lines (68 loc) · 2.73 KB
/
loglevels.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
/** ==========================================================================
* 2012 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
* with no warranties. This code is yours to share, use and modify with no
* strings attached and no restrictions or obligations.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
* ============================================================================*/
#include "g3log/loglevels.hpp"
#include <atomic>
#include <cassert>
#include <map>
namespace {
namespace {
/// As suggested in: http://stackoverflow.com/questions/13193484/how-to-declare-a-vector-of-atomic-in-c
struct atomicbool {
private:
std::atomic<bool> value_;
public:
atomicbool(): value_ {false} {}
atomicbool(const bool &value): value_ {value} {}
atomicbool(const std::atomic<bool> &value) : value_ {value.load(std::memory_order_acquire)} {}
atomicbool(const atomicbool &other): value_ {other.value_.load(std::memory_order_acquire)} {}
atomicbool &operator=(const atomicbool &other) {
value_.store(other.value_.load(std::memory_order_acquire), std::memory_order_release);
return *this;
}
bool value() {return value_.load(std::memory_order_acquire);}
std::atomic<bool>& get() {return value_;}
};
} // anonymous
}
namespace g3 {
namespace internal {
bool wasFatal(const LEVELS &level) {
return level.value >= FATAL.value;
}
#ifdef G3_DYNAMIC_LOGGING
std::map<int, atomicbool> g_log_level_status = {{g3::kDebugVaulue, true}, {INFO.value, true}, {WARNING.value, true}, {FATAL.value, true}};
#endif
} // internal
#ifdef G3_DYNAMIC_LOGGING
namespace only_change_at_initialization {
void setLogLevel(LEVELS log_level, bool enabled) {
int level = log_level.value;
internal::g_log_level_status[level].get().store(enabled, std::memory_order_release);
}
std::string printLevels() {
std::string levels;
for (auto& v : internal::g_log_level_status) {
levels += "value: " + std::to_string(v.first) + " status: " + std::to_string(v.second.value()) + "\n";
}
return levels;
}
void reset() {
internal::g_log_level_status.clear();
internal::g_log_level_status = std::map<int, atomicbool>{{g3::kDebugVaulue, true}, {INFO.value, true}, {WARNING.value, true}, {FATAL.value, true}};
}
} // only_change_at_initialization
#endif
bool logLevel(LEVELS log_level) {
#ifdef G3_DYNAMIC_LOGGING
int level = log_level.value;
bool status = internal::g_log_level_status[level].value();
return status;
#endif
return true;
}
} // g3