Skip to content

Commit

Permalink
Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Nov 4, 2023
1 parent 0533371 commit f7cb629
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
18 changes: 12 additions & 6 deletions ntcoreffi/src/main/native/cpp/DataLogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,18 @@ Thread::~Thread() {
void Thread::Main() {
// based on free disk space, scan for "old" FRC_*.wpilog files and remove
{
uintmax_t freeSpace = fs::space(m_logDir).free;
std::error_code ec;
uintmax_t freeSpace;
auto freeSpaceInfo = fs::space(m_logDir, ec);
if (!ec) {
freeSpace = freeSpaceInfo.available;
} else {
freeSpace = UINTMAX_MAX;
}
if (freeSpace < kFreeSpaceThreshold) {
// Delete oldest FRC_*.wpilog files (ignore FRC_TBD_*.wpilog as we just
// created one)
std::vector<fs::directory_entry> entries;
std::error_code ec;
for (auto&& entry : fs::directory_iterator{m_logDir, ec}) {
auto stem = entry.path().stem().string();
if (wpi::starts_with(stem, "FRC_") &&
Expand Down Expand Up @@ -462,15 +468,15 @@ static Instance& GetInstance(std::string_view dir = "",
std::string_view filename = "",
double period = 0.25) {
static Instance instance(dir, filename, period);
if (!instance.owner) {
instance.owner.Start(MakeLogDir(dir), filename, period);
}
return instance;
}

void DataLogManager::Start(std::string_view dir, std::string_view filename,
double period) {
auto& inst = GetInstance(dir, filename, period);
if (!inst.owner) {
inst.owner.Start(dir, MakeLogDir(filename), period);
}
GetInstance(dir, filename, period);
}

void DataLogManager::Stop() {
Expand Down
18 changes: 12 additions & 6 deletions wpilibc/src/main/native/cpp/DataLogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ Thread::~Thread() {
void Thread::Main() {
// based on free disk space, scan for "old" FRC_*.wpilog files and remove
{
uintmax_t freeSpace = fs::space(m_logDir).available;
std::error_code ec;
uintmax_t freeSpace;
auto freeSpaceInfo = fs::space(m_logDir, ec);
if (!ec) {
freeSpace = freeSpaceInfo.available;
} else {
freeSpace = UINTMAX_MAX;
}
if (freeSpace < kFreeSpaceThreshold) {
// Delete oldest FRC_*.wpilog files (ignore FRC_TBD_*.wpilog as we just
// created one)
std::vector<fs::directory_entry> entries;
std::error_code ec;
for (auto&& entry : fs::directory_iterator{m_logDir, ec}) {
auto stem = entry.path().stem().string();
if (wpi::starts_with(stem, "FRC_") &&
Expand Down Expand Up @@ -308,15 +314,15 @@ static Instance& GetInstance(std::string_view dir = "",
std::string_view filename = "",
double period = 0.25) {
static Instance instance(dir, filename, period);
if (!instance.owner) {
instance.owner.Start(MakeLogDir(dir), filename, period);
}
return instance;
}

void DataLogManager::Start(std::string_view dir, std::string_view filename,
double period) {
auto& inst = GetInstance(dir, filename, period);
if (!inst.owner) {
inst.owner.Start(dir, MakeLogDir(filename), period);
}
GetInstance(dir, filename, period);
}

void DataLogManager::Stop() {
Expand Down
22 changes: 18 additions & 4 deletions wpiutil/src/main/native/cpp/DataLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct DataLog::WriterThreadState {
std::string filename;
fs::path path;
fs::file_t f = fs::kInvalidFile;
uintmax_t freeSpace;
uintmax_t freeSpace = UINTMAX_MAX;
};

void DataLog::StartLogFile(WriterThreadState& state) {
Expand All @@ -333,7 +333,12 @@ void DataLog::StartLogFile(WriterThreadState& state) {
}

// get free space
state.freeSpace = fs::space(state.dirPath).available;
auto freeSpaceInfo = fs::space(state.dirPath, ec);
if (!ec) {
state.freeSpace = freeSpaceInfo.available;
} else {
state.freeSpace = UINTMAX_MAX;
}
if (state.freeSpace < kMinFreeSpace) {
WPI_ERROR(m_msglog,
"Insufficient free space ({} available), no log being saved",
Expand Down Expand Up @@ -426,7 +431,11 @@ void DataLog::WriterThreadMain(std::string_view dir) {
lock.lock();
if (!ec && !exists) {
state.Close();
WPI_INFO(m_msglog, "Log file '{}' deleted, recreating as fresh log",
fs::path path{state.filename};
state.SetFilename(fmt::format("{}.{}.{}", path.stem().string(),
++segmentCount,
path.extension().string()));
WPI_INFO(m_msglog, "Log file deleted, recreating as fresh log '{}'",
state.filename);
doStart = true;
}
Expand Down Expand Up @@ -498,7 +507,12 @@ void DataLog::WriterThreadMain(std::string_view dir) {
// update free space every 10 flushes (in case other things are writing)
if (++freeSpaceCount >= 10) {
freeSpaceCount = 0;
state.freeSpace = fs::space(state.dirPath).available;
auto freeSpaceInfo = fs::space(state.dirPath, ec);
if (!ec) {
state.freeSpace = freeSpaceInfo.available;
} else {
state.freeSpace = UINTMAX_MAX;
}
}

// write buffers to file
Expand Down

0 comments on commit f7cb629

Please sign in to comment.