scenario_simulator_v2 C++ API
record.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
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 #ifndef OPENSCENARIO_INTERPRETER__RECORD_HPP_
16 #define OPENSCENARIO_INTERPRETER__RECORD_HPP_
17 
18 #include <unistd.h>
19 
20 #ifndef OPENSCENARIO_INTERPRETER_VERBOSE_RECORD
21 #include <fcntl.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #endif // OPENSCENARIO_INTERPRETER_RECORD_QUIETLY
25 
26 #include <boost/algorithm/string.hpp> // boost::algorithm::replace_all_copy
27 #include <concealer/execute.hpp>
28 #include <cstdlib>
29 #include <iostream>
30 #include <rclcpp/logging.hpp>
31 #include <string>
32 #include <vector>
33 
35 {
36 namespace record
37 {
38 extern pid_t process_id;
39 
40 inline auto start(const std::vector<std::string> & args) -> pid_t
41 {
42  std::vector<std::string> command{
43  "python3", boost::algorithm::replace_all_copy(concealer::dollar("which ros2"), "\n", ""), "bag",
44  "record"};
45 
46  command.insert(command.end(), args.begin(), args.end());
47 
48  switch (process_id = fork()) {
49  case -1:
50  throw std::system_error(errno, std::system_category());
51 
52  case 0:
53 #ifndef OPENSCENARIO_INTERPRETER_VERBOSE_RECORD
54  if (const auto fd = ::open("/dev/null", O_WRONLY)) {
55  ::dup2(fd, STDOUT_FILENO);
56  ::close(fd);
57  }
58 #endif
59  if (concealer::execute(command) < 0) {
60  RCLCPP_ERROR_STREAM(
61  rclcpp::get_logger("record"), std::system_error(errno, std::system_category()).what());
62  std::exit(EXIT_FAILURE);
63  }
64  return 0;
65 
66  default:
67  return process_id;
68  }
69 }
70 
71 auto stop() -> void;
72 } // namespace record
73 } // namespace openscenario_interpreter
74 
75 #endif // OPENSCENARIO_INTERPRETER__RECORD_HPP_
auto dollar(const std::string &command) -> std::string
Definition: execute.cpp:57
auto execute(const std::vector< std::string > &) -> int
Definition: execute.cpp:36
auto stop() -> void
Definition: record.cpp:27
pid_t process_id
Definition: record.cpp:25
auto start(const std::vector< std::string > &args) -> pid_t
Definition: record.hpp:40
Definition: hypot.hpp:22