mirror of
https://github.com/kernelkit/infix.git
synced 2026-08-05 23:23:02 +02:00
169 lines
6.1 KiB
Diff
169 lines
6.1 KiB
Diff
From 561c28496691fc1ac17e755bcc0ff3ec8c3b1bf0 Mon Sep 17 00:00:00 2001
|
|
From: =?UTF-8?q?Mattias=20Walstr=C3=B6m?= <lazzer@gmail.com>
|
|
Date: Tue, 28 May 2024 09:28:38 +0200
|
|
Subject: [PATCH] Change systemd from a required dependency to an optional
|
|
dependency.
|
|
MIME-Version: 1.0
|
|
Content-Type: text/plain; charset=UTF-8
|
|
Content-Transfer-Encoding: 8bit
|
|
Organization: Addiva Elektronik
|
|
|
|
In addition to journal logging, enable logging to syslog and stdout.
|
|
|
|
Signed-off-by: Mattias Walström <lazzer@gmail.com>
|
|
Change-Id: I90cb9169e8fdf9103c31bc983a1ec8a5de3dd26c
|
|
---
|
|
CMakeLists.txt | 14 +++++++++++---
|
|
README.md | 3 ++-
|
|
ci/pre.yaml | 6 ++++++
|
|
src/configure.cmake.h.in | 3 +++
|
|
src/restconf/main.cpp | 36 ++++++++++++++++++++++++++++++++----
|
|
5 files changed, 54 insertions(+), 8 deletions(-)
|
|
create mode 100644 src/configure.cmake.h.in
|
|
|
|
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
|
index 7f82660..43313ba 100644
|
|
--- a/CMakeLists.txt
|
|
+++ b/CMakeLists.txt
|
|
@@ -72,11 +72,16 @@ find_package(spdlog REQUIRED)
|
|
find_package(PkgConfig)
|
|
pkg_check_modules(nghttp2 REQUIRED IMPORTED_TARGET libnghttp2_asio>=0.0.90 libnghttp2)
|
|
find_package(Boost REQUIRED COMPONENTS system thread)
|
|
-
|
|
pkg_check_modules(SYSREPO-CPP REQUIRED IMPORTED_TARGET sysrepo-cpp>=1.1.0)
|
|
pkg_check_modules(LIBYANG-CPP REQUIRED IMPORTED_TARGET libyang-cpp>=1.1.0)
|
|
-pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd)
|
|
+pkg_check_modules(SYSTEMD IMPORTED_TARGET libsystemd)
|
|
pkg_check_modules(PAM REQUIRED IMPORTED_TARGET pam)
|
|
+pkg_check_modules(DOCOPT REQUIRED IMPORTED_TARGET docopt)
|
|
+if(SYSTEMD_FOUND)
|
|
+ set(HAVE_SYSTEMD TRUE)
|
|
+endif()
|
|
+
|
|
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/configure.cmake.h.in ${CMAKE_CURRENT_BINARY_DIR}/configure.cmake.h)
|
|
|
|
add_library(rousette-http STATIC
|
|
src/http/EventStream.cpp
|
|
@@ -116,7 +121,10 @@ add_executable(watch-operational-ds
|
|
target_link_libraries(watch-operational-ds PUBLIC rousette-sysrepo)
|
|
|
|
add_executable(rousette src/restconf/main.cpp)
|
|
-target_link_libraries(rousette PUBLIC rousette-restconf PkgConfig::SYSTEMD)
|
|
+target_link_libraries(rousette PUBLIC rousette-restconf PkgConfig::DOCOPT)
|
|
+if(SYSTEMD_FOUND)
|
|
+ target_link_libraries(rousette PUBLIC PkgConfig::SYSTEMD)
|
|
+endif()
|
|
|
|
install(TARGETS
|
|
# clock-demo
|
|
diff --git a/README.md b/README.md
|
|
index 68b4b99..55b8241 100644
|
|
--- a/README.md
|
|
+++ b/README.md
|
|
@@ -37,12 +37,13 @@ The anonymous user access is disabled whenever these rules are not met.
|
|
- [nghttp2-asio](https://github.com/nghttp2/nghttp2-asio) - asynchronous C++ library for HTTP/2
|
|
- [sysrepo-cpp](https://github.com/sysrepo/sysrepo-cpp) - object-oriented bindings of the [*sysrepo*](https://github.com/sysrepo/sysrepo) library
|
|
- [libyang-cpp](https://github.com/CESNET/libyang-cpp) - C++ bindings for *libyang*
|
|
-- systemd - the shared library for logging to `sd-journal`
|
|
- [PAM](http://www.linux-pam.org/) - for authentication
|
|
- [spdlog](https://github.com/gabime/spdlog) - Very fast, header-only/compiled, C++ logging library
|
|
+- [docopt-cpp](https://github.com/docopt/docopt.cpp) - command-line argument parser
|
|
- Boost's system and thread
|
|
- C++20 compiler (e.g., GCC 10.x+, clang 10+)
|
|
- CMake 3.19+
|
|
+- optionally systemd - the shared library for logging to `sd-journal`
|
|
- optionally for built-in tests, [Doctest](https://github.com/onqtam/doctest/) as a C++ unit test framework
|
|
- optionally for built-in tests, [trompeloeil](https://github.com/rollbear/trompeloeil) for mock objects in C++
|
|
- optionally for built-in tests, [`pam_matrix` and `pam_wrapper`](https://cwrap.org/pam_wrapper.html) for PAM mocking
|
|
diff --git a/ci/pre.yaml b/ci/pre.yaml
|
|
index 51f1c66..325dbf4 100644
|
|
--- a/ci/pre.yaml
|
|
+++ b/ci/pre.yaml
|
|
@@ -11,3 +11,9 @@
|
|
name: pam_wrapper
|
|
state: present
|
|
become: true
|
|
+
|
|
+ - name: install docopt-cpp
|
|
+ package:
|
|
+ name: docopt-cpp-devel
|
|
+ state: present
|
|
+ become: true
|
|
diff --git a/src/configure.cmake.h.in b/src/configure.cmake.h.in
|
|
new file mode 100644
|
|
index 0000000..bc26981
|
|
--- /dev/null
|
|
+++ b/src/configure.cmake.h.in
|
|
@@ -0,0 +1,3 @@
|
|
+#pragma once
|
|
+
|
|
+#cmakedefine HAVE_SYSTEMD
|
|
diff --git a/src/restconf/main.cpp b/src/restconf/main.cpp
|
|
index bba3cbf..2bc9a86 100644
|
|
--- a/src/restconf/main.cpp
|
|
+++ b/src/restconf/main.cpp
|
|
@@ -9,14 +9,30 @@
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <inttypes.h>
|
|
-#include <spdlog/sinks/systemd_sink.h>
|
|
+
|
|
+#include "configure.cmake.h" /* Expose HAVE_SYSTEMD */
|
|
+
|
|
+#ifdef HAVE_SYSTEMD
|
|
+ #include <spdlog/sinks/systemd_sink.h>
|
|
+#endif
|
|
+#include <spdlog/sinks/syslog_sink.h>
|
|
#include <spdlog/sinks/ansicolor_sink.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
+#include <docopt.h>
|
|
#include <spdlog/spdlog.h>
|
|
#include <sysrepo-cpp/Session.hpp>
|
|
#include "restconf/Server.h"
|
|
+static const char usage[] =
|
|
+ R"(Rousette - RESTCONF server
|
|
+Usage:
|
|
+ rousette [--syslog] [--help]
|
|
+Options:
|
|
+ -h --help Show this screen.
|
|
+ --syslog Log to syslog.
|
|
+)";
|
|
+#ifdef HAVE_SYSTEMD
|
|
|
|
namespace {
|
|
/** @short Is stderr connected to journald? Not thread safe. */
|
|
@@ -54,13 +70,25 @@ public:
|
|
}
|
|
};
|
|
}
|
|
-
|
|
-int main(int argc [[maybe_unused]], char* argv [[maybe_unused]] [])
|
|
+#endif
|
|
+int main(int argc, char* argv [])
|
|
{
|
|
- if (is_journald_active()) {
|
|
+ auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true,""/* version */, true);
|
|
+
|
|
+ if (args["--syslog"].asBool()) {
|
|
+ auto syslog_sink = std::make_shared<spdlog::sinks::syslog_sink_mt>("rousette", LOG_PID, LOG_USER, true);
|
|
+ auto logger = std::make_shared<spdlog::logger>("rousette", syslog_sink);
|
|
+ spdlog::set_default_logger(logger);
|
|
+#ifdef HAVE_SYSTEMD
|
|
+ } else if (is_journald_active()) {
|
|
auto sink = std::make_shared<journald_sink<std::mutex>>();
|
|
auto logger = std::make_shared<spdlog::logger>("rousette", sink);
|
|
spdlog::set_default_logger(logger);
|
|
+#endif
|
|
+ } else {
|
|
+ auto stdout_sink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
|
|
+ auto logger = std::make_shared<spdlog::logger>("rousette", stdout_sink);
|
|
+ spdlog::set_default_logger(logger);
|
|
}
|
|
spdlog::set_level(spdlog::level::trace);
|
|
|
|
--
|
|
2.34.1
|
|
|