Skip to content

Commit

Permalink
Add ProtobufLogEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 16, 2024
1 parent 8d615e3 commit 3eb38cd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
8 changes: 7 additions & 1 deletion src/wpiutil/DataLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using WPIUtil.Handles;
using WPIUtil.Natives;
using WPIUtil.Serialization.Protobuf;
using WPIUtil.Serialization.Struct;

namespace WPIUtil;
Expand Down Expand Up @@ -69,7 +70,12 @@ public void AppendRaw(DataLogEntryHandle entry, ReadOnlySpan<byte> data, long ti
DataLogNative.DataLogAppend(NativeHandle, entry, data, (ulong)timestamp);
}

public void AddSchema<T>(IStruct<T> value, long timestamp = 0)
public void AddSchema(IStructBase value, long timestamp = 0)
{
throw new NotImplementedException();
}

public void AddSchema(IProtobufBase value, long timestamp = 0)
{
throw new NotImplementedException();
}
Expand Down
36 changes: 36 additions & 0 deletions src/wpiutil/Logging/ProtobufLogEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Google.Protobuf;
using WPIUtil.Serialization.Protobuf;

namespace WPIUtil.Logging;

public sealed class ProtobufLogEntry<T, MessageType> : DataLogEntry where MessageType : IMessage
where T : IProtobufSerializable<T, MessageType>
{
private readonly ProtobufBuffer<T, MessageType> m_storage = new();
private readonly object m_lockObject = new();

private ProtobufLogEntry(DataLog log, string name, IProtobufBase proto, string metadata = "", long timestamp = 0) : base(log, name, proto.TypeString, metadata, timestamp)
{
log.AddSchema(proto, timestamp);
}

public ProtobufLogEntry<T, MessageType> Create(DataLog log, string name, string metadata = "", long timestamp = 0)
{
return new ProtobufLogEntry<T, MessageType>(log, name, T.Proto, metadata, timestamp);
}

public void Append(T value, long timestamp = 0)
{
try
{
lock (m_lockObject)
{
m_log.AppendRaw(m_entry, m_storage.Write(value), timestamp);
}
}
catch
{
// ignore
}
}
}
8 changes: 1 addition & 7 deletions src/wpiutil/Logging/StructLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ namespace WPIUtil.Logging;

public sealed class StructLogEntry<T> : DataLogEntry where T : IStructSerializable<T>
{
private StructBuffer<T> m_storage;
private readonly StructBuffer<T> m_storage = new();
private readonly object m_lockObject = new();

private StructLogEntry(DataLog log, string name, IStruct<T> value, string metadata, long timestamp) : base(log, name, value.TypeString, metadata, timestamp)
{
m_storage = StructBuffer<T>.Create(value);
log.AddSchema(value, timestamp);
}

public StructLogEntry<T> Create(DataLog log, string name, IStruct<T> value, string metadata = "", long timestamp = 0)
{
return new StructLogEntry<T>(log, name, value, metadata, timestamp);
}

public StructLogEntry<T> Create(DataLog log, string name, string metadata = "", long timestamp = 0)
{
return new StructLogEntry<T>(log, name, T.Struct, metadata, timestamp);
Expand Down

0 comments on commit 3eb38cd

Please sign in to comment.