blob: 341b390251f94ae250ff3ef0a0d952069b40e58f [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áš Peckaa4336812020-07-24 15:32:58 +020012#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020013#include "utils/log-init.h"
14
Tomáš Peckaa193c032020-06-22 17:33:31 +020015/** @short Extract log level from a CLI option */
16spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
17{
18 long x;
19 try {
20 x = option.asLong();
21 } catch (std::invalid_argument&) {
22 throw std::runtime_error(name + " log level: expecting integer");
23 }
24 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
25 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
26 if (x < 0 || x > 5)
27 throw std::runtime_error(name + " log level invalid or out-of-range");
28
29 return static_cast<spdlog::level::level_enum>(5 - x);
30}
31
32static const char usage[] =
33 R"(Monitor system health status
34
35Usage:
36 veliad
37 [--log-level=<Level>]
38 [--manager-log-level=<Level>]
39 [--input-log-level=<Level>]
40 [--output-log-level=<Level>]
41 veliad (-h | --help)
42 veliad --version
43
44Options:
45 -h --help Show this screen.
46 --version Show version.
47 --log-level=<N> Log level for everything [default: 3]
48 --manager-log-level=<N> Log level for manager [default: 3]
49 --input-log-level=<N> Log level for the input providers [default: 3]
50 --output-log-level=<N> Log level for the output providers [default: 3]
51 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
52 4 -> debug, 5 -> trace)
53)";
54
55int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020056{
Tomáš Peckaa4336812020-07-24 15:32:58 +020057 std::shared_ptr<spdlog::sinks::sink> loggingSink;
58 if (velia::utils::isJournaldActive()) {
59 loggingSink = velia::utils::create_journald_sink();
60 } else {
61 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
62 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020063
Tomáš Peckaa193c032020-06-22 17:33:31 +020064 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
65
Tomáš Peckabd828862020-06-15 15:53:35 +020066 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020067 spdlog::set_level(spdlog::level::info);
68
Tomáš Peckaa193c032020-06-22 17:33:31 +020069 try {
70 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
71 // nothing for "main", that just inherit the global stuff
72 spdlog::get("manager")->set_level(parseLogLevel("Manager subsystem", args["--manager-log-level"]));
73 spdlog::get("input")->set_level(parseLogLevel("Input subsystem", args["--input-log-level"]));
74 spdlog::get("output")->set_level(parseLogLevel("Output subsystem", args["--output-log-level"]));
75 // FIXME: the default values actually mean that nothing is propagated from the generic --log-level if passed...
76
77 spdlog::get("input")->debug("Opening DBus connection");
78 auto dbusConnection = sdbus::createSystemBusConnection();
79
80 spdlog::get("input")->debug("Starting DBus event loop");
81 std::thread eventLoop([&dbusConnection]() { dbusConnection->enterEventLoop(); });
82
83 auto manager = std::make_shared<velia::StateManager>();
84
85 // output configuration
86 spdlog::get("output")->debug("Initializing LED drivers");
87 manager->m_outputSignal.connect(velia::LedOutputCallback(
88 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
89 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
90 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/")));
91
92 spdlog::get("input")->debug("All outputs initialized.");
93
94 // input configuration
95 spdlog::get("input")->debug("Starting DBus systemd watcher");
96 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *dbusConnection);
97
98 spdlog::get("input")->debug("All inputs initialized.");
99
100 // TODO: Gracefully leave dbus event loop on SIGTERM
101 // dbusConnection->leaveEventLoop();
102 eventLoop.join();
103
104 spdlog::get("manager")->debug("Shutting down");
105 return 0;
106 } catch (std::exception& e) {
107 velia::utils::fatalException(spdlog::get("main"), e, "main");
108 }
Tomáš Pecka5123e002020-06-05 15:29:21 +0200109}