blob: f905dee4ca4dd1f072d918ffa7265ba13052cf9e [file] [log] [blame]
Tomáš Peckaa193c032020-06-22 17:33:31 +02001#include <docopt.h>
2#include <future>
3#include <sdbus-c++/sdbus-c++.h>
Tomáš Pecka5123e002020-06-05 15:29:21 +02004#include <spdlog/sinks/ansicolor_sink.h>
5#include <spdlog/spdlog.h>
Tomáš Peckaa193c032020-06-22 17:33:31 +02006#include "VELIA_VERSION.h"
7#include "inputs/DbusSystemdInput.h"
8#include "manager/StateManager.h"
9#include "outputs/LedSysfsDriver.h"
10#include "outputs/callables.h"
11#include "utils/exceptions.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020012#include "utils/log-init.h"
13
Tomáš Peckaa193c032020-06-22 17:33:31 +020014/** @short Extract log level from a CLI option */
15spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
16{
17 long x;
18 try {
19 x = option.asLong();
20 } catch (std::invalid_argument&) {
21 throw std::runtime_error(name + " log level: expecting integer");
22 }
23 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
24 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
25 if (x < 0 || x > 5)
26 throw std::runtime_error(name + " log level invalid or out-of-range");
27
28 return static_cast<spdlog::level::level_enum>(5 - x);
29}
30
31static const char usage[] =
32 R"(Monitor system health status
33
34Usage:
35 veliad
36 [--log-level=<Level>]
37 [--manager-log-level=<Level>]
38 [--input-log-level=<Level>]
39 [--output-log-level=<Level>]
40 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]
47 --manager-log-level=<N> Log level for manager [default: 3]
48 --input-log-level=<N> Log level for the input providers [default: 3]
49 --output-log-level=<N> Log level for the output providers [default: 3]
50 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
51 4 -> debug, 5 -> trace)
52)";
53
54int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020055{
56 std::shared_ptr<spdlog::sinks::sink> loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
57
Tomáš Peckaa193c032020-06-22 17:33:31 +020058 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
59
Tomáš Peckabd828862020-06-15 15:53:35 +020060 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020061 spdlog::set_level(spdlog::level::info);
62
Tomáš Peckaa193c032020-06-22 17:33:31 +020063 try {
64 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
65 // nothing for "main", that just inherit the global stuff
66 spdlog::get("manager")->set_level(parseLogLevel("Manager subsystem", args["--manager-log-level"]));
67 spdlog::get("input")->set_level(parseLogLevel("Input subsystem", args["--input-log-level"]));
68 spdlog::get("output")->set_level(parseLogLevel("Output subsystem", args["--output-log-level"]));
69 // FIXME: the default values actually mean that nothing is propagated from the generic --log-level if passed...
70
71 spdlog::get("input")->debug("Opening DBus connection");
72 auto dbusConnection = sdbus::createSystemBusConnection();
73
74 spdlog::get("input")->debug("Starting DBus event loop");
75 std::thread eventLoop([&dbusConnection]() { dbusConnection->enterEventLoop(); });
76
77 auto manager = std::make_shared<velia::StateManager>();
78
79 // output configuration
80 spdlog::get("output")->debug("Initializing LED drivers");
81 manager->m_outputSignal.connect(velia::LedOutputCallback(
82 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
83 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
84 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/")));
85
86 spdlog::get("input")->debug("All outputs initialized.");
87
88 // input configuration
89 spdlog::get("input")->debug("Starting DBus systemd watcher");
90 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *dbusConnection);
91
92 spdlog::get("input")->debug("All inputs initialized.");
93
94 // TODO: Gracefully leave dbus event loop on SIGTERM
95 // dbusConnection->leaveEventLoop();
96 eventLoop.join();
97
98 spdlog::get("manager")->debug("Shutting down");
99 return 0;
100 } catch (std::exception& e) {
101 velia::utils::fatalException(spdlog::get("main"), e, "main");
102 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200103}