39 lines
include/audit_writer.h
Declares AuditWriter and the logEvent contract.
// AuditWriter: durable per-event security log interface.
#pragma once
#include <string>
 
struct AuditEvent {
  std::string userId;      // acting user identifier
  std::string action;      // name of the audited action
  std::string resourceId;  // target resource identifier
  long long   timestampMs; // event time in milliseconds since epoch
};
 
// Writes security audit events to a local log file.
// Each event is serialised as a newline-delimited JSON object.
// Thread-safe: concurrent callers must hold an external lock.
class AuditWriter {
public:
  // Parameters:
  //   path - filesystem path to the audit log file (opened in append mode)
  // Returns: new AuditWriter; check isOpen() before first use
  explicit AuditWriter(const std::string& path);
  ~AuditWriter();
 
  // Parameters:
  //   event - fully populated audit event to append to the log
  // Returns: true if the event was fully written successfully, false on any I/O error
  bool logEvent(const AuditEvent& event);
 
  // Parameters: none
  // Returns: true if the underlying file descriptor is open and ready
  bool isOpen() const { return fd_ >= 0; }
 
private:
  int fd_ = -1; // underlying file descriptor; -1 means closed
 
  // Parameters:
  //   event - audit event to format
  // Returns: newline-terminated JSON representation of the event
  std::string formatEvent(const AuditEvent& event) const;
};