audit_logger.cpp

src/storage/audit_logger.cpp

@file audit_logger.cpp @brief AuditLogger implementation — JSONL recording and hook callback.

Owners: @tvanfossen

entry_count

size_t entropic::AuditLogger::entry_count() const

Get the number of entries recorded this session.

src/storage/audit_logger.cpp:128

Show on the map →

Called by SCENARIO, SCENARIO, entropic_audit_count

Calls none

Source · 3 lines
size_t AuditLogger::entry_count() const {
    return entry_count_.load();
}
Activity · 1 steps
lines 129–129return entry_count_.load();

Requirements

REQ-STOR-008

flush

void entropic::AuditLogger::flush()

Force flush buffered entries to disk.

src/storage/audit_logger.cpp:114

Show on the map →

Called by SCENARIO, SCENARIO, SCENARIO, entropic_audit_flush

Calls none

Source · 7 lines
void AuditLogger::flush() {
    std::lock_guard<std::mutex> lock(write_mutex_);
    if (file_.is_open()) {
        file_.flush();
        buffered_count_ = 0;
    }
}
Activity · 4 steps
lines 115–115std::lock_guard<std::mutex> lock(write_mutex_);lines 116–116◇ if (file_.is_open())lines 117–117file_.flush();lines 118–118buffered_count_ = 0;

Requirements

REQ-STOR-008

hook_callback

int entropic::AuditLogger::hook_callback(entropic_hook_point_t hook_point, const char *context_json, char **modified_json, void *user_data)

Hook callback for POST_TOOL_CALL integration.

src/storage/audit_logger.cpp:211

Source · 27 lines
int AuditLogger::hook_callback(
    entropic_hook_point_t /*hook_point*/,
    const char* context_json,
    char** modified_json,
    void* user_data) {
    *modified_json = nullptr;
    auto* ctx = static_cast<AuditHookContext*>(user_data);
    if (ctx == nullptr || ctx->logger == nullptr) {
        return 0;
    }
    try {
        auto j = nlohmann::json::parse(context_json);
        AuditEntry entry;
        entry.tool_name = j.value("tool_name", "");
        entry.params_json = j.contains("args")
            ? j["args"].dump() : "{}";
        entry.result_content = j.value("result", "");
        entry.elapsed_ms = j.value("elapsed_ms", 0.0);
        entry.directives_json = j.contains("directives")
            ? j["directives"].dump() : "[]";
        populate_from_hook_context(entry, *ctx);
        ctx->logger->record(entry);
    } catch (const std::exception& e) {
        logger->error("Audit hook callback failed: {}", e.what());
    }
    return 0;
}
Activity · 6 steps
lines 216–216*modified_json = nullptr;lines 217–217auto* ctx = static_cast<AuditHookContext*>(user_data);lines 218–218◇ if (ctx == nullptr || ctx->logger == nullptr)lines 219–219return 0;lines 221–235try { auto j = nlohmann::json::parse(context_json); AuditEntry entry; entry.tool_name = j.value("tool_name", ""); entry.params_json = j.contains("args") ? j["args"].dump() : "{}"; entry.result_content = j.value("result", ""); entry.elapsed_ms = j.value("elapsed_ms", 0.0); entry.directives_json = j.contains("directives") ? j["directives"].dump() : "[]"; populate_from_hook_context(entry, *ctx); ctx->logger->record(entry); } catch (const std::exception& e) { logger->error("Audit hook callback failed: {}", e.what()); }lines 236–236return 0;

Requirements

REQ-STOR-008

initialize

bool entropic::AuditLogger::initialize()

Open the log file and prepare for writing.

src/storage/audit_logger.cpp:48

Show on the map →

Called by SCENARIO, SCENARIO

Calls none

Source · 17 lines
bool AuditLogger::initialize() {
    if (!config_.enabled) {
        logger->info("Audit logging disabled");
        return true;
    }
    std::filesystem::create_directories(config_.log_dir);
    auto path = config_.log_dir / "audit.jsonl";
    current_file_size_ = std::filesystem::exists(path)
        ? std::filesystem::file_size(path) : 0;
    file_.open(path, std::ios::app);
    if (!file_.is_open()) {
        logger->error("Failed to open audit log: {}", path.string());
        return false;
    }
    logger->info("Audit log opened: {}", path.string());
    return true;
}
Activity · 12 steps
lines 49–49◇ if (!config_.enabled)lines 50–50logger->info("Audit logging disabled");lines 51–51return true;lines 53–53std::filesystem::create_directories(config_.log_dir);lines 54–54auto path = config_.log_dir / "audit.jsonl";lines 55–56current_file_size_ = std::filesystem::exists(path) ? std::filesystem::file_size(path) : 0;lines 57–57file_.open(path, std::ios::app);lines 58–58◇ if (!file_.is_open())lines 59–59logger->error("Failed to open audit log: {}", path.string());lines 60–60return false;lines 62–62logger->info("Audit log opened: {}", path.string());lines 63–63return true;

