blob: 2727e631bc6aebe07a7ebeb945184f815dfc23a2 [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 Schuh572924a2021-07-30 22:32:12 -070053 // Queues up a message with the provided boot UUID.
Austin Schuhb8bca732021-07-30 22:32:00 -070054 void QueueSizedFlatbuffer(flatbuffers::FlatBufferBuilder *fbb,
Austin Schuh572924a2021-07-30 22:32:12 -070055 const UUID &source_node_boot_uuid,
56 aos::monotonic_clock::time_point now);
Austin Schuhb8bca732021-07-30 22:32:00 -070057
Austin Schuh572924a2021-07-30 22:32:12 -070058 // Returns the filename of the writer.
Austin Schuhb8bca732021-07-30 22:32:00 -070059 std::string_view filename() const { return writer->filename(); }
60
Austin Schuh572924a2021-07-30 22:32:12 -070061 // Signals that a node has rebooted.
62 void Reboot();
Austin Schuhb8bca732021-07-30 22:32:00 -070063
Austin Schuh572924a2021-07-30 22:32:12 -070064 void Close();
Austin Schuhb8bca732021-07-30 22:32:00 -070065
66 std::unique_ptr<DetachedBufferWriter> writer = nullptr;
Austin Schuh572924a2021-07-30 22:32:12 -070067
68 size_t node_index() const { return node_index_; }
69 const UUID &parts_uuid() const { return parts_uuid_; }
70 size_t parts_index() const { return parts_index_; }
71 const Node *node() const { return node_; }
Austin Schuhb8bca732021-07-30 22:32:00 -070072
73 private:
Austin Schuh572924a2021-07-30 22:32:12 -070074 void QueueHeader(
75 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> &&header);
76
77 const Node *const node_ = nullptr;
78 size_t node_index_ = 0;
79 LogNamer *log_namer_;
80 UUID parts_uuid_ = UUID::Random();
81 size_t parts_index_ = 0;
82
Austin Schuhb8bca732021-07-30 22:32:00 -070083 std::function<void(NewDataWriter *)> reopen_;
84 std::function<void(NewDataWriter *)> close_;
Austin Schuh572924a2021-07-30 22:32:12 -070085 bool header_written_ = false;
86 UUID source_node_boot_uuid_ = UUID::Zero();
Austin Schuhb8bca732021-07-30 22:32:00 -070087};
88
Austin Schuhcb5601b2020-09-10 15:29:59 -070089// Interface describing how to name, track, and add headers to log file parts.
90class LogNamer {
91 public:
92 // Constructs a LogNamer with the primary node (ie the one the logger runs on)
93 // being node.
Austin Schuh73340842021-07-30 22:32:06 -070094 LogNamer(const Configuration *configuration, const Node *node)
95 : configuration_(configuration), node_(node) {
96 nodes_.emplace_back(node_);
97 node_states_.resize(configuration::NodesCount(configuration_));
98 }
Austin Schuhcb5601b2020-09-10 15:29:59 -070099 virtual ~LogNamer() {}
100
Austin Schuh6bb8a822021-03-31 23:04:39 -0700101 virtual std::string_view base_name() const = 0;
102
103 // Rotate should be called at least once in between calls to set_base_name.
104 // Otherwise temporary files will not be recoverable.
105 // Rotate is called by Logger::RenameLogBase, which is currently the only user
106 // of this method.
107 // Only renaming the folder is supported, not the file base name.
108 virtual void set_base_name(std::string_view base_name) = 0;
109
Brian Silverman87ac0402020-09-17 14:47:01 -0700110 // Returns a writer for writing data from messages on this channel (on the
111 // primary node).
112 //
113 // The returned pointer will stay valid across rotations, but the object it
114 // points to will be assigned to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700115 virtual NewDataWriter *MakeWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700116
Brian Silverman87ac0402020-09-17 14:47:01 -0700117 // Returns a writer for writing timestamps from messages on this channel (on
118 // the 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 *MakeTimestampWriter(const Channel *channel) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700123
124 // Returns a writer for writing timestamps delivered over the special
125 // /aos/remote_timestamps/* channels. node is the node that the timestamps
Brian Silverman87ac0402020-09-17 14:47:01 -0700126 // are forwarded back from (to the primary node).
127 //
128 // The returned pointer will stay valid across rotations, but the object it
129 // points to will be assigned to.
Austin Schuh73340842021-07-30 22:32:06 -0700130 virtual NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
131 const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700132
Austin Schuh73340842021-07-30 22:32:06 -0700133 // Rotates all log files for the provided node.
134 virtual void Rotate(const Node *node) = 0;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700135
136 // Returns all the nodes that data is being written for.
137 const std::vector<const Node *> &nodes() const { return nodes_; }
138
139 // Returns the node the logger is running on.
140 const Node *node() const { return node_; }
141
Austin Schuh8c399962020-12-25 21:51:45 -0800142 // Writes out the nested Configuration object to the config file location.
143 virtual void WriteConfiguration(
144 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
145 std::string_view config_sha256) = 0;
146
Austin Schuh73340842021-07-30 22:32:06 -0700147 void SetHeaderTemplate(
148 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header) {
149 header_ = std::move(header);
150 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700151
Austin Schuh73340842021-07-30 22:32:06 -0700152 void SetStartTimes(size_t node_index,
153 monotonic_clock::time_point monotonic_start_time,
154 realtime_clock::time_point realtime_start_time,
155 monotonic_clock::time_point logger_monotonic_start_time,
156 realtime_clock::time_point logger_realtime_start_time) {
157 node_states_[node_index].monotonic_start_time = monotonic_start_time;
158 node_states_[node_index].realtime_start_time = realtime_start_time;
159 node_states_[node_index].logger_monotonic_start_time =
160 logger_monotonic_start_time;
161 node_states_[node_index].logger_realtime_start_time =
162 logger_realtime_start_time;
Austin Schuh572924a2021-07-30 22:32:12 -0700163
Austin Schuh73340842021-07-30 22:32:06 -0700164 // TODO(austin): Track that the header has changed and needs to be
Austin Schuh572924a2021-07-30 22:32:12 -0700165 // rewritten down here rather than up in log_writer.
Austin Schuh73340842021-07-30 22:32:06 -0700166 }
167
168 monotonic_clock::time_point monotonic_start_time(size_t node_index) const {
169 return node_states_[node_index].monotonic_start_time;
170 }
171
Austin Schuh73340842021-07-30 22:32:06 -0700172 protected:
173 // Creates a new header by copying fields out of the template and combining
174 // them with the arguments provided.
175 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader(
176 size_t node_index, const UUID &source_node_boot_uuid,
177 const UUID &parts_uuid, int parts_index) const;
178
179 const Configuration *const configuration_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700180 const Node *const node_;
181 std::vector<const Node *> nodes_;
Austin Schuh73340842021-07-30 22:32:06 -0700182
Austin Schuh572924a2021-07-30 22:32:12 -0700183 friend NewDataWriter;
184
Austin Schuh73340842021-07-30 22:32:06 -0700185 // Structure with state per node about times and such.
186 // TODO(austin): some of this lives better in NewDataWriter once we move
187 // ownership of deciding when to write headers into LogNamer.
188 struct NodeState {
189 // Time when this node started logging.
190 monotonic_clock::time_point monotonic_start_time =
191 monotonic_clock::min_time;
192 realtime_clock::time_point realtime_start_time = realtime_clock::min_time;
193
194 // Corresponding time on the logger node when it started logging.
195 monotonic_clock::time_point logger_monotonic_start_time =
196 monotonic_clock::min_time;
197 realtime_clock::time_point logger_realtime_start_time =
198 realtime_clock::min_time;
Austin Schuh73340842021-07-30 22:32:06 -0700199 };
200 std::vector<NodeState> node_states_;
201
202 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> header_ =
203 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader>::Empty();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700204};
205
206// Local log namer is a simple version which only names things
207// "base_name.part#.bfbs" and increments the part number. It doesn't support
208// any other log type.
209class LocalLogNamer : public LogNamer {
210 public:
Austin Schuh73340842021-07-30 22:32:06 -0700211 LocalLogNamer(std::string_view base_name, const Configuration *configuration,
212 const Node *node)
213 : LogNamer(configuration, node),
Austin Schuhcb5601b2020-09-10 15:29:59 -0700214 base_name_(base_name),
Austin Schuh572924a2021-07-30 22:32:12 -0700215 data_writer_(this, node,
216 [this](NewDataWriter *writer) {
217 writer->writer = std::make_unique<DetachedBufferWriter>(
218 absl::StrCat(base_name_, ".part",
219 writer->parts_index(), ".bfbs"),
220 std::make_unique<aos::logger::DummyEncoder>());
221 },
222 [](NewDataWriter * /*writer*/) {}) {}
Austin Schuhb8bca732021-07-30 22:32:00 -0700223
224 LocalLogNamer(const LocalLogNamer &) = delete;
225 LocalLogNamer(LocalLogNamer &&) = delete;
226 LocalLogNamer &operator=(const LocalLogNamer &) = delete;
227 LocalLogNamer &operator=(LocalLogNamer &&) = delete;
228
Brian Silverman0465fcf2020-09-24 00:29:18 -0700229 ~LocalLogNamer() override = default;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700230
Austin Schuh6bb8a822021-03-31 23:04:39 -0700231 std::string_view base_name() const final { return base_name_; }
232
233 void set_base_name(std::string_view base_name) final {
234 base_name_ = base_name;
235 }
236
Austin Schuhb8bca732021-07-30 22:32:00 -0700237 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700238
Austin Schuh73340842021-07-30 22:32:06 -0700239 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700240
Austin Schuhb8bca732021-07-30 22:32:00 -0700241 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700242
Austin Schuh73340842021-07-30 22:32:06 -0700243 NewDataWriter *MakeForwardedTimestampWriter(const Channel * /*channel*/,
244 const Node * /*node*/) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700245
Austin Schuh8c399962020-12-25 21:51:45 -0800246 void WriteConfiguration(
247 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
248 std::string_view config_sha256) override;
249
Austin Schuhcb5601b2020-09-10 15:29:59 -0700250 private:
Austin Schuh6bb8a822021-03-31 23:04:39 -0700251 std::string base_name_;
Austin Schuhb8bca732021-07-30 22:32:00 -0700252
253 NewDataWriter data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700254};
255
256// Log namer which uses a config and a base name to name a bunch of files.
257class MultiNodeLogNamer : public LogNamer {
258 public:
Brian Silvermancb805822020-10-06 17:43:35 -0700259 MultiNodeLogNamer(std::string_view base_name,
260 const Configuration *configuration, const Node *node);
261 ~MultiNodeLogNamer() override;
262
Austin Schuh6bb8a822021-03-31 23:04:39 -0700263 std::string_view base_name() const final { return base_name_; }
264
265 void set_base_name(std::string_view base_name) final {
266 old_base_name_ = base_name_;
267 base_name_ = base_name;
268 }
Brian Silvermancb805822020-10-06 17:43:35 -0700269
Brian Silverman48deab12020-09-30 18:39:28 -0700270 // If temp_suffix is set, then this will write files under names beginning
271 // with the specified suffix, and then rename them to the desired name after
272 // they are fully written.
273 //
274 // This is useful to enable incremental copying of the log files.
275 //
276 // Defaults to writing directly to the final filename.
Brian Silvermancb805822020-10-06 17:43:35 -0700277 void set_temp_suffix(std::string_view temp_suffix) {
278 temp_suffix_ = temp_suffix;
279 }
Austin Schuhcb5601b2020-09-10 15:29:59 -0700280
Brian Silvermancb805822020-10-06 17:43:35 -0700281 // Sets the function for creating encoders.
282 //
283 // Defaults to just creating DummyEncoders.
284 void set_encoder_factory(
285 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory) {
286 encoder_factory_ = std::move(encoder_factory);
287 }
288
289 // Sets an additional file extension.
290 //
291 // Defaults to nothing.
292 void set_extension(std::string_view extension) { extension_ = extension; }
Brian Silverman1f345222020-09-24 21:14:48 -0700293
Brian Silvermana621f522020-09-30 16:52:43 -0700294 // A list of all the filenames we've written.
295 //
296 // This only includes the part after base_name().
297 const std::vector<std::string> &all_filenames() const {
298 return all_filenames_;
299 }
300
Austin Schuh73340842021-07-30 22:32:06 -0700301 void Rotate(const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700302
Austin Schuh8c399962020-12-25 21:51:45 -0800303 void WriteConfiguration(
304 aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> *header,
305 std::string_view config_sha256) override;
306
Austin Schuhb8bca732021-07-30 22:32:00 -0700307 NewDataWriter *MakeWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700308
Austin Schuhb8bca732021-07-30 22:32:00 -0700309 NewDataWriter *MakeForwardedTimestampWriter(const Channel *channel,
Austin Schuh73340842021-07-30 22:32:06 -0700310 const Node *node) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700311
Austin Schuhb8bca732021-07-30 22:32:00 -0700312 NewDataWriter *MakeTimestampWriter(const Channel *channel) override;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700313
Brian Silverman0465fcf2020-09-24 00:29:18 -0700314 // Indicates that at least one file ran out of space. Once this happens, we
315 // stop trying to open new files, to avoid writing any files with holes from
316 // previous parts.
317 //
318 // Besides this function, this object will silently stop logging data when
319 // this occurs. If you want to ensure log files are complete, you must call
320 // this method.
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700321 bool ran_out_of_space() const {
322 return accumulate_data_writers<bool>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700323 ran_out_of_space_, [](bool x, const NewDataWriter &data_writer) {
Brian Silvermana9f2ec92020-10-06 18:00:53 -0700324 return x ||
325 (data_writer.writer && data_writer.writer->ran_out_of_space());
326 });
327 }
Brian Silverman0465fcf2020-09-24 00:29:18 -0700328
Brian Silverman1f345222020-09-24 21:14:48 -0700329 // Returns the maximum total_bytes() value for all existing
330 // DetachedBufferWriters.
331 //
332 // Returns 0 if no files are open.
333 size_t maximum_total_bytes() const {
Brian Silvermancb805822020-10-06 17:43:35 -0700334 return accumulate_data_writers<size_t>(
Austin Schuhb8bca732021-07-30 22:32:00 -0700335 0, [](size_t x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700336 return std::max(x, data_writer.writer->total_bytes());
337 });
Brian Silverman1f345222020-09-24 21:14:48 -0700338 }
339
Brian Silverman0465fcf2020-09-24 00:29:18 -0700340 // Closes all existing log files. No more data may be written after this.
341 //
342 // This may set ran_out_of_space().
343 void Close();
344
Brian Silvermancb805822020-10-06 17:43:35 -0700345 // Accessors for various statistics. See the identically-named methods in
346 // DetachedBufferWriter for documentation. These are aggregated across all
347 // past and present DetachedBufferWriters.
348 std::chrono::nanoseconds max_write_time() const {
349 return accumulate_data_writers(
350 max_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700351 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700352 return std::max(x, data_writer.writer->max_write_time());
353 });
354 }
355 int max_write_time_bytes() const {
356 return std::get<0>(accumulate_data_writers(
357 std::make_tuple(max_write_time_bytes_, max_write_time_),
358 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700359 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700360 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
361 return std::make_tuple(data_writer.writer->max_write_time_bytes(),
362 data_writer.writer->max_write_time());
363 }
364 return x;
365 }));
366 }
367 int max_write_time_messages() const {
368 return std::get<0>(accumulate_data_writers(
369 std::make_tuple(max_write_time_messages_, max_write_time_),
370 [](std::tuple<int, std::chrono::nanoseconds> x,
Austin Schuhb8bca732021-07-30 22:32:00 -0700371 const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700372 if (data_writer.writer->max_write_time() > std::get<1>(x)) {
373 return std::make_tuple(
374 data_writer.writer->max_write_time_messages(),
375 data_writer.writer->max_write_time());
376 }
377 return x;
378 }));
379 }
380 std::chrono::nanoseconds total_write_time() const {
381 return accumulate_data_writers(
382 total_write_time_,
Austin Schuhb8bca732021-07-30 22:32:00 -0700383 [](std::chrono::nanoseconds x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700384 return x + data_writer.writer->total_write_time();
385 });
386 }
387 int total_write_count() const {
388 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700389 total_write_count_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700390 return x + data_writer.writer->total_write_count();
391 });
392 }
393 int total_write_messages() const {
394 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700395 total_write_messages_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700396 return x + data_writer.writer->total_write_messages();
397 });
398 }
399 int total_write_bytes() const {
400 return accumulate_data_writers(
Austin Schuhb8bca732021-07-30 22:32:00 -0700401 total_write_bytes_, [](int x, const NewDataWriter &data_writer) {
Brian Silvermancb805822020-10-06 17:43:35 -0700402 return x + data_writer.writer->total_write_bytes();
403 });
404 }
405
406 void ResetStatistics();
407
Austin Schuhcb5601b2020-09-10 15:29:59 -0700408 private:
Austin Schuhcb5601b2020-09-10 15:29:59 -0700409 // Opens up a writer for timestamps forwarded back.
410 void OpenForwardedTimestampWriter(const Channel *channel,
Austin Schuhb8bca732021-07-30 22:32:00 -0700411 NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700412
413 // Opens up a writer for remote data.
Austin Schuhb8bca732021-07-30 22:32:00 -0700414 void OpenWriter(const Channel *channel, NewDataWriter *data_writer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700415
416 // Opens the main data writer file for this node responsible for data_writer_.
Brian Silvermana621f522020-09-30 16:52:43 -0700417 void OpenDataWriter();
Austin Schuhcb5601b2020-09-10 15:29:59 -0700418
Brian Silvermana621f522020-09-30 16:52:43 -0700419 void CreateBufferWriter(std::string_view path,
Brian Silverman0465fcf2020-09-24 00:29:18 -0700420 std::unique_ptr<DetachedBufferWriter> *destination);
421
Brian Silverman48deab12020-09-30 18:39:28 -0700422 void RenameTempFile(DetachedBufferWriter *destination);
423
Brian Silvermancb805822020-10-06 17:43:35 -0700424 void CloseWriter(std::unique_ptr<DetachedBufferWriter> *writer_pointer);
Austin Schuhcb5601b2020-09-10 15:29:59 -0700425
Brian Silvermancb805822020-10-06 17:43:35 -0700426 // A version of std::accumulate which operates over all of our DataWriters.
427 template <typename T, typename BinaryOperation>
428 T accumulate_data_writers(T t, BinaryOperation op) const {
Austin Schuhb8bca732021-07-30 22:32:00 -0700429 for (const std::pair<const Channel *const, NewDataWriter> &data_writer :
Brian Silvermancb805822020-10-06 17:43:35 -0700430 data_writers_) {
Austin Schuhad0cfc32020-12-21 12:34:26 -0800431 if (!data_writer.second.writer) continue;
Brian Silvermancb805822020-10-06 17:43:35 -0700432 t = op(std::move(t), data_writer.second);
433 }
Austin Schuhb8bca732021-07-30 22:32:00 -0700434 if (data_writer_) {
435 t = op(std::move(t), *data_writer_);
Brian Silvermancb805822020-10-06 17:43:35 -0700436 }
437 return t;
438 }
439
Austin Schuh6bb8a822021-03-31 23:04:39 -0700440 std::string base_name_;
441 std::string old_base_name_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700442
Brian Silverman0465fcf2020-09-24 00:29:18 -0700443 bool ran_out_of_space_ = false;
Brian Silvermana621f522020-09-30 16:52:43 -0700444 std::vector<std::string> all_filenames_;
Brian Silverman0465fcf2020-09-24 00:29:18 -0700445
Brian Silvermancb805822020-10-06 17:43:35 -0700446 std::string temp_suffix_;
447 std::function<std::unique_ptr<DetachedBufferEncoder>()> encoder_factory_ =
448 []() { return std::make_unique<DummyEncoder>(); };
449 std::string extension_;
450
451 // Storage for statistics from previously-rotated DetachedBufferWriters.
452 std::chrono::nanoseconds max_write_time_ = std::chrono::nanoseconds::zero();
453 int max_write_time_bytes_ = -1;
454 int max_write_time_messages_ = -1;
455 std::chrono::nanoseconds total_write_time_ = std::chrono::nanoseconds::zero();
456 int total_write_count_ = 0;
457 int total_write_messages_ = 0;
458 int total_write_bytes_ = 0;
459
Austin Schuhcb5601b2020-09-10 15:29:59 -0700460 // File to write both delivery timestamps and local data to.
Austin Schuhb8bca732021-07-30 22:32:00 -0700461 std::unique_ptr<NewDataWriter> data_writer_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700462
Austin Schuhb8bca732021-07-30 22:32:00 -0700463 std::map<const Channel *, NewDataWriter> data_writers_;
Austin Schuhcb5601b2020-09-10 15:29:59 -0700464};
465
466} // namespace logger
467} // namespace aos
468
469#endif // AOS_EVENTS_LOGGING_LOG_NAMER_H_