WPILibC++ 2024.3.2
wpi::log::DataLog Class Referencefinal

A data log. More...

#include <wpi/DataLog.h>

Public Member Functions

 DataLog (std::string_view dir="", std::string_view filename="", double period=0.25, std::string_view extraHeader="")
 Construct a new Data Log. More...
 
 DataLog (wpi::Logger &msglog, std::string_view dir="", std::string_view filename="", double period=0.25, std::string_view extraHeader="")
 Construct a new Data Log. More...
 
 DataLog (std::function< void(std::span< const uint8_t > data)> write, double period=0.25, std::string_view extraHeader="")
 Construct a new Data Log that passes its output to the provided function rather than a file. More...
 
 DataLog (wpi::Logger &msglog, std::function< void(std::span< const uint8_t > data)> write, double period=0.25, std::string_view extraHeader="")
 Construct a new Data Log that passes its output to the provided function rather than a file. More...
 
 ~DataLog ()
 
 DataLog (const DataLog &)=delete
 
DataLogoperator= (const DataLog &)=delete
 
 DataLog (DataLog &&)=delete
 
DataLogoperator= (const DataLog &&)=delete
 
void SetFilename (std::string_view filename)
 Change log filename. More...
 
void Flush ()
 Explicitly flushes the log data to disk. More...
 
void Pause ()
 Pauses appending of data records to the log. More...
 
void Resume ()
 Resumes appending of data records to the log. More...
 
void Stop ()
 Stops appending all records to the log, and closes the log file. More...
 
bool HasSchema (std::string_view name) const
 Returns whether there is a data schema already registered with the given name. More...
 
void AddSchema (std::string_view name, std::string_view type, std::span< const uint8_t > schema, int64_t timestamp=0)
 Registers a data schema. More...
 
void AddSchema (std::string_view name, std::string_view type, std::string_view schema, int64_t timestamp=0)
 Registers a data schema. More...
 
template<ProtobufSerializable T>
void AddProtobufSchema (ProtobufMessage< T > &msg, int64_t timestamp=0)
 Registers a protobuf schema. More...
 
template<typename T , typename... I>
requires StructSerializable<T, I...>
void AddStructSchema (const I &... info, int64_t timestamp=0)
 Registers a struct schema. More...
 
int Start (std::string_view name, std::string_view type, std::string_view metadata={}, int64_t timestamp=0)
 Start an entry. More...
 
void Finish (int entry, int64_t timestamp=0)
 Finish an entry. More...
 
void SetMetadata (int entry, std::string_view metadata, int64_t timestamp=0)
 Updates the metadata for an entry. More...
 
void AppendRaw (int entry, std::span< const uint8_t > data, int64_t timestamp)
 Appends a raw record to the log. More...
 
void AppendRaw2 (int entry, std::span< const std::span< const uint8_t > > data, int64_t timestamp)
 Appends a raw record to the log. More...
 
void AppendBoolean (int entry, bool value, int64_t timestamp)
 Appends a boolean record to the log. More...
 
void AppendInteger (int entry, int64_t value, int64_t timestamp)
 Appends an integer record to the log. More...
 
void AppendFloat (int entry, float value, int64_t timestamp)
 Appends a float record to the log. More...
 
void AppendDouble (int entry, double value, int64_t timestamp)
 Appends a double record to the log. More...
 
void AppendString (int entry, std::string_view value, int64_t timestamp)
 Appends a string record to the log. More...
 
void AppendBooleanArray (int entry, std::span< const bool > arr, int64_t timestamp)
 Appends a boolean array record to the log. More...
 
void AppendBooleanArray (int entry, std::span< const int > arr, int64_t timestamp)
 Appends a boolean array record to the log. More...
 
void AppendBooleanArray (int entry, std::span< const uint8_t > arr, int64_t timestamp)
 Appends a boolean array record to the log. More...
 
void AppendIntegerArray (int entry, std::span< const int64_t > arr, int64_t timestamp)
 Appends an integer array record to the log. More...
 
void AppendFloatArray (int entry, std::span< const float > arr, int64_t timestamp)
 Appends a float array record to the log. More...
 
void AppendDoubleArray (int entry, std::span< const double > arr, int64_t timestamp)
 Appends a double array record to the log. More...
 
void AppendStringArray (int entry, std::span< const std::string > arr, int64_t timestamp)
 Appends a string array record to the log. More...
 
