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

Show on the map →

Called by none

Calls none

Source · 4 lines
HandleLogScope::HandleLogScope(int handle_id)
    : prev_id_(t_current_handle_id) {
    t_current_handle_id = handle_id;
}
Activity · 1 steps
lines 522–522t_current_handle_id = handle_id;

Requirements

REQ-TYPE-001

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

Show on the map →

Called by SCENARIO

Calls none

Source · 3 lines
int current_handle_id() {
    return t_current_handle_id;
}
Activity · 1 steps
lines 512–512return t_current_handle_id;

Requirements

REQ-TYPE-001

get

std::shared_ptr< spdlog::logger > entropic::log::get(const std::string &name)

Get or create a named logger.

src/types/logging.cpp:211

Show on the map →

Called by SCENARIO, capture_prompt_log

Calls init

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
lines 212–212auto logger = spdlog::get(name);lines 213–213◇ if (logger)lines 214–214return logger;lines 216–216◇ if (!s_sink)lines 217–217init(spdlog::level::info);lines 219–219auto default_logger = spdlog::default_logger();lines 220–222auto new_logger = std::make_shared<spdlog::logger>( name, default_logger->sinks().begin(), default_logger->sinks().end());lines 226–226◇ if (s_console_disabled.load() && s_sink)lines 227–227auto& sinks = new_logger->sinks();lines 228–230sinks.erase( std::remove(sinks.begin(), sinks.end(), s_sink), sinks.end());lines 232–232new_logger->set_level(default_logger->level());lines 233–233spdlog::register_logger(new_logger);lines 234–234return new_logger;

Requirements

REQ-TYPE-001

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
lines 146–163std::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"); });

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

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
lines 473–473◇ if (handle_id == 0 || log_dir.empty())lines 473–473return;lines 474–474◇ if (!s_dispatcher)lines 474–474init(spdlog::level::info);lines 476–476auto log_file = log_dir / "session.log";lines 477–477std::filesystem::create_directories(log_file.parent_path());lines 478–480auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>( log_file.string(), /*truncate=*/true);lines 481–481file_sink->set_level(spdlog::level::trace);lines 482–483file_sink->set_pattern( "[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %v");lines 484–484s_dispatcher->register_sink(handle_id, file_sink);lines 488–488auto model_file = log_dir / "session_model.log";lines 489–489FILE* fp = fopen(model_file.string().c_str(), "w");lines 490–490◇ if (fp)lines 490–490fclose(fp);lines 492–492spdlog::flush_on(spdlog::level::trace);

Requirements

REQ-TYPE-001

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

Show on the map →

Called by SCENARIO, entropic_destroy

Calls unregister_sink

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
lines 501–501◇ if (!s_dispatcher || handle_id == 0)lines 501–501return;lines 502–502s_dispatcher->unregister_sink(handle_id);

Requirements

REQ-TYPE-001

Flows

~HandleLogScope

entropic::log::HandleLogScope::~HandleLogScope()

Exit scope; restores previous handle_id.

src/types/logging.cpp:530

Show on the map →

Called by none

Calls none

The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.

Requirements

REQ-TYPE-001