Skip to content

Commit

Permalink
Always put data into the buffer, change docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Oct 5, 2024
1 parent d5736e6 commit 415d8f8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
21 changes: 11 additions & 10 deletions wpiutil/src/main/native/cpp/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif

#include <chrono>
#include <string>
#include <string_view>
#include <thread>
#include <tuple>
Expand Down Expand Up @@ -45,8 +46,8 @@ FileLogger::FileLogger(std::string_view file,
}
FileLogger::FileLogger(std::string_view file, log::DataLog& log,
std::string_view key)
: FileLogger(file, LineBuffer([entry = log.Start(key, "string"),
&log](std::string_view line) {
: FileLogger(file, Buffer([entry = log.Start(key, "string"),
&log](std::string_view line) {
log.AppendString(entry, line, 0);
})) {}
FileLogger::FileLogger(FileLogger&& other)
Expand Down Expand Up @@ -83,21 +84,21 @@ FileLogger::~FileLogger() {
}
#endif
}
std::function<void(std::string_view)> FileLogger::LineBuffer(

std::function<void(std::string_view)> FileLogger::Buffer(
std::function<void(std::string_view)> callback) {
return [callback,
buf = wpi::SmallVector<char, 32>{}](std::string_view data) mutable {
if (!wpi::contains(data, "\n")) {
buf.append(data.begin(), data.end());
buf = wpi::SmallVector<char, 64>{}](std::string_view data) mutable {
buf.append(data.begin(), data.end());
if (!wpi::contains({data.data(), data.size()}, "\n")) {
return;
}
auto combinedData =
fmt::format("{}{}", std::string_view{buf.data(), buf.size()}, data);
auto [wholeData, extra] = wpi::rsplit(combinedData, "\n");
auto [wholeData, extra] = wpi::rsplit({buf.data(), buf.size()}, "\n");
std::string leftover{extra};

callback(wholeData);
buf.clear();
buf.append(extra.begin(), extra.end());
buf.append(leftover.begin(), leftover.end());
};
}
} // namespace wpi
8 changes: 4 additions & 4 deletions wpiutil/src/main/native/include/wpi/FileLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ class FileLogger {
FileLogger& operator=(FileLogger&& rhs);
~FileLogger();
/**
* Creates a function that chunks incoming data into lines before calling the
* callback with the individual line.
* Creates a function that chunks incoming data into blocks of whole lines and
* stores incomplete lines to add to the next block of data.
*
* @param callback The callback that logs lines.
* @param callback A callback that accepts the blocks of whole lines.
* @return The function.
*/
static std::function<void(std::string_view)> LineBuffer(
static std::function<void(std::string_view)> Buffer(
std::function<void(std::string_view)> callback);

private:
Expand Down
20 changes: 10 additions & 10 deletions wpiutil/src/test/native/cpp/FileLoggerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@

#include "wpi/FileLogger.h"

TEST(FileLoggerTest, LineBufferSingleLine) {
TEST(FileLoggerTest, BufferSingleLine) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
auto func = wpi::FileLogger::Buffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("qwertyuiop\n");
EXPECT_EQ("qwertyuiop", buf[0]);
}

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

TEST(FileLoggerTest, LineBufferPartials) {
TEST(FileLoggerTest, BufferPartials) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
auto func = wpi::FileLogger::Buffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("part 1");
func("part 2\npart 3");
Expand All @@ -37,19 +37,19 @@ TEST(FileLoggerTest, LineBufferPartials) {
EXPECT_EQ("part 3", buf[1]);
}

TEST(FileLoggerTest, LineBufferMultiplePartials) {
TEST(FileLoggerTest, BufferMultiplePartials) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
auto func = wpi::FileLogger::Buffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("part 1");
func("part 2");
func("part 3");
func("part 4\n");
EXPECT_EQ("part 1part 2part 3part 4", buf[0]);
}
TEST(FileLoggerTest, LineBufferMultipleMultiLinePartials) {
TEST(FileLoggerTest, BufferMultipleMultiLinePartials) {
std::vector<std::string> buf;
auto func = wpi::FileLogger::LineBuffer(
auto func = wpi::FileLogger::Buffer(
[&buf](std::string_view line) { buf.emplace_back(line); });
func("part 1");
func("part 2\npart 3");
Expand Down

0 comments on commit 415d8f8

Please sign in to comment.