1 #include "hmbdc/Copyright.hpp" 4 #include "hmbdc/app/Client.hpp" 5 #include "hmbdc/text/LfbStream.hpp" 6 #include "hmbdc/time/Time.hpp" 7 #include "hmbdc/pattern/GuardedSingleton.hpp" 10 #ifdef HMBDC_LOG_CONTEXT //if defined, it has to be 'compile time sized' Context 11 #include "hmbdc/app/Context.hpp" 17 #define HMBDC_LOG_D(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 18 #define HMBDC_LOG_N(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_N(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 19 #define HMBDC_LOG_W(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_W(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 20 #define HMBDC_LOG_C(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_C(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 21 #define HMBDC_LOG_DEBUG(x) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(#x "=", x, LogTrailer(__FILE__, __LINE__)) 23 #define HMBDC_LOG_d(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(__VA_ARGS__, EmptyLogTrailer()) 24 #define HMBDC_LOG_n(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_N(__VA_ARGS__, EmptyLogTrailer()) 25 #define HMBDC_LOG_w(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_W(__VA_ARGS__, EmptyLogTrailer()) 26 #define HMBDC_LOG_c(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_C(__VA_ARGS__, EmptyLogTrailer()) 28 #define HMBDC_LOGGER hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT> 32 #define HMBDC_LOG_D(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_D(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[0], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 33 #define HMBDC_LOG_N(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_N(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[1], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 34 #define HMBDC_LOG_W(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_W(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[2], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 35 #define HMBDC_LOG_C(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_C(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[3], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 36 #define HMBDC_LOG_DEBUG(x) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_D(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[0], #x "=", x, LogTrailer(__FILE__, __LINE__)) 37 #define HMBDC_LOG_d(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_D(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[0], __VA_ARGS__, '\n') 38 #define HMBDC_LOG_n(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_N(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[1], __VA_ARGS__, '\n') 39 #define HMBDC_LOG_w(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_W(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[2], __VA_ARGS__, '\n') 40 #define HMBDC_LOG_c(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().LOG_C(hmbdc::time::SysTime::now(), g_SimpleLogLevelStr[3], __VA_ARGS__, '\n') 42 #define HMBDC_LOGGER hmbdc::app::SimpleLogger 43 #endif //HMBDC_LOG_CONTEXT 45 namespace hmbdc {
namespace app {
47 char const g_LogLevelStr[][12] = {
54 char const g_SimpleLogLevelStr[][12 + 1] = {
69 friend std::ostream& operator << (std::ostream& os,
LogTrailer const& t) {
70 os <<
' ' << t.f <<
':' << t.l << std::endl;
75 friend std::ostream& operator << (std::ostream& os,
EmptyLogTrailer const&) {
88 template <
typename Ctx>
90 :
Client<LoggerT<Ctx>, LoggingT<Ctx::MAX_MESSAGE_SIZE>>
92 , MessageWrap<LoggingT<Ctx::MAX_MESSAGE_SIZE>>
93 , LoggingT<Ctx::MAX_MESSAGE_SIZE>::typeTag
96 static_assert(Ctx::MAX_MESSAGE_SIZE != 0
97 ,
"HMBDC_LOG_CONTEXT needs to be 'compile time sized'");
110 static std::unique_ptr<Ctx> pCtx_s;
113 std::string schedPolicy_;
119 LoggerT(std::ostream& log
120 , uint64_t cpuAffinityMask = 0
121 ,
char const* schedPolicy =
"SCHED_IDLE" 123 , uint16_t logBufferSizePower2Num = 8)
124 :
Stream((pCtx_s = std::unique_ptr<Ctx>(
new Ctx(logBufferSizePower2Num)))->buffer())
128 , schedPolicy_(schedPolicy)
129 , priority_(priority) {
130 #ifdef HMBDC_LOG_CONTEXT 132 , cpuAffinityMask?cpuAffinityMask:(1ul << std::thread::hardware_concurrency()) - 1ul);
143 LoggerT(std::ostream& log, Ctx& ctx
144 ,
char const* schedPolicy =
"SCHED_IDLE" 150 , schedPolicy_(schedPolicy)
151 , priority_(priority) {
157 , ts(hmbdc::time::SysTime::now()) {
163 friend std::ostream& operator << (std::ostream& os,
LogHeader const& h) {
164 os << h.ts <<
" " << g_LogLevelStr[h.l];
170 char const* hmbdcName()
const {
return "logger"; }
171 void setMinLogLevel(Level minLevel) {
172 minLevel_ = minLevel;
175 std::tuple<char const*, int> schedSpec()
const {
176 return std::make_tuple(schedPolicy_.c_str(), priority_);
179 template <
typename ...Args>
180 void LOG_D(Args&&... args) {
182 Stream::operator()(
LogHeader(L_DEBUG), std::forward<Args>(args)...);
186 template <
typename ...Args>
187 void LOG_N(Args&&... args) {
188 if (minLevel_ <= L_NOTICE)
189 Stream::operator()(
LogHeader(L_NOTICE), std::forward<Args>(args)...);
191 template <
typename ...Args>
192 void LOG_W(Args&&... args) {
193 if (minLevel_ <= L_WARNING)
194 Stream::operator()(
LogHeader(L_WARNING), std::forward<Args>(args)...);
196 template <
typename ...Args>
197 void LOG_C(Args&&... args) {
198 if (minLevel_ <= L_CRITICAL)
199 Stream::operator()(
LogHeader(L_CRITICAL), std::forward<Args>(args)...);
202 void handleMessageCb(
Logging& logItem) {
203 Stream::dump(log_, logItem);
207 template <
typename Ctx>
226 template <
typename ...Args>
227 void LOG_D(Args&&... args) {
229 std::lock_guard<std::recursive_mutex> g(mutex_);
230 log(std::forward<Args>(args)...);
234 template <
typename ...Args>
235 void LOG_N(Args&&... args) {
236 if (minLevel_ <= L_NOTICE) {
237 std::lock_guard<std::recursive_mutex> g(mutex_);
238 log(std::forward<Args>(args)...);
241 template <
typename ...Args>
242 void LOG_W(Args&&... args) {
243 if (minLevel_ <= L_WARNING) {
244 std::lock_guard<std::recursive_mutex> g(mutex_);
245 log(std::forward<Args>(args)...);
248 template <
typename ...Args>
249 void LOG_C(Args&&... args) {
250 if (minLevel_ <= L_CRITICAL) {
251 std::lock_guard<std::recursive_mutex> g(mutex_);
252 log(std::forward<Args>(args)...);
257 template <
typename Arg,
typename ...Args>
258 void log(Arg&& arg, Args&&... args) {
259 log_ << std::forward<Arg>(arg);
260 log(std::forward<Args>(args)...);
264 template <
typename ... NoOpArgs>
265 SimpleLogger(std::ostream& log, NoOpArgs&&...)
271 std::recursive_mutex mutex_;
a very straightforward logger that works safely
Definition: LoggerT.hpp:215
a high performance async logger that doesn't penalize logging threads as much when the logging load i...
Definition: LoggerT.hpp:89
Definition: Message.hpp:103
base for the Singleton that works with SingletonGuardian
Definition: GuardedSingleton.hpp:35
RAII representing the lifespan of the underlying Singleton which also ganrantees the singularity of u...
Definition: GuardedSingleton.hpp:20
Definition: LoggerT.hpp:74
Definition: LfbStream.hpp:82
Definition: Message.hpp:55
A Client represents a thread of execution/a task. The execution is managed by a Context. a Client object could participate in message dispatching as the receiver of specifed message types.
Definition: Client.hpp:47
Definition: LoggerT.hpp:61