|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using WPIUtil.Logging; |
| 3 | +using WPIUtil.Serialization.Struct; |
| 4 | +using static WPIUtil.WpiGuard; |
| 5 | + |
| 6 | +namespace Epilogue.Logging; |
| 7 | + |
| 8 | +public class FileLogger : IDataLogger |
| 9 | +{ |
| 10 | + private readonly DataLog m_dataLog; |
| 11 | + private readonly Dictionary<string, DataLogEntry> m_entries = []; |
| 12 | + private readonly Dictionary<string, SubLogger> m_subLoggers = []; |
| 13 | + |
| 14 | + public FileLogger(DataLog dataLog) |
| 15 | + { |
| 16 | + m_dataLog = RequireNotNull(dataLog); |
| 17 | + } |
| 18 | + |
| 19 | + public IDataLogger Lazy => new LazyLogger(this); |
| 20 | + public IDataLogger GetSubLogger(string path) => IDataLogger.GetDefaultSubLogger(this, path, m_subLoggers); |
| 21 | + |
| 22 | + private E GetEntry<E>(string identifier, Func<DataLog, string, E> ctor) where E : DataLogEntry |
| 23 | + { |
| 24 | + ref var entry = ref CollectionsMarshal.GetValueRefOrAddDefault(m_entries, identifier, out var _); |
| 25 | + entry ??= ctor(m_dataLog, identifier); |
| 26 | + return (E)entry; |
| 27 | + } |
| 28 | + |
| 29 | + public void Log(string identifier, bool value) |
| 30 | + { |
| 31 | + GetEntry(identifier, static (a, b) => new BooleanLogEntry(a, b)).Append(value); |
| 32 | + } |
| 33 | + |
| 34 | + public void Log(string identifier, int value) |
| 35 | + { |
| 36 | + GetEntry(identifier, static (a, b) => new IntegerLogEntry(a, b)).Append(value); |
| 37 | + } |
| 38 | + |
| 39 | + public void Log(string identifier, long value) |
| 40 | + { |
| 41 | + GetEntry(identifier, static (a, b) => new IntegerLogEntry(a, b)).Append(value); |
| 42 | + } |
| 43 | + |
| 44 | + public void Log(string identifier, float value) |
| 45 | + { |
| 46 | + GetEntry(identifier, static (a, b) => new FloatLogEntry(a, b)).Append(value); |
| 47 | + } |
| 48 | + |
| 49 | + public void Log(string identifier, double value) |
| 50 | + { |
| 51 | + GetEntry(identifier, static (a, b) => new DoubleLogEntry(a, b)).Append(value); |
| 52 | + } |
| 53 | + |
| 54 | + public void Log(string identifier, ReadOnlySpan<byte> value) |
| 55 | + { |
| 56 | + GetEntry(identifier, static (a, b) => new RawLogEntry(a, b)).Append(value); |
| 57 | + } |
| 58 | + |
| 59 | + public void Log(string identifier, ReadOnlySpan<bool> value) |
| 60 | + { |
| 61 | + GetEntry(identifier, static (a, b) => new BooleanArrayLogEntry(a, b)).Append(value); |
| 62 | + } |
| 63 | + |
| 64 | + public void Log(string identifier, ReadOnlySpan<int> value) |
| 65 | + { |
| 66 | + long[] widended = new long[value.Length]; |
| 67 | + for (int i = 0; i < value.Length; i++) |
| 68 | + { |
| 69 | + widended[i] = value[i]; |
| 70 | + } |
| 71 | + GetEntry(identifier, static (a, b) => new IntegerArrayLogEntry(a, b)).Append(widended); |
| 72 | + } |
| 73 | + |
| 74 | + public void Log(string identifier, ReadOnlySpan<long> value) |
| 75 | + { |
| 76 | + GetEntry(identifier, static (a, b) => new IntegerArrayLogEntry(a, b)).Append(value); |
| 77 | + } |
| 78 | + |
| 79 | + public void Log(string identifier, ReadOnlySpan<float> value) |
| 80 | + { |
| 81 | + GetEntry(identifier, static (a, b) => new FloatArrayLogEntry(a, b)).Append(value); |
| 82 | + } |
| 83 | + |
| 84 | + public void Log(string identifier, ReadOnlySpan<double> value) |
| 85 | + { |
| 86 | + GetEntry(identifier, static (a, b) => new DoubleArrayLogEntry(a, b)).Append(value); |
| 87 | + } |
| 88 | + |
| 89 | + public void Log(string identifier, string value) |
| 90 | + { |
| 91 | + GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value); |
| 92 | + } |
| 93 | + |
| 94 | + public void Log(string identifier, ReadOnlySpan<char> value) |
| 95 | + { |
| 96 | + GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value); |
| 97 | + } |
| 98 | + |
| 99 | + public void Log(string identifier, ReadOnlySpan<string> value) |
| 100 | + { |
| 101 | + GetEntry(identifier, static (a, b) => new StringArrayLogEntry(a, b)).Append(value); |
| 102 | + } |
| 103 | + |
| 104 | + public void Log<T>(string identifier, in T value) where T : IStructSerializable<T> |
| 105 | + { |
| 106 | + GetEntry(identifier, static (a, b) => new StructLogEntry<T>(a, b)).Append(value); |
| 107 | + } |
| 108 | + |
| 109 | + public void Log<T>(string identifier, T[] value) where T : IStructSerializable<T> |
| 110 | + { |
| 111 | + GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); |
| 112 | + } |
| 113 | + |
| 114 | + public void Log<T>(string identifier, Span<T> value) where T : IStructSerializable<T> |
| 115 | + { |
| 116 | + GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); |
| 117 | + } |
| 118 | + |
| 119 | + public void Log<T>(string identifier, ReadOnlySpan<T> value) where T : IStructSerializable<T> |
| 120 | + { |
| 121 | + GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); |
| 122 | + } |
| 123 | + |
| 124 | + public void Log<T>(string identifier, T value) where T : Enum |
| 125 | + { |
| 126 | + GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value.ToString()); |
| 127 | + } |
| 128 | +} |
0 commit comments