require "time"
require_relative "result"

module Relay
  # Append-only event log. Every recorded event carries the moment it
  # happened as a UTC ISO-8601 string.
  class Log
    def initialize(path)
      @path = path
    end

    def record(event, detail = nil)
      stamp = Time.now.utc.iso8601
      line = [stamp, event, detail].compact.join("\t")
      File.open(@path, "a") { |f| f.puts(line) }
      Result.ok(line)
    rescue SystemCallError => e
      Result.err("log write failed: #{e.message}")
    end
  end
end
