SRS-control 0.1.4
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "CLIOptionsMap.hpp"
2#include <CLI/CLI.hpp>
3#include <print>
4#include <spdlog/spdlog.h>
5#include <srs/Application.hpp>
7
8#ifdef HAS_ROOT
9#include <TROOT.h>
10#endif
11
12auto main(int argc, char** argv) -> int
13{
14 auto cli_args = CLI::App{ "SRS system command line interface" };
15 try
16 {
17 argv = cli_args.ensure_utf8(argv);
18
19 auto app_config = srs::Config{};
20
21 auto spdlog_level = spdlog::level::info;
23 auto output_filenames = std::vector<std::string>{ "output.bin" };
24 auto is_version_print = false;
25 auto is_root_version_print = false;
26 auto is_dump_needed = false;
27 const auto home_dir = std::string_view{ getenv("HOME") };
28 auto json_filepath = home_dir.empty() ? "" : std::format("{}/.config/srs-control/config.json", getenv("HOME"));
29 auto dump_config_callback = [&json_filepath, &is_dump_needed](const std::string& filename)
30 {
31 is_dump_needed = true;
32 json_filepath = filename;
33 };
34
35 cli_args.add_flag("-v, --version", is_version_print, "Show the current version");
36 cli_args.add_flag("--root-version", is_root_version_print, "Show the ROOT version if used");
37 cli_args.add_option("-l, --log-level", spdlog_level, "Set log level")
38 ->transform(CLI::CheckedTransformer(spd_log_map, CLI::ignore_case))
39 ->capture_default_str();
40 cli_args.add_option("-p, --print-mode", print_mode, "Set data print mode")
41 ->transform(CLI::CheckedTransformer(print_mode_map, CLI::ignore_case))
42 ->capture_default_str();
43 cli_args.add_option("-c, --config-file", json_filepath, "Set the path of the JSON config file")
44 ->capture_default_str();
45 cli_args
46 .add_option_function<std::string>(
47 "--dump-config", dump_config_callback, "Dump default configuration to the file")
48 ->default_val(json_filepath)
49 ->run_callback_for_default()
50 ->expected(0, 1);
51
52 cli_args.add_option("-o, --output-files", output_filenames, "Set output file (or socket) names")
53 ->capture_default_str()
54 ->expected(0, -1);
55 cli_args.parse(argc, argv);
56
57 if (is_version_print)
58 {
59 std::println("{}", SRS_PROJECT_VERSION);
60 return 0;
61 }
62
63 if (is_dump_needed)
64 {
65 srs::config::output_config_to_json(app_config, json_filepath);
66 return 0;
67 }
68
69 if (is_root_version_print)
70 {
71#ifdef HAS_ROOT
72 std::println("{}", gROOT->GetVersion());
73#else
74 std::println("ROOT is not built");
75#endif
76 return 0;
77 }
78
79 spdlog::set_level(spdlog_level);
80
81 auto app = srs::App{};
82
83 srs::config::set_config_from_json(app_config, json_filepath);
84 app.set_options(std::move(app_config));
85 // app.set_remote_endpoint(srs::common::DEFAULT_SRS_IP, srs::common::DEFAULT_SRS_CONTROL_PORT);
86 app.set_print_mode(print_mode);
87 app.set_output_filenames(output_filenames);
88
89 app.init();
90 app.read_data();
91 app.switch_on();
92 app.start_workflow();
93 // app.wait_for_finish();
94 }
95 catch (const CLI::ParseError& e)
96 {
97 cli_args.exit(e);
98 }
99 catch (std::exception& ex)
100 {
101 spdlog::critical("Exception occured: {}", ex.what());
102 }
103
104 return 0;
105}
const auto print_mode_map
const auto spd_log_map
auto main() -> int
Definition main.cpp:52
void output_config_to_json(const Config &app_config, std::string_view json_filename)
void set_config_from_json(Config &app_config, std::string_view json_filename)