#include "logging.h" #include "logging/logger_hub.h" #include "logging/logging_config.h" // #include "spark/config/config.h" // #include "spark/config/protos/deployment_type.pb.h" // #include "spark/config/protos/logging.pb.h" #include "logging/protobufs/logging.h" #include "logging/protobufs/logging.pb.h" #include "system/facades/bts_assert.h" #include "system/os.h" #include "system/protobufs/system_config.pb.h" #include "system/protobufs/threads.pb.h" #include "system/threading/dispatcher.h" #include "system/threading/thread_manager.h" #ifdef SPARK_ENABLE_CCAPI #include "logging/ccapi_logger.h" #endif #include #include #include #include #include #include #include #include #include #include #include #include #include namespace bts::logging { using bts::logging::protobufs::Logging; using bts::logging::protobufs::SinkConfig; using bts::system::protobufs::SystemConfig; using bts::system::protobufs::ThreadGroupCfg; using Path = boost::filesystem::path; static LoggerHub _default_logger_hub("logger"); thread_local LoggerHandle _thread_logger(_default_logger_hub.make_logger()); template typename M::const_iterator find_key( M const &m, typename M::mapped_type const &v) { for (auto it = m.begin(); it != m.end(); it++) if (it->second == v) return it; return m.end(); } Path log_path() { return Path{"/home/bts/.local/share/bts/sparkd/data/logging.sparkd"}; // return Path{config::get().data_path()} / // fmt::format("logging.{}/", os::get_application_name()); } void start_default_logging_thread() { _default_logger_hub.start(); } Filter get_filter(LoggingConfig &cfg, SinkConfig const &scfg) { Filter result; for (auto &l : result) l = Info; auto it = find_key(cfg.level_names, scfg.default_level()); if (it == cfg.level_names.end()) return result; auto default_level = it->first; for (auto &l : result) l = default_level; for (auto const &sub_system : scfg.subsystem()) { auto ss_it = find_key(cfg.subsystem_names, sub_system.name()); if (ss_it == cfg.subsystem_names.end()) continue; auto ss = ss_it->first; auto level_it = find_key(cfg.level_names, sub_system.level()); if (level_it == cfg.level_names.end()) continue; auto level = level_it->first; result[ss.ordinal] = level; } return result; } void configure_logging(const LoggingConfig &cfg) { _default_logger_hub.config(cfg); } void configure_logging(const Logging &cfg) { configure_logging(cfg, ThreadGroupCfg()); } void configure_logging(const Logging &cfg, const ThreadGroupCfg &thread_cfg) { LoggingConfig real_cfg; // real_cfg.deployment_type = config::get().type(); real_cfg.logger_initial_queue_size = cfg.initial_queue_size(); real_cfg.logger_max_queue_growth = cfg.max_queue_growth(); real_cfg.level_names = {{NoLog, "NoLog"}, {Diag, "Diag"}, {Info, "Info"}, {Warn, "Warn"}, {Error, "Error"}, {Fatal, "Fatal"}, {Sys, "Sys"}}; real_cfg.subsystem_names = { {Gen, "Gen"}, {Cfg, "Cfg"}, {QBus, "QBus"}, {Log, "Log"}, {Jetfix, "Jetfix"}, {md, "md"}, {Api, "Api"}, {persist, "persist"}, {User, "User"}, {OrderMan, "OrderMan"}, {Assert, "Assert"}, {Engine, "Engine"}, {Ui, "UI"}, {ExSim, "ExSim"}, {Mercury, "Mercury"}, {Testing, "Testing"}, {Latency, "Latency"}, {DataFlow, "DataFlow"}, {Plugin, "Plugin"}, {Sdk, "Sdk"}, {Http, "Http"}, {Spartan, "Spartan"}, {iLink3, "iLink3"}, }; real_cfg.sinks["console"] = std::make_pair( console_sink(real_cfg), get_filter(real_cfg, cfg.console())); real_cfg.sinks["syslog"] = std::make_pair( syslog_sink(real_cfg), get_filter(real_cfg, cfg.syslog())); real_cfg.sinks["file"] = std::make_pair(file_sink(real_cfg), get_filter(real_cfg, cfg.file())); real_cfg.thread_cfg = thread_cfg; configure_logging(real_cfg); } Path log_cache_file_path() { return log_path() / "log_control"; } LoggingConfigPoller::LoggingConfigPoller( const Logging &cfg, const ThreadGroupCfg &thread_cfg) : _poller( utils::Dispatcher::background(), 1000, [=, this]() { poll_once(); }), _last_ts(std::time(nullptr)), _log_cache_file(log_cache_file_path()), _done(false) { init(cfg, thread_cfg); } LoggingConfigPoller::LoggingConfigPoller() : LoggingConfigPoller( logging::protobufs::default_logging_config(), system::threading::ThreadManager{ system::threading::ThreadManager::default_thread_config()} .get_thread_cfg("logger")) {} void LoggingConfigPoller::init( Logging const &cfg, const ThreadGroupCfg &thread_cfg) { auto parent = _log_cache_file.parent_path(); if (!boost::filesystem::is_directory(parent)) bts_assert(boost::filesystem::create_directories(parent)); std::ofstream fs(_log_cache_file.string()); google::protobuf::io::OstreamOutputStream gs(&fs); google::protobuf::TextFormat::Print(cfg, &gs); // initialize logging configure_logging(cfg, thread_cfg); _poller.start(); } void LoggingConfigPoller::poll_once() { if (_done) return; auto current_ts = boost::filesystem::last_write_time(_log_cache_file); if (current_ts != _last_ts) { _last_ts = current_ts; Logging cfg; std::ifstream fs(_log_cache_file.string()); google::protobuf::io::IstreamInputStream gs(&fs); if (google::protobuf::TextFormat::Parse(&gs, &cfg)) { LOG(Log, Info, "Detected logging configuration change. Reconfiguring logging."); configure_logging(cfg); } else LOG(Log, Error, "Detected invalid logging configuration. Ignoring."); } } LoggingConfigPoller::~LoggingConfigPoller() { _done = true; _poller.stop(); } } // namespace bts::logging namespace bts::logging::interop { const char *intern(const char *str) { static std::mutex mutex; static boost::container::flat_set set; std::lock_guard lock(mutex); const auto iter = set.find(str); if (iter != set.end()) { return *iter; } else { #ifdef _WIN32 const char *v = _strdup(str); #else const char *v = strdup(str); #endif set.insert(v); return v; } } extern "C" BTS_EXPORT void Logger_log( int level, int subsystem, const char *message, const char *file, int line) { if (bts::logging::_thread_logger.logger != nullptr) { bts::logging::_thread_logger.logger->enqueue( logging::log_ts(), logging::Subsystem{(uint8_t)subsystem}, logging::Level{(uint8_t)level}, intern(file), line, "{}", std::string(message)); } } } // namespace bts::logging::interop #ifdef SPARK_ENABLE_CCAPI namespace ccapi { CcapiLogger ccapi_logger; Logger *Logger::logger = &ccapi_logger; } // namespace ccapi #endif