Files
spark/src/lib/config/ConfigLog.h
T

135 lines
3.3 KiB
C++

#pragma once
#include "lib/logging/logging.h"
#include "lib/utils/bts_no_copy.h"
#include <memory>
#include <sstream>
#include <string>
namespace bts {
namespace config {
namespace internal {
// A back door though the logging system
// To allow config diagnostic logging
class ConfigLog {
public:
ConfigLog();
~ConfigLog();
void info(const std::string &msg);
void error(const std::string &msg);
void warning(const std::string &msg);
bool enqueue(
bts::log::Level level, const char *file, int line, std::string msg);
template <typename... Args>
bool enqueue(
bts::log::Level level,
const char *file,
int line,
const char *msg_template,
Args &&...msg_args);
bool is_loggable(bts::log::Level level) const;
bool print_errors() const;
void set_print_errors(bool value);
private:
void start();
bool print_errors_;
log::LoggerHub logger_hub_;
log::Logger &logger_;
};
//////////////////////////////////////////////////
// Definition
//////////////////////////////////////////////////
inline ConfigLog::ConfigLog()
: print_errors_(true), logger_hub_("config_logger"),
logger_(logger_hub_.make_logger()) {
log::LoggingConfig cfg;
cfg.level_names = {{Diag, "Diag"}, {Info, "Info"}, {Warn, "Warn"},
{Error, "Error"}, {Fatal, "Fatal"}, {Sys, "Sys"}};
cfg.subsystem_names = {{log::Gen, "Gen"}, {log::Log, "Log"}};
cfg.logger_initial_queue_size = 10;
log::Filter f;
for (auto &l : f) {
l = Warn;
}
cfg.deployment_type = "Config";
f[log::Cfg.ordinal] = Warn;
cfg.sinks["console"] = std::make_pair(log::console_sink(cfg), f);
f[log::Cfg.ordinal] = Info;
cfg.sinks["syslog"] = std::make_pair(log::syslog_sink(cfg), f);
logger_hub_.config(cfg);
start();
}
inline void ConfigLog::start() { logger_hub_.start(); }
inline ConfigLog::~ConfigLog() { logger_.mark_inactive(); }
inline bool ConfigLog::print_errors() const { return print_errors_; }
inline void ConfigLog::set_print_errors(bool value) { print_errors_ = value; }
inline void ConfigLog::info(const std::string &msg) {
if (!print_errors_) {
return;
}
logger_.enqueue(
log::log_ts(), log::Cfg, Info, __FILE__, __LINE__, "{0}", msg);
}
inline void ConfigLog::error(const std::string &msg) {
if (!print_errors_) {
return;
}
logger_.enqueue(
log::log_ts(), log::Cfg, Error, __FILE__, __LINE__, "{0}", msg);
}
inline void ConfigLog::warning(const std::string &msg) {
if (!print_errors_) {
return;
}
logger_.enqueue(
log::log_ts(), log::Cfg, Warn, __FILE__, __LINE__, "{0}", msg);
}
inline bool ConfigLog::enqueue(
bts::log::Level level, const char *file, int line, std::string msg) {
return enqueue(level, file, line, "{}", std::move(msg));
}
inline bool ConfigLog::is_loggable(bts::log::Level level) const {
return print_errors_ && logger_.is_loggable(bts::log::Cfg, level);
}
template <typename... Args>
inline bool ConfigLog::enqueue(
bts::log::Level level,
const char *file,
int line,
const char *msg_template,
Args &&...msg_args) {
if (!print_errors_) {
return false;
}
return logger_.enqueue(
log::log_ts(), log::Cfg, level, file, line, msg_template, msg_args...);
}
} // namespace internal
} // namespace config
} // namespace bts