Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 1 | #include <csignal> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 2 | #include <docopt.h> |
| 3 | #include <future> |
| 4 | #include <sdbus-c++/sdbus-c++.h> |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 5 | #include <spdlog/sinks/ansicolor_sink.h> |
| 6 | #include <spdlog/spdlog.h> |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 7 | #include "VELIA_VERSION.h" |
| 8 | #include "inputs/DbusSystemdInput.h" |
| 9 | #include "manager/StateManager.h" |
| 10 | #include "outputs/LedSysfsDriver.h" |
Tomáš Pecka | 7ff533b | 2020-07-17 15:01:16 +0200 | [diff] [blame] | 11 | #include "outputs/SlotWrapper.h" |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 12 | #include "outputs/callables.h" |
| 13 | #include "utils/exceptions.h" |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 14 | #include "utils/journal.h" |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 15 | #include "utils/log-init.h" |
| 16 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 17 | /** @short Extract log level from a CLI option */ |
| 18 | spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option) |
| 19 | { |
| 20 | long x; |
| 21 | try { |
| 22 | x = option.asLong(); |
| 23 | } catch (std::invalid_argument&) { |
| 24 | throw std::runtime_error(name + " log level: expecting integer"); |
| 25 | } |
| 26 | static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed"); |
| 27 | static_assert(spdlog::level::off == 6, "spdlog::level levels have changed"); |
| 28 | if (x < 0 || x > 5) |
| 29 | throw std::runtime_error(name + " log level invalid or out-of-range"); |
| 30 | |
| 31 | return static_cast<spdlog::level::level_enum>(5 - x); |
| 32 | } |
| 33 | |
| 34 | static const char usage[] = |
| 35 | R"(Monitor system health status |
| 36 | |
| 37 | Usage: |
| 38 | veliad |
| 39 | [--log-level=<Level>] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 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] |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 47 | (0 -> critical, 1 -> error, 2 -> warning, 3 -> info, |
| 48 | 4 -> debug, 5 -> trace) |
| 49 | )"; |
| 50 | |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 51 | std::unique_ptr<sdbus::IConnection> g_dbusConnection; |
| 52 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 53 | int main(int argc, char* argv[]) |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 54 | { |
Tomáš Pecka | a433681 | 2020-07-24 15:32:58 +0200 | [diff] [blame] | 55 | std::shared_ptr<spdlog::sinks::sink> loggingSink; |
| 56 | if (velia::utils::isJournaldActive()) { |
| 57 | loggingSink = velia::utils::create_journald_sink(); |
| 58 | } else { |
| 59 | loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>(); |
| 60 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 61 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 62 | auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true); |
| 63 | |
Tomáš Pecka | bd82886 | 2020-06-15 15:53:35 +0200 | [diff] [blame] | 64 | velia::utils::initLogs(loggingSink); |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 65 | spdlog::set_level(spdlog::level::info); |
| 66 | |
Jan Kundrát | 8e487f0 | 2020-10-09 21:33:42 +0200 | [diff] [blame] | 67 | std::thread eventLoop; |
| 68 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 69 | try { |
| 70 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 71 | spdlog::get("main")->debug("Opening DBus connection"); |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 72 | g_dbusConnection = sdbus::createSystemBusConnection(); |
| 73 | |
| 74 | // Gracefully leave dbus event loop on SIGTERM |
| 75 | struct sigaction sigact; |
| 76 | memset(&sigact, 0, sizeof(sigact)); |
| 77 | sigact.sa_handler = [](int) { g_dbusConnection->leaveEventLoop(); }; // sdbus-c++'s implementation doesn't mind if called before entering the event loop. It simply leaves the loop on entry |
| 78 | sigact.sa_flags = SA_SIGINFO; |
| 79 | sigaction(SIGTERM, &sigact, nullptr); |
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("Starting DBus event loop"); |
Jan Kundrát | 8e487f0 | 2020-10-09 21:33:42 +0200 | [diff] [blame] | 82 | eventLoop = std::thread([] { g_dbusConnection->enterEventLoop(); }); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 83 | |
| 84 | auto manager = std::make_shared<velia::StateManager>(); |
| 85 | |
| 86 | // output configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 87 | spdlog::get("main")->debug("Initializing LED drivers"); |
Tomáš Pecka | 7ff533b | 2020-07-17 15:01:16 +0200 | [diff] [blame] | 88 | 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] | 89 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"), |
| 90 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"), |
Tomáš Pecka | 7ff533b | 2020-07-17 15:01:16 +0200 | [diff] [blame] | 91 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/")))); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 92 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 93 | spdlog::get("main")->debug("All outputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 94 | |
| 95 | // input configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 96 | spdlog::get("main")->debug("Starting DBus systemd watcher"); |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame] | 97 | auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *g_dbusConnection); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 98 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 99 | spdlog::get("main")->debug("All inputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 100 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 101 | eventLoop.join(); |
| 102 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 103 | spdlog::get("main")->debug("Shutting down"); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 104 | return 0; |
| 105 | } catch (std::exception& e) { |
| 106 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 107 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 108 | } |