Rerun C++ SDK
Loading...
Searching...
No Matches
recording_stream.hpp
1#pragma once
2
3#include <chrono>
4#include <cstdint> // uint32_t etc.
5#include <filesystem>
6#include <optional>
7#include <string_view>
8#include <type_traits>
9#include <vector>
10
11#include "as_components.hpp"
12#include "component_column.hpp"
13#include "error.hpp"
14#include "spawn_options.hpp"
15#include "time_column.hpp"
16
17namespace rerun {
18 struct ComponentBatch;
19
20 enum class StoreKind {
21 Recording,
22 Blueprint,
23 };
24
25 /// A `RecordingStream` handles everything related to logging data into Rerun.
26 ///
27 /// ## Multithreading and ordering
28 ///
29 /// A `RecordingStream` is thread-safe.
30 ///
31 /// Internally, all operations are linearized into a pipeline:
32 /// - All operations sent by a given thread will take effect in the same exact order as that
33 /// thread originally sent them in, from its point of view.
34 /// - There isn't any well defined global order across multiple threads.
35 ///
36 /// This means that e.g. flushing the pipeline (`flush_blocking`) guarantees that all
37 /// previous data sent by the calling thread has been recorded; no more, no less.
38 /// (e.g. it does not mean that all file caches are flushed)
39 ///
40 /// ## Shutdown
41 ///
42 /// The `RecordingStream` can only be shutdown by dropping all instances of it, at which point
43 /// it will automatically take care of flushing any pending data that might remain in the
44 /// pipeline.
45 ///
46 /// TODO(andreas): The only way of having two instances of a `RecordingStream` is currently to
47 /// set it as a the global.
48 ///
49 /// Shutting down cannot ever block.
50 ///
51 /// ## Logging
52 ///
53 /// Internally, the stream will automatically micro-batch multiple log calls to optimize
54 /// transport.
55 /// See [SDK Micro Batching](https://www.rerun.io/docs/reference/sdk/micro-batching) for
56 /// more information.
57 ///
58 /// The data will be timestamped automatically based on the `RecordingStream`'s
59 /// internal clock.
61 public:
62 /// Creates a new recording stream to log to.
63 ///
64 /// \param app_id The user-chosen name of the application doing the logging.
65 /// \param recording_id The user-chosen name of the recording being logged to.
66 /// \param store_kind Whether to log to the recording store or the blueprint store.
68 std::string_view app_id, std::string_view recording_id = std::string_view(),
69 StoreKind store_kind = StoreKind::Recording
70 );
72
73 /// \private
75
76 // TODO(andreas): We could easily make the recording stream trivial to copy by bumping Rusts
77 // ref counter by adding a copy of the recording stream to the list of C recording streams.
78 // Doing it this way would likely yield the most consistent behavior when interacting with
79 // global streams (and especially when interacting with different languages in the same
80 // application).
81 /// \private
82 RecordingStream(const RecordingStream&) = delete;
83 /// \private
84 RecordingStream() = delete;
85
86 // -----------------------------------------------------------------------------------------
87 /// \name Properties
88 /// @{
89
90 /// Returns the store kind as passed during construction
91 StoreKind kind() const {
92 return _store_kind;
93 }
94
95 /// Returns whether the recording stream is enabled.
96 ///
97 /// All log functions early out if a recording stream is disabled.
98 /// Naturally, logging functions that take unserialized data will skip the serialization step as well.
99 bool is_enabled() const {
100 return _enabled;
101 }
102
103 /// @}
104
105 // -----------------------------------------------------------------------------------------
106 /// \name Controlling globally available instances of RecordingStream.
107 /// @{
108
109 /// Replaces the currently active recording for this stream's store kind in the global scope
110 /// with this one.
111 ///
112 /// Afterwards, destroying this recording stream will *not* change the global recording
113 /// stream, as it increases an internal ref-count.
114 void set_global() const;
115
116 /// Replaces the currently active recording for this stream's store kind in the thread-local
117 /// scope with this one
118 ///
119 /// Afterwards, destroying this recording stream will *not* change the thread local
120 /// recording stream, as it increases an internal ref-count.
121 void set_thread_local() const;
122
123 /// Retrieves the most appropriate globally available recording stream for the given kind.
124 ///
125 /// I.e. thread-local first, then global.
126 /// If neither was set, any operations on the returned stream will be no-ops.
127 static RecordingStream& current(StoreKind store_kind = StoreKind::Recording);
128
129 /// @}
130
131 // -----------------------------------------------------------------------------------------
132 /// \name Directing the recording stream.
133 /// \details Either of these needs to be called, otherwise the stream will buffer up indefinitely.
134 /// @{
135
136 /// Connect to a remote Rerun Viewer on the given ip:port.
137 ///
138 /// Requires that you first start a Rerun Viewer by typing 'rerun' in a terminal.
139 ///
140 /// flush_timeout_sec:
141 /// The minimum time the SDK will wait during a flush before potentially
142 /// dropping data if progress is not being made. Passing a negative value indicates no
143 /// timeout, and can cause a call to `flush` to block indefinitely.
144 ///
145 /// This function returns immediately.
146 [[deprecated("Use `connect_tcp` instead")]] Error connect(
147 std::string_view tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0
148 ) const;
149
150 /// Connect to a remote Rerun Viewer on the given ip:port.
151 ///
152 /// Requires that you first start a Rerun Viewer by typing 'rerun' in a terminal.
153 ///
154 /// flush_timeout_sec:
155 /// The minimum time the SDK will wait during a flush before potentially
156 /// dropping data if progress is not being made. Passing a negative value indicates no
157 /// timeout, and can cause a call to `flush` to block indefinitely.
158 ///
159 /// This function returns immediately.
161 std::string_view tcp_addr = "127.0.0.1:9876", float flush_timeout_sec = 2.0
162 ) const;
163
164 /// Spawns a new Rerun Viewer process from an executable available in PATH, then connects to it
165 /// over TCP.
166 ///
167 /// If a Rerun Viewer is already listening on this TCP port, the stream will be redirected to
168 /// that viewer instead of starting a new one.
169 ///
170 /// ## Parameters
171 /// options:
172 /// See `rerun::SpawnOptions` for more information.
173 ///
174 /// flush_timeout_sec:
175 /// The minimum time the SDK will wait during a flush before potentially
176 /// dropping data if progress is not being made. Passing a negative value indicates no
177 /// timeout, and can cause a call to `flush` to block indefinitely.
178 Error spawn(const SpawnOptions& options = {}, float flush_timeout_sec = 2.0) const;
179
180 /// @see RecordingStream::spawn
181 template <typename TRep, typename TPeriod>
183 const SpawnOptions& options = {},
184 std::chrono::duration<TRep, TPeriod> flush_timeout = std::chrono::seconds(2)
185 ) const {
186 using seconds_float = std::chrono::duration<float>; // Default ratio is 1:1 == seconds.
187 return spawn(options, std::chrono::duration_cast<seconds_float>(flush_timeout).count());
188 }
189
190 /// Stream all log-data to a given `.rrd` file.
191 ///
192 /// The Rerun Viewer is able to read continuously from the resulting rrd file while it is being written.
193 /// However, depending on your OS and configuration, changes may not be immediately visible due to file caching.
194 /// This is a common issue on Windows and (to a lesser extent) on MacOS.
195 ///
196 /// This function returns immediately.
197 Error save(std::string_view path) const;
198
199 /// Stream all log-data to standard output.
200 ///
201 /// Pipe the result into the Rerun Viewer to visualize it.
202 ///
203 /// If there isn't any listener at the other end of the pipe, the `RecordingStream` will
204 /// default back to `buffered` mode, in order not to break the user's terminal.
205 ///
206 /// This function returns immediately.
207 //
208 // NOTE: This should be called `stdout` like in other SDK, but turns out that `stdout` is a
209 // macro when compiling with msvc [1].
210 // [1]: https://learn.microsoft.com/en-us/cpp/c-runtime-library/stdin-stdout-stderr?view=msvc-170
212
213 /// Initiates a flush the batching pipeline and waits for it to propagate.
214 ///
215 /// See `RecordingStream` docs for ordering semantics and multithreading guarantees.
216 void flush_blocking() const;
217
218 /// @}
219
220 // -----------------------------------------------------------------------------------------
221 /// \name Controlling log time.
222 /// \details
223 /// @{
224
225 /// Set the current time of the recording, for the current calling thread.
226 ///
227 /// Used for all subsequent logging performed from this same thread, until the next call
228 /// to one of the time setting methods.
229 ///
230 /// For example: `rec.set_time_sequence("frame_nr", frame_nr)`.
231 ///
232 /// You can remove a timeline from subsequent log calls again using `rec.disable_timeline`.
233 /// @see set_time_seconds, set_time_nanos, reset_time, set_time, disable_timeline
234 void set_time_sequence(std::string_view timeline_name, int64_t sequence_nr) const;
235
236 /// Set the current time of the recording, for the current calling thread.
237 ///
238 /// Used for all subsequent logging performed from this same thread, until the next call
239 /// to one of the time setting methods.
240 ///
241 /// For example: `rec.set_time("sim_time", sim_time_secs)`.
242 ///
243 /// You can remove a timeline from subsequent log calls again using `rec.disable_timeline`.
244 /// @see set_time_sequence, set_time_seconds, set_time_nanos, reset_time, disable_timeline
245 template <typename TClock>
246 void set_time(std::string_view timeline_name, std::chrono::time_point<TClock> time) const {
247 set_time(timeline_name, time.time_since_epoch());
248 }
249
250 /// Set the current time of the recording, for the current calling thread.
251 ///
252 /// Used for all subsequent logging performed from this same thread, until the next call
253 /// to one of the time setting methods.
254 ///
255 /// For example: `rec.set_time("sim_time", sim_time_secs)`.
256 ///
257 /// You can remove a timeline from subsequent log calls again using `rec.disable_timeline`.
258 /// @see set_time_sequence, set_time_seconds, set_time_nanos, reset_time, disable_timeline
259 template <typename TRep, typename TPeriod>
260 void set_time(std::string_view timeline_name, std::chrono::duration<TRep, TPeriod> time)
261 const {
262 if constexpr (std::is_floating_point<TRep>::value) {
263 using seconds_double =
264 std::chrono::duration<double>; // Default ratio is 1:1 == seconds.
266 timeline_name,
267 std::chrono::duration_cast<seconds_double>(time).count()
268 );
269 } else {
271 timeline_name,
272 std::chrono::duration_cast<std::chrono::nanoseconds>(time).count()
273 );
274 }
275 }
276
277 /// Set the current time of the recording, for the current calling thread.
278 ///
279 /// Used for all subsequent logging performed from this same thread, until the next call
280 /// to one of the time setting methods.
281 ///
282 /// For example: `rec.set_time_seconds("sim_time", sim_time_secs)`.
283 ///
284 /// You can remove a timeline from subsequent log calls again using `rec.disable_timeline`.
285 /// @see set_time_sequence, set_time_nanos, reset_time, set_time, disable_timeline
286 void set_time_seconds(std::string_view timeline_name, double seconds) const;
287
288 /// Set the current time of the recording, for the current calling thread.
289 ///
290 /// Used for all subsequent logging performed from this same thread, until the next call
291 /// to one of the time setting methods.
292 ///
293 /// For example: `rec.set_time_nanos("sim_time", sim_time_nanos)`.
294 ///
295 /// You can remove a timeline from subsequent log calls again using `rec.disable_timeline`.
296 /// @see set_time_sequence, set_time_seconds, reset_time, set_time, disable_timeline
297 void set_time_nanos(std::string_view timeline_name, int64_t nanos) const;
298
299 /// Stops logging to the specified timeline for subsequent log calls.
300 ///
301 /// The timeline is still there, but will not be updated with any new data.
302 ///
303 /// No-op if the timeline doesn't exist.
304 ///
305 /// @see set_time_sequence, set_time_seconds, set_time, reset_time
306 void disable_timeline(std::string_view timeline_name) const;
307
308 /// Clears out the current time of the recording, for the current calling thread.
309 ///
310 /// Used for all subsequent logging performed from this same thread, until the next call
311 /// to one of the time setting methods.
312 ///
313 /// For example: `rec.reset_time()`.
314 /// @see set_time_sequence, set_time_seconds, set_time_nanos, disable_timeline
315 void reset_time() const;
316
317 /// @}
318
319 // -----------------------------------------------------------------------------------------
320 /// \name Sending & logging data.
321 /// @{
322
323 /// Logs one or more archetype and/or component batches.
324 ///
325 /// This is the main entry point for logging data to rerun. It can be used to log anything
326 /// that implements the `AsComponents<T>` trait.
327 ///
328 /// When logging data, you must always provide an [entity_path](https://www.rerun.io/docs/concepts/entity-path)
329 /// for identifying the data. Note that the path prefix "rerun/" is considered reserved for use by the Rerun SDK
330 /// itself and should not be used for logging user data. This is where Rerun will log additional information
331 /// such as warnings.
332 ///
333 /// The most common way to log is with one of the rerun archetypes, all of which implement the `AsComponents` trait.
334 ///
335 /// For example, to log two 3D points:
336 /// ```
337 /// rec.log("my/point", rerun::Points3D({{0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f}}));
338 /// ```
339 ///
340 /// The `log` function can flexibly accept an arbitrary number of additional objects which will
341 /// be merged into the first entity, for instance:
342 /// ```
343 /// // Log three points with arrows sticking out of them:
344 /// rec.log(
345 /// "my/points",
346 /// rerun::Points3D({{0.2f, 0.5f, 0.3f}, {0.9f, 1.2f, 0.1f}, {1.0f, 4.2f, 0.3f}})
347 /// .with_radii({0.1, 0.2, 0.3}),
348 /// rerun::Arrows3D::from_vectors({{0.3f, 2.1f, 0.2f}, {0.9f, -1.1, 2.3f}, {-0.4f, 0.5f, 2.9f}})
349 /// );
350 /// ```
351 ///
352 /// Any failures that may are handled with `Error::handle`.
353 ///
354 /// \param entity_path Path to the entity in the space hierarchy.
355 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
356 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
357 /// You can implement `AsComponents` for your own types as well
358 ///
359 /// @see try_log, log_static, try_log_with_static
360 template <typename... Ts>
361 void log(std::string_view entity_path, const Ts&... as_components) const {
362 if (!is_enabled()) {
363 return;
364 }
365 try_log_with_static(entity_path, false, as_components...).handle();
366 }
367
368 /// Logs one or more archetype and/or component batches as static data.
369 ///
370 /// Like `log` but logs the data as static:
371 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
372 /// any temporal data of the same type.
373 ///
374 /// Failures are handled with `Error::handle`.
375 ///
376 /// \param entity_path Path to the entity in the space hierarchy.
377 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
378 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
379 /// You can implement `AsComponents` for your own types as well
380 ///
381 /// @see log, try_log_static, try_log_with_static
382 template <typename... Ts>
383 void log_static(std::string_view entity_path, const Ts&... as_components) const {
384 if (!is_enabled()) {
385 return;
386 }
387 try_log_with_static(entity_path, true, as_components...).handle();
388 }
389
390 /// Logs one or more archetype and/or component batches.
391 ///
392 /// See `log` for more information.
393 /// Unlike `log` this method returns an error if an error occurs.
394 ///
395 /// \param entity_path Path to the entity in the space hierarchy.
396 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
397 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
398 /// You can implement `AsComponents` for your own types as well
399 ///
400 /// @see log, try_log_static, try_log_with_static
401 template <typename... Ts>
402 Error try_log(std::string_view entity_path, const Ts&... as_components) const {
403 if (!is_enabled()) {
404 return Error::ok();
405 }
406 return try_log_with_static(entity_path, false, as_components...);
407 }
408
409 /// Logs one or more archetype and/or component batches as static data, returning an error.
410 ///
411 /// See `log`/`log_static` for more information.
412 /// Unlike `log_static` this method returns if an error occurs.
413 ///
414 /// \param entity_path Path to the entity in the space hierarchy.
415 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
416 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
417 /// You can implement `AsComponents` for your own types as well
418 /// \returns An error if an error occurs during evaluation of `AsComponents` or logging.
419 ///
420 /// @see log_static, try_log, try_log_with_static
421 template <typename... Ts>
422 Error try_log_static(std::string_view entity_path, const Ts&... as_components) const {
423 if (!is_enabled()) {
424 return Error::ok();
425 }
426 return try_log_with_static(entity_path, true, as_components...);
427 }
428
429 /// Logs one or more archetype and/or component batches optionally static, returning an error.
430 ///
431 /// See `log`/`log_static` for more information.
432 /// Returns an error if an error occurs during evaluation of `AsComponents` or logging.
433 ///
434 /// \param entity_path Path to the entity in the space hierarchy.
435 /// \param static_ If true, the logged components will be static.
436 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
437 /// any temporal data of the same type.
438 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
439 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
440 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
441 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
442 /// You can implement `AsComponents` for your own types as well
443 ///
444 /// @see log, try_log, log_static, try_log_static
445 template <typename... Ts>
446 void log_with_static(std::string_view entity_path, bool static_, const Ts&... as_components)
447 const {
448 try_log_with_static(entity_path, static_, as_components...).handle();
449 }
450
451 /// Logs one or more archetype and/or component batches optionally static, returning an error.
452 ///
453 /// See `log`/`log_static` for more information.
454 /// Returns an error if an error occurs during evaluation of `AsComponents` or logging.
455 ///
456 /// \param entity_path Path to the entity in the space hierarchy.
457 /// \param static_ If true, the logged components will be static.
458 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
459 /// any temporal data of the same type.
460 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
461 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
462 /// \param as_components Any type for which the `AsComponents<T>` trait is implemented.
463 /// This is the case for any archetype as well as individual or collection of `ComponentBatch`.
464 /// You can implement `AsComponents` for your own types as well
465 /// \returns An error if an error occurs during evaluation of `AsComponents` or logging.
466 ///
467 /// @see log, try_log, log_static, try_log_static
468 template <typename... Ts>
470 std::string_view entity_path, bool static_, const Ts&... as_components
471 ) const {
472 if (!is_enabled()) {
473 return Error::ok();
474 }
475 std::vector<ComponentBatch> serialized_columns;
476 Error err;
477 (
478 [&] {
479 if (err.is_err()) {
480 return;
481 }
482
483 const Result<Collection<ComponentBatch>> serialization_result =
484 AsComponents<Ts>().as_batches(as_components);
485 if (serialization_result.is_err()) {
486 err = serialization_result.error;
487 return;
488 }
489
490 if (serialized_columns.empty()) {
491 // Fast path for the first batch (which is usually the only one!)
492 serialized_columns = std::move(serialization_result.value).to_vector();
493 } else {
494 serialized_columns.insert(
495 serialized_columns.end(),
496 std::make_move_iterator(serialization_result.value.begin()),
497 std::make_move_iterator(serialization_result.value.end())
498 );
499 }
500 }(),
501 ...
502 );
503 RR_RETURN_NOT_OK(err);
504
505 return try_log_serialized_batches(entity_path, static_, std::move(serialized_columns));
506 }
507
508 /// Logs several serialized batches batches, returning an error on failure.
509 ///
510 /// This is a more low-level API than `log`/`log_static\ and requires you to already serialize the data
511 /// ahead of time.
512 ///
513 /// \param entity_path Path to the entity in the space hierarchy.
514 /// \param static_ If true, the logged components will be static.
515 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
516 /// any temporal data of the same type.
517 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
518 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
519 /// \param batches The serialized batches to log.
520 ///
521 /// \see `log`, `try_log`, `log_static`, `try_log_static`, `try_log_with_static`
523 std::string_view entity_path, bool static_, std::vector<ComponentBatch> batches
524 ) const;
525
526 /// Bottom level API that logs raw data cells to the recording stream.
527 ///
528 /// In order to use this you need to pass serialized Arrow data cells.
529 ///
530 /// \param entity_path Path to the entity in the space hierarchy.
531 /// \param num_data_cells Number of data cells passed in.
532 /// \param data_cells The data cells to log.
533 /// \param inject_time
534 /// If set to `true`, the row's timestamp data will be overridden using the recording
535 /// streams internal clock.
536 ///
537 /// \see `try_log_serialized_batches`
539 std::string_view entity_path, size_t num_data_cells, const ComponentBatch* data_cells,
540 bool inject_time
541 ) const;
542
543 /// Logs the file at the given `path` using all `DataLoader`s available.
544 ///
545 /// A single `path` might be handled by more than one loader.
546 ///
547 /// This method blocks until either at least one `DataLoader` starts streaming data in
548 /// or all of them fail.
549 ///
550 /// See <https://www.rerun.io/docs/reference/data-loaders/overview> for more information.
551 ///
552 /// \param filepath Path to the file to be logged.
553 /// \param entity_path_prefix What should the logged entity paths be prefixed with?
554 /// \param static_ If true, the logged components will be static.
555 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
556 /// any temporal data of the same type.
557 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
558 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
559 ///
560 /// \see `try_log_file_from_path`
562 const std::filesystem::path& filepath,
563 std::string_view entity_path_prefix = std::string_view(), bool static_ = false
564 ) const {
565 try_log_file_from_path(filepath, entity_path_prefix, static_).handle();
566 }
567
568 /// Logs the file at the given `path` using all `DataLoader`s available.
569 ///
570 /// A single `path` might be handled by more than one loader.
571 ///
572 /// This method blocks until either at least one `DataLoader` starts streaming data in
573 /// or all of them fail.
574 ///
575 /// See <https://www.rerun.io/docs/reference/data-loaders/overview> for more information.
576 ///
577 /// \param filepath Path to the file to be logged.
578 /// \param entity_path_prefix What should the logged entity paths be prefixed with?
579 /// \param static_ If true, the logged components will be static.
580 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
581 /// any temporal data of the same type.
582 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
583 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
584 ///
585 /// \see `log_file_from_path`
587 const std::filesystem::path& filepath,
588 std::string_view entity_path_prefix = std::string_view(), bool static_ = false
589 ) const;
590
591 /// Logs the given `contents` using all `DataLoader`s available.
592 ///
593 /// A single `path` might be handled by more than one loader.
594 ///
595 /// This method blocks until either at least one `DataLoader` starts streaming data in
596 /// or all of them fail.
597 ///
598 /// See <https://www.rerun.io/docs/reference/data-loaders/overview> for more information.
599 ///
600 /// \param filepath Path to the file that the `contents` belong to.
601 /// \param contents Contents to be logged.
602 /// \param contents_size Size in bytes of the `contents`.
603 /// \param entity_path_prefix What should the logged entity paths be prefixed with?
604 /// \param static_ If true, the logged components will be static.
605 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
606 /// any temporal data of the same type.
607 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
608 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
609 ///
610 /// \see `try_log_file_from_contents`
612 const std::filesystem::path& filepath, const std::byte* contents, size_t contents_size,
613 std::string_view entity_path_prefix = std::string_view(), bool static_ = false
614 ) const {
616 filepath,
617 contents,
618 contents_size,
619 entity_path_prefix,
620 static_
621 )
622 .handle();
623 }
624
625 /// Logs the given `contents` using all `DataLoader`s available.
626 ///
627 /// A single `path` might be handled by more than one loader.
628 ///
629 /// This method blocks until either at least one `DataLoader` starts streaming data in
630 /// or all of them fail.
631 ///
632 /// See <https://www.rerun.io/docs/reference/data-loaders/overview> for more information.
633 ///
634 /// \param filepath Path to the file that the `contents` belong to.
635 /// \param contents Contents to be logged.
636 /// \param contents_size Size in bytes of the `contents`.
637 /// \param entity_path_prefix What should the logged entity paths be prefixed with?
638 /// \param static_ If true, the logged components will be static.
639 /// Static data has no time associated with it, exists on all timelines, and unconditionally shadows
640 /// any temporal data of the same type.
641 /// Otherwise, the data will be timestamped automatically with `log_time` and `log_tick`.
642 /// Additional timelines set by `set_time_sequence` or `set_time` will also be included.
643 ///
644 /// \see `log_file_from_contents`
646 const std::filesystem::path& filepath, const std::byte* contents, size_t contents_size,
647 std::string_view entity_path_prefix = std::string_view(), bool static_ = false
648 ) const;
649
650 /// Directly log a columns of data to Rerun.
651 ///
652 /// This variant takes in arbitrary amount of `ComponentColumn`s and `ComponentColumn` collections.
653 ///
654 /// Unlike the regular `log` API, which is row-oriented, this API lets you submit the data
655 /// in a columnar form. Each `TimeColumn` and `ComponentColumn` represents a column of data that will be sent to Rerun.
656 /// The lengths of all of these columns must match, and all
657 /// data that shares the same index across the different columns will act as a single logical row,
658 /// equivalent to a single call to `RecordingStream::log`.
659 ///
660 /// Note that this API ignores any stateful time set on the log stream via the `RecordingStream::set_time_*` APIs.
661 /// Furthermore, this will _not_ inject the default timelines `log_tick` and `log_time` timeline columns.
662 ///
663 /// Any failures that may occur during serialization are handled with `Error::handle`.
664 ///
665 /// \param entity_path Path to the entity in the space hierarchy.
666 /// \param time_columns The time columns to send.
667 /// \param component_columns The columns of components to send. Both individual `ComponentColumn`s and `Collection<ComponentColumn>`s are accepted.
668 /// \see `try_send_columns`
669 template <typename... Ts>
671 std::string_view entity_path, Collection<TimeColumn> time_columns,
672 Ts... component_columns // NOLINT
673 ) const {
674 try_send_columns(entity_path, time_columns, component_columns...).handle();
675 }
676
677 /// Directly log a columns of data to Rerun.
678 ///
679 /// This variant takes in arbitrary amount of `ComponentColumn`s and `ComponentColumn` collections.
680 ///
681 /// Unlike the regular `log` API, which is row-oriented, this API lets you submit the data
682 /// in a columnar form. Each `TimeColumn` and `ComponentColumn` represents a column of data that will be sent to Rerun.
683 /// The lengths of all of these columns must match, and all
684 /// data that shares the same index across the different columns will act as a single logical row,
685 /// equivalent to a single call to `RecordingStream::log`.
686 ///
687 /// Note that this API ignores any stateful time set on the log stream via the `RecordingStream::set_time_*` APIs.
688 /// Furthermore, this will _not_ inject the default timelines `log_tick` and `log_time` timeline columns.
689 ///
690 /// \param entity_path Path to the entity in the space hierarchy.
691 /// \param time_columns The time columns to send.
692 /// \param component_columns The columns of components to send. Both individual `ComponentColumn`s and `Collection<ComponentColumn>`s are accepted.
693 /// \see `send_columns`
694 template <typename... Ts>
696 std::string_view entity_path, Collection<TimeColumn> time_columns,
697 Ts... component_columns // NOLINT
698 ) const {
699 if constexpr (sizeof...(Ts) == 1) {
700 // Directly forward if this is only a single element,
701 // skipping collection of component column vector.
702 return try_send_columns(
703 entity_path,
704 std::move(time_columns),
705 Collection(std::forward<Ts...>(component_columns...))
706 );
707 }
708
709 std::vector<ComponentColumn> flat_column_list;
710 (
711 [&] {
712 static_assert(
713 std::is_same_v<std::remove_cv_t<Ts>, ComponentColumn> ||
714 std::is_constructible_v<Collection<ComponentColumn>, Ts>,
715 "Ts must be ComponentColumn or a collection thereof"
716 );
717
718 push_back_columns(flat_column_list, std::move(component_columns));
719 }(),
720 ...
721 );
722 return try_send_columns(
723 entity_path,
724 std::move(time_columns),
725 // Need to create collection explicitly, otherwise this becomes a recursive call.
726 Collection<ComponentColumn>(std::move(flat_column_list))
727 );
728 }
729
730 /// Directly log a columns of data to Rerun.
731 ///
732 /// Unlike the regular `log` API, which is row-oriented, this API lets you submit the data
733 /// in a columnar form. Each `TimeColumn` and `ComponentColumn` represents a column of data that will be sent to Rerun.
734 /// The lengths of all of these columns must match, and all
735 /// data that shares the same index across the different columns will act as a single logical row,
736 /// equivalent to a single call to `RecordingStream::log`.
737 ///
738 /// Note that this API ignores any stateful time set on the log stream via the `RecordingStream::set_time_*` APIs.
739 /// Furthermore, this will _not_ inject the default timelines `log_tick` and `log_time` timeline columns.
740 ///
741 /// Any failures that may occur during serialization are handled with `Error::handle`.
742 ///
743 /// \param entity_path Path to the entity in the space hierarchy.
744 /// \param time_columns The time columns to send.
745 /// \param component_columns The columns of components to send.
746 /// \see `try_send_columns`
748 std::string_view entity_path, Collection<TimeColumn> time_columns,
749 Collection<ComponentColumn> component_columns
750 ) const {
751 try_send_columns(entity_path, time_columns, component_columns).handle();
752 }
753
754 /// Directly log a columns of data to Rerun.
755 ///
756 /// Unlike the regular `log` API, which is row-oriented, this API lets you submit the data
757 /// in a columnar form. Each `TimeColumn` and `ComponentColumn` represents a column of data that will be sent to Rerun.
758 /// The lengths of all of these columns must match, and all
759 /// data that shares the same index across the different columns will act as a single logical row,
760 /// equivalent to a single call to `RecordingStream::log`.
761 ///
762 /// Note that this API ignores any stateful time set on the log stream via the `RecordingStream::set_time_*` APIs.
763 /// Furthermore, this will _not_ inject the default timelines `log_tick` and `log_time` timeline columns.
764 ///
765 /// \param entity_path Path to the entity in the space hierarchy.
766 /// \param time_columns The time columns to send.
767 /// \param component_columns The columns of components to send.
768 /// \see `send_columns`
770 std::string_view entity_path, Collection<TimeColumn> time_columns,
771 Collection<ComponentColumn> component_columns
772 ) const;
773
774 /// @}
775
776 private:
777 // Utility function to implement `try_send_columns` variadic template.
778 static void push_back_columns(
779 std::vector<ComponentColumn>& component_columns, Collection<ComponentColumn> new_columns
780 ) {
781 for (const auto& new_column : new_columns) {
782 component_columns.emplace_back(std::move(new_column));
783 }
784 }
785
786 static void push_back_columns(
787 std::vector<ComponentColumn>& component_columns, ComponentColumn new_column
788 ) {
789 component_columns.emplace_back(std::move(new_column));
790 }
791
792 RecordingStream(uint32_t id, StoreKind store_kind);
793
794 uint32_t _id;
795 StoreKind _store_kind;
796 bool _enabled;
797 };
798} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:96
void handle() const
Handle this error based on the set log handler.
bool is_err() const
Returns true if the code is not Ok.
Definition error.hpp:132
static Error ok()
Creates a new error set to ok.
Definition error.hpp:117
A RecordingStream handles everything related to logging data into Rerun.
Definition recording_stream.hpp:60
Error try_log_with_static(std::string_view entity_path, bool static_, const Ts &... as_components) const
Logs one or more archetype and/or component batches optionally static, returning an error.
Definition recording_stream.hpp:469
Error spawn(const SpawnOptions &options={}, float flush_timeout_sec=2.0) const
Spawns a new Rerun Viewer process from an executable available in PATH, then connects to it over TCP.
Error try_send_columns(std::string_view entity_path, Collection< TimeColumn > time_columns, Collection< ComponentColumn > component_columns) const
Directly log a columns of data to Rerun.
void log_file_from_path(const std::filesystem::path &filepath, std::string_view entity_path_prefix=std::string_view(), bool static_=false) const
Logs the file at the given path using all DataLoaders available.
Definition recording_stream.hpp:561
bool is_enabled() const
Returns whether the recording stream is enabled.
Definition recording_stream.hpp:99
Error try_log(std::string_view entity_path, const Ts &... as_components) const
Logs one or more archetype and/or component batches.
Definition recording_stream.hpp:402
void disable_timeline(std::string_view timeline_name) const
Stops logging to the specified timeline for subsequent log calls.
void reset_time() const
Clears out the current time of the recording, for the current calling thread.
Error to_stdout() const
Stream all log-data to standard output.
void send_columns(std::string_view entity_path, Collection< TimeColumn > time_columns, Collection< ComponentColumn > component_columns) const
Directly log a columns of data to Rerun.
Definition recording_stream.hpp:747
Error try_log_file_from_path(const std::filesystem::path &filepath, std::string_view entity_path_prefix=std::string_view(), bool static_=false) const
Logs the file at the given path using all DataLoaders available.
Error save(std::string_view path) const
Stream all log-data to a given .rrd file.
Error try_log_static(std::string_view entity_path, const Ts &... as_components) const
Logs one or more archetype and/or component batches as static data, returning an error.
Definition recording_stream.hpp:422
StoreKind kind() const
Returns the store kind as passed during construction.
Definition recording_stream.hpp:91
Error spawn(const SpawnOptions &options={}, std::chrono::duration< TRep, TPeriod > flush_timeout=std::chrono::seconds(2)) const
Definition recording_stream.hpp:182
Error try_log_data_row(std::string_view entity_path, size_t num_data_cells, const ComponentBatch *data_cells, bool inject_time) const
Bottom level API that logs raw data cells to the recording stream.
void log_file_from_contents(const std::filesystem::path &filepath, const std::byte *contents, size_t contents_size, std::string_view entity_path_prefix=std::string_view(), bool static_=false) const
Logs the given contents using all DataLoaders available.
Definition recording_stream.hpp:611
void flush_blocking() const
Initiates a flush the batching pipeline and waits for it to propagate.
void set_time_nanos(std::string_view timeline_name, int64_t nanos) const
Set the current time of the recording, for the current calling thread.
void set_time(std::string_view timeline_name, std::chrono::duration< TRep, TPeriod > time) const
Set the current time of the recording, for the current calling thread.
Definition recording_stream.hpp:260
void log_with_static(std::string_view entity_path, bool static_, const Ts &... as_components) const
Logs one or more archetype and/or component batches optionally static, returning an error.
Definition recording_stream.hpp:446
static RecordingStream & current(StoreKind store_kind=StoreKind::Recording)
Retrieves the most appropriate globally available recording stream for the given kind.
RecordingStream(std::string_view app_id, std::string_view recording_id=std::string_view(), StoreKind store_kind=StoreKind::Recording)
Creates a new recording stream to log to.
void log(std::string_view entity_path, const Ts &... as_components) const
Logs one or more archetype and/or component batches.
Definition recording_stream.hpp:361
void set_thread_local() const
Replaces the currently active recording for this stream's store kind in the thread-local scope with t...
Error try_log_serialized_batches(std::string_view entity_path, bool static_, std::vector< ComponentBatch > batches) const
Logs several serialized batches batches, returning an error on failure.
void set_time(std::string_view timeline_name, std::chrono::time_point< TClock > time) const
Set the current time of the recording, for the current calling thread.
Definition recording_stream.hpp:246
Error connect_tcp(std::string_view tcp_addr="127.0.0.1:9876", float flush_timeout_sec=2.0) const
Connect to a remote Rerun Viewer on the given ip:port.
void set_time_seconds(std::string_view timeline_name, double seconds) const
Set the current time of the recording, for the current calling thread.
void send_columns(std::string_view entity_path, Collection< TimeColumn > time_columns, Ts... component_columns) const
Directly log a columns of data to Rerun.
Definition recording_stream.hpp:670
Error try_log_file_from_contents(const std::filesystem::path &filepath, const std::byte *contents, size_t contents_size, std::string_view entity_path_prefix=std::string_view(), bool static_=false) const
Logs the given contents using all DataLoaders available.
void set_global() const
Replaces the currently active recording for this stream's store kind in the global scope with this on...
Error connect(std::string_view tcp_addr="127.0.0.1:9876", float flush_timeout_sec=2.0) const
Connect to a remote Rerun Viewer on the given ip:port.
void set_time_sequence(std::string_view timeline_name, int64_t sequence_nr) const
Set the current time of the recording, for the current calling thread.
Error try_send_columns(std::string_view entity_path, Collection< TimeColumn > time_columns, Ts... component_columns) const
Directly log a columns of data to Rerun.
Definition recording_stream.hpp:695
void log_static(std::string_view entity_path, const Ts &... as_components) const
Logs one or more archetype and/or component batches as static data.
Definition recording_stream.hpp:383
A class for representing either a usable value, or an error.
Definition result.hpp:14
bool is_err() const
Returns true if error is not set to rerun::ErrorCode::Ok, implying that no value is contained,...
Definition result.hpp:44
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:23
Arrow-encoded data of a single batch of components together with a component descriptor.
Definition component_batch.hpp:28
Arrow-encoded data of a column of components.
Definition component_column.hpp:20
Options to control the behavior of spawn.
Definition spawn_options.hpp:17