blob: 34de00a8ef1ddb635679aabd42760ebcd23e34a7 [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.
Austin Schuh71a40d42023-02-04 21:22:22 -080026 [[nodiscard]] virtual size_t Copy(uint8_t *data, size_t start_byte,
27 size_t end_byte) = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -070028
29 private:
30 size_t size_;
31 };
32
33 // Coppies a span. The span must have a longer lifetime than the coppier is
34 // being used.
35 class SpanCopier : public Copier {
36 public:
37 SpanCopier(absl::Span<const uint8_t> data)
38 : Copier(data.size()), data_(data) {
Austin Schuh71a40d42023-02-04 21:22:22 -080039 CHECK(data_.data() != nullptr);
Austin Schuh48d10d62022-10-16 22:19:23 -070040 }
41
Austin Schuh71a40d42023-02-04 21:22:22 -080042 size_t Copy(uint8_t *data, size_t start_byte, size_t end_byte) final {
43 DCHECK_LE(start_byte, end_byte);
44 DCHECK_LE(end_byte, data_.size());
45
46 std::memcpy(data, data_.data() + start_byte, end_byte - start_byte);
47 return end_byte - start_byte;
Austin Schuh48d10d62022-10-16 22:19:23 -070048 }
49
50 private:
51 const absl::Span<const uint8_t> data_;
52 };
53
54 // Returns true if there is space in the buffer for the next request, or if
55 // the output needs to be flushed.
56 virtual bool HasSpace(size_t request) const = 0;
57
58 // Encodes and enqueues the given data encoder.
59 virtual void Encode(Copier *copy) = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070060
61 // If this returns true, the encoder may be bypassed by writing directly to
62 // the file.
63 virtual bool may_bypass() const { return false; }
64
65 // Finalizes the encoding process. After this, queue_size() represents the
66 // full extent of data which will be written to this file.
67 //
68 // Encode may not be called after this method.
69 virtual void Finish() = 0;
70
71 // Clears the first n encoded buffers from the queue.
72 virtual void Clear(int n) = 0;
73
Austin Schuh48d10d62022-10-16 22:19:23 -070074 // Returns a view of the queue of encoded buffers. Valid until any other
75 // method on this class is called.
76 virtual absl::Span<const absl::Span<const uint8_t>> queue() = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -070077
78 // Returns the total number of of bytes currently queued up.
79 virtual size_t queued_bytes() const = 0;
80
81 // Returns the cumulative number of bytes which have been queued. This
82 // includes data which has been removed via Clear.
83 virtual size_t total_bytes() const = 0;
84
85 // Returns the number of elements in the queue.
86 virtual size_t queue_size() const = 0;
87};
88
89// This class does not encode the data. It just claims ownership of the raw data
90// and queues it up as is.
Austin Schuh48d10d62022-10-16 22:19:23 -070091class DummyEncoder final : public DataEncoder {
Brian Silvermanf51499a2020-09-21 12:49:08 -070092 public:
Austin Schuh48d10d62022-10-16 22:19:23 -070093 DummyEncoder(size_t max_buffer_size);
Austin Schuhc41603c2020-10-11 16:17:37 -070094 DummyEncoder(const DummyEncoder &) = delete;
95 DummyEncoder(DummyEncoder &&other) = delete;
96 DummyEncoder &operator=(const DummyEncoder &) = delete;
97 DummyEncoder &operator=(DummyEncoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -070098 ~DummyEncoder() override = default;
99
Austin Schuh48d10d62022-10-16 22:19:23 -0700100 bool HasSpace(size_t request) const final;
101 void Encode(Copier *copy) final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700102 bool may_bypass() const final { return true; }
103 void Finish() final {}
104 void Clear(int n) final;
Austin Schuh48d10d62022-10-16 22:19:23 -0700105 absl::Span<const absl::Span<const uint8_t>> queue() final;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700106 size_t queued_bytes() const final;
107 size_t total_bytes() const final { return total_bytes_; }
Austin Schuh48d10d62022-10-16 22:19:23 -0700108 size_t queue_size() const final {
109 return input_buffer_.size() != 0 ? 1u : 0u;
110 }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700111
112 private:
Brian Silvermanf51499a2020-09-21 12:49:08 -0700113 size_t total_bytes_ = 0;
Austin Schuh48d10d62022-10-16 22:19:23 -0700114
115 ResizeableBuffer input_buffer_;
116 std::vector<absl::Span<const uint8_t>> return_queue_;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700117};
118
119// Interface to decode chunks of data. Implementations of this interface will
120// manage opening, reading, and closing the file stream.
121class DataDecoder {
122 public:
123 virtual ~DataDecoder() = default;
124
125 // Reads data into the given range. Returns the number of bytes read.
126 //
127 // Returns less than end-begin if all bytes have been read. Otherwise, this
128 // will always fill the whole range.
129 virtual size_t Read(uint8_t *begin, uint8_t *end) = 0;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700130
131 // Returns the underlying filename, for debugging purposes.
132 virtual std::string_view filename() const = 0;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700133};
134
135// Simply reads the contents of the file into the target buffer.
136class DummyDecoder final : public DataDecoder {
137 public:
138 explicit DummyDecoder(std::string_view filename);
Austin Schuhc41603c2020-10-11 16:17:37 -0700139 DummyDecoder(const DummyDecoder &) = delete;
140 DummyDecoder(DummyDecoder &&other) = delete;
141 DummyDecoder &operator=(const DummyDecoder &) = delete;
142 DummyDecoder &operator=(DummyDecoder &&other) = delete;
Brian Silvermanf51499a2020-09-21 12:49:08 -0700143 ~DummyDecoder() override;
144
145 size_t Read(uint8_t *begin, uint8_t *end) final;
Tyler Chatow2015bc62021-08-04 21:15:09 -0700146 std::string_view filename() const final { return filename_; }
Brian Silvermanf51499a2020-09-21 12:49:08 -0700147
148 private:
Tyler Chatow2015bc62021-08-04 21:15:09 -0700149 const std::string filename_;
150
Brian Silvermanf51499a2020-09-21 12:49:08 -0700151 // File descriptor for the log file.
152 int fd_;
153
154 // Cached bit for if we have reached the end of the file. Otherwise we will
155 // hammer on the kernel asking for more data each time we send.
156 bool end_of_file_ = false;
157};
158
159} // namespace aos::logger
160
161#endif // AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_