logging.cpp¶
src/types/logging.cpp
@file logging.cpp @brief spdlog initialization, logger factory, and formatting utilities.
Owners: @tvanfossen
HandleLogScope
entropic::log::HandleLogScope::HandleLogScope(int handle_id)
Enter scope; sets thread current handle_id.
src/types/logging.cpp:520
Source · 4 lines
HandleLogScope::HandleLogScope(int handle_id)
: prev_id_(t_current_handle_id) {
t_current_handle_id = handle_id;
}Activity · 1 steps
Requirements
current_handle_id
int entropic::log::current_handle_id()
gh#59 (v2.3.1): query the current thread's handle_id.
src/types/logging.cpp:511
Source · 3 lines
int current_handle_id() {
return t_current_handle_id;
}Activity · 1 steps
Requirements
get
std::shared_ptr< spdlog::logger > entropic::log::get(const std::string &name)
Get or create a named logger.
src/types/logging.cpp:211
Source · 25 lines
std::shared_ptr<spdlog::logger> get(const std::string& name) {
auto logger = spdlog::get(name);
if (logger) {
return logger;
}
if (!s_sink) {
init(spdlog::level::info);
}
auto default_logger = spdlog::default_logger();
auto new_logger = std::make_shared<spdlog::logger>(
name, default_logger->sinks().begin(),
default_logger->sinks().end());
// gh#59 follow-up (v2.3.7): if the console was disabled, the
// default logger may still carry s_sink (or a copy raced in);
// strip it so this lazily-created logger doesn't leak to stderr.
if (s_console_disabled.load() && s_sink) {
auto& sinks = new_logger->sinks();
sinks.erase(
std::remove(sinks.begin(), sinks.end(), s_sink),
sinks.end());
}
new_logger->set_level(default_logger->level());
spdlog::register_logger(new_logger);
return new_logger;
}Activity · 13 steps
Requirements
init
void entropic::log::init(spdlog::level::level_enum level=spdlog::level::info)
Initialize the logging subsystem.
src/types/logging.cpp:145
Source · 20 lines
void init(spdlog::level::level_enum level) {
std::call_once(s_init_flag, [level]() {
s_sink = std::make_shared<spdlog::sinks::stderr_color_sink_mt>();
s_sink->set_level(level);
// gh#59 (v2.3.1): install the per-handle dispatcher alongside
// stderr on the default logger. Subsystems' loggers (created
// via get()) inherit both sinks. The dispatcher routes file
// writes based on thread_local current handle_id; stderr is
// shared across handles (process-global console is acceptable
// since operators reading stderr already see everything).
s_dispatcher = std::make_shared<HandleAwareSink>();
s_dispatcher->set_level(level);
auto root = std::make_shared<spdlog::logger>(
"entropic",
spdlog::sinks_init_list{s_sink, s_dispatcher});
spdlog::set_default_logger(root);
spdlog::set_level(level);
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%n] [%^%l%$] %v");
});
}Activity · 1 steps
Flows
register_handle_log
void entropic::log::register_handle_log(int handle_id, const std::filesystem::path &log_dir)
gh#59 (v2.3.1): register a per-handle session.log file.
src/types/logging.cpp:470
Called by SCENARIO, do_configure_dir, do_configure_from_file, do_configure_json
Calls init, register_sink
Source · 24 lines
void register_handle_log(
int handle_id, const std::filesystem::path& log_dir)
{
if (handle_id == 0 || log_dir.empty()) { return; }
if (!s_dispatcher) { init(spdlog::level::info); }
auto log_file = log_dir / "session.log";
std::filesystem::create_directories(log_file.parent_path());
auto file_sink =
std::make_shared<spdlog::sinks::basic_file_sink_mt>(
log_file.string(), /*truncate=*/true);
file_sink->set_level(spdlog::level::trace);
file_sink->set_pattern(
"[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v");
s_dispatcher->register_sink(handle_id, file_sink);
// Match the v2.2.5 setup_session behavior: truncate
// session_model.log too so a fresh session starts clean.
auto model_file = log_dir / "session_model.log";
FILE* fp = fopen(model_file.string().c_str(), "w");
if (fp) { fclose(fp); }
spdlog::flush_on(spdlog::level::trace);
}Activity · 15 steps
Requirements
unregister_handle_log
void entropic::log::unregister_handle_log(int handle_id)
gh#59 (v2.3.1): release a handle's session.log file sink.
src/types/logging.cpp:500
Source · 4 lines
void unregister_handle_log(int handle_id) {
if (!s_dispatcher || handle_id == 0) { return; }
s_dispatcher->unregister_sink(handle_id);
}Activity · 3 steps
Requirements
Flows
~HandleLogScope
entropic::log::HandleLogScope::~HandleLogScope()
Exit scope; restores previous handle_id.
src/types/logging.cpp:530
The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.