Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_ |
| 2 | #define AOS_EVENTS_LOGGING_BUFFER_ENCODER_H_ |
| 3 | |
| 4 | #include "absl/types/span.h" |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 5 | #include "aos/containers/resizeable_buffer.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 6 | #include "aos/events/logging/logger_generated.h" |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 7 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 9 | |
| 10 | namespace aos::logger { |
| 11 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 12 | // Interface to encode data as it is written to a file. |
| 13 | class DataEncoder { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 14 | public: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 15 | virtual ~DataEncoder() = default; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 16 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 17 | // 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 Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 26 | [[nodiscard]] virtual size_t Copy(uint8_t *data, size_t start_byte, |
| 27 | size_t end_byte) = 0; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 28 | |
| 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 Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 39 | CHECK(data_.data() != nullptr); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Austin Schuh | 71a40d4 | 2023-02-04 21:22:22 -0800 | [diff] [blame] | 42 | 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 Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 48 | } |
| 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 60 | |
| 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 Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 74 | // 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 77 | |
| 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 Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 91 | class DummyEncoder final : public DataEncoder { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 92 | public: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 93 | DummyEncoder(size_t max_buffer_size); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 94 | DummyEncoder(const DummyEncoder &) = delete; |
| 95 | DummyEncoder(DummyEncoder &&other) = delete; |
| 96 | DummyEncoder &operator=(const DummyEncoder &) = delete; |
| 97 | DummyEncoder &operator=(DummyEncoder &&other) = delete; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 98 | ~DummyEncoder() override = default; |
| 99 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 100 | bool HasSpace(size_t request) const final; |
| 101 | void Encode(Copier *copy) final; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | bool may_bypass() const final { return true; } |
| 103 | void Finish() final {} |
| 104 | void Clear(int n) final; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 105 | absl::Span<const absl::Span<const uint8_t>> queue() final; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 106 | size_t queued_bytes() const final; |
| 107 | size_t total_bytes() const final { return total_bytes_; } |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 108 | size_t queue_size() const final { |
| 109 | return input_buffer_.size() != 0 ? 1u : 0u; |
| 110 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 111 | |
| 112 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 113 | size_t total_bytes_ = 0; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 114 | |
| 115 | ResizeableBuffer input_buffer_; |
| 116 | std::vector<absl::Span<const uint8_t>> return_queue_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Interface to decode chunks of data. Implementations of this interface will |
| 120 | // manage opening, reading, and closing the file stream. |
| 121 | class 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 Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 130 | |
| 131 | // Returns the underlying filename, for debugging purposes. |
| 132 | virtual std::string_view filename() const = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | // Simply reads the contents of the file into the target buffer. |
| 136 | class DummyDecoder final : public DataDecoder { |
| 137 | public: |
| 138 | explicit DummyDecoder(std::string_view filename); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 139 | DummyDecoder(const DummyDecoder &) = delete; |
| 140 | DummyDecoder(DummyDecoder &&other) = delete; |
| 141 | DummyDecoder &operator=(const DummyDecoder &) = delete; |
| 142 | DummyDecoder &operator=(DummyDecoder &&other) = delete; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 143 | ~DummyDecoder() override; |
| 144 | |
| 145 | size_t Read(uint8_t *begin, uint8_t *end) final; |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 146 | std::string_view filename() const final { return filename_; } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 147 | |
| 148 | private: |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 149 | const std::string filename_; |
| 150 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 151 | // 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_ |