[wpiutil] Split DataLog background writer into different class (#6590)

DataLog is now a base class, with DataLogBackgroundWriter being the
background thread version and DataLogWriter being a non-threaded version.

Also split the C header into a separate file to make it more wpiformat friendly.
This commit is contained in:
Peter Johnson
2024-05-12 14:09:43 -07:00
committed by GitHub
parent 305a0657e2
commit 178fe99f12
20 changed files with 1588 additions and 1089 deletions

View File

@@ -0,0 +1,76 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "wpi/DataLogWriter.h"
#include "wpi/raw_ostream.h"
using namespace wpi::log;
DataLogWriter::DataLogWriter(std::string_view filename, std::error_code& ec,
std::string_view extraHeader)
: DataLogWriter{s_defaultMessageLog, filename, ec, extraHeader} {}
DataLogWriter::DataLogWriter(wpi::Logger& msglog, std::string_view filename,
std::error_code& ec, std::string_view extraHeader)
: DataLogWriter{msglog, std::make_unique<raw_fd_ostream>(filename, ec),
extraHeader} {
if (ec) {
Stop();
}
}
DataLogWriter::DataLogWriter(std::unique_ptr<wpi::raw_ostream> os,
std::string_view extraHeader)
: DataLogWriter{s_defaultMessageLog, std::move(os), extraHeader} {}
DataLogWriter::DataLogWriter(wpi::Logger& msglog,
std::unique_ptr<wpi::raw_ostream> os,
std::string_view extraHeader)
: DataLog{msglog, extraHeader}, m_os{std::move(os)} {
StartFile();
}
DataLogWriter::~DataLogWriter() {
if (m_os) {
Flush();
}
}
void DataLogWriter::Flush() {
if (!m_os) {
return;
}
std::vector<Buffer> writeBufs;
FlushBufs(&writeBufs);
for (auto&& buf : writeBufs) {
(*m_os) << buf.GetData();
}
ReleaseBufs(&writeBufs);
}
void DataLogWriter::Stop() {
DataLog::Stop();
Flush();
m_os.reset();
}
bool DataLogWriter::BufferFull() {
Flush();
return false;
}
extern "C" {
struct WPI_DataLog* WPI_DataLog_CreateWriter(const char* filename,
int* errorCode,
const char* extraHeader) {
std::error_code ec;
auto rv = reinterpret_cast<WPI_DataLog*>(
new DataLogWriter{filename, ec, extraHeader});
*errorCode = ec.value();
return rv;
}
} // extern "C"