Files
spark/src/lib/logging/LoggerHub.cpp
T

205 lines
5.8 KiB
C++

#include "LoggerHub.h"
#include "Logger.h"
#include "lib/os/os.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/bts_chrono.h"
#include "lib/utils/thread_manager.h"
#include <algorithm>
#include <atomic>
#include <mutex>
#include <thread>
namespace bts {
namespace log {
std::atomic<size_t> thread_idx;
struct LoggerHub::SinkRec {
Sink sink;
Filter filter;
std::string name;
};
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), thread_cfg() {}
std::mutex lock;
std::vector<SinkRec> sinks;
std::vector<std::unique_ptr<Logger>> loggers;
std::array<std::string, Level::Max + 1> level_names;
std::array<std::string, Subsystem::Max + 1> subsystem_names;
std::unique_ptr<std::thread> worker;
std::atomic<bool> done;
size_t logger_initial_queue_size;
size_t logger_max_queue_growth;
std::string logger_name;
config::ThreadGroupCfg thread_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<std::mutex> 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++)
me->level_names[i] = std::to_string(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->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});
}
rebuild_logger_filters();
update_logger_queue_limits();
me->thread_cfg = cfg.thread_cfg;
}
void LoggerHub::start() {
bts_assert(!me->worker);
me->worker = std::make_unique<std::thread>(
threading::make_thread(me->logger_name.c_str(),
std::bind(&LoggerHub::do_work, this),me->thread_cfg));
}
LoggerHub::~LoggerHub() {
me->done = true;
if (me->worker)
me->worker->join();
}
Logger &LoggerHub::make_logger() {
std::lock_guard<std::mutex> l(me->lock);
me->loggers.push_back(std::make_unique<Logger>(
thread_idx++, get_base_filter(), me->logger_initial_queue_size,
me->logger_max_queue_growth));
return *me->loggers.back();
}
void LoggerHub::do_work() {
while (!me->done) {
size_t consumed = 0;
{
std::lock_guard<std::mutex> l(me->lock);
consumed = consume_log_recs();
}
if (consumed == 0)
std::this_thread::sleep_for(10ms);
}
for (;;) {
auto all_shutdown = false;
{
std::lock_guard<std::mutex> l(me->lock);
consume_log_recs();
all_shutdown = me->loggers.empty();
}
if (all_shutdown)
return;
else
std::this_thread::yield();
}
}
size_t LoggerHub::consume_log_recs() {
std::vector<std::unique_ptr<Logger>> to_keep;
to_keep.reserve(me->loggers.size());
size_t consumed = 0;
for (size_t i = 0; i < me->loggers.size(); i++) {
auto &l = me->loggers[i];
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<LogRec> &msgs) {
consumed += (dropped_msg_count + logged_msg_count);
if (dropped_msg_count) {
LogRec lrec;
lrec.ts = log_ts();
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, thread_name, msgs[i]);
if (active)
to_keep.emplace_back(std::move(l));
});
}
me->loggers = std::move(to_keep);
return consumed;
}
void LoggerHub::dispatch_to_sinks(size_t tid, std::string const &thread_name,
LogRec &lr) {
auto msg = lr.unpack_msg();
auto lm = LogMsg{lr.ts,
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() {
auto f = get_base_filter();
for (auto &l : me->loggers)
l->update_filter(f);
}
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);
return f;
}
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);
}
}
}