-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
1,525 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
namespace Epilogue; | ||
|
||
[AttributeUsage(AttributeTargets.Class)] | ||
public sealed class CustomLoggerForAttribute : Attribute { | ||
public sealed class CustomLoggerForAttribute : Attribute | ||
{ | ||
Type[] Types { get; init; } = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
using Epilogue.Logging; | ||
using Epilogue.Logging.Errors; | ||
using NetworkTables; | ||
|
||
namespace Epilogue; | ||
|
||
public class EpilogueConfiguration { | ||
public DataLogger DataLogger { get; set; } = new NTDataLogger(NetworkTableInstance.Default); | ||
public class EpilogueConfiguration | ||
{ | ||
public IDataLogger DataLogger { get; set; } = new NTDataLogger(NetworkTableInstance.Default); | ||
public LogImportance MinimumImportance { get; set; } = LogImportance.Debug; | ||
public ErrorHandler ErrorHandler { get; set; } = new(); | ||
public IErrorHandler ErrorHandler { get; set; } = new ErrorPrinter(); | ||
public string Root { get; set; } = "Robot"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
namespace Epilogue; | ||
|
||
public enum LogImportance { | ||
public enum LogImportance | ||
{ | ||
Debug, | ||
Info, | ||
Critical, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
namespace Epilogue; | ||
|
||
public enum LogStrategy { | ||
public enum LogStrategy | ||
{ | ||
OptIn, | ||
OptOut, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,71 @@ | ||
using System.Runtime.InteropServices; | ||
using Epilogue.Logging.Errors; | ||
using WPIUtil.Sendable; | ||
|
||
namespace Epilogue.Logging; | ||
|
||
public abstract class ClassSpecificLogger { | ||
public abstract class ClassSpecificLogger | ||
{ | ||
private readonly Dictionary<ISendable, ISendableBuilder> m_sendables = []; | ||
|
||
protected ClassSpecificLogger(Type loggedType) { | ||
protected ClassSpecificLogger(Type loggedType) | ||
{ | ||
LoggedType = loggedType; | ||
} | ||
|
||
public bool Disabled { get; private set; } | ||
|
||
public void Disable() { | ||
public void Disable() | ||
{ | ||
Disabled = true; | ||
} | ||
|
||
public void Reenable() { | ||
public void Reenable() | ||
{ | ||
Disabled = false; | ||
} | ||
|
||
public Type LoggedType { get; } | ||
|
||
protected virtual void LogSendable(DataLogger dataLogger, ISendable? sendable) { | ||
if (sendable == null) { | ||
protected virtual void LogSendable(IDataLogger dataLogger, ISendable? sendable) | ||
{ | ||
if (sendable is null) | ||
{ | ||
return; | ||
} | ||
|
||
var builder = m_sendables. | ||
} | ||
ref ISendableBuilder? builder = ref CollectionsMarshal.GetValueRefOrAddDefault(m_sendables, sendable, out var _); | ||
if (builder is null) | ||
{ | ||
builder = new LogBackedSenabledBuilder(dataLogger); | ||
sendable.InitSendable(builder); | ||
} | ||
builder.Update(); | ||
} | ||
} | ||
|
||
public abstract class ClassSpecificLogger<T> : ClassSpecificLogger { | ||
public abstract class ClassSpecificLogger<T> : ClassSpecificLogger | ||
{ | ||
protected ClassSpecificLogger() : base(typeof(T)) | ||
{ | ||
} | ||
|
||
protected abstract void Update(DataLogger dataLogger, T obj); | ||
protected abstract void Update(IDataLogger dataLogger, T obj); | ||
|
||
public void TryUpdate(DataLogger dataLogger, T obj, ErrorHandler errorHandler) { | ||
if (Disabled) { | ||
public void TryUpdate(IDataLogger dataLogger, T obj, IErrorHandler errorHandler) | ||
{ | ||
if (Disabled) | ||
{ | ||
return; | ||
} | ||
|
||
try { | ||
try | ||
{ | ||
Update(dataLogger, obj); | ||
} catch (Exception e) { | ||
errorHandler(e, this); | ||
} | ||
catch (Exception e) | ||
{ | ||
errorHandler.Handle(e, this); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
namespace Epilogue.Logging.Errors; | ||
|
||
public class CrashOnError : IErrorHandler | ||
{ | ||
public void Handle(Exception exception, ClassSpecificLogger logger) | ||
{ | ||
#pragma warning disable CA2201 // Do not raise reserved exception types | ||
throw new Exception($"[EPILOGUE] An error occured while logging an instance of {logger.LoggedType.Name}", exception); | ||
#pragma warning restore CA2201 // Do not raise reserved exception types | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
namespace Epilogue.Logging.Errors; | ||
|
||
public class ErrorPrinter : IErrorHandler | ||
{ | ||
public void Handle(Exception exception, ClassSpecificLogger logger) | ||
{ | ||
Console.Error.WriteLine($"[EPILOGUE] An error occured while logging an instance of {logger.LoggedType.Name}: {exception.Message}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
namespace Epilogue.Logging.Errors; | ||
|
||
public interface IErrorHandler { | ||
public interface IErrorHandler | ||
{ | ||
void Handle(Exception exception, ClassSpecificLogger logger); | ||
|
||
public static IErrorHandler CrashOnError() => new CrashOnError(); | ||
|
||
public static IErrorHandler PrintErrorMessages() => new ErrorPrint(); | ||
public static IErrorHandler PrintErrorMessages() => new ErrorPrinter(); | ||
|
||
public static LoggerDisabler Disabling(int maximumPermissableErrors) => LoggerDisabler.ForLimit(maximumPermissableErrors); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,39 @@ | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
namespace Epilogue.Logging.Errors; | ||
|
||
public class LoggerDisabler : IErrorHandler { | ||
public class LoggerDisabler : IErrorHandler | ||
{ | ||
private readonly int m_threshold; | ||
private readonly Dictionary<ClassSpecificLogger, int> m_errorCounts = []; | ||
|
||
public LoggerDisabler(int threshold) { | ||
public LoggerDisabler(int threshold) | ||
{ | ||
m_threshold = threshold; | ||
} | ||
|
||
public static LoggerDisabler ForLimit(int threshold) => new LoggerDisabler(threshold); | ||
public static LoggerDisabler ForLimit(int threshold) => new(threshold); | ||
|
||
public void Handle(Exception exception, ClassSpecificLogger logger) | ||
{ | ||
throw new NotImplementedException(); | ||
int errorCount = ++CollectionsMarshal.GetValueRefOrAddDefault(m_errorCounts, logger, out var _); | ||
|
||
if (errorCount > m_threshold) | ||
{ | ||
logger.Disable(); | ||
Console.Error.WriteLine($"[EPILOGUE] Too many errors detected in {logger.GetType().Name} (maximum allowed: {m_threshold}). The most recent error follows:"); | ||
Console.Error.WriteLine(exception.Message); | ||
Console.Error.WriteLine(exception.StackTrace); | ||
} | ||
} | ||
|
||
public void Reset() | ||
{ | ||
foreach (var logger in m_errorCounts) | ||
{ | ||
logger.Key.Reenable(); | ||
} | ||
m_errorCounts.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System.Runtime.InteropServices; | ||
using WPIUtil.Logging; | ||
using WPIUtil.Serialization.Struct; | ||
using static WPIUtil.WpiGuard; | ||
|
||
namespace Epilogue.Logging; | ||
|
||
public class FileLogger : IDataLogger | ||
{ | ||
private readonly DataLog m_dataLog; | ||
private readonly Dictionary<string, DataLogEntry> m_entries = []; | ||
private readonly Dictionary<string, SubLogger> m_subLoggers = []; | ||
|
||
public FileLogger(DataLog dataLog) | ||
{ | ||
m_dataLog = RequireNotNull(dataLog); | ||
} | ||
|
||
public IDataLogger Lazy => new LazyLogger(this); | ||
public IDataLogger GetSubLogger(string path) => IDataLogger.GetDefaultSubLogger(this, path, m_subLoggers); | ||
|
||
private E GetEntry<E>(string identifier, Func<DataLog, string, E> ctor) where E : DataLogEntry | ||
{ | ||
ref var entry = ref CollectionsMarshal.GetValueRefOrAddDefault(m_entries, identifier, out var _); | ||
entry ??= ctor(m_dataLog, identifier); | ||
return (E)entry; | ||
} | ||
|
||
public void Log(string identifier, bool value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new BooleanLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, int value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new IntegerLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, long value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new IntegerLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, float value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new FloatLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, double value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new DoubleLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<byte> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new RawLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<bool> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new BooleanArrayLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<int> value) | ||
{ | ||
long[] widended = new long[value.Length]; | ||
for (int i = 0; i < value.Length; i++) | ||
{ | ||
widended[i] = value[i]; | ||
} | ||
GetEntry(identifier, static (a, b) => new IntegerArrayLogEntry(a, b)).Append(widended); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<long> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new IntegerArrayLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<float> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new FloatArrayLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<double> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new DoubleArrayLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, string value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<char> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log(string identifier, ReadOnlySpan<string> value) | ||
{ | ||
GetEntry(identifier, static (a, b) => new StringArrayLogEntry(a, b)).Append(value); | ||
} | ||
|
||
public void Log<T>(string identifier, in T value) where T : IStructSerializable<T> | ||
{ | ||
GetEntry(identifier, static (a, b) => new StructLogEntry<T>(a, b)).Append(value); | ||
} | ||
|
||
public void Log<T>(string identifier, T[] value) where T : IStructSerializable<T> | ||
{ | ||
GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); | ||
} | ||
|
||
public void Log<T>(string identifier, Span<T> value) where T : IStructSerializable<T> | ||
{ | ||
GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); | ||
} | ||
|
||
public void Log<T>(string identifier, ReadOnlySpan<T> value) where T : IStructSerializable<T> | ||
{ | ||
GetEntry(identifier, static (a, b) => new StructArrayLogEntry<T>(a, b)).Append(value); | ||
} | ||
|
||
public void Log<T>(string identifier, T value) where T : Enum | ||
{ | ||
GetEntry(identifier, static (a, b) => new StringLogEntry(a, b)).Append(value.ToString()); | ||
} | ||
} |
Oops, something went wrong.