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