blob: 931e673a16757f062514e91f6269f07a73cb1d44 [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>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020038 veliad (-h | --help)
39 veliad --version
40
41Options:
42 -h --help Show this screen.
43 --version Show version.
44 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020045 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
46 4 -> debug, 5 -> trace)
47)";
48
49int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020050{
Tomáš Peckaa4336812020-07-24 15:32:58 +020051 std::shared_ptr<spdlog::sinks::sink> loggingSink;
52 if (velia::utils::isJournaldActive()) {
53 loggingSink = velia::utils::create_journald_sink();
54 } else {
55 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
56 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020057
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"]));
Tomáš Pecka31caebd2020-08-21 11:14:10 +020065 spdlog::get("main")->debug("Opening DBus connection");
Tomáš Peckaa193c032020-06-22 17:33:31 +020066 auto dbusConnection = sdbus::createSystemBusConnection();
67
Tomáš Pecka31caebd2020-08-21 11:14:10 +020068 spdlog::get("main")->debug("Starting DBus event loop");
Tomáš Peckaa193c032020-06-22 17:33:31 +020069 std::thread eventLoop([&dbusConnection]() { dbusConnection->enterEventLoop(); });
70
71 auto manager = std::make_shared<velia::StateManager>();
72
73 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020074 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Peckaa193c032020-06-22 17:33:31 +020075 manager->m_outputSignal.connect(velia::LedOutputCallback(
76 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
77 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
78 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/")));
79
Tomáš Pecka31caebd2020-08-21 11:14:10 +020080 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020081
82 // input configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020083 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaa193c032020-06-22 17:33:31 +020084 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *dbusConnection);
85
Tomáš Pecka31caebd2020-08-21 11:14:10 +020086 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020087
88 // TODO: Gracefully leave dbus event loop on SIGTERM
89 // dbusConnection->leaveEventLoop();
90 eventLoop.join();
91
Tomáš Pecka31caebd2020-08-21 11:14:10 +020092 spdlog::get("main")->debug("Shutting down");
Tomáš Peckaa193c032020-06-22 17:33:31 +020093 return 0;
94 } catch (std::exception& e) {
95 velia::utils::fatalException(spdlog::get("main"), e, "main");
96 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020097}