From 0e6f37d6ee79249d2083324b3af9e2b07abe5f76 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Tue, 26 Dec 2017 14:15:26 -0500 Subject: [PATCH] Improved logging with respect to threading information. --- src/lib/api/core/ApiClientInterop.cpp | 2 +- src/lib/api/core/ApiServerInterop.cpp | 2 +- src/lib/logging/Logger.h | 12 +- src/lib/logging/LoggerHub.cpp | 172 ++++++++++++-------------- src/lib/logging/LoggerHub.h | 2 +- src/lib/logging/Sink.cpp | 3 +- src/lib/logging/Sink.h | 3 +- src/lib/os/os.cpp | 13 ++ src/lib/os/os.h | 1 + src/lib/utils/bts_assert.h | 25 ++-- 10 files changed, 124 insertions(+), 111 deletions(-) diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 8724d5de1..839f7bc8a 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -102,7 +102,7 @@ struct ApiClient { CAPI ApiClient* api_client_create(Quickbus* pqbus, const char* type_name, const char* service_qualifier) { bts_assert(pqbus); auto service_name = base_topic(type_name, service_qualifier); - LOG(Api,Info, "create client: {}",service_name); + LOG(Api,Info, "Create client: {}",service_name); ApiClient* pclient = new ApiClient(*pqbus, service_name); return pclient; } diff --git a/src/lib/api/core/ApiServerInterop.cpp b/src/lib/api/core/ApiServerInterop.cpp index 89182710c..547d31ef5 100644 --- a/src/lib/api/core/ApiServerInterop.cpp +++ b/src/lib/api/core/ApiServerInterop.cpp @@ -30,7 +30,7 @@ CAPI ApiServerCore* api_server_create(Quickbus* pbus, const char* type_name, con bts_assert(strchr(service_qualifier, ch) == nullptr); } auto service_name = base_topic(type_name, service_qualifier); - LOG(Api,Info,"create server: {}",service_name); + LOG(Api,Info,"Create server: {}",service_name); return new ApiServerCore(*pbus, service_name, [=](qbus::RPCRequestPtr const& req, qbus::ReplyHandler const& reply) { // Extend the lifetime of the ReplyHandler by moving it to the heap qbus::ReplyHandler* phandler = new qbus::ReplyHandler(std::move(reply)); diff --git a/src/lib/logging/Logger.h b/src/lib/logging/Logger.h index 2d80391aa..815554abf 100644 --- a/src/lib/logging/Logger.h +++ b/src/lib/logging/Logger.h @@ -2,6 +2,7 @@ #include "MsgPacking.h" #include "LogRec.h" +#include "lib/os/os.h" #include #include @@ -15,7 +16,7 @@ public: Logger(const Logger&) = delete; Logger&operator=(const Logger&) = delete; - Logger(std::thread::id tid, + Logger(size_t tid, Filter const&f, size_t initial_queue_size, size_t max_queue_growth): @@ -29,8 +30,10 @@ public: _queue_b(initial_queue_size), _tid(tid), _active(true), - _max_queue_size(initial_queue_size*max_queue_growth) + _max_queue_size(initial_queue_size*max_queue_growth), + _thread_name() { + os::get_thread_name(&_thread_name); } template @@ -91,7 +94,7 @@ public: _drop_count = 0; unlock(); - s(_tid,active,dropped_msg_count,logged_msg_count,*_read_q); + s(_tid,_thread_name,active,dropped_msg_count,logged_msg_count,*_read_q); if ((logged_msg_count > _read_q->size() / 2) && _read_q->size() < _max_queue_size) @@ -151,8 +154,9 @@ private: size_t _drop_count; std::vector _queue_a; std::vector _queue_b; - std::thread::id _tid; + size_t _tid; bool _active; size_t _max_queue_size; + std::string _thread_name; }; }} diff --git a/src/lib/logging/LoggerHub.cpp b/src/lib/logging/LoggerHub.cpp index a3db68bfe..c5f6fc261 100644 --- a/src/lib/logging/LoggerHub.cpp +++ b/src/lib/logging/LoggerHub.cpp @@ -1,19 +1,20 @@ #include "LoggerHub.h" -#include "lib/utils/bts_assert.h" -#include "lib/utils/bts_chrono.h" #include "Logger.h" #include "lib/os/os.h" #include "lib/utils/bts_assert.h" +#include "lib/utils/bts_chrono.h" -#include -#include -#include #include +#include +#include +#include namespace bts { namespace log { +std::atomic thread_idx; + struct LoggerHub::SinkRec { Sink sink; Filter filter; @@ -21,24 +22,16 @@ struct LoggerHub::SinkRec { }; struct LoggerHub::PImpl { - PImpl(std::string const&logger_name) : - lock(), - sinks(), - loggers(), - level_names(), - subsystem_names(), - worker(), - done(false), - logger_initial_queue_size(), - logger_max_queue_growth(), - logger_name(logger_name) - {} + PImpl(std::string const &logger_name) + : lock(), sinks(), loggers(), level_names(), subsystem_names(), worker(), + done(false), logger_initial_queue_size(), logger_max_queue_growth(), + logger_name(logger_name) {} std::mutex lock; std::vector sinks; std::vector> loggers; - std::array level_names; - std::array subsystem_names; + std::array level_names; + std::array subsystem_names; std::unique_ptr worker; std::atomic done; size_t logger_initial_queue_size; @@ -46,40 +39,38 @@ struct LoggerHub::PImpl { std::string logger_name; }; -LoggerHub::LoggerHub(std::string const&logger_name) - : me (new PImpl(logger_name)) -{ - LoggingConfig cfg ; +LoggerHub::LoggerHub(std::string const &logger_name) + : me(new PImpl(logger_name)) { + LoggingConfig cfg; config(cfg); // we cannot launch the worker thread here because thread creation // is not possible during static initialization } -void LoggerHub::config(const LoggingConfig &cfg) -{ - std::lock_guard l (me->lock); +void LoggerHub::config(const LoggingConfig &cfg) { + std::lock_guard l(me->lock); me->logger_initial_queue_size = cfg.logger_initial_queue_size; me->logger_max_queue_growth = cfg.logger_max_queue_growth; - for (size_t i = 0; i <= Level::Max ; i++) + for (size_t i = 0; i <= Level::Max; i++) me->level_names[i] = std::to_string(i); - for (size_t i = 0; i <= Subsystem::Max ; i++) + for (size_t i = 0; i <= Subsystem::Max; i++) me->subsystem_names[i] = std::to_string(i); for (auto const &kvp : cfg.level_names) me->level_names[kvp.first.ordinal] = kvp.second; - + for (auto const &kvp : cfg.subsystem_names) - me->subsystem_names[kvp.first.ordinal ] = kvp.second; + me->subsystem_names[kvp.first.ordinal] = kvp.second; me->sinks.clear(); for (auto const &kvp : cfg.sinks) { auto const &name = kvp.first; auto const &sink = kvp.second.first; auto const &filter = kvp.second.second; - me->sinks.push_back(SinkRec{sink,filter,name}); + me->sinks.push_back(SinkRec{sink, filter, name}); } rebuild_logger_filters(); update_logger_queue_limits(); @@ -87,125 +78,122 @@ void LoggerHub::config(const LoggingConfig &cfg) void LoggerHub::start() { bts_assert(!me->worker); - me->worker = std::make_unique (&LoggerHub::do_work, this); + me->worker = std::make_unique(&LoggerHub::do_work, this); } -LoggerHub::~LoggerHub() -{ - me->done=true; +LoggerHub::~LoggerHub() { + me->done = true; if (me->worker) me->worker->join(); } -Logger &LoggerHub::make_logger() -{ - std::lock_guard l (me->lock); - me->loggers.push_back(std::make_unique (std::this_thread::get_id(), - get_base_filter(), - me->logger_initial_queue_size, - me->logger_max_queue_growth)); +Logger &LoggerHub::make_logger() { + std::lock_guard l(me->lock); + me->loggers.push_back(std::make_unique( + thread_idx++, get_base_filter(), me->logger_initial_queue_size, + me->logger_max_queue_growth)); return *me->loggers.back(); } -void LoggerHub::do_work() -{ +void LoggerHub::do_work() { os::set_thread_name(me->logger_name.c_str()); while (!me->done) { size_t consumed = 0; { - std::lock_guard l (me->lock); + std::lock_guard l(me->lock); consumed = consume_log_recs(); } - if (consumed == 0) - std::this_thread::sleep_for (10ms); + if (consumed == 0) + std::this_thread::sleep_for(10ms); } for (;;) { auto all_shutdown = false; { - std::lock_guard l (me->lock); + std::lock_guard l(me->lock); consume_log_recs(); all_shutdown = me->loggers.empty(); } if (all_shutdown) - return ; + return; else std::this_thread::yield(); } } -size_t LoggerHub::consume_log_recs() -{ +size_t LoggerHub::consume_log_recs() { std::vector> to_keep; to_keep.reserve(me->loggers.size()); size_t consumed = 0; - for (size_t i = 0; i < me->loggers.size();i++) { + for (size_t i = 0; i < me->loggers.size(); i++) { auto &l = me->loggers[i]; - l->unqueue([&l,&to_keep,&consumed,this] (std::thread::id tid, bool active, - size_t dropped_msg_count, - size_t logged_msg_count, - std::vector &msgs) { - consumed+= (dropped_msg_count + logged_msg_count); - if (dropped_msg_count) { - LogRec lrec; - lrec.file = __FILE__; - lrec.line = __LINE__; - lrec.level = Level {Level::Max}; - lrec.subsystem = Subsystem {Subsystem::Max}; - lrec.pack_msg ("Dropped {} messages. Increase logging buffer size.", - dropped_msg_count); - dispatch_to_sinks (tid,lrec); - } + l->unqueue([&l, &to_keep, &consumed, + this](size_t tid, std::string const &thread_name, bool active, + size_t dropped_msg_count, size_t logged_msg_count, + std::vector &msgs) { + consumed += (dropped_msg_count + logged_msg_count); + if (dropped_msg_count) { + LogRec lrec; + lrec.file = __FILE__; + lrec.line = __LINE__; + lrec.level = Level{Level::Max}; + lrec.subsystem = Subsystem{Subsystem::Max}; + lrec.pack_msg("Dropped {} messages. Increase logging buffer size.", + dropped_msg_count); + dispatch_to_sinks(tid, thread_name, lrec); + } - for (size_t i = 0; i < logged_msg_count; i++) - dispatch_to_sinks(tid,msgs[i]); + for (size_t i = 0; i < logged_msg_count; i++) + dispatch_to_sinks(tid, thread_name, msgs[i]); - if (active) - to_keep.emplace_back(std::move(l)); - }); + if (active) + to_keep.emplace_back(std::move(l)); + }); } me->loggers = std::move(to_keep); return consumed; } -void LoggerHub::dispatch_to_sinks(std::thread::id tid,LogRec&lr) -{ +void LoggerHub::dispatch_to_sinks(size_t tid, std::string const &thread_name, + LogRec &lr) { auto msg = lr.unpack_msg(); - auto lm = LogMsg { msg, lr.file, lr.line, - lr.level, me->level_names[lr.level.ordinal], - lr.subsystem, me->subsystem_names[lr.subsystem.ordinal], - tid }; - + auto lm = LogMsg{msg, + lr.file, + lr.line, + lr.level, + me->level_names[lr.level.ordinal], + lr.subsystem, + me->subsystem_names[lr.subsystem.ordinal], + tid, + thread_name}; + for (auto &s : me->sinks) if (lr.level.ordinal >= s.filter[lr.subsystem.ordinal].ordinal) s.sink(lm); } - -void LoggerHub::rebuild_logger_filters() -{ +void LoggerHub::rebuild_logger_filters() { auto f = get_base_filter(); for (auto &l : me->loggers) l->update_filter(f); } -Filter LoggerHub::get_base_filter() const -{ +Filter LoggerHub::get_base_filter() const { Filter f; for (auto &l : f) l.ordinal = 255; - + for (auto &s : me->sinks) - for(size_t i = 0; i < Subsystem::Max+1; i++) - f[i].ordinal = std::min(f[i].ordinal,s.filter[i].ordinal); + for (size_t i = 0; i < Subsystem::Max + 1; i++) + f[i].ordinal = std::min(f[i].ordinal, s.filter[i].ordinal); return f; } -void LoggerHub::update_logger_queue_limits() -{ +void LoggerHub::update_logger_queue_limits() { for (auto &l : me->loggers) - l->update_queue_limits(me->logger_initial_queue_size,me->logger_max_queue_growth); + l->update_queue_limits(me->logger_initial_queue_size, + me->logger_max_queue_growth); +} +} } - -}} diff --git a/src/lib/logging/LoggerHub.h b/src/lib/logging/LoggerHub.h index 1ef80ea20..cb26db8a6 100644 --- a/src/lib/logging/LoggerHub.h +++ b/src/lib/logging/LoggerHub.h @@ -29,7 +29,7 @@ private: void update_logger_queue_limits(); size_t consume_log_recs(); void rebuild_logger_filters(); - void dispatch_to_sinks(std::thread::id,LogRec&); + void dispatch_to_sinks(size_t tid,std::string const&thread_name,LogRec&); Filter get_base_filter() const; }; diff --git a/src/lib/logging/Sink.cpp b/src/lib/logging/Sink.cpp index 4760fa9f8..0c955e127 100644 --- a/src/lib/logging/Sink.cpp +++ b/src/lib/logging/Sink.cpp @@ -41,11 +41,12 @@ struct InitSysLog { auto formatter() { return [] (LogMsg const&m) { - return fmt::format("{}|{:<8}|{:<4}|{:<15}|{:<4}|{:<35}| {} ", + return fmt::format("{}|{:<8}|{:<5}|{:<3}|{:<16}|{:<4}|{:<35}| {} ", datetime::time_string(datetime::now()), m.subsystem_name, m.level_name, m.tid, + m.thread_name, os::process_id(), fmt::format("{}:{}",boost::filesystem::path(m.file).filename().string() , m.line), m.msg); diff --git a/src/lib/logging/Sink.h b/src/lib/logging/Sink.h index ada2aba7d..50321f3ab 100644 --- a/src/lib/logging/Sink.h +++ b/src/lib/logging/Sink.h @@ -21,7 +21,8 @@ struct LogMsg { const Subsystem subsystem; const std::string &subsystem_name; - const std::thread::id tid; + const size_t tid; + const std::string &thread_name; }; GCC_DIAG_RESTORE() diff --git a/src/lib/os/os.cpp b/src/lib/os/os.cpp index 26f213e5d..8ebeb9ab7 100755 --- a/src/lib/os/os.cpp +++ b/src/lib/os/os.cpp @@ -1031,6 +1031,18 @@ BTS_EXPORT void set_thread_name(const char* name) #endif } +BTS_EXPORT void get_thread_name(std::string *out) +{ +#ifdef _WIN32 + *out = "unknown"; +#else + out->resize(16); + pthread_getname_np(pthread_self(),&((*out)[0]),16); + auto sz = strlen(out->data()); + out->resize(sz); +#endif +} + BTS_EXPORT bool is_nan(double d) { #ifdef _WIN32 @@ -1093,3 +1105,4 @@ extern "C" BTS_EXPORT void free_bts_data_path (char *dp) } }} // end namespaces + diff --git a/src/lib/os/os.h b/src/lib/os/os.h index 28cda16ce..d729d427d 100755 --- a/src/lib/os/os.h +++ b/src/lib/os/os.h @@ -74,6 +74,7 @@ BTS_EXPORT void setenv(const char* name, const char* value); BTS_EXPORT void set_thread_name(std::string const&); BTS_EXPORT void set_thread_name(const char* name); +BTS_EXPORT void get_thread_name(std::string *out); BTS_EXPORT bool is_nan(double d); BTS_EXPORT bool is_nan(float f); diff --git a/src/lib/utils/bts_assert.h b/src/lib/utils/bts_assert.h index 71beae0ec..c4d0c4565 100644 --- a/src/lib/utils/bts_assert.h +++ b/src/lib/utils/bts_assert.h @@ -1,6 +1,8 @@ #pragma once -#include +#include #include +#include +#include #include "lib/logging/logging.h" #include "lib/utils/bts_debugger.h" @@ -8,16 +10,20 @@ #ifdef _WIN32 #define TERMINATE ::_exit(-1); #else -#define TERMINATE abort (); +#define TERMINATE abort(); #endif -#define bts_assert_msg(EXP, msg) do { \ - if (!(EXP)) { \ - LOG(Assert,Fatal, "Assert failure: {} {}",std::string(#EXP),std::string(msg) ); \ - if (is_debugger_present()) \ - debug_break(); \ - TERMINATE; \ - }} while(0) +#define bts_assert_msg(EXP, msg) \ + do { \ + if (!(EXP)) { \ + LOG(Assert, Fatal, "Assert failure: {} {}", std::string(#EXP), \ + std::string(msg)); \ + std::this_thread::sleep_for(std::chrono::seconds(1)); \ + if (is_debugger_present()) \ + debug_break(); \ + TERMINATE; \ + } \ + } while (0) #define bts_assert(EXP) bts_assert_msg(EXP, "") @@ -26,4 +32,3 @@ #else #define bts_assert_d(EXP) #endif -