Apollo  6.0
Open source self driving car software
message_util.h
Go to the documentation of this file.
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 
37 namespace apollo {
38 namespace common {
39 namespace util {
40 
41 template <typename T, typename std::enable_if<
43  int>::type = 0>
44 static void FillHeader(const std::string& module_name, T* msg) {
45  static std::atomic<uint64_t> sequence_num = {0};
46  auto* header = msg->mutable_header();
47  double timestamp = ::apollo::cyber::Clock::NowInSeconds();
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 
54 template <typename T, typename std::enable_if<
55  std::is_base_of<google::protobuf::Message, T>::value,
56  int>::type = 0>
57 bool 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 
79 inline 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
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
bool DirectoryExists(const std::string &directory_path)
Check if the directory specified by directory_path exists and is indeed a directory.
bool DumpMessage(const std::shared_ptr< T > &msg, const std::string &dump_dir="/tmp")
Definition: message_util.h:57
bool SetProtoToASCIIFile(const google::protobuf::Message &message, int file_descriptor)
size_t MessageFingerprint(const google::protobuf::Message &message)
Definition: message_util.h:79
apollo::cyber::base::std value
bool EnsureDirectory(const std::string &directory_path)
Check if a specified directory specified by directory_path exists. If not, recursively create the dir...
#define AERROR
Definition: log.h:44
#define AWARN
Definition: log.h:43
static double NowInSeconds()
gets the current time in second.