mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[datalog] Move all DataLog functionality to new datalog library (#7641)
Currently the major DataLog backend API (reading and writing) is split between wpiutil and glass. In the interest of allowing code that wants to use these APIs to not need to link to glass and declutter wpiutil, all of those APIs are moved to a new library named "datalog". Signed-off-by: Jade Turner <spacey-sooty@proton.me> Co-authored-by: Jade Turner <spacey-sooty@proton.me> Co-authored-by: Gold856 <117957790+Gold856@users.noreply.github.com>
This commit is contained in:
88
datalog/src/main/native/cpp/DataLogWriter.cpp
Normal file
88
datalog/src/main/native/cpp/DataLogWriter.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
// 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 <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "wpi/datalog/DataLogWriter.h"
|
||||
|
||||
using namespace wpi::log;
|
||||
|
||||
static std::unique_ptr<wpi::raw_ostream> CheckOpen(std::string_view filename,
|
||||
std::error_code& ec) {
|
||||
auto rv = std::make_unique<wpi::raw_fd_ostream>(filename, ec);
|
||||
if (ec) {
|
||||
return nullptr;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
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, CheckOpen(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 struct WPI_String* filename, int* errorCode,
|
||||
const struct WPI_String* extraHeader) {
|
||||
std::error_code ec;
|
||||
auto rv = reinterpret_cast<WPI_DataLog*>(new DataLogWriter{
|
||||
wpi::to_string_view(filename), ec, wpi::to_string_view(extraHeader)});
|
||||
*errorCode = ec.value();
|
||||
return rv;
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Reference in New Issue
Block a user