WPILibC++ 2025.0.0-alpha-1-14-g3b6f38d
DataLog_c.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stddef.h> // NOLINT
8
9#include <stdint.h>
10#include <wpi/string.h>
11
12#ifdef __cplusplus
13extern "C" {
14#endif
15
16/** C-compatible data log (opaque struct). */
17struct WPI_DataLog;
18
19/**
20 * Construct a new Data Log.
21 *
22 * @param filename filename to use
23 * @param errorCode error if file failed to open (output)
24 * @param extraHeader extra header data
25 */
26struct WPI_DataLog* WPI_DataLog_CreateWriter(
27 const struct WPI_String* filename, int* errorCode,
28 const struct WPI_String* extraHeader);
29
30/**
31 * Construct a new Data Log background writer. The log will be initially
32 * created with a temporary filename.
33 *
34 * @param dir directory to store the log
35 * @param filename filename to use; if none provided, a random filename is
36 * generated of the form "wpilog_{}.wpilog"
37 * @param period time between automatic flushes to disk, in seconds;
38 * this is a time/storage tradeoff
39 * @param extraHeader extra header data
40 */
42 const struct WPI_String* dir, const struct WPI_String* filename,
43 double period, const struct WPI_String* extraHeader);
44
45/**
46 * Construct a new Data Log background writer that passes its output to the
47 * provided function rather than a file. The write function will be called on a
48 * separate background thread and may block. The write function is called with
49 * an empty data array (data=NULL, len=0) when the thread is terminating.
50 *
51 * @param write write function
52 * @param ptr pointer to pass to write function ptr parameter
53 * @param period time between automatic calls to write, in seconds;
54 * this is a time/storage tradeoff
55 * @param extraHeader extra header data
56 */
58 void (*write)(void* ptr, const uint8_t* data, size_t len), void* ptr,
59 double period, const struct WPI_String* extraHeader);
60
61/**
62 * Change log filename. Can only be used on background writer data logs.
63 *
64 * @param datalog data log
65 * @param filename filename
66 */
67void WPI_DataLog_SetBackgroundWriterFilename(struct WPI_DataLog* datalog,
68 const struct WPI_String* filename);
69
70/**
71 * Releases a data log object. Closes the file and returns resources to the
72 * system.
73 *
74 * @param datalog data log
75 */
76void WPI_DataLog_Release(struct WPI_DataLog* datalog);
77
78/**
79 * Explicitly flushes the log data to disk.
80 *
81 * @param datalog data log
82 */
83void WPI_DataLog_Flush(struct WPI_DataLog* datalog);
84
85/**
86 * Pauses appending of data records to the log. While paused, no data records
87 * are saved (e.g. AppendX is a no-op). Has no effect on entry starts /
88 * finishes / metadata changes.
89 *
90 * @param datalog data log
91 */
92void WPI_DataLog_Pause(struct WPI_DataLog* datalog);
93
94/**
95 * Resumes appending of data records to the log. If called after Stop(),
96 * opens a new file (with random name if SetFilename was not called after
97 * Stop()) and appends Start records and schema data values for all previously
98 * started entries and schemas.
99 *
100 * @param datalog data log
101 */
102void WPI_DataLog_Resume(struct WPI_DataLog* datalog);
103
104/**
105 * Stops appending all records to the log, and closes the log file.
106 *
107 * @param datalog data log
108 */
109void WPI_DataLog_Stop(struct WPI_DataLog* datalog);
110
111/**
112 * Start an entry. Duplicate names are allowed (with the same type), and
113 * result in the same index being returned (Start/Finish are reference
114 * counted). A duplicate name with a different type will result in an error
115 * message being printed to the console and 0 being returned (which will be
116 * ignored by the Append functions).
117 *
118 * @param datalog data log
119 * @param name Name
120 * @param type Data type
121 * @param metadata Initial metadata (e.g. data properties)
122 * @param timestamp Time stamp (may be 0 to indicate now)
123 *
124 * @return Entry index
125 */
126int WPI_DataLog_Start(struct WPI_DataLog* datalog,
127 const struct WPI_String* name,
128 const struct WPI_String* type,
129 const struct WPI_String* metadata, int64_t timestamp);
130
131/**
132 * Finish an entry.
133 *
134 * @param datalog data log
135 * @param entry Entry index
136 * @param timestamp Time stamp (may be 0 to indicate now)
137 */
138void WPI_DataLog_Finish(struct WPI_DataLog* datalog, int entry,
139 int64_t timestamp);
140
141/**
142 * Updates the metadata for an entry.
143 *
144 * @param datalog data log
145 * @param entry Entry index
146 * @param metadata New metadata for the entry
147 * @param timestamp Time stamp (may be 0 to indicate now)
148 */
149void WPI_DataLog_SetMetadata(struct WPI_DataLog* datalog, int entry,
150 const struct WPI_String* metadata,
151 int64_t timestamp);
152
153/**
154 * Appends a raw record to the log.
155 *
156 * @param datalog data log
157 * @param entry Entry index, as returned by WPI_DataLog_Start()
158 * @param data Byte array to record
159 * @param len Length of byte array
160 * @param timestamp Time stamp (may be 0 to indicate now)
161 */
162void WPI_DataLog_AppendRaw(struct WPI_DataLog* datalog, int entry,
163 const uint8_t* data, size_t len, int64_t timestamp);
164
165/**
166 * Appends a boolean record to the log.
167 *
168 * @param datalog data log
169 * @param entry Entry index, as returned by WPI_DataLog_Start()
170 * @param value Boolean value to record
171 * @param timestamp Time stamp (may be 0 to indicate now)
172 */
173void WPI_DataLog_AppendBoolean(struct WPI_DataLog* datalog, int entry,
174 int value, int64_t timestamp);
175
176/**
177 * Appends an integer record to the log.
178 *
179 * @param datalog data log
180 * @param entry Entry index, as returned by WPI_DataLog_Start()
181 * @param value Integer value to record
182 * @param timestamp Time stamp (may be 0 to indicate now)
183 */
184void WPI_DataLog_AppendInteger(struct WPI_DataLog* datalog, int entry,
185 int64_t value, int64_t timestamp);
186
187/**
188 * Appends a float record to the log.
189 *
190 * @param datalog data log
191 * @param entry Entry index, as returned by WPI_DataLog_Start()
192 * @param value Float value to record
193 * @param timestamp Time stamp (may be 0 to indicate now)
194 */
195void WPI_DataLog_AppendFloat(struct WPI_DataLog* datalog, int entry,
196 float value, int64_t timestamp);
197
198/**
199 * Appends a double record to the log.
200 *
201 * @param datalog data log
202 * @param entry Entry index, as returned by WPI_DataLog_Start()
203 * @param value Double value to record
204 * @param timestamp Time stamp (may be 0 to indicate now)
205 */
206void WPI_DataLog_AppendDouble(struct WPI_DataLog* datalog, int entry,
207 double value, int64_t timestamp);
208
209/**
210 * Appends a string record to the log.
211 *
212 * @param datalog data log
213 * @param entry Entry index, as returned by WPI_DataLog_Start()
214 * @param value String value to record
215 * @param timestamp Time stamp (may be 0 to indicate now)
216 */
217void WPI_DataLog_AppendString(struct WPI_DataLog* datalog, int entry,
218 const struct WPI_String* value,
219 int64_t timestamp);
220
221/**
222 * Appends a boolean array record to the log.
223 *
224 * @param datalog data log
225 * @param entry Entry index, as returned by WPI_DataLog_Start()
226 * @param arr Boolean array to record
227 * @param len Number of elements in array
228 * @param timestamp Time stamp (may be 0 to indicate now)
229 */
230void WPI_DataLog_AppendBooleanArray(struct WPI_DataLog* datalog, int entry,
231 const int* arr, size_t len,
232 int64_t timestamp);
233
234/**
235 * Appends a boolean array record to the log.
236 *
237 * @param datalog data log
238 * @param entry Entry index, as returned by WPI_DataLog_Start()
239 * @param arr Boolean array to record
240 * @param len Number of elements in array
241 * @param timestamp Time stamp (may be 0 to indicate now)
242 */
243void WPI_DataLog_AppendBooleanArrayByte(struct WPI_DataLog* datalog, int entry,
244 const uint8_t* arr, size_t len,
245 int64_t timestamp);
246
247/**
248 * Appends an integer array record to the log.
249 *
250 * @param datalog data log
251 * @param entry Entry index, as returned by WPI_DataLog_Start()
252 * @param arr Integer array to record
253 * @param len Number of elements in array
254 * @param timestamp Time stamp (may be 0 to indicate now)
255 */
256void WPI_DataLog_AppendIntegerArray(struct WPI_DataLog* datalog, int entry,
257 const int64_t* arr, size_t len,
258 int64_t timestamp);
259
260/**
261 * Appends a float array record to the log.
262 *
263 * @param datalog data log
264 * @param entry Entry index, as returned by WPI_DataLog_Start()
265 * @param arr Float array to record
266 * @param len Number of elements in array
267 * @param timestamp Time stamp (may be 0 to indicate now)
268 */
269void WPI_DataLog_AppendFloatArray(struct WPI_DataLog* datalog, int entry,
270 const float* arr, size_t len,
271 int64_t timestamp);
272
273/**
274 * Appends a double array record to the log.
275 *
276 * @param datalog data log
277 * @param entry Entry index, as returned by WPI_DataLog_Start()
278 * @param arr Double array to record
279 * @param len Number of elements in array
280 * @param timestamp Time stamp (may be 0 to indicate now)
281 */
282void WPI_DataLog_AppendDoubleArray(struct WPI_DataLog* datalog, int entry,
283 const double* arr, size_t len,
284 int64_t timestamp);
285
286/**
287 * Appends a string array record to the log.
288 *
289 * @param datalog data log
290 * @param entry Entry index, as returned by WPI_DataLog_Start()
291 * @param arr String array to record
292 * @param len Number of elements in array
293 * @param timestamp Time stamp (may be 0 to indicate now)
294 */
295void WPI_DataLog_AppendStringArray(struct WPI_DataLog* datalog, int entry,
296 const struct WPI_String* arr, size_t len,
297 int64_t timestamp);
298
299void WPI_DataLog_AddSchemaString(struct WPI_DataLog* datalog,
300 const struct WPI_String* name,
301 const struct WPI_String* type,
302 const struct WPI_String* schema,
303 int64_t timestamp);
304
305void WPI_DataLog_AddSchema(struct WPI_DataLog* datalog,
306 const struct WPI_String* name,
307 const struct WPI_String* type, const uint8_t* schema,
308 size_t schema_len, int64_t timestamp);
309
310#ifdef __cplusplus
311} // extern "C"
312#endif
void WPI_DataLog_AppendInteger(struct WPI_DataLog *datalog, int entry, int64_t value, int64_t timestamp)
Appends an integer record to the log.
struct WPI_DataLog * WPI_DataLog_CreateBackgroundWriter(const struct WPI_String *dir, const struct WPI_String *filename, double period, const struct WPI_String *extraHeader)
Construct a new Data Log background writer.
void WPI_DataLog_AppendBoolean(struct WPI_DataLog *datalog, int entry, int value, int64_t timestamp)
Appends a boolean record to the log.
void WPI_DataLog_AddSchema(struct WPI_DataLog *datalog, const struct WPI_String *name, const struct WPI_String *type, const uint8_t *schema, size_t schema_len, int64_t timestamp)
void WPI_DataLog_AppendDoubleArray(struct WPI_DataLog *datalog, int entry, const double *arr, size_t len, int64_t timestamp)
Appends a double array record to the log.
void WPI_DataLog_Stop(struct WPI_DataLog *datalog)
Stops appending all records to the log, and closes the log file.
void WPI_DataLog_AppendRaw(struct WPI_DataLog *datalog, int entry, const uint8_t *data, size_t len, int64_t timestamp)
Appends a raw record to the log.
void WPI_DataLog_Resume(struct WPI_DataLog *datalog)
Resumes appending of data records to the log.
void WPI_DataLog_AppendBooleanArrayByte(struct WPI_DataLog *datalog, int entry, const uint8_t *arr, size_t len, int64_t timestamp)
Appends a boolean array record to the log.
void WPI_DataLog_Pause(struct WPI_DataLog *datalog)
Pauses appending of data records to the log.
void WPI_DataLog_AppendString(struct WPI_DataLog *datalog, int entry, const struct WPI_String *value, int64_t timestamp)
Appends a string record to the log.
void WPI_DataLog_AddSchemaString(struct WPI_DataLog *datalog, const struct WPI_String *name, const struct WPI_String *type, const struct WPI_String *schema, int64_t timestamp)
struct WPI_DataLog * WPI_DataLog_CreateWriter(const struct WPI_String *filename, int *errorCode, const struct WPI_String *extraHeader)
Construct a new Data Log.
void WPI_DataLog_Finish(struct WPI_DataLog *datalog, int entry, int64_t timestamp)
Finish an entry.
void WPI_DataLog_AppendDouble(struct WPI_DataLog *datalog, int entry, double value, int64_t timestamp)
Appends a double record to the log.
void WPI_DataLog_Release(struct WPI_DataLog *datalog)
Releases a data log object.
void WPI_DataLog_AppendStringArray(struct WPI_DataLog *datalog, int entry, const struct WPI_String *arr, size_t len, int64_t timestamp)
Appends a string array record to the log.
void WPI_DataLog_AppendBooleanArray(struct WPI_DataLog *datalog, int entry, const int *arr, size_t len, int64_t timestamp)
Appends a boolean array record to the log.
void WPI_DataLog_Flush(struct WPI_DataLog *datalog)
Explicitly flushes the log data to disk.
int WPI_DataLog_Start(struct WPI_DataLog *datalog, const struct WPI_String *name, const struct WPI_String *type, const struct WPI_String *metadata, int64_t timestamp)
Start an entry.
void WPI_DataLog_AppendFloatArray(struct WPI_DataLog *datalog, int entry, const float *arr, size_t len, int64_t timestamp)
Appends a float array record to the log.
void WPI_DataLog_AppendIntegerArray(struct WPI_DataLog *datalog, int entry, const int64_t *arr, size_t len, int64_t timestamp)
Appends an integer array record to the log.
void WPI_DataLog_SetMetadata(struct WPI_DataLog *datalog, int entry, const struct WPI_String *metadata, int64_t timestamp)
Updates the metadata for an entry.
void WPI_DataLog_AppendFloat(struct WPI_DataLog *datalog, int entry, float value, int64_t timestamp)
Appends a float record to the log.
struct WPI_DataLog * WPI_DataLog_CreateBackgroundWriter_Func(void(*write)(void *ptr, const uint8_t *data, size_t len), void *ptr, double period, const struct WPI_String *extraHeader)
Construct a new Data Log background writer that passes its output to the provided function rather tha...
void WPI_DataLog_SetBackgroundWriterFilename(struct WPI_DataLog *datalog, const struct WPI_String *filename)
Change log filename.
auto write(OutputIt out, const std::tm &time, const std::locale &loc, char format, char modifier=0) -> OutputIt
Definition: chrono.h:449
type
Definition: base.h:646
constexpr const char * name(const T &)
auto ptr(const std::unique_ptr< T, Deleter > &p) -> const void *
Definition: std.h:602
A const UTF8 string.
Definition: string.h:14