srs-control 0.2.2
Loading...
Searching...
No Matches
srs_control.cpp
Go to the documentation of this file.
1#include "srs/Application.hpp"
6#include <CLI/CLI.hpp>
7#include <cstdlib>
8#include <exception>
9#include <fmt/format.h>
10#include <fmt/ranges.h>
11#include <format>
12#include <memory>
13#include <print>
14#include <spdlog/common.h>
15#include <spdlog/sinks/stdout_color_sinks.h>
16#include <spdlog/spdlog.h>
17#include <string>
18#include <utility>
19
20#ifdef HAS_ROOT
21#include <TROOT.h>
22#endif
23
27
31
35
36// NOLINTNEXTLINE (bugprone-exception-escape)
37auto main(int argc, char** argv) -> int
38{
39 auto cli_args = CLI::App{ "SRS system command line interface" };
40 try
41 {
42 argv = cli_args.ensure_utf8(argv);
43
44 auto app_config = srs::Config{};
45
46 auto spdlog_level = spdlog::level::info;
48 auto is_version_print = false;
49 auto is_root_version_print = false;
50 auto is_dump_needed = false;
51 auto runtime_sec = std::size_t{ 0 };
52 const auto home_dir = []() -> std::string
53 {
54 const auto* home_var = getenv("HOME");
55 if (home_var != nullptr)
56 {
57 return std::string{ home_var };
58 }
59 return {};
60 }();
61 auto json_filepath = home_dir.empty() ? "" : srs::common::get_default_config_path().string();
62 auto action_mode_enum = srs::common::ActionMode::all;
63
64 auto dump_config_callback = [&json_filepath, &is_dump_needed](const std::string& filename)
65 {
66 is_dump_needed = true;
67 json_filepath = filename;
68 };
69 auto set_config_callback = [&app_config](const std::string& filename)
70 {
71 auto is_not_ok = srs::config::set_config_from_file(app_config, filename, true);
72 if (is_not_ok)
73 {
74 throw CLI::ValidationError("--config-file", is_not_ok.value());
75 }
76 };
77
78 cli_args.add_flag("-v, --version", is_version_print, "Show the current version");
79 cli_args.add_flag("--root-version", is_root_version_print, "Show the ROOT version if used");
80
81 cli_args
82 .add_option(
83 "-a, --action",
84 action_mode_enum,
85 fmt::format("Set the action of program\nAvailable options: [{}]", fmt::join(ACTION_MODE_NAMES, ", ")))
86 ->transform(CLI::CheckedTransformer(action_mode_map, CLI::ignore_case).description(""))
87 ->default_str(get_enum_dashed_name(action_mode_enum));
88
89 cli_args
90 .add_option("-l, --log-level",
91 spdlog_level,
92 fmt::format("Set log level\nAvailable options: [{}]", fmt::join(SPDLOG_LOG_NAMES, ", ")))
93 ->transform(CLI::CheckedTransformer(spdlog_map, CLI::ignore_case).description(""))
94 ->default_str(get_enum_dashed_name(spdlog_level));
95
96 cli_args.add_option("-r, --run-time", runtime_sec, "Set the runtime of the application. Non-stop if 0")
97 ->expected(0, 1)
98 ->capture_default_str();
99
100 cli_args
101 .add_option("-p, --print-mode",
102 print_mode,
103 fmt::format("Set data print mode\nAvailable options: [{}]", fmt::join(PRINT_MODE_NAMES, ", ")))
104 ->transform(CLI::CheckedTransformer(print_mode_map, CLI::ignore_case).description(""))
105 ->default_str(get_enum_dashed_name(print_mode));
106
107 cli_args
108 .add_option_function<std::string>(
109 "-c, --config-file", set_config_callback, "Configure the application with the JSON config file")
110 ->expected(0, 1)
111 ->default_val(json_filepath);
112 cli_args
113 .add_option(
114 "-s, --split-output", app_config.output_split, "Splitting the output data into different files.")
115 ->capture_default_str();
116 cli_args
117 .add_option_function<std::string>(
118 "--dump-config", dump_config_callback, "Dump default configuration to the file")
119 ->default_val(json_filepath)
120 ->run_callback_for_default()
121 ->expected(0, 1);
122
123 cli_args.add_option("-o, --output-files", app_config.output_filenames, "Set output file (or socket) names")
124 ->capture_default_str()
125 ->expected(0, -1);
126 cli_args.parse(argc, argv);
127
128 if (is_version_print)
129 {
130 std::println("{}", SRS_PROJECT_VERSION);
131 return 0;
132 }
133
134 if (is_dump_needed)
135 {
136 auto is_not_ok = srs::config::output_config_to_file(app_config, json_filepath);
137 if (is_not_ok)
138 {
139 spdlog::error("{}", is_not_ok.value());
140 return EXIT_FAILURE;
141 }
142 return EXIT_SUCCESS;
143 }
144
145 if (is_root_version_print)
146 {
147#ifdef HAS_ROOT
148 std::println("{}", gROOT->GetVersion());
149#else
150 std::println("ROOT is not built");
151#endif
152 return EXIT_FAILURE;
153 }
154
155 auto app = srs::App{ std::move(app_config) };
156
157 for (auto sink : spdlog::default_logger()->sinks())
158 {
159 if (auto console_sink = std::dynamic_pointer_cast<spdlog::sinks::stdout_color_sink_mt>(sink);
160 console_sink != nullptr)
161 {
162 console_sink->set_level(spdlog_level);
163 }
164 }
165
166 app.set_print_mode(print_mode);
167 app.set_runtime_sec(runtime_sec);
168
169 app.init();
170
171 switch (action_mode_enum)
172 {
173 using enum srs::common::ActionMode;
174 case all:
175 {
176 app.start_workflow();
177 app.read_data();
178 app.switch_on();
179 app.wait_for_workflow();
180 break;
181 }
182 case acq_on:
183 {
184 app.switch_on();
185 break;
186 }
187 case acq_off:
188 {
189 app.switch_off();
190 break;
191 }
192 case read_only:
193 {
194
195 app.start_workflow();
196 app.read_data();
197 app.wait_for_workflow();
198 break;
199 }
200 default:
201 return 0;
202 }
203
204 // app.wait_for_finish();
205 }
206 catch (const CLI::ParseError& e)
207 {
208 cli_args.exit(e);
209 }
210 catch (std::exception& ex)
211 {
212 spdlog::critical("Exception occurred: {}", ex.what());
213 }
214 catch (...)
215 {
216 spdlog::critical("A unrecognized exception occurred!");
217 }
218
219 return 0;
220}
The primary interface class of SRS-Control.
void set_print_mode(common::DataPrintMode mode)
Set the print mode.
constexpr auto get_enum_dashed_names()
constexpr auto get_enum_dashed_name()
@ print_speed
Print the data reading rate.
auto get_default_config_path() -> std::filesystem::path
auto output_config_to_file(const auto &config, std::string_view filename, bool is_overwrite=false) -> std::optional< std::string >
auto set_config_from_file(auto &config, std::string_view filename, bool is_default_generated=true) -> std::optional< std::string >
constexpr auto ACTION_MODE_NAMES
const auto action_mode_map
constexpr auto get_enum_dashed_names()
constexpr auto PRINT_MODE_NAMES
constexpr auto get_enum_dashed_name()
const auto print_mode_map
auto main(int argc, char **argv) -> int
constexpr auto get_enum_dash_map()
const auto spdlog_map
constexpr auto SPDLOG_LOG_NAMES
Main configuration struct.