Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 1 | #include <docopt.h> |
| 2 | #include <spdlog/sinks/ansicolor_sink.h> |
| 3 | #include <spdlog/spdlog.h> |
| 4 | #include <sysrepo-cpp/Session.hpp> |
| 5 | #include "VELIA_VERSION.h" |
| 6 | #include "main.h" |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame^] | 7 | #include "system/IETFSystem.h" |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 8 | #include "utils/exceptions.h" |
| 9 | #include "utils/journal.h" |
| 10 | #include "utils/log-init.h" |
| 11 | |
| 12 | /** @short Extract log level from a CLI option */ |
| 13 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 14 | { |
| 15 | long x; |
| 16 | try { |
| 17 | x = option.asLong(); |
| 18 | } catch (std::invalid_argument&) { |
| 19 | throw std::runtime_error(name + " log level: expecting integer"); |
| 20 | } |
| 21 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 22 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 23 | if (x < 0 || x > 5) |
| 24 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 25 | |
| 26 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 27 | } |
| 28 | |
| 29 | static const char usage[] = |
| 30 | R"(Sysrepo-powered system management. |
| 31 | |
| 32 | Usage: |
| 33 | veliad-system |
| 34 | [--log-level=<Level>] |
| 35 | [--sysrepo-log-level=<Level>] |
| 36 | [--system-log-level=<Level>] |
| 37 | veliad-system (-h | --help) |
| 38 | veliad-system --version |
| 39 | |
| 40 | Options: |
| 41 | -h --help Show this screen. |
| 42 | --version Show version. |
| 43 | --log-level=<N> Log level for everything [default: 3] |
| 44 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 45 | 4 -> debug, 5 -> trace) |
| 46 | --sysrepo-log-level=<N> Log level for the sysrepo library [default: 3] |
| 47 | --system-log-level=<N> Log level for the system stuff [default: 3] |
| 48 | )"; |
| 49 | |
| 50 | DBUS_EVENTLOOP_INIT |
| 51 | |
| 52 | int main(int argc, char* argv[]) |
| 53 | { |
| 54 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 55 | if (velia::utils::isJournaldActive()) { |
| 56 | loggingSink = velia::utils::create_journald_sink(); |
| 57 | } else { |
| 58 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 59 | } |
| 60 | |
| 61 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-system " VELIA_VERSION, true); |
| 62 | |
| 63 | velia::utils::initLogs(loggingSink); |
| 64 | spdlog::set_level(spdlog::level::info); |
| 65 | |
| 66 | try { |
| 67 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
| 68 | spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"])); |
| 69 | |
| 70 | spdlog::get("main")->debug("Opening Sysrepo connection"); |
| 71 | auto srConn = std::make_shared<sysrepo::Connection>(); |
| 72 | auto srSess = std::make_shared<sysrepo::Session>(srConn); |
| 73 | |
| 74 | DBUS_EVENTLOOP_START |
| 75 | |
| 76 | // initialize ietf-system |
| 77 | spdlog::get("main")->debug("Initializing Sysrepo for system models"); |
Tomáš Pecka | f976c5b | 2021-01-23 21:19:52 +0100 | [diff] [blame^] | 78 | auto sysrepoIETFSystem = velia::system::IETFSystem(srSess, "/etc/os-release"); |
Tomáš Pecka | 625b7a1 | 2021-01-23 20:55:51 +0100 | [diff] [blame] | 79 | |
| 80 | DBUS_EVENTLOOP_END |
| 81 | return 0; |
| 82 | } catch (std::exception& e) { |
| 83 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 84 | } |
| 85 | } |