blob: 9aaf901de82c3daedd02c9c673754f0052507a7c [file] [log] [blame]
Tomáš Peckae8239a72020-07-17 11:29:22 +02001#include <csignal>
Tomáš Peckaa193c032020-06-22 17:33:31 +02002#include <docopt.h>
3#include <future>
4#include <sdbus-c++/sdbus-c++.h>
Tomáš Pecka5123e002020-06-05 15:29:21 +02005#include <spdlog/sinks/ansicolor_sink.h>
6#include <spdlog/spdlog.h>
Tomáš Peckaa193c032020-06-22 17:33:31 +02007#include "VELIA_VERSION.h"
8#include "inputs/DbusSystemdInput.h"
9#include "manager/StateManager.h"
10#include "outputs/LedSysfsDriver.h"
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020011#include "outputs/SlotWrapper.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020012#include "outputs/callables.h"
13#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020014#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020015#include "utils/log-init.h"
16
Tomáš Peckaa193c032020-06-22 17:33:31 +020017/** @short Extract log level from a CLI option */
18spdlog::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
34static const char usage[] =
35 R"(Monitor system health status
36
37Usage:
38 veliad
39 [--log-level=<Level>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020040 veliad (-h | --help)
41 veliad --version
42
43Options:
44 -h --help Show this screen.
45 --version Show version.
46 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020047 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
48 4 -> debug, 5 -> trace)
49)";
50
Tomáš Peckae8239a72020-07-17 11:29:22 +020051std::unique_ptr<sdbus::IConnection> g_dbusConnection;
52
Tomáš Peckaa193c032020-06-22 17:33:31 +020053int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020054{
Tomáš Peckaa4336812020-07-24 15:32:58 +020055 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áš Pecka5123e002020-06-05 15:29:21 +020061
Tomáš Peckaa193c032020-06-22 17:33:31 +020062 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
63
Tomáš Peckabd828862020-06-15 15:53:35 +020064 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020065 spdlog::set_level(spdlog::level::info);
66
Tomáš Peckaa193c032020-06-22 17:33:31 +020067 try {
68 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka31caebd2020-08-21 11:14:10 +020069 spdlog::get("main")->debug("Opening DBus connection");
Tomáš Peckae8239a72020-07-17 11:29:22 +020070 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áš Peckaa193c032020-06-22 17:33:31 +020078
Tomáš Pecka31caebd2020-08-21 11:14:10 +020079 spdlog::get("main")->debug("Starting DBus event loop");
Tomáš Peckae8239a72020-07-17 11:29:22 +020080 std::thread eventLoop([] { g_dbusConnection->enterEventLoop(); });
Tomáš Peckaa193c032020-06-22 17:33:31 +020081
82 auto manager = std::make_shared<velia::StateManager>();
83
84 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020085 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020086 manager->m_outputSignal.connect(velia::boost::signals2::SlotWrapper<void, velia::State>(std::make_shared<velia::LedOutputCallback>(
Tomáš Peckaa193c032020-06-22 17:33:31 +020087 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
88 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020089 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/"))));
Tomáš Peckaa193c032020-06-22 17:33:31 +020090
Tomáš Pecka31caebd2020-08-21 11:14:10 +020091 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020092
93 // input configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020094 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckae8239a72020-07-17 11:29:22 +020095 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +020096
Tomáš Pecka31caebd2020-08-21 11:14:10 +020097 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020098
Tomáš Peckaa193c032020-06-22 17:33:31 +020099 eventLoop.join();
100
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200101 spdlog::get("main")->debug("Shutting down");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200102 return 0;
103 } catch (std::exception& e) {
104 velia::utils::fatalException(spdlog::get("main"), e, "main");
105 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200106}