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. |
| 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 56 | |
| 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 Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 70 | // 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 73 | |
| 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 Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 87 | class DummyEncoder final : public DataEncoder { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 88 | public: |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 89 | DummyEncoder(size_t max_buffer_size); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 90 | DummyEncoder(const DummyEncoder &) = delete; |
| 91 | DummyEncoder(DummyEncoder &&other) = delete; |
| 92 | DummyEncoder &operator=(const DummyEncoder &) = delete; |
| 93 | DummyEncoder &operator=(DummyEncoder &&other) = delete; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 94 | ~DummyEncoder() override = default; |
| 95 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 96 | bool HasSpace(size_t request) const final; |
| 97 | void Encode(Copier *copy) final; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 98 | bool may_bypass() const final { return true; } |
| 99 | void Finish() final {} |
| 100 | void Clear(int n) final; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 101 | absl::Span<const absl::Span<const uint8_t>> queue() final; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 102 | size_t queued_bytes() const final; |
| 103 | size_t total_bytes() const final { return total_bytes_; } |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 104 | size_t queue_size() const final { |
| 105 | return input_buffer_.size() != 0 ? 1u : 0u; |
| 106 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 107 | |
| 108 | private: |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 109 | size_t total_bytes_ = 0; |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 110 | |
| 111 | ResizeableBuffer input_buffer_; |
| 112 | std::vector<absl::Span<const uint8_t>> return_queue_; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // Interface to decode chunks of data. Implementations of this interface will |
| 116 | // manage opening, reading, and closing the file stream. |
| 117 | class 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 Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 126 | |
| 127 | // Returns the underlying filename, for debugging purposes. |
| 128 | virtual std::string_view filename() const = 0; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // Simply reads the contents of the file into the target buffer. |
| 132 | class DummyDecoder final : public DataDecoder { |
| 133 | public: |
| 134 | explicit DummyDecoder(std::string_view filename); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 135 | DummyDecoder(const DummyDecoder &) = delete; |
| 136 | DummyDecoder(DummyDecoder &&other) = delete; |
| 137 | DummyDecoder &operator=(const DummyDecoder &) = delete; |
| 138 | DummyDecoder &operator=(DummyDecoder &&other) = delete; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 139 | ~DummyDecoder() override; |
| 140 | |
| 141 | size_t Read(uint8_t *begin, uint8_t *end) final; |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 142 | std::string_view filename() const final { return filename_; } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 143 | |
| 144 | private: |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 145 | const std::string filename_; |
| 146 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 147 | // 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_ |