[wpiutil, wpilib] Add FileLogger and log console output (#6977)

This commit is contained in:
Gold856
2024-09-13 01:13:06 -04:00
committed by GitHub
parent 32252f7d6a
commit 3bbbf86632
12 changed files with 428 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <functional>
#include <string_view>
#include <thread>
#include "wpi/DataLog.h"
namespace wpi {
/**
* A class version of `tail -f`, otherwise known as `tail -f` at home. Watches
* a file and puts the data somewhere else. Only works on Linux-based platforms.
*/
class FileLogger {
public:
FileLogger() = default;
/**
* Construct a FileLogger. When the specified file is modified, the callback
* will be called with the appended changes.
*
* @param file The path to the file.
* @param callback A callback that accepts the appended file data.
*/
FileLogger(std::string_view file,
std::function<void(std::string_view)> callback);
/**
* Construct a FileLogger. When the specified file is modified, appended data
* will be appended to the specified data log.
*
* @param file The path to the file.
* @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(FileLogger&& other);
FileLogger& operator=(FileLogger&& rhs);
~FileLogger();
/**
* Creates a function that chunks incoming data into lines before calling the
* callback with the individual line.
*
* @param callback The callback that logs lines.
* @return The function.
*/
static std::function<void(std::string_view)> LineBuffer(
std::function<void(std::string_view)> callback);
private:
#ifdef __linux__
int m_fileHandle = -1;
int m_inotifyHandle = -1;
int m_inotifyWatchHandle = -1;
std::thread m_thread;
#endif
};
} // namespace wpi