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