Skip to content

Commit

Permalink
[wpiutil] Fix FileLogger only logging one byte at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
Gold856 committed Oct 3, 2024
1 parent f82e1c9 commit 431544c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions wpiutil/src/main/native/cpp/FileLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
// the WPILib BSD license file in the root directory of this project.

#include "wpi/FileLogger.h"
#include <chrono>
#include <thread>

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

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

#include "wpi/print.h"
#include "wpi/StringExtras.h"

namespace wpi {
Expand All @@ -28,13 +32,14 @@ FileLogger::FileLogger(std::string_view file,
m_thread{[=, this] {
char buf[4000];
struct inotify_event ev;
int len = 0;
lseek(m_fileHandle, 0, SEEK_END);
while ((len = read(m_inotifyHandle, &ev, sizeof(ev))) > 0) {
int bufLen = 0;
if ((bufLen = read(m_fileHandle, buf, sizeof(buf)) > 0)) {
while (read(m_inotifyHandle, &ev, sizeof(ev)) > 0) {
int bufLen = read(m_fileHandle, buf, sizeof(buf));
while (bufLen > 0) {
callback(std::string_view{buf, static_cast<size_t>(bufLen)});
bufLen = read(m_fileHandle, buf, sizeof(buf));
}
std::this_thread::sleep_for(std::chrono::milliseconds(100))
}
}}
#endif
Expand Down

0 comments on commit 431544c

Please sign in to comment.