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 <string>
31 #include <vector>
32 
34 {
35 namespace record
36 {
37 extern pid_t process_id;
38 
39 template <typename... Ts>
40 auto start(Ts &&... xs) -> pid_t
41 {
42  const std::vector<std::string> command{
43  "python3", boost::algorithm::replace_all_copy(concealer::dollar("which ros2"), "\n", ""), "bag",
44  "record", std::forward<decltype(xs)>(xs)...};
45 
46  switch (process_id = fork()) {
47  case -1:
48  throw std::system_error(errno, std::system_category());
49 
50  case 0:
51 #ifndef OPENSCENARIO_INTERPRETER_VERBOSE_RECORD
52  if (const auto fd = ::open("/dev/null", O_WRONLY)) {
53  ::dup2(fd, STDOUT_FILENO);
54  ::close(fd);
55  }
56 #endif
57  if (concealer::execute(command) < 0) {
58  std::cerr << std::system_error(errno, std::system_category()).what() << std::endl;
59  std::exit(EXIT_FAILURE);
60  }
61  return 0;
62 
63  default:
64  return process_id;
65  }
66 }
67 
68 auto stop() -> void;
69 } // namespace record
70 } // namespace openscenario_interpreter
71 
72 #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(Ts &&... xs) -> pid_t
Definition: record.hpp:40
Definition: escape_sequence.hpp:22
Definition: junit5.hpp:25