34 #define BOOST_ALLOW_DEPRECATED_HEADERS
36 #include <boost/lexical_cast.hpp>
37 #include <boost/uuid/uuid_generators.hpp>
38 #include <boost/uuid/uuid_io.hpp>
53 std::ostream& output = std::cout;
54 std::ostream& error = std::cerr;
61 bool allow_empty =
false;
62 bool write_output =
true;
63 bool require_success =
false;
64 bool report_progress =
true;
67 Simulation* GLOBAL_SIMULATION_INSTANCE{
nullptr};
69 template <
typename Func>
70 auto handle_cloe_error(std::ostream& out, Func f) -> decltype(f()) {
74 out <<
"Error: " << e.what() << std::endl;
75 if (e.has_explanation()) {
82 inline int run(
const RunOptions& opt,
const std::vector<std::string>& filepaths) {
83 cloe::logger::get(
"cloe")->info(
"Cloe {}", CLOE_ENGINE_VERSION);
88 if (!opt.uuid.empty()) {
90 }
else if (std::getenv(CLOE_SIMULATION_UUID_VAR) !=
nullptr) {
91 uuid = std::getenv(CLOE_SIMULATION_UUID_VAR);
93 uuid = boost::lexical_cast<std::string>(boost::uuids::random_generator()());
95 stack_opt.environment->set(CLOE_SIMULATION_UUID_VAR, uuid);
100 handle_cloe_error(*stack_opt.error, [&]() {
101 s = cloe::new_stack(stack_opt, filepaths);
102 if (!opt.allow_empty) {
103 s.check_completeness();
111 Simulation sim(s, uuid);
112 GLOBAL_SIMULATION_INSTANCE = ∼
113 std::signal(SIGINT, handle_signal);
116 sim.set_report_progress(opt.report_progress);
119 auto result = handle_cloe_error(*stack_opt.error, [&]() { return sim.run(); });
120 if (result.outcome == SimulationOutcome::NoStart) {
127 if (opt.write_output) {
128 sim.write_output(result);
130 opt.output << cloe::Json(result).dump(opt.json_indent) << std::endl;
132 switch (result.outcome) {
133 case SimulationOutcome::Success:
134 return EXIT_OUTCOME_SUCCESS;
135 case SimulationOutcome::Stopped:
136 return (opt.require_success ? EXIT_OUTCOME_STOPPED : EXIT_OUTCOME_SUCCESS);
137 case SimulationOutcome::Aborted:
138 return EXIT_OUTCOME_ABORTED;
139 case SimulationOutcome::NoStart:
140 return EXIT_OUTCOME_NOSTART;
141 case SimulationOutcome::Failure:
142 return EXIT_OUTCOME_FAILURE;
144 return EXIT_OUTCOME_UNKNOWN;
164 static size_t interrupts = 0;
172 std::cerr << std::endl;
173 if (++interrupts == 3) {
174 std::signal(sig, SIG_DFL);
176 if (GLOBAL_SIMULATION_INSTANCE) {
Definition: stack.hpp:901
Definition: simulation.hpp:102
void signal_abort()
Definition: simulation.cpp:1387
void handle_signal(int)
Definition: main_run.hpp:163
Definition: main_stack.hpp:38
Definition: main_run.hpp:51
std::string indent_string(std::string s, const std::string &indent=" ")
Definition: utility.cpp:104