RTC Toolkit 5.1.0
Loading...
Searching...
No Matches
csvDataRecorder.ipp
Go to the documentation of this file.
1
13// Note this is a template implementation file and should not be included directly.
14// The typical header protection macro is not added to avoid it showing up in Doxygen API
15// documentation.
16#pragma once
17
19
20#include <fmt/core.h>
21#include <iomanip>
22#include <iostream>
23#include <ostream>
24#include <sstream>
25#include <string_view>
26#include <type_traits>
27
29
30template <class... T>
32 const std::array<bool, sizeof...(T)>& disabled)
33 : DataRecorder<T...>(columns, disabled) {
34}
35
36template <class... T>
37void CsvDataRecorder<T...>::Open(const std::filesystem::path& file) {
38 m_output.open(file, std::ios::out);
39 bool first = true;
40 for (const auto& column : this->m_column_description) {
41 if (!first) {
42 m_output << ",";
43 } else {
44 first = false;
45 }
46 if (column.unit != "") {
47 m_output << std::quoted(fmt::format("{} [{}]", column.name, column.unit));
48 } else {
49 m_output << std::quoted(column.name);
50 }
51 }
52 m_output << "\n";
53}
54
55template <class... T>
57 m_output.close();
58}
59
60template <class... T>
61void CsvDataRecorder<T...>::Write(const std::tuple<T...>& data) {
62 bool first = true;
63 size_t column_index = 0;
64
65 std::apply(
66 [this, &first, &column_index](auto&&... args) mutable {
67 auto printer = [this, &first, &column_index](auto&& arg) mutable {
68 if (this->m_disabled_fields[column_index]) {
70 return;
71 }
72 if (first) {
73 if constexpr (std::is_same_v<std::decay_t<decltype(arg)>, std::string>) {
74 m_output << std::quoted(arg);
75 } else {
76 m_output << "\"" << arg << "\"";
77 }
78 first = false;
79 } else {
80 if constexpr (std::is_same_v<std::decay_t<decltype(arg)>, std::string>) {
81 m_output << "," << std::quoted(arg);
82 } else {
83 m_output << ",\"" << arg << "\"";
84 }
85 }
86 };
87 (printer(args), ...);
88 },
89 data);
90 m_output << "\n";
91}
92
93} // namespace rtctk::componentFramework
void Close() override
Close the currently open file.
Definition csvDataRecorder.ipp:56
CsvDataRecorder(const ColumnDescription &columns, const std::array< bool, sizeof...(T)> &disabled={})
Create a new recorder.
Definition csvDataRecorder.ipp:31
typename DataRecorder< T... >::ColumnDescription ColumnDescription
Definition csvDataRecorder.hpp:35
void Open(const std::filesystem::path &file) override
Open a file.
Definition csvDataRecorder.ipp:37
void Write(const std::tuple< T... > &data) override
Write a single row of data.
Definition csvDataRecorder.ipp:61
This is an abstract class that can be used to implement an OutputStage for a Recording Unit.
Definition dataRecorder.hpp:29
CSVDataRecorder allows to record as CSV files .
Definition commandReplier.cpp:22
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23