10#include <initializer_list>
125 std::span<const uint8_t> schema, int64_t timestamp = 0);
146 std::span<const uint8_t>{
147 reinterpret_cast<const uint8_t*
>(schema.data()), schema.size()},
159 template <ProtobufSerializable T>
161 if (timestamp == 0) {
165 [
this](
auto typeString) {
return HasSchema(typeString); },
166 [
this, timestamp](
auto typeString,
auto schema) {
167 AddSchema(typeString,
"proto:FileDescriptorProto", schema, timestamp);
179 template <
typename T,
typename... I>
182 if (timestamp == 0) {
185 ForEachStructSchema<T>(
186 [
this, timestamp](
auto typeString,
auto schema) {
187 this->
AddSchema(typeString,
"structschema", schema, timestamp);
215 void Finish(
int entry, int64_t timestamp = 0);
233 void AppendRaw(
int entry, std::span<const uint8_t> data, int64_t timestamp);
242 void AppendRaw2(
int entry, std::span<
const std::span<const uint8_t>> data,
387 : m_buf{new uint8_t[alloc]}, m_maxLen{alloc} {}
394 : m_buf{oth.m_buf}, m_len{oth.m_len}, m_maxLen{oth.m_maxLen} {
406 m_maxLen = oth.m_maxLen;
415 uint8_t* rv = m_buf + m_len;
426 std::span<uint8_t>
GetData() {
return {m_buf, m_len}; }
427 std::span<const uint8_t>
GetData()
const {
return {m_buf, m_len}; }
443 :
m_msglog{msglog}, m_extraHeader{extraHeader} {}
488 static constexpr size_t kMaxBufferCount = 1024 * 1024 /
kBlockSize;
489 static constexpr size_t kMaxFreeCount = 256 * 1024 /
kBlockSize;
494 uint8_t* StartRecord(uint32_t entry, uint64_t timestamp, uint32_t payloadSize,
496 uint8_t* Reserve(
size_t size);
497 void AppendImpl(std::span<const uint8_t> data);
501 void DoReleaseBufs(std::vector<Buffer>* bufs);
508 bool m_active =
false;
509 bool m_paused =
false;
510 std::string m_extraHeader;
511 std::vector<Buffer> m_free;
512 std::vector<Buffer> m_outgoing;
515 std::vector<uint8_t> schemaData;
520 std::string metadata;
554 explicit operator bool()
const {
return m_log !=
nullptr; }
589 int64_t timestamp = 0)
601 void Append(std::span<const uint8_t> data, int64_t timestamp = 0) {
602 m_log->AppendRaw(m_entry, data, timestamp);
626 void Append(
bool value, int64_t timestamp = 0) {
627 m_log->AppendBoolean(m_entry, value, timestamp);
651 void Append(int64_t value, int64_t timestamp = 0) {
652 m_log->AppendInteger(m_entry, value, timestamp);
667 int64_t timestamp = 0)
676 void Append(
float value, int64_t timestamp = 0) {
677 m_log->AppendFloat(m_entry, value, timestamp);
692 int64_t timestamp = 0)
701 void Append(
double value, int64_t timestamp = 0) {
702 m_log->AppendDouble(m_entry, value, timestamp);
711 static constexpr const char* kDataType =
"string";
717 int64_t timestamp = 0)
730 m_log->AppendString(m_entry, value, timestamp);
739 static constexpr const char* kDataType =
"boolean[]";
743 int64_t timestamp = 0)
756 void Append(std::span<const bool> arr, int64_t timestamp = 0) {
757 m_log->AppendBooleanArray(m_entry, arr, timestamp);
766 void Append(std::initializer_list<bool> arr, int64_t timestamp = 0) {
767 Append(std::span{arr.begin(), arr.end()}, timestamp);
776 void Append(std::span<const int> arr, int64_t timestamp = 0) {
777 m_log->AppendBooleanArray(m_entry, arr, timestamp);
786 void Append(std::initializer_list<int> arr, int64_t timestamp = 0) {
787 Append(std::span{arr.begin(), arr.end()}, timestamp);
796 void Append(std::span<const uint8_t> arr, int64_t timestamp = 0) {
797 m_log->AppendBooleanArray(m_entry, arr, timestamp);
806 static constexpr const char* kDataType =
"int64[]";
810 int64_t timestamp = 0)
822 void Append(std::span<const int64_t> arr, int64_t timestamp = 0) {
823 m_log->AppendIntegerArray(m_entry, arr, timestamp);
832 void Append(std::initializer_list<int64_t> arr, int64_t timestamp = 0) {
833 Append({arr.begin(), arr.end()}, timestamp);
842 static constexpr const char* kDataType =
"float[]";
857 void Append(std::span<const float> arr, int64_t timestamp = 0) {
858 m_log->AppendFloatArray(m_entry, arr, timestamp);
867 void Append(std::initializer_list<float> arr, int64_t timestamp = 0) {
868 Append({arr.begin(), arr.end()}, timestamp);
877 static constexpr const char* kDataType =
"double[]";
881 int64_t timestamp = 0)
893 void Append(std::span<const double> arr, int64_t timestamp = 0) {
894 m_log->AppendDoubleArray(m_entry, arr, timestamp);
903 void Append(std::initializer_list<double> arr, int64_t timestamp = 0) {
904 Append({arr.begin(), arr.end()}, timestamp);
913 static constexpr const char* kDataType =
"string[]";
917 int64_t timestamp = 0)
929 void Append(std::span<const std::string> arr, int64_t timestamp = 0) {
930 m_log->AppendStringArray(m_entry, arr, timestamp);
939 void Append(std::span<const std::string_view> arr, int64_t timestamp = 0) {
940 m_log->AppendStringArray(m_entry, arr, timestamp);
949 void Append(std::initializer_list<std::string_view> arr,
950 int64_t timestamp = 0) {
951 Append(std::span<const std::string_view>{arr.begin(), arr.end()},
959template <
typename T,
typename... I>
967 int64_t timestamp = 0)
970 I... info, int64_t timestamp = 0)
971 : m_info{
std::move(info)...} {
973 log.AddStructSchema<T, I...>(info..., timestamp);
983 void Append(
const T& data, int64_t timestamp = 0) {
984 if constexpr (
sizeof...(I) == 0) {
986 uint8_t buf[S::GetSize()];
988 m_log->AppendRaw(m_entry, buf, timestamp);
994 std::apply([&](
const I&... info) { S::Pack(buf, data, info...); }, m_info);
995 m_log->AppendRaw(m_entry, buf, timestamp);
999 [[no_unique_address]]
1000 std::tuple<I...> m_info;
1006template <
typename T,
typename... I>
1014 int64_t timestamp = 0)
1018 int64_t timestamp = 0)
1019 : m_info{
std::move(info)...} {
1021 log.AddStructSchema<T, I...>(info..., timestamp);
1022 m_entry =
log.Start(
1023 name, MakeStructArrayTypeString<T, std::dynamic_extent>(info...),
1024 metadata, timestamp);
1033 template <
typename U>
1034#if __cpp_lib_ranges >= 201911L
1035 requires std::ranges::range<U> &&
1036 std::convertible_to<std::ranges::range_value_t<U>, T>
1038 void Append(U&& data, int64_t timestamp = 0) {
1040 [&](
const I&... info) {
1042 std::forward<U>(data),
1043 [&](
auto bytes) { m_log->AppendRaw(m_entry,
bytes, timestamp); },
1055 void Append(std::span<const T> data, int64_t timestamp = 0) {
1057 [&](
const I&... info) {
1060 [&](
auto bytes) { m_log->AppendRaw(m_entry,
bytes, timestamp); },
1068 [[no_unique_address]]
1069 std::tuple<I...> m_info;
1075template <ProtobufSerializable T>
1086 log.AddProtobufSchema<T>(m_msg, timestamp);
1087 m_entry =
log.Start(
name, m_msg.GetTypeString(), metadata, timestamp);
1096 void Append(
const T& data, int64_t timestamp = 0) {
1099 std::scoped_lock lock{m_mutex};
1100 m_msg.Pack(buf, data);
1102 m_log->AppendRaw(m_entry, buf, timestamp);
This file defines the DenseMap class.
This file defines the SmallVector class.
This file defines the StringMap class.
Definition: format.h:4108
Owning wrapper (ala std::unique_ptr) for google::protobuf::Message* that does not require the protobu...
Definition: Protobuf.h:158
void ForEachProtobufDescriptor(function_ref< bool(std::string_view filename)> exists, function_ref< void(std::string_view filename, std::span< const uint8_t > descriptor)> fn)
Loops over all protobuf descriptors including nested/referenced descriptors.
Definition: Protobuf.h:248
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1211
void resize_for_overwrite(size_type N)
Like resize, but T is POD, the new values won't be initialized.
Definition: SmallVector.h:656
Log array of boolean values.
Definition: DataLog.h:737
void Append(std::span< const uint8_t > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:796
void Append(std::initializer_list< bool > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:766
BooleanArrayLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:742
void Append(std::initializer_list< int > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:786
void Append(std::span< const int > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:776
void Append(std::span< const bool > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:756
BooleanArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:745
BooleanArrayLogEntry()=default
Log boolean values.
Definition: DataLog.h:609
BooleanLogEntry()=default
BooleanLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:616
void Append(bool value, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:626
BooleanLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:614
Definition: DataLog.h:384
std::span< uint8_t > GetData()
Definition: DataLog.h:426
Buffer(size_t alloc=kBlockSize)
Definition: DataLog.h:386
Buffer(Buffer &&oth)
Definition: DataLog.h:393
Buffer & operator=(Buffer &&oth)
Definition: DataLog.h:400
~Buffer()
Definition: DataLog.h:388
std::span< const uint8_t > GetData() const
Definition: DataLog.h:427
void Clear()
Definition: DataLog.h:422
Buffer(const Buffer &)=delete
size_t GetRemaining() const
Definition: DataLog.h:424
void Unreserve(size_t size)
Definition: DataLog.h:420
Buffer & operator=(const Buffer &)=delete
uint8_t * Reserve(size_t size)
Definition: DataLog.h:413
Log entry base class.
Definition: DataLog.h:530
DataLogEntry(DataLogEntry &&rhs)
Definition: DataLog.h:541
DataLogEntry(const DataLogEntry &)=delete
void Finish(int64_t timestamp=0)
Finishes the entry.
Definition: DataLog.h:571
DataLog * m_log
Definition: DataLog.h:574
DataLogEntry(DataLog &log, std::string_view name, std::string_view type, std::string_view metadata={}, int64_t timestamp=0)
Definition: DataLog.h:533
DataLogEntry & operator=(const DataLogEntry &)=delete
int m_entry
Definition: DataLog.h:575
DataLogEntry & operator=(DataLogEntry &&rhs)
Definition: DataLog.h:544
void SetMetadata(std::string_view metadata, int64_t timestamp=0)
Updates the metadata for the entry.
Definition: DataLog.h:562
A data log for high-speed writing of data values.
Definition: DataLog.h:68
void AppendBooleanArray(int entry, std::span< const bool > arr, int64_t timestamp)
Appends a boolean array record to the log.
void AppendBooleanArray(int entry, std::span< const int > arr, int64_t timestamp)
Appends a boolean array record to the log.
void SetMetadata(int entry, std::string_view metadata, int64_t timestamp=0)
Updates the metadata for an entry.
void AppendStringArray(int entry, std::span< const std::string > arr, int64_t timestamp)
Appends a string array record to the log.
virtual void Stop()
Stops appending start/metadata/schema records to the log.
int Start(std::string_view name, std::string_view type, std::string_view metadata={}, int64_t timestamp=0)
Start an entry.
DataLog & operator=(const DataLog &)=delete
void AppendRaw(int entry, std::span< const uint8_t > data, int64_t timestamp)
Appends a raw record to the log.
void AppendIntegerArray(int entry, std::span< const int64_t > arr, int64_t timestamp)
Appends an integer array record to the log.
void AddStructSchema(const I &... info, int64_t timestamp=0)
Registers a struct schema.
Definition: DataLog.h:181
DataLog(const DataLog &)=delete
void ReleaseBufs(std::vector< Buffer > *bufs)
Releases memory for a set of buffers back to the internal buffer pool.
DataLog(wpi::Logger &msglog, std::string_view extraHeader="")
Constructs the log.
Definition: DataLog.h:442
void AppendStringArray(int entry, std::span< const struct WPI_String > arr, int64_t timestamp)
Appends a string array record to the log.
static wpi::Logger s_defaultMessageLog
Definition: DataLog.h:382
void AddSchema(std::string_view name, std::string_view type, std::string_view schema, int64_t timestamp=0)
Registers a data schema.
Definition: DataLog.h:142
void AppendBooleanArray(int entry, std::span< const uint8_t > arr, int64_t timestamp)
Appends a boolean array record to the log.
void AppendDouble(int entry, double value, int64_t timestamp)
Appends a double record to the log.
void AppendFloat(int entry, float value, int64_t timestamp)
Appends a float record to the log.
DataLog & operator=(const DataLog &&)=delete
wpi::Logger & m_msglog
Definition: DataLog.h:504
void AppendFloatArray(int entry, std::span< const float > arr, int64_t timestamp)
Appends a float array record to the log.
virtual void Resume()
Resumes appending of data records to the log.
void AppendString(int entry, std::string_view value, int64_t timestamp)
Appends a string record to the log.
void AddSchema(std::string_view name, std::string_view type, std::span< const uint8_t > schema, int64_t timestamp=0)
Registers a data schema.
DataLog(DataLog &&)=delete
virtual ~DataLog()=default
static constexpr size_t kBlockSize
Definition: DataLog.h:381
virtual void Pause()
Pauses appending of data records to the log.
void AppendStringArray(int entry, std::span< const std::string_view > arr, int64_t timestamp)
Appends a string array record to the log.
bool HasSchema(std::string_view name) const
Returns whether there is a data schema already registered with the given name.
virtual bool BufferFull()=0
Called when internal buffers reach the maximum count.
void StartFile()
Starts the log.
void AppendRaw2(int entry, std::span< const std::span< const uint8_t > > data, int64_t timestamp)
Appends a raw record to the log.
void Finish(int entry, int64_t timestamp=0)
Finish an entry.
void AppendDoubleArray(int entry, std::span< const double > arr, int64_t timestamp)
Appends a double array record to the log.
virtual void Flush()=0
Explicitly flushes the log data to disk.
void AppendInteger(int entry, int64_t value, int64_t timestamp)
Appends an integer record to the log.
void FlushBufs(std::vector< Buffer > *writeBufs)
Provides complete set of all buffers that need to be written.
virtual void BufferHalfFull()
Called when internal buffers are half the maximum count.
void AddProtobufSchema(ProtobufMessage< T > &msg, int64_t timestamp=0)
Registers a protobuf schema.
Definition: DataLog.h:160
void AppendBoolean(int entry, bool value, int64_t timestamp)
Appends a boolean record to the log.
Log array of double values.
Definition: DataLog.h:875
DoubleArrayLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:880
void Append(std::span< const double > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:893
void Append(std::initializer_list< double > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:903
DoubleArrayLogEntry()=default
DoubleArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:883
Log double values.
Definition: DataLog.h:684
DoubleLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:691
void Append(double value, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:701
DoubleLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:689
Log array of float values.
Definition: DataLog.h:840
FloatArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:847
void Append(std::span< const float > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:857
void Append(std::initializer_list< float > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:867
FloatArrayLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:845
FloatArrayLogEntry()=default
Log float values.
Definition: DataLog.h:659
FloatLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:664
FloatLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:666
void Append(float value, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:676
Log array of integer values.
Definition: DataLog.h:804
IntegerArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:812
void Append(std::span< const int64_t > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:822
IntegerArrayLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:809
void Append(std::initializer_list< int64_t > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:832
IntegerArrayLogEntry()=default
Log integer values.
Definition: DataLog.h:634
IntegerLogEntry()=default
IntegerLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:641
IntegerLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:639
void Append(int64_t value, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:651
Log protobuf serializable objects.
Definition: DataLog.h:1076
ProtobufLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:1081
void Append(const T &data, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:1096
ProtobufLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:1083
ProtobufLogEntry()=default
Log arbitrary byte data.
Definition: DataLog.h:581
RawLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:588
RawLogEntry(DataLog &log, std::string_view name, std::string_view metadata, std::string_view type, int64_t timestamp=0)
Definition: DataLog.h:591
static constexpr std::string_view kDataType
Definition: DataLog.h:583
void Append(std::span< const uint8_t > data, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:601
RawLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:586
Log array of string values.
Definition: DataLog.h:911
void Append(std::initializer_list< std::string_view > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:949
StringArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:919
void Append(std::span< const std::string > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:929
void Append(std::span< const std::string_view > arr, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:939
StringArrayLogEntry()=default
StringArrayLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:916
Log string values.
Definition: DataLog.h:709
StringLogEntry(DataLog &log, std::string_view name, std::string_view metadata, int64_t timestamp=0)
Definition: DataLog.h:716
void Append(std::string_view value, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:729
StringLogEntry(DataLog &log, std::string_view name, std::string_view metadata, std::string_view type, int64_t timestamp=0)
Definition: DataLog.h:719
StringLogEntry(DataLog &log, std::string_view name, int64_t timestamp=0)
Definition: DataLog.h:714
Log raw struct serializable array of objects.
Definition: DataLog.h:1008
StructArrayLogEntry(DataLog &log, std::string_view name, I... info, int64_t timestamp=0)
Definition: DataLog.h:1013
void Append(U &&data, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:1038
StructArrayLogEntry()=default
void Append(std::span< const T > data, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:1055
StructArrayLogEntry(DataLog &log, std::string_view name, std::string_view metadata, I... info, int64_t timestamp=0)
Definition: DataLog.h:1016
Log raw struct serializable objects.
Definition: DataLog.h:961
StructLogEntry(DataLog &log, std::string_view name, I... info, int64_t timestamp=0)
Definition: DataLog.h:966
void Append(const T &data, int64_t timestamp=0)
Appends a record to the log.
Definition: DataLog.h:983
StructLogEntry(DataLog &log, std::string_view name, std::string_view metadata, I... info, int64_t timestamp=0)
Definition: DataLog.h:969
Specifies that a type is capable of raw struct serialization and deserialization.
Definition: Struct.h:68
basic_string_view< char > string_view
Definition: core.h:518
constexpr dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:351
constexpr auto count() -> size_t
Definition: core.h:1222
type
Definition: core.h:573
Implement std::hash so that hash_code can be used in STL containers.
Definition: array.h:89
constexpr const char * name(const T &)
std::string GetTypeString(const google::protobuf::Message &msg)
ControlRecordType
Definition: DataLog.h:38
@ kControlFinish
Definition: DataLog.h:40
@ kControlStart
Definition: DataLog.h:39
@ kControlSetMetadata
Definition: DataLog.h:41
Definition: ntcore_cpp.h:31
Definition: ntcore_cpp.h:26
constexpr bool is_constexpr(Lambda)
Definition: type_traits.h:81
uint64_t Now()
Return a value representing the current time in microseconds.
Protobuf serialization template.
Definition: Protobuf.h:37
Struct serialization template.
Definition: Struct.h:38
::std::mutex mutex
Definition: mutex.h:17