blob: 4cd661d322699d446de31e9d5e7c38c71ba651b6 [file] [log] [blame]
Brian Silvermanf51499a2020-09-21 12:49:08 -07001#ifndef AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_
2#define AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_
3
4#include "absl/types/span.h"
Austin Schuh48d10d62022-10-16 22:19:23 -07005#include "aos/containers/resizeable_buffer.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07006#include "aos/events/logging/logger_generated.h"
Tyler Chatow2015bc62021-08-04 21:15:09 -07007#include "flatbuffers/flatbuffers.h"
Austin Schuh48d10d62022-10-16 22:19:23 -07008#include "glog/logging.h"
Brian Silvermanf51499a2020-09-21 12:49:08 -07009
10namespace aos::logger {
11
Austin Schuh48d10d62022-10-16 22:19:23 -070012// Interface to encode data as it is written to a file.
13class DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070014 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070015 virtual ~DataEncoder() = default;
Brian Silvermanf51499a2020-09-21 12:49:08 -070016
Austin Schuh48d10d62022-10-16 22:19:23 -070017 // Interface to copy data into a buffer.
18 class Copier {
19 public:
20 Copier(size_t size) : size_(size) {}
21
22 // Returns the data this will write.
23 size_t size() const { return size_; }
24
25 // Writes size() bytes to data, and returns the data written.
26 [[nodiscard]] virtual size_t Copy(uint8_t *data) = 0;
27
28 private:
29 size_t size_;
30 };
31
32 // Coppies a span. The span must have a longer lifetime than the coppier is
33 // being used.
34 class SpanCopier : public Copier {
35 public:
36 SpanCopier(absl::Span<const uint8_t> data)
37 : Copier(data.size()), data_(data) {
38 CHECK(data_.data());
39 }
40
41 size_t Copy(uint8_t *data) final {
42 std::memcpy(data, data_.data(), data_.size());
43 return data_.size();
44 }
45
46 private:
47 const absl::Span<const uint8_t> data_;
48 };
49
50 // Returns true if there is space in the buffer for the next request, or if
51 // the output needs to be flushed.
52 virtual bool HasSpace(size_t request) const = 0;
53
54 // Encodes and enqueues the given data encoder.
55 virtual void Encode(Copier *copy) = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070056
57 // If this returns true, the encoder may be bypassed by writing directly to
58 // the file.
59 virtual bool may_bypass() const { return false; }
60
61 // Finalizes the encoding process. After this, queue_size() represents the
62 // full extent of data which will be written to this file.
63 //
64 // Encode may not be called after this method.
65 virtual void Finish() = 0;
66
67 // Clears the first n encoded buffers from the queue.
68 virtual void Clear(int n) = 0;
69
Austin Schuh48d10d62022-10-16 22:19:23 -070070 // Returns a view of the queue of encoded buffers. Valid until any other
71 // method on this class is called.
72 virtual absl::Span<const absl::Span<const uint8_t>> queue() = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070073
74 // Returns the total number of of bytes currently queued up.
75 virtual size_t queued_bytes() const = 0;
76
77 // Returns the cumulative number of bytes which have been queued. This
78 // includes data which has been removed via Clear.
79 virtual size_t total_bytes() const = 0;
80
81 // Returns the number of elements in the queue.
82 virtual size_t queue_size() const = 0;
83};
84
85// This class does not encode the data. It just claims ownership of the raw data
86// and queues it up as is.
Austin Schuh48d10d62022-10-16 22:19:23 -070087class DummyEncoder final : public DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070088 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070089 DummyEncoder(size_t max_buffer_size);
Austin Schuhc41603c2020-10-11 16:17:37 -070090 DummyEncoder(const DummyEncoder &) = delete;
91 DummyEncoder(DummyEncoder &&other) = delete;
92 DummyEncoder &operator=(const DummyEncoder &) = delete;
93 DummyEncoder &operator=(DummyEncoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -070094 ~DummyEncoder() override = default;
95
Austin Schuh48d10d62022-10-16 22:19:23 -070096 bool HasSpace(size_t request) const final;
97 void Encode(Copier *copy) final;
Brian Silvermanf51499a2020-09-21 12:49:08 -070098 bool may_bypass() const final { return true; }
99 void Finish() final {}
100 void Clear(int n) final;
Austin Schuh48d10d62022-10-16 22:19:23 -0700101 absl::Span<const absl::Span<const uint8_t>> queue() final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 size_t queued_bytes() const final;
103 size_t total_bytes() const final { return total_bytes_; }
Austin Schuh48d10d62022-10-16 22:19:23 -0700104 size_t queue_size() const final {
105 return input_buffer_.size() != 0 ? 1u : 0u;
106 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700107
108 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700109 size_t total_bytes_ = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -0700110
111 ResizeableBuffer input_buffer_;
112 std::vector<absl::Span<const uint8_t>> return_queue_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113};
114
115// Interface to decode chunks of data. Implementations of this interface will
116// manage opening, reading, and closing the file stream.
117class DataDecoder {
118 public:
119 virtual ~DataDecoder() = default;
120
121 // Reads data into the given range. Returns the number of bytes read.
122 //
123 // Returns less than end-begin if all bytes have been read. Otherwise, this
124 // will always fill the whole range.
125 virtual size_t Read(uint8_t *begin, uint8_t *end) = 0;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700126
127 // Returns the underlying filename, for debugging purposes.
128 virtual std::string_view filename() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700129};
130
131// Simply reads the contents of the file into the target buffer.
132class DummyDecoder final : public DataDecoder {
133 public:
134 explicit DummyDecoder(std::string_view filename);
Austin Schuhc41603c2020-10-11 16:17:37 -0700135 DummyDecoder(const DummyDecoder &) = delete;
136 DummyDecoder(DummyDecoder &&other) = delete;
137 DummyDecoder &operator=(const DummyDecoder &) = delete;
138 DummyDecoder &operator=(DummyDecoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700139 ~DummyDecoder() override;
140
141 size_t Read(uint8_t *begin, uint8_t *end) final;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700142 std::string_view filename() const final { return filename_; }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700143
144 private:
Tyler Chatow2015bc62021-08-04 21:15:09 -0700145 const std::string filename_;
146
Brian Silvermanf51499a2020-09-21 12:49:08 -0700147 // File descriptor for the log file.
148 int fd_;
149
150 // Cached bit for if we have reached the end of the file. Otherwise we will
151 // hammer on the kernel asking for more data each time we send.
152 bool end_of_file_ = false;
153};
154
155} // namespace aos::logger
156
157#endif // AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_