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
Source · 3 lines
size_t AuditLogger::entry_count() const {
return entry_count_.load();
}Activity · 1 steps
Requirements
flush
void entropic::AuditLogger::flush()
Force flush buffered entries to disk.
src/storage/audit_logger.cpp:114
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
Requirements
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
Requirements
initialize
bool entropic::AuditLogger::initialize()
Open the log file and prepare for writing.
src/storage/audit_logger.cpp:48
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
Requirements
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
Source · 3 lines
std::filesystem::path AuditLogger::log_path() const {
return config_.log_dir / "audit.jsonl";
}Activity · 1 steps
Requirements
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
Requirements
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
Requirements
write_line
void entropic::AuditLogger::write_line(const std::string &line)
Write a serialized JSON line under mutex.
src/storage/audit_logger.cpp:95
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;
}
}