forked from dsutanto/bytetrack-counter-cpp
36 lines
960 B
C++
36 lines
960 B
C++
#include "csv_logger.hpp"
|
|
#include <filesystem>
|
|
#include <iostream>
|
|
|
|
CsvLogger::CsvLogger(const std::string& path, const std::vector<std::string>& header) {
|
|
std::filesystem::path p(path);
|
|
if (p.has_parent_path())
|
|
std::filesystem::create_directories(p.parent_path());
|
|
|
|
bool new_file = !std::filesystem::exists(p) || std::filesystem::file_size(p) == 0;
|
|
file_.open(path, std::ios::app);
|
|
file_.exceptions(std::ios::failbit | std::ios::badbit);
|
|
|
|
if (new_file) {
|
|
for (size_t i = 0; i < header.size(); ++i) {
|
|
if (i > 0) file_ << ",";
|
|
file_ << header[i];
|
|
}
|
|
file_ << "\n";
|
|
file_.flush();
|
|
}
|
|
}
|
|
|
|
CsvLogger::~CsvLogger() {
|
|
if (file_.is_open()) file_.close();
|
|
}
|
|
|
|
void CsvLogger::write_row(const std::vector<std::string>& row) {
|
|
for (size_t i = 0; i < row.size(); ++i) {
|
|
if (i > 0) file_ << ",";
|
|
file_ << row[i];
|
|
}
|
|
file_ << "\n";
|
|
file_.flush();
|
|
}
|