Skip to content

Commit

Permalink
Buffer leftover data and move DataLog ctor to cpp file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Aug 24, 2024
1 parent 5dacd88 commit b1faa06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
34 changes: 34 additions & 0 deletions wpiutil/src/main/native/cpp/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

#include "wpi/FileLogger.h"

#ifdef __linux__
#include <fcntl.h>
#include <sys/inotify.h>
#include <unistd.h>
#endif

#include <string>
#include <string_view>
#include <tuple>
#include <utility>

#include <fmt/format.h>

#include "wpi/StringExtras.h"

namespace wpi {
FileLogger::FileLogger(std::string_view file,
std::function<void(std::string_view)> callback)
Expand All @@ -26,6 +41,25 @@ FileLogger::FileLogger(std::string_view file,
#endif
{
}
FileLogger::FileLogger(std::string_view file, log::DataLog& log,
std::string_view key)
: FileLogger(file, [entry = log.Start(key, "string"),
buf = wpi::SmallVector<char, 16>{},
&log](std::string_view data) mutable {
if (!wpi::contains(data, "\n")) {
buf.append(data.begin(), data.end());
return;
}
std::string_view left;
std::string_view right;
std::string combinedData = fmt::format("{}{}", buf.data(), data);
buf.clear();
do {
std::tie(left, right) = wpi::rsplit(data, "\n");
log.AppendString(entry, left, 0);
} while (wpi::contains(right, "\n"));
buf.append(right.begin(), right.end());
}) {}
FileLogger::FileLogger(FileLogger&& other)
#ifdef __linux__
: m_fileHandle{std::exchange(other.m_fileHandle, -1)},
Expand Down
19 changes: 1 addition & 18 deletions wpiutil/src/main/native/include/wpi/FileLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@

#pragma once

#ifdef __linux__
#include <fcntl.h>
#include <sys/inotify.h>
#include <unistd.h>
#endif

#include <functional>
#include <string_view>
#include <thread>
#include <utility>

#include "wpi/DataLog.h"
#include "wpi/SmallVector.h"
#include "wpi/StringExtras.h"

namespace wpi {
/**
Expand Down Expand Up @@ -45,15 +36,7 @@ class FileLogger {
* @param log A data log.
* @param key The log key to append data to.
*/
FileLogger(std::string_view file, log::DataLog& log, std::string_view key)
: FileLogger(file, [entry = log.Start(key, "string"),
&log](std::string_view data) {
wpi::SmallVector<std::string_view, 16> parts;
wpi::split(data, parts, "\n");
for (auto line : parts) {
log.AppendString(entry, line, 0);
}
}) {}
FileLogger(std::string_view file, log::DataLog& log, std::string_view key);
FileLogger(FileLogger&& other);
FileLogger& operator=(FileLogger&& rhs);
~FileLogger();
Expand Down

0 comments on commit b1faa06

Please sign in to comment.