blob: 18d92b01e83cc5cf7e682d68bfbf7a550755dae5 [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"
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020010#include "outputs/SlotWrapper.h"
Tomáš Peckaa193c032020-06-22 17:33:31 +020011#include "outputs/callables.h"
12#include "utils/exceptions.h"
Tomáš Peckaa4336812020-07-24 15:32:58 +020013#include "utils/journal.h"
Tomáš Pecka5123e002020-06-05 15:29:21 +020014#include "utils/log-init.h"
15
Tomáš Peckaa193c032020-06-22 17:33:31 +020016/** @short Extract log level from a CLI option */
17spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
18{
19 long x;
20 try {
21 x = option.asLong();
22 } catch (std::invalid_argument&) {
23 throw std::runtime_error(name + " log level: expecting integer");
24 }
25 static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
26 static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
27 if (x < 0 || x > 5)
28 throw std::runtime_error(name + " log level invalid or out-of-range");
29
30 return static_cast<spdlog::level::level_enum>(5 - x);
31}
32
33static const char usage[] =
34 R"(Monitor system health status
35
36Usage:
37 veliad
38 [--log-level=<Level>]
Tomáš Peckaa193c032020-06-22 17:33:31 +020039 veliad (-h | --help)
40 veliad --version
41
42Options:
43 -h --help Show this screen.
44 --version Show version.
45 --log-level=<N> Log level for everything [default: 3]
Tomáš Peckaa193c032020-06-22 17:33:31 +020046 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
47 4 -> debug, 5 -> trace)
48)";
49
50int main(int argc, char* argv[])
Tomáš Pecka5123e002020-06-05 15:29:21 +020051{
Tomáš Peckaa4336812020-07-24 15:32:58 +020052 std::shared_ptr<spdlog::sinks::sink> loggingSink;
53 if (velia::utils::isJournaldActive()) {
54 loggingSink = velia::utils::create_journald_sink();
55 } else {
56 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
57 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020058
Tomáš Peckaa193c032020-06-22 17:33:31 +020059 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad " VELIA_VERSION, true);
60
Tomáš Peckabd828862020-06-15 15:53:35 +020061 velia::utils::initLogs(loggingSink);
Tomáš Pecka5123e002020-06-05 15:29:21 +020062 spdlog::set_level(spdlog::level::info);
63
Tomáš Peckaa193c032020-06-22 17:33:31 +020064 try {
65 spdlog::set_level(parseLogLevel("Generic", args["--log-level"]));
Tomáš Pecka31caebd2020-08-21 11:14:10 +020066 spdlog::get("main")->debug("Opening DBus connection");
Tomáš Peckaa193c032020-06-22 17:33:31 +020067 auto dbusConnection = sdbus::createSystemBusConnection();
68
Tomáš Pecka31caebd2020-08-21 11:14:10 +020069 spdlog::get("main")->debug("Starting DBus event loop");
Tomáš Peckaa193c032020-06-22 17:33:31 +020070 std::thread eventLoop([&dbusConnection]() { dbusConnection->enterEventLoop(); });
71
72 auto manager = std::make_shared<velia::StateManager>();
73
74 // output configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020075 spdlog::get("main")->debug("Initializing LED drivers");
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020076 manager->m_outputSignal.connect(velia::boost::signals2::SlotWrapper<void, velia::State>(std::make_shared<velia::LedOutputCallback>(
Tomáš Peckaa193c032020-06-22 17:33:31 +020077 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:red/"),
78 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:green/"),
Tomáš Pecka7ff533b2020-07-17 15:01:16 +020079 std::make_shared<velia::LedSysfsDriver>("/sys/class/leds/status:blue/"))));
Tomáš Peckaa193c032020-06-22 17:33:31 +020080
Tomáš Pecka31caebd2020-08-21 11:14:10 +020081 spdlog::get("main")->debug("All outputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020082
83 // input configuration
Tomáš Pecka31caebd2020-08-21 11:14:10 +020084 spdlog::get("main")->debug("Starting DBus systemd watcher");
Tomáš Peckaa193c032020-06-22 17:33:31 +020085 auto inputSystemdDbus = std::make_shared<velia::DbusSystemdInput>(manager, *dbusConnection);
86
Tomáš Pecka31caebd2020-08-21 11:14:10 +020087 spdlog::get("main")->debug("All inputs initialized.");
Tomáš Peckaa193c032020-06-22 17:33:31 +020088
89 // TODO: Gracefully leave dbus event loop on SIGTERM
90 // dbusConnection->leaveEventLoop();
91 eventLoop.join();
92
Tomáš Pecka31caebd2020-08-21 11:14:10 +020093 spdlog::get("main")->debug("Shutting down");
Tomáš Peckaa193c032020-06-22 17:33:31 +020094 return 0;
95 } catch (std::exception& e) {
96 velia::utils::fatalException(spdlog::get("main"), e, "main");
97 }
Tomáš Pecka5123e002020-06-05 15:29:21 +020098}