Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/buffer_encoder.h" |
| 2 | |
| 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
| 5 | #include <sys/types.h> |
| 6 | |
| 7 | #include "glog/logging.h" |
| 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "aos/flatbuffers.h" |
| 10 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 11 | namespace aos::logger { |
| 12 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 13 | DummyEncoder::DummyEncoder(size_t /*max_message_size*/, size_t buffer_size) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 14 | // Round up to the nearest page size. |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 15 | input_buffer_.reserve(buffer_size); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 16 | return_queue_.resize(1); |
| 17 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 18 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 19 | size_t DummyEncoder::space() const { |
| 20 | return input_buffer_.capacity() - input_buffer_.size(); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 23 | bool DummyEncoder::HasSpace(size_t request) const { return request <= space(); } |
| 24 | |
| 25 | size_t DummyEncoder::Encode(Copier *copy, size_t start_byte) { |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 26 | const size_t input_buffer_initial_size = input_buffer_.size(); |
| 27 | |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 28 | size_t expected_write_size = |
| 29 | std::min(input_buffer_.capacity() - input_buffer_initial_size, |
| 30 | copy->size() - start_byte); |
| 31 | input_buffer_.resize(input_buffer_initial_size + expected_write_size); |
| 32 | const size_t written_size = |
| 33 | copy->Copy(input_buffer_.data() + input_buffer_initial_size, start_byte, |
| 34 | expected_write_size + start_byte); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 35 | |
| 36 | total_bytes_ += written_size; |
Austin Schuh | 8bdfc49 | 2023-02-11 12:53:13 -0800 | [diff] [blame] | 37 | |
| 38 | return written_size; |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void DummyEncoder::Clear(const int n) { |
| 42 | CHECK_GE(n, 0); |
| 43 | CHECK_LE(static_cast<size_t>(n), queue_size()); |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 44 | if (n != 0) { |
| 45 | input_buffer_.resize(0u); |
| 46 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 49 | absl::Span<const absl::Span<const uint8_t>> DummyEncoder::queue() { |
| 50 | if (input_buffer_.size() != 0) { |
| 51 | return_queue_[0] = |
| 52 | absl::Span<const uint8_t>(input_buffer_.data(), input_buffer_.size()); |
| 53 | return return_queue_; |
| 54 | } else { |
| 55 | return absl::Span<const absl::Span<const uint8_t>>(); |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 56 | } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Austin Schuh | 48d10d6 | 2022-10-16 22:19:23 -0700 | [diff] [blame] | 59 | size_t DummyEncoder::queued_bytes() const { return input_buffer_.size(); } |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 60 | |
| 61 | DummyDecoder::DummyDecoder(std::string_view filename) |
Tyler Chatow | 2015bc6 | 2021-08-04 21:15:09 -0700 | [diff] [blame] | 62 | : filename_(filename), fd_(open(filename_.c_str(), O_RDONLY | O_CLOEXEC)) { |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 63 | PCHECK(fd_ != -1) << ": Failed to open " << filename; |
| 64 | } |
| 65 | |
| 66 | DummyDecoder::~DummyDecoder() { |
| 67 | int status = close(fd_); |
| 68 | if (status != 0) { |
| 69 | PLOG(ERROR) << "DummyDecoder: Failed to close file"; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | size_t DummyDecoder::Read(uint8_t *begin, uint8_t *end) { |
| 74 | if (end_of_file_) { |
| 75 | return 0; |
| 76 | } |
| 77 | const ssize_t count = read(fd_, begin, end - begin); |
| 78 | PCHECK(count >= 0) << ": Failed to read from file"; |
| 79 | if (count == 0) { |
| 80 | end_of_file_ = true; |
| 81 | } |
| 82 | return static_cast<size_t>(count); |
| 83 | } |
| 84 | |
| 85 | } // namespace aos::logger |