WPILibC++ 2025.0.0-alpha-1-14-g3b6f38d
DataLogBackgroundWriter.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 <stdint.h>
8
9#include <functional>
10#include <span>
11#include <string>
12#include <string_view>
13#include <thread>
14
15#include "wpi/DataLog.h"
17#include "wpi/mutex.h"
18
19namespace wpi {
20class Logger;
21} // namespace wpi
22
23namespace wpi::log {
24
25/**
26 * A data log background writer that periodically flushes the data log on a
27 * background thread. The data log file is created immediately upon
28 * construction with a temporary filename. The file may be renamed at any time
29 * using the SetFilename() function.
30 *
31 * The lifetime of this object must be longer than any data log entry objects
32 * that refer to it.
33 *
34 * The data log is periodically flushed to disk. It can also be explicitly
35 * flushed to disk by using the Flush() function. This operation is, however,
36 * non-blocking.
37 */
38class DataLogBackgroundWriter final : public DataLog {
39 public:
40 /**
41 * Construct a new Data Log. The log will be initially created with a
42 * temporary filename.
43 *
44 * @param dir directory to store the log
45 * @param filename filename to use; if none provided, a random filename is
46 * generated of the form "wpilog_{}.wpilog"
47 * @param period time between automatic flushes to disk, in seconds;
48 * this is a time/storage tradeoff
49 * @param extraHeader extra header data
50 */
52 std::string_view filename = "",
53 double period = 0.25,
54 std::string_view extraHeader = "");
55
56 /**
57 * Construct a new Data Log. The log will be initially created with a
58 * temporary filename.
59 *
60 * @param msglog message logger (will be called from separate thread)
61 * @param dir directory to store the log
62 * @param filename filename to use; if none provided, a random filename is
63 * generated of the form "wpilog_{}.wpilog"
64 * @param period time between automatic flushes to disk, in seconds;
65 * this is a time/storage tradeoff
66 * @param extraHeader extra header data
67 */
69 std::string_view dir = "",
70 std::string_view filename = "",
71 double period = 0.25,
72 std::string_view extraHeader = "");
73
74 /**
75 * Construct a new Data Log that passes its output to the provided function
76 * rather than a file. The write function will be called on a separate
77 * background thread and may block. The write function is called with an
78 * empty data array when the thread is terminating.
79 *
80 * @param write write function
81 * @param period time between automatic calls to write, in seconds;
82 * this is a time/storage tradeoff
83 * @param extraHeader extra header data
84 */
86 std::function<void(std::span<const uint8_t> data)> write,
87 double period = 0.25, std::string_view extraHeader = "");
88
89 /**
90 * Construct a new Data Log that passes its output to the provided function
91 * rather than a file. The write function will be called on a separate
92 * background thread and may block. The write function is called with an
93 * empty data array when the thread is terminating.
94 *
95 * @param msglog message logger (will be called from separate thread)
96 * @param write write function
97 * @param period time between automatic calls to write, in seconds;
98 * this is a time/storage tradeoff
99 * @param extraHeader extra header data
100 */
102 wpi::Logger& msglog,
103 std::function<void(std::span<const uint8_t> data)> write,
104 double period = 0.25, std::string_view extraHeader = "");
105
111
112 /**
113 * Change log filename.
114 *
115 * @param filename filename
116 */
117 void SetFilename(std::string_view filename);
118
119 /**
120 * Explicitly flushes the log data to disk.
121 */
122 void Flush() final;
123
124 /**
125 * Pauses appending of data records to the log. While paused, no data records
126 * are saved (e.g. AppendX is a no-op). Has no effect on entry starts /
127 * finishes / metadata changes.
128 */
129 void Pause() final;
130
131 /**
132 * Resumes appending of data records to the log. If called after Stop(),
133 * opens a new file (with random name if SetFilename was not called after
134 * Stop()) and appends Start records and schema data values for all previously
135 * started entries and schemas.
136 */
137 void Resume() final;
138
139 /**
140 * Stops appending all records to the log, and closes the log file.
141 */
142 void Stop() final;
143
144 private:
145 struct WriterThreadState;
146
147 void BufferHalfFull() final;
148 bool BufferFull() final;
149
150 void StartLogFile(WriterThreadState& state);
151 void WriterThreadMain(std::string_view dir);
152 void WriterThreadMain(
153 std::function<void(std::span<const uint8_t> data)> write);
154
155 mutable wpi::mutex m_mutex;
156 wpi::condition_variable m_cond;
157 bool m_doFlush{false};
158 bool m_shutdown{false};
159 enum State {
160 kStart,
161 kActive,
162 kPaused,
163 kStopped,
164 } m_state = kActive;
165 double m_period;
166 std::string m_newFilename;
167 std::thread m_thread;
168};
169
170} // namespace wpi::log
Definition: Logger.h:27
A data log background writer that periodically flushes the data log on a background thread.
Definition: DataLogBackgroundWriter.h:38
DataLogBackgroundWriter(std::string_view dir="", std::string_view filename="", double period=0.25, std::string_view extraHeader="")
Construct a new Data Log.
void Flush() final
Explicitly flushes the log data to disk.
void Resume() final
Resumes appending of data records to the log.
DataLogBackgroundWriter(std::function< void(std::span< const uint8_t > data)> write, double period=0.25, std::string_view extraHeader="")
Construct a new Data Log that passes its output to the provided function rather than a file.
void Pause() final
Pauses appending of data records to the log.
void Stop() final
Stops appending all records to the log, and closes the log file.
DataLogBackgroundWriter(wpi::Logger &msglog, std::string_view dir="", std::string_view filename="", double period=0.25, std::string_view extraHeader="")
Construct a new Data Log.
DataLogBackgroundWriter(wpi::Logger &msglog, std::function< void(std::span< const uint8_t > data)> write, double period=0.25, std::string_view extraHeader="")
Construct a new Data Log that passes its output to the provided function rather than a file.
void SetFilename(std::string_view filename)
Change log filename.
A data log for high-speed writing of data values.
Definition: DataLog.h:68
auto write(OutputIt out, const std::tm &time, const std::locale &loc, char format, char modifier=0) -> OutputIt
Definition: chrono.h:449
state
Definition: base.h:2361
Implement std::hash so that hash_code can be used in STL containers.
Definition: array.h:89
Definition: ntcore_cpp.h:31
Definition: ntcore_cpp.h:26
::std::condition_variable condition_variable
Definition: condition_variable.h:16
::std::mutex mutex
Definition: mutex.h:17
basic_string_view< char > string_view
Definition: base.h:601