blob: b5e5a13ffb8627b01bca563de374974b4a077e79 [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
Jan Kundrát8e487f02020-10-09 21:33:42 +020067 std::thread eventLoop;
68
Tomáš Peckaa193c032020-06-22 17:33:31 +020069 try {
70 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka31caebd2020-08-21 11:14:10 +020071 spdlog::get("main")->debug("Opening DBus connection");
Tomáš Peckae8239a72020-07-17 11:29:22 +020072 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áš Peckaa193c032020-06-22 17:33:31 +020080
Tomáš Pecka31caebd2020-08-21 11:14:10 +020081 spdlog::get("main")->debug("Starting DBus event loop");
Jan Kundrát8e487f02020-10-09 21:33:42 +020082 eventLoop = std::thread([] { g_dbusConnection->enterEventLoop(); });
Tomáš Peckaa193c032020-06-22 17:33:31 +020083
84 auto manager = std::make_shared<velia::StateManager>();
85
86 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020087 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020088 manager->m_outputSignal.connect(velia::boost::signals2::SlotWrapper<void, velia::State>(std::make_shared<velia::LedOutputCallback>(
Tomáš Peckaa193c032020-06-22 17:33:31 +020089 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
90 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020091 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/"))));
Tomáš Peckaa193c032020-06-22 17:33:31 +020092
Tomáš Pecka31caebd2020-08-21 11:14:10 +020093 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020094
95 // input configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020096 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckae8239a72020-07-17 11:29:22 +020097 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *g_dbusConnection);
Tomáš Peckaa193c032020-06-22 17:33:31 +020098
Tomáš Pecka31caebd2020-08-21 11:14:10 +020099 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200100
Tomáš Peckaa193c032020-06-22 17:33:31 +0200101 eventLoop.join();
102
Tomáš Pecka31caebd2020-08-21 11:14:10 +0200103 spdlog::get("main")->debug("Shutting down");
Tomáš Peckaa193c032020-06-22 17:33:31 +0200104 return 0;
105 } catch (std::exception& e) {
106 velia::utils::fatalException(spdlog::get("main"), e, "main");
107 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200108}