27#include "absl/strings/str_cat.h"
28#include "google/protobuf/message.h"
41template <
typename T,
typename std::enable_if<
42 std::is_base_of<google::protobuf::Message, T>::value,
44static void FillHeader(
const std::string& module_name, T* msg) {
45 static std::atomic<uint64_t> sequence_num = {0};
46 auto* header = msg->mutable_header();
48 header->set_module_name(module_name);
49 header->set_timestamp_sec(timestamp);
50 header->set_sequence_num(
51 static_cast<unsigned int>(sequence_num.fetch_add(1)));
54template <
typename T,
typename std::enable_if<
55 std::is_base_of<google::protobuf::Message, T>::value,
58 const std::string& dump_dir =
"/tmp") {
60 AWARN <<
"Message to be dumped is nullptr!";
63 auto type_name = T::descriptor()->full_name();
64 std::string dump_path = dump_dir +
"/" + type_name;
67 AERROR <<
"Cannot enable dumping for '" << type_name
68 <<
"' because the path " << dump_path
69 <<
" cannot be created or is not a directory.";
74 auto sequence_num = msg->header().sequence_num();
76 *msg, absl::StrCat(dump_path,
"/", sequence_num,
".pb.txt"));
80 static std::hash<std::string> hash_fn;
81 std::string proto_bytes;
82 message.SerializeToString(&proto_bytes);
83 return hash_fn(proto_bytes);
static double NowInSeconds()
gets the current time in second.
size_t MessageFingerprint(const google::protobuf::Message &message)
bool DumpMessage(const std::shared_ptr< T > &msg, const std::string &dump_dir="/tmp")
bool SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.