Skip to content

Commit

Permalink
Update DataLog to be split
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed May 27, 2024
1 parent 329e057 commit 7a9d4e5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/wpilibsharp/DataLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace WPILib;

public static class DataLogManger
{
private static DataLog? m_log;
private static DataLogBackgroundWriter? m_log;
private static bool m_stopped;
private static string? m_logDir;
private static bool m_filenameOverride;
Expand Down Expand Up @@ -51,7 +51,7 @@ public static void Start(string dir = "", string filename = "", double period =
Console.Error.WriteLine($"DataLogManager: could not delete {file}");
}
}
m_log = new DataLog(m_logDir, MakeLogFilename(filename), period);
m_log = new(m_logDir, MakeLogFilename(filename), period);
m_messageLog = new StringLogEntry(m_log, "messages");

if (m_ntLoggerEnabled)
Expand Down
49 changes: 6 additions & 43 deletions src/wpiutil/Logging/DataLog.cs
Original file line number Diff line number Diff line change
@@ -1,57 +1,21 @@
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WPIUtil.Handles;
using WPIUtil.Natives;
using WPIUtil.Serialization.Protobuf;
using WPIUtil.Serialization.Struct;

namespace WPIUtil.Logging;

public sealed unsafe class DataLog : IDisposable
public unsafe class DataLog : IDisposable
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static void NativeDataLogCallback(void* ptr, byte* data, nuint len)
{
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
if (handle.Target is DataLog datalog)
{
datalog.callback?.Invoke(new ReadOnlySpan<byte>(data, (int)len));
}
}

private GCHandle? gcHandle;
private readonly DataLogCallback? callback;

public delegate void DataLogCallback(ReadOnlySpan<byte> data);

public DataLog(string dir = "", string filename = "", double period = 0.25, string extraHeader = "")
{
NativeHandle = DataLogNative.Create(dir, filename, period, extraHeader);
protected DataLog(OpaqueDataLog* impl) {
NativeHandle = impl;
}

public DataLog(DataLogCallback callback, double period = 0.25, string extraHeader = "")
{
gcHandle = GCHandle.Alloc(this);
this.callback = callback;
NativeHandle = DataLogNative.CreateFunc(&NativeDataLogCallback, (void*)GCHandle.ToIntPtr(gcHandle.Value), period, extraHeader);
}

public void Dispose()
public virtual void Dispose()
{
GC.SuppressFinalize(this);
DataLogNative.Release(NativeHandle);
if (gcHandle.HasValue)
{
gcHandle.Value.Free();
}
}

public string Filename
{
set
{
DataLogNative.SetFilename(NativeHandle, value);
}
}

public void Flush()
Expand Down Expand Up @@ -228,6 +192,5 @@ private void AddSchemaImpl(IStructBase value, long timestamp, HashSet<string> se
}

private readonly ConcurrentDictionary<string, int> m_schemaMap = [];

public unsafe OpaqueDataLog* NativeHandle { get; } = null;
public unsafe OpaqueDataLog* NativeHandle { get; protected init;}
}
50 changes: 50 additions & 0 deletions src/wpiutil/Logging/DataLogBackgroundWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WPIUtil.Natives;

namespace WPIUtil.Logging;

public sealed unsafe class DataLogBackgroundWriter : DataLog
{
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static void NativeDataLogCallback(void* ptr, byte* data, nuint len)
{
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
if (handle.Target is DataLogBackgroundWriter datalog)
{
datalog.callback?.Invoke(new ReadOnlySpan<byte>(data, (int)len));
}
}

private GCHandle? gcHandle;
private readonly DataLogCallback? callback;

public delegate void DataLogCallback(ReadOnlySpan<byte> data);

public DataLogBackgroundWriter(string dir = "", string filename = "", double period = 0.25, string extraHeader = "") : base(DataLogNative.CreateBg(dir, filename, period, extraHeader)) {

}

public DataLogBackgroundWriter(DataLogCallback callback, double period = 0.25, string extraHeader = "") : base(null)
{
gcHandle = GCHandle.Alloc(this);
this.callback = callback;
NativeHandle = DataLogNative.CreateBgFunc(&NativeDataLogCallback, (void*)GCHandle.ToIntPtr(gcHandle.Value), period, extraHeader);
}

public override void Dispose()
{
base.Dispose();
if (gcHandle.HasValue) {
gcHandle.Value.Free();
}
}

public string Filename
{
set
{
DataLogNative.SetFilename(NativeHandle, value);
}
}
}
9 changes: 9 additions & 0 deletions src/wpiutil/Logging/DataLogWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using WPIUtil.Natives;

namespace WPIUtil.Logging;

public unsafe class DataLogWriter : DataLog
{
public DataLogWriter(string filename, string extraHeader = "") : base(DataLogNative.Create(filename, extraHeader)) {
}
}
12 changes: 8 additions & 4 deletions src/wpiutil/Natives/DataLogNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public struct OpaqueDataLog { }

public static partial class DataLogNative
{
[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_Create")]
[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_CreateWriter")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial OpaqueDataLog* Create(WpiString dir, WpiString filename, double period, WpiString extraHeader);
public static unsafe partial OpaqueDataLog* Create(WpiString dir, WpiString filename);

[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_Create_Func")]
[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_CreateBackgroundWriter")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial OpaqueDataLog* CreateFunc(delegate* unmanaged[Cdecl]<void*, byte*, nuint, void> write, void* ptr, double period, WpiString extraHeader);
public static unsafe partial OpaqueDataLog* CreateBg(WpiString dir, WpiString filename, double period, WpiString extraHeader);

[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_CreateBackgroundWriter_Func")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static unsafe partial OpaqueDataLog* CreateBgFunc(delegate* unmanaged[Cdecl]<void*, byte*, nuint, void> write, void* ptr, double period, WpiString extraHeader);

[LibraryImport("wpiutil", EntryPoint = "WPI_DataLog_Release")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
Expand Down
2 changes: 1 addition & 1 deletion test/wpiutil.test/DataLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void TestDoesntCrash()
{
CallbackData cb = new();
{
using DataLog dl = new(cb.Callback);
using var dl = new DataLogBackgroundWriter(cb.Callback);
}
}
}

0 comments on commit 7a9d4e5

Please sign in to comment.