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" 9 #ifdef HMBDC_LOG_CONTEXT //if defined it has to be 'compile time sized' Context 10 #include "hmbdc/app/Context.hpp" 16 #define HMBDC_LOG_D(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 17 #define HMBDC_LOG_N(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_N(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 18 #define HMBDC_LOG_W(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_W(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 19 #define HMBDC_LOG_C(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_C(__VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 20 #define HMBDC_LOG_DEBUG(x) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(#x "=", x, LogTrailer(__FILE__, __LINE__)) 22 #define HMBDC_LOG_d(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_D(__VA_ARGS__, EmptyLogTrailer()) 23 #define HMBDC_LOG_n(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_N(__VA_ARGS__, EmptyLogTrailer()) 24 #define HMBDC_LOG_w(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_W(__VA_ARGS__, EmptyLogTrailer()) 25 #define HMBDC_LOG_c(...) hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT>::instance().LOG_C(__VA_ARGS__, EmptyLogTrailer()) 27 #define HMBDC_LOGGER hmbdc::app::LoggerT<HMBDC_LOG_CONTEXT> 31 #define HMBDC_LOG_D(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[0], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 32 #define HMBDC_LOG_N(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[1], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 33 #define HMBDC_LOG_W(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[2], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 34 #define HMBDC_LOG_C(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[3], __VA_ARGS__, LogTrailer(__FILE__, __LINE__)) 35 #define HMBDC_LOG_DEBUG(x) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[0], #x "=", x, LogTrailer(__FILE__, __LINE__)) 36 #define HMBDC_LOG_d(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[0], __VA_ARGS__, '\n') 37 #define HMBDC_LOG_n(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[1], __VA_ARGS__, '\n') 38 #define HMBDC_LOG_w(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[2], __VA_ARGS__, '\n') 39 #define HMBDC_LOG_c(...) if (&hmbdc::app::SimpleLogger::instance()) hmbdc::app::SimpleLogger::instance().log(time::SysTime::now(), g_SimpleLogLevelStr[3], __VA_ARGS__, '\n') 41 #define HMBDC_LOGGER hmbdc::app::SimpleLogger 42 #endif //HMBDC_LOG_CONTEXT 44 namespace hmbdc {
namespace app {
46 using namespace hmbdc;
50 char const g_LogLevelStr[][12] = {
57 char const g_SimpleLogLevelStr[][12 + 1] = {
72 friend std::ostream& operator << (std::ostream& os,
LogTrailer const& t) {
73 os <<
' ' << t.f <<
':' << t.l << std::endl;
78 friend std::ostream& operator << (std::ostream& os,
EmptyLogTrailer const&) {
89 template <
typename Ctx>
91 :
Client<LoggerT<Ctx>, LoggingT<Ctx::MAX_MESSAGE_SIZE>>
93 , MessageWrap<LoggingT<Ctx::MAX_MESSAGE_SIZE>>
94 , LoggingT<Ctx::MAX_MESSAGE_SIZE>::typeTag
97 static_assert(Ctx::MAX_MESSAGE_SIZE != 0
98 ,
"HMBDC_LOG_CONTEXT needs to be 'compile time sized'");
111 static std::unique_ptr<Ctx> pCtx_s;
114 std::string schedPolicy_;
120 LoggerT(std::ostream& log
121 , uint64_t cpuAffinityMask = 0
122 ,
char const* schedPolicy =
"SCHED_IDLE" 124 , uint16_t logBufferSizePower2Num = 8)
125 :
Stream((pCtx_s = std::unique_ptr<Ctx>(
new Ctx(logBufferSizePower2Num)))->buffer())
129 , schedPolicy_(schedPolicy)
130 , priority_(priority) {
131 #ifdef HMBDC_LOG_CONTEXT 133 , cpuAffinityMask?cpuAffinityMask:(1ul << std::thread::hardware_concurrency()) - 1ul);
144 LoggerT(std::ostream& log, Ctx& ctx
145 ,
char const* schedPolicy =
"SCHED_IDLE" 151 , schedPolicy_(schedPolicy)
152 , priority_(priority) {
156 LogHeader(Level level)
158 , ts(time::SysTime::now()) {
164 friend std::ostream& operator << (std::ostream& os, LogHeader
const& h) {
165 os << h.ts <<
" " << g_LogLevelStr[h.l];
171 char const* hmbdcName()
const {
return "logger"; }
172 void setMinLogLevel(Level minLevel) {
173 minLevel_ = minLevel;
176 std::tuple<char const*, int> schedSpec()
const {
177 return std::make_tuple(schedPolicy_.c_str(), priority_);
180 template <
typename ...Args>
181 void LOG_D(Args&&... args) {
183 Stream::operator()(LogHeader(L_DEBUG), std::forward<Args>(args)...);
187 template <
typename ...Args>
188 void LOG_N(Args&&... args) {
189 if (minLevel_ <= L_NOTICE)
190 Stream::operator()(LogHeader(L_NOTICE), std::forward<Args>(args)...);
192 template <
typename ...Args>
193 void LOG_W(Args&&... args) {
194 if (minLevel_ <= L_WARNING)
195 Stream::operator()(LogHeader(L_WARNING), std::forward<Args>(args)...);
197 template <
typename ...Args>
198 void LOG_C(Args&&... args) {
199 if (minLevel_ <= L_CRITICAL)
200 Stream::operator()(LogHeader(L_CRITICAL), std::forward<Args>(args)...);
203 void handleMessageCb(
Logging& logItem) {
204 Stream::dump(log_, logItem);
208 template <
typename Ctx>
218 template <
typename Arg,
typename ...Args>
219 void log(Arg&& arg, Args&&... args) {
220 log_ << std::forward<Arg>(arg);
221 log(std::forward<Args>(args)...);
227 template <
typename ... NoOpArgs>
228 SimpleLogger(std::ostream& log, NoOpArgs&&...)
a very naive logger that works safely
Definition: LoggerT.hpp:216
Definition: LoggerT.hpp:90
Definition: Message.hpp:94
Definition: GuardedSingleton.hpp:19
Definition: GuardedSingleton.hpp:12
Definition: LoggerT.hpp:77
Definition: GuardedSingleton.hpp:9
Definition: LfbStream.hpp:82
Definition: Message.hpp:46
Definition: Client.hpp:39
Definition: Client.hpp:11
Definition: LfbStream.hpp:11
Definition: LoggerT.hpp:64