void AppendStringArray (int entry, std::span< const std::string_view > arr, int64_t timestamp)
 Appends a string array record to the log. More...
 
void AppendStringArray (int entry, std::span< const WPI_DataLog_String > arr, int64_t timestamp)
 Appends a string array record to the log. More...
 

Detailed Description

A data log.

The log file is created immediately upon construction with a temporary filename. The file may be renamed at any time using the SetFilename() function.

The lifetime of the data log object must be longer than any data log entry objects that refer to it.

The data log is periodically flushed to disk. It can also be explicitly flushed to disk by using the Flush() function.

Finish() is needed only to indicate in the log that a particular entry is no longer being used (it releases the name to ID mapping). Finish() is not required to be called for data to be flushed to disk; entries in the log are written as Append() calls are being made. In fact, Finish() does not need to be called at all; this is helpful to avoid shutdown races where the DataLog object might be destroyed before other objects. It's often not a good idea to call Finish() from destructors for this reason.

DataLog calls are thread safe. DataLog uses a typical multiple-supplier, single-consumer setup. Writes to the log are atomic, but there is no guaranteed order in the log when multiple threads are writing to it; whichever thread grabs the write mutex first will get written first. For this reason (as well as the fact that timestamps can be set to arbitrary values), records in the log are not guaranteed to be sorted by timestamp.

Constructor & Destructor Documentation

◆ DataLog() [1/6]

wpi::log::DataLog::DataLog ( std::string_view  dir = "",
std::string_view  filename = "",
double  period = 0.25,
std::string_view  extraHeader = "" 
)
explicit

Construct a new Data Log.

The log will be initially created with a temporary filename.

Parameters
dirdirectory to store the log
filenamefilename to use; if none provided, a random filename is generated of the form "wpilog_{}.wpilog"
periodtime between automatic flushes to disk, in seconds; this is a time/storage tradeoff
extraHeaderextra header data

◆ DataLog() [2/6]

wpi::log::DataLog::DataLog ( wpi::Logger msglog,
std::string_view  dir = "",
std::string_view  filename = "",
double  period = 0.25,
std::string_view  extraHeader = "" 
)
explicit

Construct a new Data Log.

The log will be initially created with a temporary filename.

Parameters
msglogmessage logger (will be called from separate thread)
dirdirectory to store the log
filenamefilename to use; if none provided, a random filename is generated of the form "wpilog_{}.wpilog"
periodtime between automatic flushes to disk, in seconds; this is a time/storage tradeoff
extraHeaderextra header data

◆ DataLog() [3/6]

wpi::log::DataLog::DataLog ( std::function< void(std::span< const uint8_t > data)>  write,
double  period = 0.25,
std::string_view  extraHeader = "" 
)
explicit

Construct a new Data Log that passes its output to the provided function rather than a file.

The write function will be called on a separate background thread and may block. The write function is called with an empty data array when the thread is terminating.

Parameters
writewrite function
periodtime between automatic calls to write, in seconds; this is a time/storage tradeoff
extraHeaderextra header data

◆ DataLog() [4/6]

wpi::log::DataLog::DataLog ( wpi::Logger msglog,
std::function< void(std::span< const uint8_t > data)>  write,
double  period = 0.25,
std::string_view  extraHeader = "" 
)
explicit

Construct a new Data Log that passes its output to the provided function rather than a file.

The write function will be called on a separate background thread and may block. The write function is called with an empty data array when the thread is terminating.

Parameters
msglogmessage logger (will be called from separate thread)
writewrite function
periodtime between automatic calls to write, in seconds; this is a time/storage tradeoff
extraHeaderextra header data

◆ ~DataLog()

wpi::log::DataLog::~DataLog ( )

◆ DataLog() [5/6]

wpi::log::DataLog::DataLog ( const DataLog )
delete

◆ DataLog() [6/6]

wpi::log::DataLog::DataLog ( DataLog &&  )
delete

Member Function Documentation

◆ AddProtobufSchema()

template<ProtobufSerializable T>
void wpi::log::DataLog::AddProtobufSchema ( ProtobufMessage< T > &  msg,
int64_t  timestamp = 0 
)
inline

Registers a protobuf schema.

Duplicate calls to this function with the same name are silently ignored.

