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 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 67 | try { |
| 68 | spdlog::set_level(parseLogLevel("Generic", args["--log-level"])); |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 69 | spdlog::get("main")->debug("Opening DBus connection"); |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame^] | 70 | g_dbusConnection = sdbus::createSystemBusConnection(); |
| 71 | |
| 72 | // Gracefully leave dbus event loop on SIGTERM |
| 73 | struct sigaction sigact; |
| 74 | memset(&sigact, 0, sizeof(sigact)); |
| 75 | 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 |
| 76 | sigact.sa_flags = SA_SIGINFO; |
| 77 | sigaction(SIGTERM, &sigact, nullptr); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 78 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 79 | spdlog::get("main")->debug("Starting DBus event loop"); |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame^] | 80 | std::thread eventLoop([] { g_dbusConnection->enterEventLoop(); }); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 81 | |
| 82 | auto manager = std::make_shared<velia::StateManager>(); |
| 83 | |
| 84 | // output configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 85 | spdlog::get("main")->debug("Initializing LED drivers"); |
Tomáš Pecka | 7ff533b | 2020-07-17 15:01:16 +0200 | [diff] [blame] | 86 | 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] | 87 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"), |
| 88 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"), |
Tomáš Pecka | 7ff533b | 2020-07-17 15:01:16 +0200 | [diff] [blame] | 89 | std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/")))); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 90 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 91 | spdlog::get("main")->debug("All outputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 92 | |
| 93 | // input configuration |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 94 | spdlog::get("main")->debug("Starting DBus systemd watcher"); |
Tomáš Pecka | e8239a7 | 2020-07-17 11:29:22 +0200 | [diff] [blame^] | 95 | auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *g_dbusConnection); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 96 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 97 | spdlog::get("main")->debug("All inputs initialized."); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 98 | |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 99 | eventLoop.join(); |
| 100 | |
Tomáš Pecka | 31caebd | 2020-08-21 11:14:10 +0200 | [diff] [blame] | 101 | spdlog::get("main")->debug("Shutting down"); |
Tomáš Pecka | a193c03 | 2020-06-22 17:33:31 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | } catch (std::exception& e) { |
| 104 | velia::utils::fatalException(spdlog::get("main"), e, "main"); |
| 105 | } |
Tomáš Pecka | 5123e00 | 2020-06-05 15:29:21 +0200 | [diff] [blame] | 106 | } |