Requirements

REQ-STOR-008

log_path

std::filesystem::path entropic::AuditLogger::log_path() const

Get the file path of the current audit log.

src/storage/audit_logger.cpp:138

Show on the map →

Called by SCENARIO, SCENARIO

Calls none

Source · 3 lines
std::filesystem::path AuditLogger::log_path() const {
    return config_.log_dir / "audit.jsonl";
}
Activity · 1 steps
lines 139–139return config_.log_dir / "audit.jsonl";

Requirements

REQ-STOR-008

populate_from_hook_context

void entropic::populate_from_hook_context(AuditEntry &entry, const AuditHookContext &ctx)

Populate AuditEntry fields from AuditHookContext state.

src/storage/audit_logger.cpp:246

Source · 10 lines
void populate_from_hook_context(AuditEntry& entry,
                                const AuditHookContext& ctx) {
    entry.caller_id = ctx.caller_id ? *ctx.caller_id : "unknown";
    entry.delegation_depth = ctx.delegation_depth ? *ctx.delegation_depth : 0;
    entry.iteration = ctx.iteration ? *ctx.iteration : 0;
    entry.parent_conversation_id = ctx.parent_conversation_id
        ? *ctx.parent_conversation_id : "";
    entry.result_status = entry.result_content.empty()
        ? "error" : "success";
}
Activity · 5 steps
lines 248–248entry.caller_id = ctx.caller_id ? *ctx.caller_id : "unknown";lines 249–249entry.delegation_depth = ctx.delegation_depth ? *ctx.delegation_depth : 0;lines 250–250entry.iteration = ctx.iteration ? *ctx.iteration : 0;lines 251–252entry.parent_conversation_id = ctx.parent_conversation_id ? *ctx.parent_conversation_id : "";lines 253–254entry.result_status = entry.result_content.empty() ? "error" : "success";

Requirements

REQ-STOR-008

record

void entropic::AuditLogger::record(const AuditEntry &entry)

Record a tool call audit entry.

src/storage/audit_logger.cpp:72

Source · 16 lines
void AuditLogger::record(const AuditEntry& entry) {
    if (!config_.enabled || !file_.is_open()) {
        return;
    }
    nlohmann::json line = audit_entry_to_json(entry);
    line["version"] = 1;
    line["timestamp"] = utc_timestamp();
    line["session_id"] = config_.session_id;
    line["sequence"] = static_cast<int64_t>(sequence_.fetch_add(1));

    std::string serialized = line.dump();
    write_line(serialized);
    auto seq = entry_count_.fetch_add(1);
    logger->info("Audit: tool='{}', caller='{}', seq={}",
                 entry.tool_name, entry.caller_id, seq);
}
Activity · 11 steps
lines 73–73◇ if (!config_.enabled || !file_.is_open())lines 74–74return;lines 76–76nlohmann::json line = audit_entry_to_json(entry);lines 77–77line["version"] = 1;lines 78–78line["timestamp"] = utc_timestamp();lines 79–79line["session_id"] = config_.session_id;lines 80–80line["sequence"] = static_cast<int64_t>(sequence_.fetch_add(1));lines 82–82std::string serialized = line.dump();lines 83–83write_line(serialized);lines 84–84auto seq = entry_count_.fetch_add(1);lines 85–86logger->info("Audit: tool='{}', caller='{}', seq={}", entry.tool_name, entry.caller_id, seq);

Requirements

REQ-STOR-008

write_line

void entropic::AuditLogger::write_line(const std::string &line)

Write a serialized JSON line under mutex.

src/storage/audit_logger.cpp:95

Show on the map →

Called by record

Calls rotate_if_needed

Source · 13 lines
void AuditLogger::write_line(const std::string& line) {
    std::lock_guard<std::mutex> lock(write_mutex_);
    rotate_if_needed();
    file_ << line << '\n';
    current_file_size_ += line.size() + 1;
    buffered_count_++;
    bool should_flush = (config_.flush_interval_entries == 0)
        || (buffered_count_ >= config_.flush_interval_entries);
    if (should_flush) {
        file_.flush();
        buffered_count_ = 0;
    }
}
Activity · 9 steps
lines 96–96std::lock_guard<std::mutex> lock(write_mutex_);lines 97–97rotate_if_needed();lines 98–98file_ << line << '\n';lines 99–99current_file_size_ += line.size() + 1;lines 100–100buffered_count_++;lines 101–102bool should_flush = (config_.flush_interval_entries == 0) || (buffered_count_ >= config_.flush_interval_entries);lines 103–103◇ if (should_flush)lines 104–104file_.flush();lines 105–105buffered_count_ = 0;

Requirements

REQ-STOR-008