/workspaces/astro/sol3-sdk/cpp/sol3/core/size_delimited_log_reader.h Source File

Space-ng SDK: /workspaces/astro/sol3-sdk/cpp/sol3/core/size_delimited_log_reader.h Source File
Space-ng SDK
size_delimited_log_reader.h
Go to the documentation of this file.
1 // Copyright (c) Space-ng, inc. All rights reserved.
2 
3 #pragma once
4 
7 #include "sol3/cpp/filesystem.h"
8 
9 #include <flatbuffers/flatbuffers.h>
10 #include <tl/expected.hpp>
11 
12 #include <cstddef>
13 #include <memory>
14 #include <optional>
15 #include <stdexcept>
16 #include <string>
17 #include <string_view>
18 
19 namespace sol3::core {
24  : public std::enable_shared_from_this<SizeDelimitedLogReader> {
25  public:
27  struct ReadError {
28  enum class Code {
30  kEndOfFile,
32  kNotOpen,
39  };
40 
44  size_t offset = 0;
46  size_t size = 0;
49  };
50 
52  class ReadErrorException : public std::runtime_error {
53  public:
55  ReadErrorException(ReadError err, std::string_view context);
56 
59  };
60 
61  class Iterator;
62 
66  class FrameView {
67  public:
69 
74  flatbuffers::span<uint8_t const> span() const {
75  return flatbuffers::span<uint8_t const>(span_data_, span_size_);
76  }
79  flatbuffers::span<uint8_t const> spanNoPrefix() const;
81  size_t offsetBytes() const { return offset_; }
82 
83  private:
84  friend class SizeDelimitedLogReader;
85  friend class Iterator;
86  FrameView(
87  std::shared_ptr<SizeDelimitedLogReader const> reader,
88  flatbuffers::span<uint8_t const> span,
89  size_t offset);
90 
91  std::shared_ptr<SizeDelimitedLogReader const> reader_;
92  uint8_t const* span_data_ = nullptr;
93  size_t span_size_ = 0;
94  size_t offset_ = 0;
95  };
96 
101  class Iterator {
102  public:
104  using reference = FrameView const&;
105  using pointer = FrameView const*;
106 
108  explicit Iterator(tl::expected<FrameView, ReadError> current);
109  Iterator(Iterator const&) = default;
110  Iterator& operator=(Iterator const& other) = default;
111  Iterator(Iterator&&) = default;
112  Iterator& operator=(Iterator&& other) = default;
113  ~Iterator() = default;
114 
122  bool operator==(Iterator const& other) const;
123  bool operator!=(Iterator const& other) const;
124 
126  tl::expected<FrameView, ReadError> const& frameView() const;
127 
128  private:
129  void advance();
130  reference frame() const;
131 
132  tl::expected<FrameView, ReadError> current_;
133  };
134 
139 
144  static std::shared_ptr<SizeDelimitedLogReader> open(
145  cpp::fs::path const& log_path);
146 
151  tl::expected<FrameView, ReadError> readAtOffset(size_t offset) const;
152 
156  Iterator begin() const;
158  Iterator const& end() const;
159 
161  cpp::fs::path const& logPath() const { return log_path_; }
162 
163  private:
164  explicit SizeDelimitedLogReader(cpp::fs::path const& log_path);
165  void openFile(cpp::fs::path const& log_path);
166  void resetFile();
167 
168  cpp::fs::path log_path_;
169  FileDescriptor fd_;
170  impl::Mmap mmap_;
171  uint8_t const* data_ = nullptr;
172  size_t size_ = 0;
173 
174  Iterator end_it_;
175 };
176 
181  SizeDelimitedLogReader::ReadError const& err, std::string_view context);
182 // NOLINTBEGIN(readability-identifier-naming)
187 // NOLINTEND(readability-identifier-naming)
188 } // namespace sol3::core
RAII wrapper for POSIX file descriptors.
Definition: file_descriptor.h:12
Definition: size_delimited_log_reader.h:66
size_t offsetBytes() const
Byte offset of the frame within the file.
Definition: size_delimited_log_reader.h:81
flatbuffers::span< uint8_t const > spanNoPrefix() const
flatbuffers::span< uint8_t const > span() const
Definition: size_delimited_log_reader.h:74
Definition: size_delimited_log_reader.h:101
Iterator(tl::expected< FrameView, ReadError > current)
bool operator==(Iterator const &other) const
FrameView const & reference
Definition: size_delimited_log_reader.h:104
reference operator*() const
Access the current frame; throws ReadErrorException if in error state.
tl::expected< FrameView, ReadError > const & frameView() const
Access the current frame or the read error, if any.
Iterator & operator=(Iterator const &other)=default
Iterator & operator=(Iterator &&other)=default
pointer operator->() const
Access the current frame; throws ReadErrorException if in error state.
FrameView const * pointer
Definition: size_delimited_log_reader.h:105
bool operator!=(Iterator const &other) const
Exception wrapper that exposes the ReadError and context string.
Definition: size_delimited_log_reader.h:52
ReadErrorException(ReadError err, std::string_view context)
Builds a formatted error message for the provided ReadError.
std::string context
Definition: size_delimited_log_reader.h:58
ReadError error
Definition: size_delimited_log_reader.h:57
Definition: size_delimited_log_reader.h:24
SizeDelimitedLogReader & operator=(SizeDelimitedLogReader const &)=delete
SizeDelimitedLogReader(SizeDelimitedLogReader const &)=delete
SizeDelimitedLogReader & operator=(SizeDelimitedLogReader &&)=delete
tl::expected< FrameView, ReadError > readAtOffset(size_t offset) const
Iterator const & end() const
Sentinel iterator representing end-of-file.
SizeDelimitedLogReader(SizeDelimitedLogReader &&)=delete
static std::shared_ptr< SizeDelimitedLogReader > open(cpp::fs::path const &log_path)
cpp::fs::path const & logPath() const
Path for the currently opened log file.
Definition: size_delimited_log_reader.h:161
Definition: shmem_buffer_impl.h:13
enum ArtifactFormat string
Camera/host holding the original file.
Definition: any_message_input.h:15
std::string format_as(AnyMessageView &view)
Definition: any_message_view.h:111
std::string formatReadError(SizeDelimitedLogReader::ReadError const &err, std::string_view context)
Format a ReadError for logs or exceptions.
char const * readErrorCodeToString(SizeDelimitedLogReader::ReadError::Code code)
String conversion for error codes (stable, human-readable strings).
Read error details returned from readAtOffset() and iteration.
Definition: size_delimited_log_reader.h:27
Code
Definition: size_delimited_log_reader.h:28
@ kZeroSizeFrame
Size prefix declares a zero-length payload.
@ kNotOpen
File is not open or has been reset.
@ kTruncatedPrefix
Size prefix is truncated (not enough bytes for uint32_t).
@ kTruncatedBuffer
Payload or padding extends past the mapped file size.
@ kEndOfFile
Requested offset is at or past the logical end of file.
size_t offset
Byte offset used for the read attempt.
Definition: size_delimited_log_reader.h:44
size_t size
Total mapped file size at the time of the read.
Definition: size_delimited_log_reader.h:46
std::string path
Log file path associated with the read, when available.
Definition: size_delimited_log_reader.h:48
Code code
Error code describing the failure mode.
Definition: size_delimited_log_reader.h:42