-
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
6 changed files
with
76 additions
and
50 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
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,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); | ||
} | ||
} | ||
} |
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,9 @@ | ||
using WPIUtil.Natives; | ||
|
||
namespace WPIUtil.Logging; | ||
|
||
public unsafe class DataLogWriter : DataLog | ||
{ | ||
public DataLogWriter(string filename, string extraHeader = "") : base(DataLogNative.Create(filename, extraHeader)) { | ||
} | ||
} |
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