Skip to content

Commit

Permalink
Refactor a bunch
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Oct 4, 2024
1 parent d409cb5 commit 07f0b92
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
22 changes: 10 additions & 12 deletions wpiutil/src/main/native/cpp/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// the WPILib BSD license file in the root directory of this project.

#include "wpi/FileLogger.h"
#include "fmt/format.h"

#ifdef __linux__
#include <fcntl.h>
Expand All @@ -27,12 +28,12 @@ FileLogger::FileLogger(std::string_view file,
m_inotifyWatchHandle{
inotify_add_watch(m_inotifyHandle, file.data(), IN_MODIFY)},
m_thread{[=, this] {
char buf[4000];
char buf[8000];
char eventBuf[sizeof(struct inotify_event) + NAME_MAX + 1];
lseek(m_fileHandle, 0, SEEK_END);
while (read(m_inotifyHandle, eventBuf, sizeof(eventBuf)) > 0) {
int bufLen = 0;
while ((bufLen = read(m_fileHandle, buf, sizeof(buf))) > 0) {
if ((bufLen = read(m_fileHandle, buf, sizeof(buf))) > 0) {
callback(std::string_view{buf, static_cast<size_t>(bufLen)});
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
Expand Down Expand Up @@ -89,18 +90,15 @@ std::function<void(std::string_view)> FileLogger::LineBuffer(
buf.append(data.begin(), data.end());
return;
}
std::string_view line;
std::string_view remainingData;
std::tie(line, remainingData) = wpi::split(data, "\n");
buf.append(line.begin(), line.end());
callback(std::string_view{buf.data(), buf.size()});
auto combinedData =
fmt::format("{}{}", std::string_view{buf.data(), buf.size()}, data);
std::string_view wholeData;
std::string_view extra;
std::tie(wholeData, extra) = wpi::rsplit(combinedData, "\n");

while (wpi::contains(remainingData, "\n")) {
std::tie(line, remainingData) = wpi::split(remainingData, "\n");
callback(line);
}
callback(wholeData);
buf.clear();
buf.append(remainingData.begin(), remainingData.end());
buf.append(extra.begin(), extra.end());
};
}
} // namespace wpi
24 changes: 15 additions & 9 deletions wpiutil/src/test/native/cpp/FileLoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ TEST(FileLoggerTest, LineBufferSingleLine) {
auto func = wpi::FileLogger::LineBuffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("qwertyuiop\n");
EXPECT_EQ(buf.front(), "qwertyuiop");
buf.clear();
EXPECT_EQ("qwertyuiop", buf[0]);
}

TEST(FileLoggerTest, LineBufferMultiLine) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("line 1\nline 2\nline 3\n");
EXPECT_EQ("line 1", buf[0]);
EXPECT_EQ("line 2", buf[1]);
EXPECT_EQ("line 3", buf[2]);
EXPECT_EQ("line 1\nline 2\nline 3", buf[0]);
}

TEST(FileLoggerTest, LineBufferPartials) {
Expand All @@ -35,10 +32,9 @@ TEST(FileLoggerTest, LineBufferPartials) {
[&buf](std::string_view line) { buf.emplace_back(line); });
func("part 1");
func("part 2\npart 3");
EXPECT_EQ("part 1part 2", buf.front());
buf.clear();
EXPECT_EQ("part 1part 2", buf[0]);
func("\n");
EXPECT_EQ("part 3", buf.front());
EXPECT_EQ("part 3", buf[1]);
}

TEST(FileLoggerTest, LineBufferMultiplePartials) {
Expand All @@ -49,5 +45,15 @@ TEST(FileLoggerTest, LineBufferMultiplePartials) {
func("part 2");
func("part 3");
func("part 4\n");
EXPECT_EQ("part 1part 2part 3part 4", buf.front());
EXPECT_EQ("part 1part 2part 3part 4", buf[0]);
}
TEST(FileLoggerTest, LineBufferMultipleMultiLinePartials) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("part 1");
func("part 2\npart 3");
func("part 4\n");
EXPECT_EQ("part 1part 2", buf[0]);
EXPECT_EQ("part 3part 4", buf[1]);
}

0 comments on commit 07f0b92

Please sign in to comment.