using System.Text.Json;
namespace ZB.MOM.WW.ScadaBridge.CLI.Commands;
///
/// Renders a page of audit-log events to a writer. The audit query command picks
/// a formatter from the --format option. The default JSONL formatter is defined
/// here; the human-readable table formatter is supplied by Bundle C.
///
public interface IAuditFormatter
{
/// Renders one page of events. Called once per fetched page.
/// The audit events on this page.
/// Writer to render the formatted output to.
void WritePage(IReadOnlyList events, TextWriter output);
}
///
/// Default formatter: one JSON object per line (JSONL). Streamable — each page's events
/// are flushed as they arrive, so --all over many pages does not accumulate.
///
public sealed class JsonLinesAuditFormatter : IAuditFormatter
{
private static readonly JsonSerializerOptions Compact = new() { WriteIndented = false };
///
public void WritePage(IReadOnlyList events, TextWriter output)
{
foreach (var evt in events)
output.WriteLine(JsonSerializer.Serialize(evt, Compact));
}
}
///
/// Resolves an for a given --format value:
/// table renders a column-aligned text table (),
/// any other value (including json) renders JSONL.
///
public static class AuditFormatterFactory
{
///
/// Returns an for the given format name.
///
/// Format name; table selects the table formatter, any other value selects JSONL.
/// Writer for notice messages emitted during formatting.
public static IAuditFormatter Create(string format, TextWriter notices)
{
if (string.Equals(format, "table", StringComparison.OrdinalIgnoreCase))
return new TableAuditFormatter();
return new JsonLinesAuditFormatter();
}
}