WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
FileLogger.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8#include <string_view>
9#include <thread>
10
11#include "wpi/DataLog.h"
12
13namespace wpi {
14/**
15 * A class version of `tail -f`, otherwise known as `tail -f` at home. Watches
16 * a file and puts the data somewhere else. Only works on Linux-based platforms.
17 */
19 public:
20 FileLogger() = default;
21 /**
22 * Construct a FileLogger. When the specified file is modified, the callback
23 * will be called with the appended changes.
24 *
25 * @param file The path to the file.
26 * @param callback A callback that accepts the appended file data.
27 */
28 FileLogger(std::string_view file,
29 std::function<void(std::string_view)> callback);
30
31 /**
32 * Construct a FileLogger. When the specified file is modified, appended data
33 * will be appended to the specified data log.
34 *
35 * @param file The path to the file.
36 * @param log A data log.
37 * @param key The log key to append data to.
38 */
39 FileLogger(std::string_view file, log::DataLog& log, std::string_view key);
43 /**
44 * Creates a function that chunks incoming data into blocks of whole lines and
45 * stores incomplete lines to add to the next block of data.
46 *
47 * @param callback A callback that accepts the blocks of whole lines.
48 * @return The function.
49 */
50 static std::function<void(std::string_view)> Buffer(
51 std::function<void(std::string_view)> callback);
52
53 private:
54#ifdef __linux__
55 int m_fileHandle = -1;
56 int m_inotifyHandle = -1;
57 int m_inotifyWatchHandle = -1;
58 std::thread m_thread;
59#endif
60};
61} // namespace wpi
A class version of tail -f, otherwise known as tail -f at home.
Definition FileLogger.h:18
FileLogger()=default
FileLogger(std::string_view file, std::function< void(std::string_view)> callback)
Construct a FileLogger.
static std::function< void(std::string_view)> Buffer(std::function< void(std::string_view)> callback)
Creates a function that chunks incoming data into blocks of whole lines and stores incomplete lines t...
FileLogger(FileLogger &&other)
FileLogger(std::string_view file, log::DataLog &log, std::string_view key)
Construct a FileLogger.
FileLogger & operator=(FileLogger &&rhs)
A data log for high-speed writing of data values.
Definition DataLog.h:70
Foonathan namespace.
Definition ntcore_cpp.h:26