67 std::vector<std::string> titles;
68 std::unique_ptr<std::ostream> pOs;
70 std::string log_name_bare;
71 std::string log_name_suffix;
73 std::string delim =
",";
75 int64_t n_line_max = INT64_MAX;
76 void update_ofstream(
const std::string &fname)
78 std::filesystem::path outFile{fname};
79 std::filesystem::create_directories(outFile.parent_path() /
".");
80 pOs = std::make_unique<std::ofstream>(fname);
86 template <
class T_titles>
87 CsvLog(T_titles &&n_titles, std::unique_ptr<std::ostream> n_pOs)
88 : titles(std::forward<T_titles>(n_titles)), pOs(std::move(n_pOs)) {}
95 template <
class T_titles>
97 const std::string &n_log_name_bare,
98 const std::string &n_log_name_suffix =
".csv",
99 int64_t n_n_line_max = 10000)
100 : titles(std::forward<T_titles>(n_titles)),
101 log_name_bare(n_log_name_bare),
102 log_name_suffix(n_log_name_suffix),
103 n_line_max(n_n_line_max)
105 update_ofstream(log_name_bare + n_log_name_suffix);
112 template <
class TMap>
117 (*pOs) << std::setprecision(nPrecision) << std::scientific;
118 for (
size_t i = 0; i < titles.size(); i++)
119 (*pOs) << title_to_value[titles[i]] << ((i == (titles.size() - 1)) ?
"" :
",");
122 if (n_line >= n_line_max)
127 update_ofstream(log_name_bare +
"_" + std::to_string(iBlock) + log_name_suffix);
134 for (
size_t i = 0; i < titles.size(); i++)
135 (*pOs) << titles[i] << ((i == (titles.size() - 1)) ?
"" :
",");
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DNDS_check_throw_info(expr, info)
Same as DNDS_check_throw but attaches a user-supplied info message to the thrown std::runtime_error.
Append-only CSV logger with automatic file rotation after a line-count limit.
void WriteLine(TMap &&title_to_value, int nPrecision)
Append one CSV row by looking up each column in title_to_value.
void WriteTitle()
Emit the title row (called automatically on the first WriteLine).
CsvLog(T_titles &&n_titles, std::unique_ptr< std::ostream > n_pOs)
Construct a logger with externally-provided titles and output stream.
CsvLog(T_titles &&n_titles, const std::string &n_log_name_bare, const std::string &n_log_name_suffix=".csv", int64_t n_n_line_max=10000)
Construct a logger that creates / manages its own output files.
the host side operators are provided as implemented
DNDS_CONSTANT const index UnInitIndex
Sentinel "not initialised" index value (= INT64_MIN).
std::map< std::string, LogSimpleDIValue > tLogSimpleDIValueMap
Convenience alias: the title -> value map type passed to CsvLog::WriteLine.
DNDS_CONSTANT const real UnInitReal
Sentinel "not initialised" real value (NaN). Cheap to detect with std::isnan or IsUnInitReal; survive...
Storage-agnostic "either int or double" value with CSV-friendly streaming.
std::enable_if_t< std::is_integral_v< T >, LogSimpleDIValue & > operator=(T v)
friend std::ostream & operator<<(std::ostream &o, const LogSimpleDIValue &v)
std::enable_if_t<!std::is_integral_v< T >, LogSimpleDIValue & > operator=(T v)
Eigen::Matrix< real, 5, 1 > v