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