25 #ifndef DCCLLOGGER20121009H
26 #define DCCLLOGGER20121009H
38 #include "thread_safety.h"
52 ALL = DEBUG3 | (DEBUG3 - 1),
53 WARN_PLUS = WARN | (WARN - 1),
54 INFO_PLUS = INFO | (INFO - 1),
55 DEBUG1_PLUS = DEBUG1 | (DEBUG1 - 1),
56 DEBUG2_PLUS = DEBUG2 | (DEBUG2 - 1),
57 DEBUG3_PLUS = DEBUG3 | (DEBUG3 - 1),
70 void to_ostream(
const std::string& msg, dccl::logger::Verbosity vrb, dccl::logger::Group grp,
71 std::ostream* os,
bool add_timestamp);
84 template <
typename Slot>
void connect(
int verbosity_mask, Slot slot)
86 enabled_verbosities_ |= verbosity_mask;
87 if (verbosity_mask & logger::WARN)
88 warn_signal.emplace_back(slot);
89 if (verbosity_mask & logger::INFO)
90 info_signal.emplace_back(slot);
91 if (verbosity_mask & logger::DEBUG1)
92 debug1_signal.emplace_back(slot);
93 if (verbosity_mask & logger::DEBUG2)
94 debug2_signal.emplace_back(slot);
95 if (verbosity_mask & logger::DEBUG3)
96 debug3_signal.emplace_back(slot);
99 void disconnect(
int verbosity_mask)
101 enabled_verbosities_ &= ~verbosity_mask;
102 if (verbosity_mask & logger::WARN)
104 if (verbosity_mask & logger::INFO)
106 if (verbosity_mask & logger::DEBUG1)
107 debug1_signal.clear();
108 if (verbosity_mask & logger::DEBUG2)
109 debug2_signal.clear();
110 if (verbosity_mask & logger::DEBUG3)
111 debug3_signal.clear();
115 void set_verbosity(logger::Verbosity verbosity) { verbosity_.push(verbosity); }
117 void set_group(logger::Group group) { group_.push(group); }
119 bool contains(logger::Verbosity verbosity)
const {
return verbosity & enabled_verbosities_; }
128 int overflow(
int c = EOF)
override;
130 void display(
const std::string& s)
132 if (verbosity() & logger::WARN)
134 for (
auto& slot : warn_signal) slot(s, logger::WARN, group());
136 if (verbosity() & logger::INFO)
138 for (
auto& slot : info_signal) slot(s, logger::INFO, group());
140 if (verbosity() & logger::DEBUG1)
142 for (
auto& slot : debug1_signal) slot(s, logger::DEBUG1, group());
144 if (verbosity() & logger::DEBUG2)
146 for (
auto& slot : debug2_signal) slot(s, logger::DEBUG2, group());
148 if (verbosity() & logger::DEBUG3)
150 for (
auto& slot : debug3_signal) slot(s, logger::DEBUG3, group());
154 logger::Verbosity verbosity()
156 return verbosity_.empty() ? logger::UNKNOWN : verbosity_.top();
158 logger::Group group() {
return group_.empty() ? logger::GENERAL : group_.top(); }
161 std::stack<logger::Verbosity> verbosity_;
162 std::stack<logger::Group> group_;
163 std::deque<std::string> buffer_;
164 int enabled_verbosities_;
166 using LogSignal = std::vector<
167 std::function<void(
const std::string& msg, logger::Verbosity vrb, logger::Group grp)>>;
169 LogSignal warn_signal, info_signal, debug1_signal, debug2_signal, debug3_signal;
177 Logger() : std::ostream(&buf_) {}
178 ~
Logger()
override =
default;
181 bool check(logger::Verbosity verbosity) {
return buf_.contains(verbosity); }
192 bool is(logger::Verbosity verbosity, logger::Group group = logger::GENERAL)
194 if (!buf_.contains(verbosity))
200 #if DCCL_THREAD_SUPPORT
204 buf_.set_group(group);
214 template <
typename Slot>
void connect(
int verbosity_mask, Slot slot)
217 buf_.
connect(verbosity_mask, slot);
226 template <
typename Obj>
228 void (Obj::*mem_func)(
const std::string& msg, logger::Verbosity vrb,
232 connect(verbosity_mask, std::bind(mem_func, obj, std::placeholders::_1,
233 std::placeholders::_2, std::placeholders::_3));
241 void connect(
int verbosity_mask, std::ostream* os,
bool add_timestamp =
true)
245 std::bind(to_ostream, std::placeholders::_1, std::placeholders::_2,
246 std::placeholders::_3, os, add_timestamp));
253 buf_.disconnect(verbosity_mask);
262 inline std::string hash_as_string(std::size_t hash)
264 std::stringstream hash_stream;
265 hash_stream <<
"0x" << std::setfill(
'0') << std::setw(
sizeof(std::size_t) * 2) << std::hex
267 return hash_stream.str();
272 #endif // DCCLLOGGER20121009H