Apollo 10.0
自动驾驶开放平台
message_util.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
22#pragma once
23
24#include <memory>
25#include <string>
26
27#include "absl/strings/str_cat.h"
28#include "google/protobuf/message.h"
29
30#include "cyber/common/file.h"
31#include "cyber/time/clock.h"
32
37namespace apollo {
38namespace common {
39namespace util {
40
41template <typename T, typename std::enable_if<
42 std::is_base_of<google::protobuf::Message, T>::value,
43 int>::type = 0>
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)));
52}
53
54template <typename T, typename std::enable_if<
55 std::is_base_of<google::protobuf::Message, T>::value,
56 int>::type = 0>
57bool DumpMessage(const std::shared_ptr<T>& msg,
58 const std::string& dump_dir = "/tmp") {
59 if (!msg) {
60 AWARN << "Message to be dumped is nullptr!";
61 }
62
63 auto type_name = T::descriptor()->full_name();
64 std::string dump_path = dump_dir + "/" + type_name;
65 if (!cyber::common::DirectoryExists(dump_path)) {
66 if (!cyber::common::EnsureDirectory(dump_path)) {
67 AERROR << "Cannot enable dumping for '" << type_name
68 << "' because the path " << dump_path
69 << " cannot be created or is not a directory.";
70 return false;
71 }
72 }
73
74 auto sequence_num = msg->header().sequence_num();
76 *msg, absl::StrCat(dump_path, "/", sequence_num, ".pb.txt"));
77}
78
79inline size_t MessageFingerprint(const google::protobuf::Message& message) {
80 static std::hash<std::string> hash_fn;
81 std::string proto_bytes;
82 message.SerializeToString(&proto_bytes);
83 return hash_fn(proto_bytes);
84}
85
86} // namespace util
87} // namespace common
88} // namespace apollo
static double NowInSeconds()
gets the current time in second.
Definition clock.cc:56
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
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)
Definition file.cc:43
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
Definition file.cc:207
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists.
Definition file.cc:299
class register implement
Definition arena_queue.h:37