Refactor parseLogLevel
Change-Id: Icde51a1b219cd4a9ee5e0d0631bdf74e0e5863e9
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 857499e..6c4359b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -77,6 +77,7 @@
src/utils/libyang.cpp
src/utils/libyang.h
src/utils/log.h
+ src/utils/log.cpp
src/utils/log-fwd.h
src/utils/log-init.cpp
src/utils/log-init.h
@@ -87,7 +88,7 @@
src/utils/waitUntilSignalled.cpp
src/utils/waitUntilSignalled.h
)
-target_link_libraries(velia-utils PUBLIC spdlog::spdlog PRIVATE PkgConfig::SYSTEMD PkgConfig::SYSREPO fmt::fmt)
+target_link_libraries(velia-utils PUBLIC spdlog::spdlog PRIVATE PkgConfig::SYSTEMD PkgConfig::SYSREPO fmt::fmt docopt)
# - health
add_library(velia-health STATIC
diff --git a/src/main-firewall.cpp b/src/main-firewall.cpp
index d9d0066..82a0ff6 100644
--- a/src/main-firewall.cpp
+++ b/src/main-firewall.cpp
@@ -11,25 +11,9 @@
#include "utils/exec.h"
#include "utils/journal.h"
#include "utils/log-init.h"
+#include "utils/log.h"
#include "utils/waitUntilSignalled.h"
-/** @short Extract log level from a CLI option */
-spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
-{
- long x;
- try {
- x = option.asLong();
- } catch (std::invalid_argument&) {
- throw std::runtime_error(name + " log level: expecting integer");
- }
- static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
- static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
- if (x < 0 || x > 5)
- throw std::runtime_error(name + " log level invalid or out-of-range");
-
- return static_cast<spdlog::level::level_enum>(5 - x);
-}
-
static const char usage[] =
R"(Bridge between sysrepo and nftables.
diff --git a/src/main-hardware.cpp b/src/main-hardware.cpp
index 7b0bd2e..e357119 100644
--- a/src/main-hardware.cpp
+++ b/src/main-hardware.cpp
@@ -8,26 +8,10 @@
#include "ietf-hardware/sysrepo/Sysrepo.h"
#include "utils/exceptions.h"
#include "utils/journal.h"
+#include "utils/log.h"
#include "utils/log-init.h"
#include "utils/waitUntilSignalled.h"
-/** @short Extract log level from a CLI option */
-spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
-{
- long x;
- try {
- x = option.asLong();
- } catch (std::invalid_argument&) {
- throw std::runtime_error(name + " log level: expecting integer");
- }
- static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
- static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
- if (x < 0 || x > 5)
- throw std::runtime_error(name + " log level invalid or out-of-range");
-
- return static_cast<spdlog::level::level_enum>(5 - x);
-}
-
static const char usage[] =
R"(Hardware monitoring via Sysrepo.
diff --git a/src/main-health.cpp b/src/main-health.cpp
index fcf96fc..be829af 100644
--- a/src/main-health.cpp
+++ b/src/main-health.cpp
@@ -11,25 +11,9 @@
#include "main.h"
#include "utils/exceptions.h"
#include "utils/journal.h"
+#include "utils/log.h"
#include "utils/log-init.h"
-/** @short Extract log level from a CLI option */
-spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
-{
- long x;
- try {
- x = option.asLong();
- } catch (std::invalid_argument&) {
- throw std::runtime_error(name + " log level: expecting integer");
- }
- static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
- static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
- if (x < 0 || x > 5)
- throw std::runtime_error(name + " log level invalid or out-of-range");
-
- return static_cast<spdlog::level::level_enum>(5 - x);
-}
-
static const char usage[] =
R"(Monitor system health status.
diff --git a/src/main-system.cpp b/src/main-system.cpp
index 77f5e4f..280fe79 100644
--- a/src/main-system.cpp
+++ b/src/main-system.cpp
@@ -10,25 +10,9 @@
#include "system/IETFSystem.h"
#include "utils/exceptions.h"
#include "utils/journal.h"
+#include "utils/log.h"
#include "utils/log-init.h"
-/** @short Extract log level from a CLI option */
-spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
-{
- long x;
- try {
- x = option.asLong();
- } catch (std::invalid_argument&) {
- throw std::runtime_error(name + " log level: expecting integer");
- }
- static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
- static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
- if (x < 0 || x > 5)
- throw std::runtime_error(name + " log level invalid or out-of-range");
-
- return static_cast<spdlog::level::level_enum>(5 - x);
-}
-
static const char usage[] =
R"(Sysrepo-powered system management.
diff --git a/src/utils/log.cpp b/src/utils/log.cpp
new file mode 100644
index 0000000..f6fe716
--- /dev/null
+++ b/src/utils/log.cpp
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
+ *
+ * Written by Václav Kubernát <kubernat@cesnet.cz>
+ *
+*/
+
+#include <docopt.h>
+#include "log.h"
+
+spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option)
+{
+ long x;
+ try {
+ x = option.asLong();
+ } catch (std::invalid_argument&) {
+ throw std::runtime_error(name + " log level: expecting integer");
+ }
+ static_assert(spdlog::level::trace < spdlog::level::off, "spdlog::level levels have changed");
+ static_assert(spdlog::level::off == 6, "spdlog::level levels have changed");
+ if (x < 0 || x > 5)
+ throw std::runtime_error(name + " log level invalid or out-of-range");
+
+ return static_cast<spdlog::level::level_enum>(5 - x);
+}
diff --git a/src/utils/log.h b/src/utils/log.h
index 4ae7bca..1e2ed63 100644
--- a/src/utils/log.h
+++ b/src/utils/log.h
@@ -13,3 +13,10 @@
//#define SPDLOG_ENABLE_SYSLOG
#include <spdlog/spdlog.h>
+
+namespace docopt {
+ class value;
+}
+
+/** @short Extract log level from a CLI option */
+spdlog::level::level_enum parseLogLevel(const std::string& name, const docopt::value& option);