57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System.Text;
|
|
|
|
namespace CanCommon;
|
|
|
|
/// <summary>
|
|
/// Tees output to the console and a log file. The log ends with a machine-readable
|
|
/// <c>SUMMARY key=value</c> block so a report script can build the comparison table.
|
|
/// </summary>
|
|
public sealed class RunLogger : IDisposable
|
|
{
|
|
private readonly StreamWriter _file;
|
|
public string Path { get; }
|
|
|
|
public RunLogger(string path)
|
|
{
|
|
Path = path;
|
|
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(System.IO.Path.GetFullPath(path))!);
|
|
_file = new StreamWriter(path, append: false) { AutoFlush = true };
|
|
}
|
|
|
|
/// <summary>Console + file.</summary>
|
|
public void Line(string text = "")
|
|
{
|
|
Console.WriteLine(text);
|
|
_file.WriteLine(text);
|
|
}
|
|
|
|
/// <summary>File only (e.g. per-second samples we don't want to spam the log with twice).</summary>
|
|
public void FileOnly(string text)
|
|
{
|
|
_file.WriteLine(text);
|
|
}
|
|
|
|
/// <summary>Emit a machine-readable summary line: <c>SUMMARY key1=v1 key2=v2 ...</c>.</summary>
|
|
public void Summary(IReadOnlyDictionary<string, string> kv)
|
|
{
|
|
var sb = new StringBuilder("SUMMARY");
|
|
foreach (var (k, v) in kv) sb.Append(' ').Append(k).Append('=').Append(v);
|
|
Line(sb.ToString());
|
|
}
|
|
|
|
public void Dispose() => _file.Dispose();
|
|
|
|
/// <summary>Build a timestamped log path, e.g. results/can_master_can_com3_500k_20260815-093012.log</summary>
|
|
public static string BuildPath(string dir, string role, string iface, int bitrate)
|
|
{
|
|
string rate = bitrate switch
|
|
{
|
|
1_000_000 => "1m",
|
|
500_000 => "500k",
|
|
_ => $"{bitrate / 1000}k",
|
|
};
|
|
string ts = DateTime.Now.ToString("yyyyMMdd-HHmmss");
|
|
return System.IO.Path.Combine(dir, $"can_{role}_{iface}_{rate}_{ts}.log");
|
|
}
|
|
}
|