farm-ng-core
event_log_writer.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Copyright (c) farm-ng, inc. All rights reserved.
16 
17 #pragma once
18 
19 #include <farm_ng/core/event.pb.h>
20 
21 #include <filesystem>
22 #include <memory>
23 #include <string>
24 
25 namespace farm_ng {
26 
27 /// Implementation of the `EventLogWriter` class
28 ///
30  public:
31  /// https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
32  virtual ~EventLogWriterImpl() {}
33 
34  virtual void write(
35  std::string path,
36  google::protobuf::Message const& message,
37  google::protobuf::RepeatedPtrField<core::proto::Timestamp> const&
38  timestamps) = 0;
39 
40  /// Returns the number of bytes written to the file so far
41  virtual ssize_t getBytesWritten() = 0;
42 };
43 
44 /// Class that serializes incoming protobuf events to a file in disk.
46  public:
47  /// Main constructor of the class.
48  ///
49  /// Precondition: It must be possible to create the folder ``log_path_`` if it
50  /// does not exist. If ``log-path`` does exist, it must be a folder and it
51  /// must be empty.
52  ///
53  EventLogWriter(std::filesystem::path const& log_path);
54 
55  /// Main destructor
56  virtual ~EventLogWriter() noexcept;
57 
58  /// Writes an incoming protobuf in the log file
59  void write(
60  std::string const& path,
61  google::protobuf::Message const& message,
62  std::vector<core::proto::Timestamp> const& timestamps =
63  std::vector<core::proto::Timestamp>());
64  void write(
65  std::string const& path,
66  google::protobuf::Message const& message,
67  google::protobuf::RepeatedPtrField<core::proto::Timestamp> const&
68  timestamps);
69 
70  /// Returns the path including the fileaname
71  [[nodiscard]] std::filesystem::path getPath() const { return log_path_; }
72 
73  /// Returns the number of bytes written to the file so far
74  [[nodiscard]] ssize_t getBytesWritten();
75 
76  private:
77  // The log path including the filename
78  std::filesystem::path log_path_;
79  // Implementation pointer of the class
80  std::unique_ptr<EventLogWriterImpl> impl_;
81 };
82 
83 core::proto::Timestamp makeWriteStamp();
84 
85 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
farm_ng::EventLogWriter::~EventLogWriter
virtual ~EventLogWriter() noexcept
Main destructor.
Definition: event_log_writer.cpp:124
farm_ng::makeWriteStamp
auto makeWriteStamp() -> core::proto::Timestamp
Definition: event_log_writer.cpp:56
farm_ng::EventLogWriter::getPath
std::filesystem::path getPath() const
Returns the path including the fileaname.
Definition: event_log_writer.h:71
farm_ng::EventLogWriter
Class that serializes incoming protobuf events to a file in disk.
Definition: event_log_writer.h:45
farm_ng::EventLogWriterImpl::getBytesWritten
virtual ssize_t getBytesWritten()=0
Returns the number of bytes written to the file so far.
farm_ng::EventLogWriterImpl::write
virtual void write(std::string path, google::protobuf::Message const &message, google::protobuf::RepeatedPtrField< core::proto::Timestamp > const &timestamps)=0
message
cmake message("farm_ng_cmake_DIR" ${farm_ng_cmake_DIR}) include($
Definition: CMakeLists.txt:8
farm_ng::EventLogWriterImpl
Implementation of the EventLogWriter class.
Definition: event_log_writer.h:29
farm_ng::EventLogWriter::write
void write(std::string const &path, google::protobuf::Message const &message, std::vector< core::proto::Timestamp > const &timestamps=std::vector< core::proto::Timestamp >())
Writes an incoming protobuf in the log file.
Definition: event_log_writer.cpp:126
farm_ng::EventLogWriter::EventLogWriter
EventLogWriter(std::filesystem::path const &log_path)
Main constructor of the class.
Definition: event_log_writer.cpp:109
farm_ng::EventLogWriter::getBytesWritten
ssize_t getBytesWritten()
Returns the number of bytes written to the file so far.
Definition: event_log_writer.cpp:145
farm_ng::EventLogWriterImpl::~EventLogWriterImpl
virtual ~EventLogWriterImpl()
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-dtor
Definition: event_log_writer.h:32