blob: b6efb80e254f8969d6ec852b9c7e5c421a918aff [file] [log] [blame]
Austin Schuhcb5601b2020-09-10 15:29:59 -07001#ifndef AOS_EVENTS_LOGGING_LOG_NAMER_H_
2#define AOS_EVENTS_LOGGING_LOG_NAMER_H_
3
4#include <functional>
5#include <map>
6#include <memory>
7#include <string_view>
8#include <vector>
9
10#include "aos/events/logging/logfile_utils.h"
11#include "aos/events/logging/logger_generated.h"
Austin Schuh4385b142021-03-14 21:31:13 -070012#include "aos/uuid.h"
Austin Schuhcb5601b2020-09-10 15:29:59 -070013#include "flatbuffers/flatbuffers.h"
14
15namespace aos {
16namespace logger {
17
Austin Schuh572924a2021-07-30 22:32:12 -070018class LogNamer;
19
Austin Schuhb8bca732021-07-30 22:32:00 -070020// TODO(austin): Rename this back to DataWriter once all other callers are of
21// the old DataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070022//
23// Class to manage writing data to log files. This lets us track which boot the
24// written header has in it, and if the header has been written or not.
Austin Schuhb8bca732021-07-30 22:32:00 -070025class NewDataWriter {
26 public:
27 // Constructs a NewDataWriter.
Austin Schuh572924a2021-07-30 22:32:12 -070028 // log_namer is the log namer which holds the config and any other data we
29 // need for our header.
30 // node is the node whom's prespective we are logging from.
Austin Schuhb8bca732021-07-30 22:32:00 -070031 // reopen is called whenever a file needs to be reopened.
32 // close is called to close that file and extract any statistics.
Austin Schuh572924a2021-07-30 22:32:12 -070033 NewDataWriter(LogNamer *log_namer, const Node *node,
34 std::function<void(NewDataWriter *)> reopen,
35 std::function<void(NewDataWriter *)> close);
Austin Schuhb8bca732021-07-30 22:32:00 -070036
37 NewDataWriter(NewDataWriter &&other) = default;
38 aos::logger::NewDataWriter &operator=(NewDataWriter &&other) = default;
39 NewDataWriter(const NewDataWriter &) = delete;
40 void operator=(const NewDataWriter &) = delete;
41
Austin Schuh572924a2021-07-30 22:32:12 -070042 ~NewDataWriter();
Austin Schuhb8bca732021-07-30 22:32:00 -070043
Austin Schuh572924a2021-07-30 22:32:12 -070044 // Rotates the log file, delaying writing the new header until data arrives.
45 void Rotate();
Austin Schuhb8bca732021-07-30 22:32:00 -070046
Austin Schuhb8bca732021-07-30 22:32:00 -070047 // TODO(austin): Copy header and add all UUIDs and such when available
48 // whenever data is written.
49 //
Austin Schuhb8bca732021-07-30 22:32:00 -070050 // TODO(austin): Add known timestamps for each node every time we cycle a log
51 // for sorting.
52
Austin Schuhe46492f2021-07-31 19:49:41 -070053 void UpdateRemote(size_t remote_node_index,
54 const UUID &remote_node_boot_uuid);
Austin Schuh572924a2021-07-30 22:32:12 -070055 // Queues up a message with the provided boot UUID.
Austin Schuhe46492f2021-07-31 19:49:41 -070056 void QueueMessage(flatbuffers::FlatBufferBuilder *fbb,
57 const UUID &node_boot_uuid,
58 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070059
Austin Schuh572924a2021-07-30 22:32:12 -070060 // Returns the filename of the writer.
Austin Schuhb8bca732021-07-30 22:32:00 -070061 std::string_view filename() const { return writer->filename(); }
62
Austin Schuh572924a2021-07-30 22:32:12 -070063 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070064
65 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070066
67 size_t node_index() const { return node_index_; }
68 const UUID &parts_uuid() const { return parts_uuid_; }
69 size_t parts_index() const { return parts_index_; }
70 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070071
72 private:
Austin Schuhe46492f2021-07-31 19:49:41 -070073 // Signals that a node has rebooted.
74 void Reboot();
75
Austin Schuh572924a2021-07-30 22:32:12 -070076 void QueueHeader(
77 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
78
Austin Schuhe46492f2021-07-31 19:49:41 -070079 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader();
80
Austin Schuh572924a2021-07-30 22:32:12 -070081 const Node *const node_ = nullptr;
Austin Schuhe46492f2021-07-31 19:49:41 -070082 const size_t node_index_ = 0;
Austin Schuh572924a2021-07-30 22:32:12 -070083 LogNamer *log_namer_;
84 UUID parts_uuid_ = UUID::Random();
85 size_t parts_index_ = 0;
86
Austin Schuhb8bca732021-07-30 22:32:00 -070087 std::function<void(NewDataWriter *)> reopen_;
88 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -070089 bool header_written_ = false;
Austin Schuhe46492f2021-07-31 19:49:41 -070090
91 std::vector<UUID> boot_uuids_;
Austin Schuhb8bca732021-07-30 22:32:00 -070092};
93
Austin Schuhcb5601b2020-09-10 15:29:59 -070094// Interface describing how to name, track, and add headers to log file parts.
95class LogNamer {
96 public:
97 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
98 // being node.
Austin Schuh73340842021-07-30 22:32:06 -070099 LogNamer(const Configuration *configuration, const Node *node)
Austin Schuhe46492f2021-07-31 19:49:41 -0700100 : configuration_(configuration),
101 node_(node),
102 logger_node_index_(configuration::GetNodeIndex(configuration_, node)) {
Austin Schuh73340842021-07-30 22:32:06 -0700103 nodes_.emplace_back(node_);
104 node_states_.resize(configuration::NodesCount(configuration_));
105 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700106 virtual ~LogNamer() {}
107
Austin Schuh6bb8a822021-03-31 23:04:39 -0700108 virtual std::string_view base_name() const = 0;
109
110 // Rotate should be called at least once in between calls to set_base_name.
111 // Otherwise temporary files will not be recoverable.
112 // Rotate is called by Logger::RenameLogBase, which is currently the only user
113 // of this method.
114 // Only renaming the folder is supported, not the file base name.
115 virtual void set_base_name(std::string_view base_name) = 0;
116
Brian Silverman87ac0402020-09-17 14:47:01 -0700117 // Returns a writer for writing data from messages on this channel (on the
118 // primary node).
119 //
120 // The returned pointer will stay valid across rotations, but the object it
121 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700122 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700123
Brian Silverman87ac0402020-09-17 14:47:01 -0700124 // Returns a writer for writing timestamps from messages on this channel (on
125 // the primary node).
126 //
127 // The returned pointer will stay valid across rotations, but the object it
128 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700129 virtual NewDataWriter *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700130
131 // Returns a writer for writing timestamps delivered over the special
132 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700133 // are forwarded back from (to the primary node).
134 //
135 // The returned pointer will stay valid across rotations, but the object it
136 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700137 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
138 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700139
Austin Schuh73340842021-07-30 22:32:06 -0700140 // Rotates all log files for the provided node.
141 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700142
143 // Returns all the nodes that data is being written for.
144 const std::vector<const Node *> &nodes() const { return nodes_; }
145
146 // Returns the node the logger is running on.
147 const Node *node() const { return node_; }
Austin Schuhe46492f2021-07-31 19:49:41 -0700148 const UUID &logger_node_boot_uuid() const { return logger_node_boot_uuid_; }
149 size_t logger_node_index() const { return logger_node_index_; }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700150
Austin Schuh8c399962020-12-25 21:51:45 -0800151 // Writes out the nested Configuration object to the config file location.
152 virtual void WriteConfiguration(
153 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
154 std::string_view config_sha256) = 0;
155
Austin Schuh73340842021-07-30 22:32:06 -0700156 void SetHeaderTemplate(
157 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
158 header_ = std::move(header);
Austin Schuhe46492f2021-07-31 19:49:41 -0700159 logger_node_boot_uuid_ =
160 UUID::FromString(header_.message().logger_node_boot_uuid());
Austin Schuh73340842021-07-30 22:32:06 -0700161 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700162
Austin Schuh73340842021-07-30 22:32:06 -0700163 void SetStartTimes(size_t node_index,
164 monotonic_clock::time_point monotonic_start_time,
165 realtime_clock::time_point realtime_start_time,
166 monotonic_clock::time_point logger_monotonic_start_time,
167 realtime_clock::time_point logger_realtime_start_time) {
168 node_states_[node_index].monotonic_start_time = monotonic_start_time;
169 node_states_[node_index].realtime_start_time = realtime_start_time;
170 node_states_[node_index].logger_monotonic_start_time =
171 logger_monotonic_start_time;
172 node_states_[node_index].logger_realtime_start_time =
173 logger_realtime_start_time;
Austin Schuh572924a2021-07-30 22:32:12 -0700174
Austin Schuh73340842021-07-30 22:32:06 -0700175 // TODO(austin): Track that the header has changed and needs to be
Austin Schuh572924a2021-07-30 22:32:12 -0700176 // rewritten down here rather than up in log_writer.
Austin Schuh73340842021-07-30 22:32:06 -0700177 }
178
179 monotonic_clock::time_point monotonic_start_time(size_t node_index) const {
180 return node_states_[node_index].monotonic_start_time;
181 }
182
Austin Schuh73340842021-07-30 22:32:06 -0700183 protected:
184 // Creates a new header by copying fields out of the template and combining
185 // them with the arguments provided.
186 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
Austin Schuhe46492f2021-07-31 19:49:41 -0700187 size_t node_index, const std::vector<UUID> &boot_uuids,
Austin Schuh73340842021-07-30 22:32:06 -0700188 const UUID &parts_uuid, int parts_index) const;
189
190 const Configuration *const configuration_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700191 const Node *const node_;
Austin Schuhe46492f2021-07-31 19:49:41 -0700192 const size_t logger_node_index_;
193 UUID logger_node_boot_uuid_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700194 std::vector<const Node *> nodes_;
Austin Schuh73340842021-07-30 22:32:06 -0700195
Austin Schuh572924a2021-07-30 22:32:12 -0700196 friend NewDataWriter;
197
Austin Schuh73340842021-07-30 22:32:06 -0700198 // Structure with state per node about times and such.
199 // TODO(austin): some of this lives better in NewDataWriter once we move
200 // ownership of deciding when to write headers into LogNamer.
201 struct NodeState {
202 // Time when this node started logging.
203 monotonic_clock::time_point monotonic_start_time =
204 monotonic_clock::min_time;
205 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
206
207 // Corresponding time on the logger node when it started logging.
208 monotonic_clock::time_point logger_monotonic_start_time =
209 monotonic_clock::min_time;
210 realtime_clock::time_point logger_realtime_start_time =
211 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700212 };
213 std::vector<NodeState> node_states_;
214
215 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
216 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700217};
218
219// Local log namer is a simple version which only names things
220// "base_name.part#.bfbs" and increments the part number. It doesn't support
221// any other log type.
222class LocalLogNamer : public LogNamer {
223 public:
Austin Schuh73340842021-07-30 22:32:06 -0700224 LocalLogNamer(std::string_view base_name, const Configuration *configuration,
225 const Node *node)
226 : LogNamer(configuration, node),
Austin Schuhcb5601b2020-09-10 15:29:59 -0700227 base_name_(base_name),
Austin Schuh572924a2021-07-30 22:32:12 -0700228 data_writer_(this, node,
229 [this](NewDataWriter *writer) {
230 writer->writer = std::make_unique<DetachedBufferWriter>(
231 absl::StrCat(base_name_, ".part",
232 writer->parts_index(), ".bfbs"),
233 std::make_unique<aos::logger::DummyEncoder>());
234 },
235 [](NewDataWriter * /*writer*/) {}) {}
Austin Schuhb8bca732021-07-30 22:32:00 -0700236
237 LocalLogNamer(const LocalLogNamer &) = delete;
238 LocalLogNamer(LocalLogNamer &&) = delete;
239 LocalLogNamer &operator=(const LocalLogNamer &) = delete;
240 LocalLogNamer &operator=(LocalLogNamer &&) = delete;
241
Brian Silverman0465fcf2020-09-24 00:29:18 -0700242 ~LocalLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700243
Austin Schuh6bb8a822021-03-31 23:04:39 -0700244 std::string_view base_name() const final { return base_name_; }
245
246 void set_base_name(std::string_view base_name) final {
247 base_name_ = base_name;
248 }
249
Austin Schuhb8bca732021-07-30 22:32:00 -0700250 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700251
Austin Schuh73340842021-07-30 22:32:06 -0700252 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700253
Austin Schuhb8bca732021-07-30 22:32:00 -0700254 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700255
Austin Schuh73340842021-07-30 22:32:06 -0700256 NewDataWriter *MakeForwardedTimestampWriter(const Channel * /*channel*/,
257 const Node * /*node*/) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700258
Austin Schuh8c399962020-12-25 21:51:45 -0800259 void WriteConfiguration(
260 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
261 std::string_view config_sha256) override;
262
Austin Schuhcb5601b2020-09-10 15:29:59 -0700263 private:
Austin Schuh6bb8a822021-03-31 23:04:39 -0700264 std::string base_name_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700265
266 NewDataWriter data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700267};
268
269// Log namer which uses a config and a base name to name a bunch of files.
270class MultiNodeLogNamer : public LogNamer {
271 public:
Brian Silvermancb805822020-10-06 17:43:35 -0700272 MultiNodeLogNamer(std::string_view base_name,
273 const Configuration *configuration, const Node *node);
274 ~MultiNodeLogNamer() override;
275
Austin Schuh6bb8a822021-03-31 23:04:39 -0700276 std::string_view base_name() const final { return base_name_; }
277
278 void set_base_name(std::string_view base_name) final {
279 old_base_name_ = base_name_;
280 base_name_ = base_name;
281 }
Brian Silvermancb805822020-10-06 17:43:35 -0700282
Brian Silverman48deab12020-09-30 18:39:28 -0700283 // If temp_suffix is set, then this will write files under names beginning
284 // with the specified suffix, and then rename them to the desired name after
285 // they are fully written.
286 //
287 // This is useful to enable incremental copying of the log files.
288 //
289 // Defaults to writing directly to the final filename.
Brian Silvermancb805822020-10-06 17:43:35 -0700290 void set_temp_suffix(std::string_view temp_suffix) {
291 temp_suffix_ = temp_suffix;
292 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700293
Brian Silvermancb805822020-10-06 17:43:35 -0700294 // Sets the function for creating encoders.
295 //
296 // Defaults to just creating DummyEncoders.
297 void set_encoder_factory(
298 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) {
299 encoder_factory_ = std::move(encoder_factory);
300 }
301
302 // Sets an additional file extension.
303 //
304 // Defaults to nothing.
305 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700306
Brian Silvermana621f522020-09-30 16:52:43 -0700307 // A list of all the filenames we've written.
308 //
309 // This only includes the part after base_name().
310 const std::vector<std::string> &all_filenames() const {
311 return all_filenames_;
312 }
313
Austin Schuh73340842021-07-30 22:32:06 -0700314 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700315
Austin Schuh8c399962020-12-25 21:51:45 -0800316 void WriteConfiguration(
317 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
318 std::string_view config_sha256) override;
319
Austin Schuhb8bca732021-07-30 22:32:00 -0700320 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700321
Austin Schuhb8bca732021-07-30 22:32:00 -0700322 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700323 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700324
Austin Schuhb8bca732021-07-30 22:32:00 -0700325 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700326
Brian Silverman0465fcf2020-09-24 00:29:18 -0700327 // Indicates that at least one file ran out of space. Once this happens, we
328 // stop trying to open new files, to avoid writing any files with holes from
329 // previous parts.
330 //
331 // Besides this function, this object will silently stop logging data when
332 // this occurs. If you want to ensure log files are complete, you must call
333 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700334 bool ran_out_of_space() const {
335 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700336 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700337 return x ||
338 (data_writer.writer && data_writer.writer->ran_out_of_space());
339 });
340 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700341
Brian Silverman1f345222020-09-24 21:14:48 -0700342 // Returns the maximum total_bytes() value for all existing
343 // DetachedBufferWriters.
344 //
345 // Returns 0 if no files are open.
346 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700347 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700348 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700349 return std::max(x, data_writer.writer->total_bytes());
350 });
Brian Silverman1f345222020-09-24 21:14:48 -0700351 }
352
Brian Silverman0465fcf2020-09-24 00:29:18 -0700353 // Closes all existing log files. No more data may be written after this.
354 //
355 // This may set ran_out_of_space().
356 void Close();
357
Brian Silvermancb805822020-10-06 17:43:35 -0700358 // Accessors for various statistics. See the identically-named methods in
359 // DetachedBufferWriter for documentation. These are aggregated across all
360 // past and present DetachedBufferWriters.
361 std::chrono::nanoseconds max_write_time() const {
362 return accumulate_data_writers(
363 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700364 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700365 return std::max(x, data_writer.writer->max_write_time());
366 });
367 }
368 int max_write_time_bytes() const {
369 return std::get<0>(accumulate_data_writers(
370 std::make_tuple(max_write_time_bytes_, max_write_time_),
371 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700372 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700373 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
374 return std::make_tuple(data_writer.writer->max_write_time_bytes(),
375 data_writer.writer->max_write_time());
376 }
377 return x;
378 }));
379 }
380 int max_write_time_messages() const {
381 return std::get<0>(accumulate_data_writers(
382 std::make_tuple(max_write_time_messages_, max_write_time_),
383 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700384 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700385 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
386 return std::make_tuple(
387 data_writer.writer->max_write_time_messages(),
388 data_writer.writer->max_write_time());
389 }
390 return x;
391 }));
392 }
393 std::chrono::nanoseconds total_write_time() const {
394 return accumulate_data_writers(
395 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700396 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700397 return x + data_writer.writer->total_write_time();
398 });
399 }
400 int total_write_count() const {
401 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700402 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700403 return x + data_writer.writer->total_write_count();
404 });
405 }
406 int total_write_messages() const {
407 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700408 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700409 return x + data_writer.writer->total_write_messages();
410 });
411 }
412 int total_write_bytes() const {
413 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700414 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700415 return x + data_writer.writer->total_write_bytes();
416 });
417 }
418
419 void ResetStatistics();
420
Austin Schuhcb5601b2020-09-10 15:29:59 -0700421 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700422 // Opens up a writer for timestamps forwarded back.
423 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700424 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700425
426 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700427 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700428
429 // Opens the main data writer file for this node responsible for data_writer_.
Brian Silvermana621f522020-09-30 16:52:43 -0700430 void OpenDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700431
Brian Silvermana621f522020-09-30 16:52:43 -0700432 void CreateBufferWriter(std::string_view path,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700433 std::unique_ptr<DetachedBufferWriter> *destination);
434
Brian Silverman48deab12020-09-30 18:39:28 -0700435 void RenameTempFile(DetachedBufferWriter *destination);
436
Brian Silvermancb805822020-10-06 17:43:35 -0700437 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700438
Brian Silvermancb805822020-10-06 17:43:35 -0700439 // A version of std::accumulate which operates over all of our DataWriters.
440 template <typename T, typename BinaryOperation>
441 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700442 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700443 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800444 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700445 t = op(std::move(t), data_writer.second);
446 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700447 if (data_writer_) {
448 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700449 }
450 return t;
451 }
452
Austin Schuh6bb8a822021-03-31 23:04:39 -0700453 std::string base_name_;
454 std::string old_base_name_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700455
Brian Silverman0465fcf2020-09-24 00:29:18 -0700456 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700457 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700458
Brian Silvermancb805822020-10-06 17:43:35 -0700459 std::string temp_suffix_;
460 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ =
461 []() { return std::make_unique<DummyEncoder>(); };
462 std::string extension_;
463
464 // Storage for statistics from previously-rotated DetachedBufferWriters.
465 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
466 int max_write_time_bytes_ = -1;
467 int max_write_time_messages_ = -1;
468 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
469 int total_write_count_ = 0;
470 int total_write_messages_ = 0;
471 int total_write_bytes_ = 0;
472
Austin Schuhcb5601b2020-09-10 15:29:59 -0700473 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700474 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700475
Austin Schuhb8bca732021-07-30 22:32:00 -0700476 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700477};
478
479} // namespace logger
480} // namespace aos
481
482#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_