Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame^] | 1 | #include <docopt.h> |
| 2 | #include <future> |
| 3 | #include <sdbus-c++/sdbus-c++.h> |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 4 | #include <spdlog/sinks/ansicolor_sink.h> |
| 5 | #include <spdlog/spdlog.h> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame^] | 6 | #include "VELIA_VERSION.h" |
| 7 | #include "inputs/DbusSystemdInput.h" |
| 8 | #include "manager/StateManager.h" |
| 9 | #include "outputs/LedSysfsDriver.h" |
| 10 | #include "outputs/callables.h" |
| 11 | #include "utils/exceptions.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 12 | #include "utils/log-init.h" |
| 13 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame^] | 14 | /** @short Extract log level from a CLI option */ |
| 15 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 16 | { |
| 17 | long x; |
| 18 | try { |
| 19 | x = option.asLong(); |
| 20 | } catch (std::invalid_argument&) { |
| 21 | throw std::runtime_error(name + " log level: expecting integer"); |
| 22 | } |
| 23 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 24 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 25 | if (x < 0 || x > 5) |
| 26 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 27 | |
| 28 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 29 | } |
| 30 | |
| 31 | static const char usage[] = |
| 32 | R"(Monitor system health status |
| 33 | |
| 34 | Usage: |
| 35 | veliad |
| 36 | [--log-level=<Level>] |
| 37 | [--manager-log-level=<Level>] |
| 38 | [--input-log-level=<Level>] |
| 39 | [--output-log-level=<Level>] |
| 40 | veliad (-h | --help) |
| 41 | veliad --version |
| 42 | |
| 43 | Options: |
| 44 | -h --help Show this screen. |
| 45 | --version Show version. |
| 46 | --log-level=<N> Log level for everything [default: 3] |
| 47 | --manager-log-level=<N> Log level for manager [default: 3] |
| 48 | --input-log-level=<N> Log level for the input providers [default: 3] |
| 49 | --output-log-level=<N> Log level for the output providers [default: 3] |
| 50 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 51 | 4 -> debug, 5 -> trace) |
| 52 | )"; |
| 53 | |
| 54 | int main(int argc, char* argv[]) |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 55 | { |
| 56 | std::shared_ptr<spdlog::sinks::sink> loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 57 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame^] | 58 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true); |
| 59 | |
Tomáš Pecka | bd82886 | 2020-06-15 15:53:35 +0200 | [diff] [blame] | 60 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 61 | spdlog::set_level(spdlog::level::info); |
| 62 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame^] | 63 | try { |
| 64 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
| 65 | // nothing for "main", that just inherit the global stuff |
| 66 | spdlog::get("manager")->set_level(parseLogLevel("Manager subsystem", args["--manager-log-level"])); |
| 67 | spdlog::get("input")->set_level(parseLogLevel("Input subsystem", args["--input-log-level"])); |
| 68 | spdlog::get("output")->set_level(parseLogLevel("Output subsystem", args["--output-log-level"])); |
| 69 | // FIXME: the default values actually mean that nothing is propagated from the generic --log-level if passed... |
| 70 | |
| 71 | spdlog::get("input")->debug("Opening DBus connection"); |
| 72 | auto dbusConnection = sdbus::createSystemBusConnection(); |
| 73 | |
| 74 | spdlog::get("input")->debug("Starting DBus event loop"); |
| 75 | std::thread eventLoop([&dbusConnection]() { dbusConnection->enterEventLoop(); }); |
| 76 | |
| 77 | auto manager = std::make_shared<velia::StateManager>(); |
| 78 | |
| 79 | // output configuration |
| 80 | spdlog::get("output")->debug("Initializing LED drivers"); |
| 81 | manager->m_outputSignal.connect(velia::LedOutputCallback( |
| 82 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"), |
| 83 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"), |
| 84 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/"))); |
| 85 | |
| 86 | spdlog::get("input")->debug("All outputs initialized."); |
| 87 | |
| 88 | // input configuration |
| 89 | spdlog::get("input")->debug("Starting DBus systemd watcher"); |
| 90 | auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *dbusConnection); |
| 91 | |
| 92 | spdlog::get("input")->debug("All inputs initialized."); |
| 93 | |
| 94 | // TODO: Gracefully leave dbus event loop on SIGTERM |
| 95 | // dbusConnection->leaveEventLoop(); |
| 96 | eventLoop.join(); |
| 97 | |
| 98 | spdlog::get("manager")->debug("Shutting down"); |
| 99 | return 0; |
| 100 | } catch (std::exception& e) { |
| 101 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 102 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 103 | } |