Template Parameters
Tprotobuf serializable type
Parameters
msgprotobuf message
timestampTime stamp (0 to indicate now)

◆ AddSchema() [1/2]

void wpi::log::DataLog::AddSchema ( std::string_view  name,
std::string_view  type,
std::span< const uint8_t >  schema,
int64_t  timestamp = 0 
)

Registers a data schema.

Data schemas provide information for how a certain data type string can be decoded. The type string of a data schema indicates the type of the schema itself (e.g. "protobuf" for protobuf schemas, "struct" for struct schemas, etc). In the data log, schemas are saved just like normal records, with the name being generated from the provided name: "/.schema/<name>". Duplicate calls to this function with the same name are silently ignored.

Parameters
nameName (the string passed as the data type for records using this schema)
typeType of schema (e.g. "protobuf", "struct", etc)
schemaSchema data
timestampTime stamp (may be 0 to indicate now)

◆ AddSchema() [2/2]

void wpi::log::DataLog::AddSchema ( std::string_view  name,
std::string_view  type,
std::string_view  schema,
int64_t  timestamp = 0 
)
inline

Registers a data schema.

Data schemas provide information for how a certain data type string can be decoded. The type string of a data schema indicates the type of the schema itself (e.g. "protobuf" for protobuf schemas, "struct" for struct schemas, etc). In the data log, schemas are saved just like normal records, with the name being generated from the provided name: "/.schema/<name>". Duplicate calls to this function with the same name are silently ignored.

Parameters
nameName (the string passed as the data type for records using this schema)
typeType of schema (e.g. "protobuf", "struct", etc)
schemaSchema data
timestampTime stamp (may be 0 to indicate now)

◆ AddStructSchema()

template<typename T , typename... I>
requires StructSerializable<T, I...>
void wpi::log::DataLog::AddStructSchema ( const I &...  info,
int64_t  timestamp = 0 
)
inline

Registers a struct schema.

Duplicate calls to this function with the same name are silently ignored.

Template Parameters
Tstruct serializable type
Parameters
infooptional struct type info
timestampTime stamp (0 to indicate now)

◆ AppendBoolean()

void wpi::log::DataLog::AppendBoolean ( int  entry,
bool  value,
int64_t  timestamp 
)

Appends a boolean record to the log.

Parameters
entryEntry index, as returned by Start()
valueBoolean value to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendBooleanArray() [1/3]

void wpi::log::DataLog::AppendBooleanArray ( int  entry,
std::span< const bool >  arr,
int64_t  timestamp 
)

Appends a boolean array record to the log.

Parameters
entryEntry index, as returned by Start()
arrBoolean array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendBooleanArray() [2/3]

void wpi::log::DataLog::AppendBooleanArray ( int  entry,
std::span< const int >  arr,
int64_t  timestamp 
)

Appends a boolean array record to the log.

Parameters
entryEntry index, as returned by Start()
arrBoolean array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendBooleanArray() [3/3]

void wpi::log::DataLog::AppendBooleanArray ( int  entry,
std::span< const uint8_t >  arr,
int64_t  timestamp 
)

Appends a boolean array record to the log.

Parameters
entryEntry index, as returned by Start()
arrBoolean array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendDouble()

void wpi::log::DataLog::AppendDouble ( int  entry,
double  value,
int64_t  timestamp 
)

Appends a double record to the log.

Parameters
entryEntry index, as returned by Start()
valueDouble value to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendDoubleArray()

void wpi::log::DataLog::AppendDoubleArray ( int  entry,
std::span< const double >  arr,
int64_t  timestamp 
)

Appends a double array record to the log.

Parameters
entryEntry index, as returned by Start()
arrDouble array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendFloat()

void wpi::log::DataLog::AppendFloat ( int  entry,
float  value,
int64_t  timestamp 
)

Appends a float record to the log.

Parameters
entryEntry index, as returned by Start()
valueFloat value to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendFloatArray()

void wpi::log::DataLog::AppendFloatArray ( int  entry,
std::span< const float >  arr,
int64_t  timestamp 
)

Appends a float array record to the log.

Parameters
entryEntry index, as returned by Start()
arrFloat array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendInteger()

void wpi::log::DataLog::AppendInteger ( int  entry,
int64_t  value,
int64_t  timestamp 
)

Appends an integer record to the log.

Parameters
entryEntry index, as returned by Start()
valueInteger value to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendIntegerArray()

