Thunderbots Project
Loading...
Searching...
No Matches
proto_logger.h
1#pragma once
2
3#include <google/protobuf/message.h>
4
5#include <functional>
6#include <string>
7#include <thread>
8
9#include "software/multithreading/thread_safe_buffer.hpp"
10
35{
39 struct SerializedProtoLog
40 {
41 std::string protobuf_type_full_name;
42 std::string serialized_proto;
43 double receive_time_sec;
44 };
45
46 public:
53 explicit ProtoLogger(const std::string& log_path,
54 std::function<double()> time_provider,
55 bool friendly_colour_yellow);
56
57 ProtoLogger() = delete;
58
63 ProtoLogger(const ProtoLogger&) = delete;
64 ProtoLogger& operator=(const ProtoLogger&) = delete;
65
67
74 void saveSerializedProto(const std::string& protobuf_type_full_name,
75 const std::string& serialized_proto);
76
84 template <typename ProtoType>
85 inline void saveSerializedProto(const std::string& serialized_proto)
86 {
87 static_assert(std::is_base_of<google::protobuf::Message, ProtoType>::value,
88 "ProtoType has to be a protobuf message type");
89
90 saveSerializedProto(ProtoType::GetDescriptor()->full_name(), serialized_proto);
91 };
92
97 void updateTimeProvider(std::function<double()> time_provider);
98
107 void flushAndStopLogging();
108
117 static std::string createLogEntry(const std::string& proto_full_name,
118 const std::string& serialized_proto,
119 double receive_time_sec);
120
121 private:
125 void logProtobufs();
126
131 bool shouldStopLogging() const;
132
133 std::string log_path_;
134 std::string log_folder_;
135 std::function<double()> time_provider_;
136 double start_time_;
137 bool friendly_colour_yellow_;
138 unsigned int failed_logs_frequency_counter_ = 0;
139
140 std::thread log_thread_;
141 std::atomic<bool> stop_logging_;
142 double destructor_called_time_sec_;
143
145
146 const Duration BUFFER_BLOCK_TIMEOUT = Duration::fromSeconds(0.1);
147 const std::string REPLAY_FILE_PREFIX = "proto_";
148 const std::string REPLAY_FILE_TIME_FORMAT = "%Y_%m_%d_%H_%M_%S";
149 static constexpr unsigned int PROTOBUF_BUFFER_SIZE = 1000;
150 static constexpr unsigned int REPLAY_MAX_CHUNK_SIZE_BYTES = 1024 * 1024; // 1 MB
151 static constexpr unsigned int FAILED_LOG_PRINT_FREQUENCY = 100;
152};
Definition duration.h:12
static const Duration fromSeconds(double seconds)
Definition duration.cpp:13
Definition proto_logger.h:35
void saveSerializedProto(const std::string &serialized_proto)
Definition proto_logger.h:85
ProtoLogger(const ProtoLogger &)=delete
void saveSerializedProto(const std::string &protobuf_type_full_name, const std::string &serialized_proto)
Definition proto_logger.cpp:45
void flushAndStopLogging()
Definition proto_logger.cpp:170
void updateTimeProvider(std::function< double()> time_provider)
Definition proto_logger.cpp:149
static std::string createLogEntry(const std::string &proto_full_name, const std::string &serialized_proto, double receive_time_sec)
Definition proto_logger.cpp:138
Definition thread_safe_buffer.hpp:23