mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
[wpiutil, wpilib] Add FileLogger and log console output (#6977)
This commit is contained in:
61
wpiutil/src/main/native/include/wpi/FileLogger.h
Normal file
61
wpiutil/src/main/native/include/wpi/FileLogger.h
Normal 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
|
||||
Reference in New Issue
Block a user