void wpi::log::DataLog::AppendIntegerArray ( int  entry,
std::span< const int64_t >  arr,
int64_t  timestamp 
)

Appends an integer array record to the log.

Parameters
entryEntry index, as returned by Start()
arrInteger array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendRaw()

void wpi::log::DataLog::AppendRaw ( int  entry,
std::span< const uint8_t >  data,
int64_t  timestamp 
)

Appends a raw record to the log.

Parameters
entryEntry index, as returned by Start()
dataByte array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendRaw2()

void wpi::log::DataLog::AppendRaw2 ( int  entry,
std::span< const std::span< const uint8_t > >  data,
int64_t  timestamp 
)

Appends a raw record to the log.

Parameters
entryEntry index, as returned by Start()
dataByte array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendString()

void wpi::log::DataLog::AppendString ( int  entry,
std::string_view  value,
int64_t  timestamp 
)

Appends a string record to the log.

Parameters
entryEntry index, as returned by Start()
valueString value to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendStringArray() [1/3]

void wpi::log::DataLog::AppendStringArray ( int  entry,
std::span< const std::string >  arr,
int64_t  timestamp 
)

Appends a string array record to the log.

Parameters
entryEntry index, as returned by Start()
arrString array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendStringArray() [2/3]

void wpi::log::DataLog::AppendStringArray ( int  entry,
std::span< const std::string_view arr,
int64_t  timestamp 
)

Appends a string array record to the log.

Parameters
entryEntry index, as returned by Start()
arrString array to record
timestampTime stamp (may be 0 to indicate now)

◆ AppendStringArray() [3/3]

void wpi::log::DataLog::AppendStringArray ( int  entry,
std::span< const WPI_DataLog_String arr,
int64_t  timestamp 
)

Appends a string array record to the log.

Parameters
entryEntry index, as returned by Start()
arrString array to record
timestampTime stamp (may be 0 to indicate now)

◆ Finish()

void wpi::log::DataLog::Finish ( int  entry,
int64_t  timestamp = 0 
)

Finish an entry.

Parameters
entryEntry index
timestampTime stamp (may be 0 to indicate now)

◆ Flush()

void wpi::log::DataLog::Flush ( )

Explicitly flushes the log data to disk.

◆ HasSchema()

bool wpi::log::DataLog::HasSchema ( std::string_view  name) const

Returns whether there is a data schema already registered with the given name.

Parameters
nameName (the string passed as the data type for records using this schema)
Returns
True if schema already registered

◆ operator=() [1/2]

DataLog & wpi::log::DataLog::operator= ( const DataLog &&  )
delete

◆ operator=() [2/2]

DataLog & wpi::log::DataLog::operator= ( const DataLog )
delete

◆ Pause()

void wpi::log::DataLog::Pause ( )

Pauses appending of data records to the log.

While paused, no data records are saved (e.g. AppendX is a no-op). Has no effect on entry starts / finishes / metadata changes.

◆ Resume()

void wpi::log::DataLog::Resume ( )

Resumes appending of data records to the log.

If called after Stop(), opens a new file (with random name if SetFilename was not called after Stop()) and appends Start records and schema data values for all previously started entries and schemas.

◆ SetFilename()

void wpi::log::DataLog::SetFilename ( std::string_view  filename)

Change log filename.

Parameters
filenamefilename

◆ SetMetadata()

void wpi::log::DataLog::SetMetadata ( int  entry,
std::string_view  metadata,
int64_t  timestamp = 0 
)

Updates the metadata for an entry.

Parameters
entryEntry index
metadataNew metadata for the entry
timestampTime stamp (may be 0 to indicate now)

◆ Start()

int wpi::log::DataLog::Start ( std::string_view  name,
std::string_view  type,
std::string_view  metadata = {},
int64_t  timestamp = 0 
)

Start an entry.

Duplicate names are allowed (with the same type), and result in the same index being returned (Start/Finish are reference counted). A duplicate name with a different type will result in an error message being printed to the console and 0 being returned (which will be ignored by the Append functions).

Parameters
nameName
typeData type
metadataInitial metadata (e.g. data properties)
timestampTime stamp (may be 0 to indicate now)
Returns
Entry index

◆ Stop()

void wpi::log::DataLog::Stop ( )

Stops appending all records to the log, and closes the log file.


The documentation for this class was generated from the following file: