From 0fab5761256d83acbff596506aed1f05b934c6dc Mon Sep 17 00:00:00 2001 From: black-desk Date: Thu, 7 Mar 2024 01:22:36 +0800 Subject: [PATCH] feat: use without link to spdlog Signed-off-by: black-desk --- CMakeLists.txt | 1 + examples/parse-config/src/main.cpp | 6 +- examples/use-default-logger/CMakeLists.txt | 2 + examples/use-default-logger/src/main.cpp | 108 +++++++++++++++++++++ examples/using-crun/src/main.cpp | 8 +- include/ocppi/cli/CommonCLI.hpp | 6 +- include/ocppi/cli/crun/Crun.hpp | 5 +- include/ocppi/cli/runc/Runc.hpp | 5 +- include/ocppi/cli/youki/Youki.hpp | 4 +- src/ocppi/cli/CommonCLI.cpp | 13 ++- src/ocppi/cli/crun/Crun.cpp | 10 +- src/ocppi/cli/runc/Runc.cpp | 2 + src/ocppi/cli/youki/Youki.cpp | 2 + 13 files changed, 154 insertions(+), 18 deletions(-) create mode 100644 examples/use-default-logger/CMakeLists.txt create mode 100644 examples/use-default-logger/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 27eba4d..77bf979 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,7 @@ pfl_add_library( EXAMPLES parse-config using-crun + use-default-logger LINK_LIBRARIES PUBLIC nlohmann_json::nlohmann_json diff --git a/examples/parse-config/src/main.cpp b/examples/parse-config/src/main.cpp index f8d029c..e6dca20 100644 --- a/examples/parse-config/src/main.cpp +++ b/examples/parse-config/src/main.cpp @@ -30,7 +30,7 @@ class sink; } // namespace sinks } // namespace spdlog -void printException(const std::unique_ptr &logger, +void printException(const std::shared_ptr &logger, std::string_view msg, std::exception_ptr ptr) noexcept try { std::rethrow_exception(ptr); @@ -42,7 +42,7 @@ try { int main() { - std::unique_ptr logger; + std::shared_ptr logger; { auto sinks = std::vector>( { std::make_shared( @@ -52,7 +52,7 @@ int main() spdlog::sinks::stderr_color_sink_mt>()); } - logger = std::make_unique( + logger = std::make_shared( "ocppi", sinks.begin(), sinks.end()); logger->set_level(spdlog::level::trace); diff --git a/examples/use-default-logger/CMakeLists.txt b/examples/use-default-logger/CMakeLists.txt new file mode 100644 index 0000000..5760341 --- /dev/null +++ b/examples/use-default-logger/CMakeLists.txt @@ -0,0 +1,2 @@ +pfl_add_executable(INTERNAL SOURCES src/main.cpp LINK_LIBRARIES PUBLIC + ocppi::ocppi) diff --git a/examples/use-default-logger/src/main.cpp b/examples/use-default-logger/src/main.cpp new file mode 100644 index 0000000..82cd489 --- /dev/null +++ b/examples/use-default-logger/src/main.cpp @@ -0,0 +1,108 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "nlohmann/json.hpp" +#include "nlohmann/json_fwd.hpp" +#include "ocppi/cli/CLI.hpp" +#include "ocppi/cli/crun/Crun.hpp" +#include "ocppi/runtime/Signal.hpp" +#include "ocppi/runtime/state/types/Generators.hpp" // IWYU pragma: keep +#include "ocppi/types/ContainerListItem.hpp" +#include "ocppi/types/Generators.hpp" // IWYU pragma: keep +#include "tl/expected.hpp" + +namespace spdlog +{ +namespace sinks +{ +class sink; +} // namespace sinks +} // namespace spdlog + +void printException(std::string_view msg, std::exception_ptr ptr) noexcept +try { + std::rethrow_exception(std::move(ptr)); +} catch (const std::exception &e) { + std::cerr << msg << ": " << e.what() << std::endl; +} catch (...) { + std::cerr << msg << ": unknown exception" << std::endl; +} + +auto main() -> int +{ + try { + std::unique_ptr cli; + { + auto crun = + ocppi::cli::crun::Crun::New("/usr/bin/crun"); + if (!crun) { + printException("New crun object failed", + crun.error()); + return -1; + } + cli = std::move(crun.value()); + } + + auto bin = cli->bin(); + std::cerr << "Using OCI runtime CLI program \"" << bin.string() + << "\"" << std::endl; + + auto containerList = cli->list(); + if (!containerList) { + printException("Run crun list failed", + containerList.error()); + return -1; + } + + if (containerList->empty()) { + std::cerr << "No container exists." << std::endl; + return 0; + } + + for (auto container : containerList.value()) { + nlohmann::json containerJSON = container; + std::cerr << "Existing container " + << containerJSON.dump() << std::endl; + } + + auto state = cli->state(containerList->front().id); + + if (!state) { + printException("Run crun state failed", state.error()); + return -1; + } + + nlohmann::json stateJSON = state.value(); + std::cout << stateJSON.dump(1, '\t') << std::endl; + + auto killResult = cli->kill(containerList->front().id, + ocppi::runtime::Signal("SIGTERM")); + + if (!killResult) { + printException("Run crun kill failed", + killResult.error()); + return -1; + } + + return 0; + } catch (...) { + printException("Failed to kill first crun container", + std::current_exception()); + return -1; + } + + return 0; +} diff --git a/examples/using-crun/src/main.cpp b/examples/using-crun/src/main.cpp index e37c09e..c719548 100644 --- a/examples/using-crun/src/main.cpp +++ b/examples/using-crun/src/main.cpp @@ -38,7 +38,7 @@ class sink; } // namespace sinks } // namespace spdlog -void printException(const std::unique_ptr &logger, +void printException(const std::shared_ptr &logger, std::string_view msg, std::exception_ptr ptr) noexcept try { std::rethrow_exception(std::move(ptr)); @@ -50,7 +50,7 @@ try { auto main() -> int { - std::unique_ptr logger; + std::shared_ptr logger; { auto sinks = std::vector>( { std::make_shared( @@ -60,7 +60,7 @@ auto main() -> int spdlog::sinks::stderr_color_sink_mt>()); } - logger = std::make_unique( + logger = std::make_shared( "ocppi", sinks.begin(), sinks.end()); logger->set_level(spdlog::level::trace); @@ -92,7 +92,7 @@ auto main() -> int } if (containerList->empty()) { - SPDLOG_LOGGER_ERROR(logger, "No container exists."); + SPDLOG_LOGGER_INFO(logger, "No container exists."); return 0; } diff --git a/include/ocppi/cli/CommonCLI.hpp b/include/ocppi/cli/CommonCLI.hpp index ed921b4..740b3f6 100644 --- a/include/ocppi/cli/CommonCLI.hpp +++ b/include/ocppi/cli/CommonCLI.hpp @@ -53,10 +53,10 @@ namespace ocppi::cli class CommonCLI : public virtual CLI { protected: CommonCLI(std::filesystem::path, - const std::unique_ptr &); + const std::shared_ptr &); [[nodiscard]] - auto logger() const -> const std::unique_ptr &; + auto logger() const -> const std::shared_ptr &; [[nodiscard]] auto generateGlobalOptions(const runtime::GlobalOption &option) @@ -185,7 +185,7 @@ class CommonCLI : public virtual CLI { private: std::filesystem::path bin_; - const std::unique_ptr &logger_; + std::shared_ptr logger_; }; } diff --git a/include/ocppi/cli/crun/Crun.hpp b/include/ocppi/cli/crun/Crun.hpp index 5e439a3..89ae674 100644 --- a/include/ocppi/cli/crun/Crun.hpp +++ b/include/ocppi/cli/crun/Crun.hpp @@ -19,8 +19,11 @@ class Crun final : public CommonCLI { using CommonCLI::CommonCLI; public: + static auto New(const std::filesystem::path &bin) noexcept + -> tl::expected, std::exception_ptr>; + static auto New(const std::filesystem::path &bin, - const std::unique_ptr &logger) noexcept + const std::shared_ptr &logger) noexcept -> tl::expected, std::exception_ptr>; }; } diff --git a/include/ocppi/cli/runc/Runc.hpp b/include/ocppi/cli/runc/Runc.hpp index c91fa81..1e17a93 100644 --- a/include/ocppi/cli/runc/Runc.hpp +++ b/include/ocppi/cli/runc/Runc.hpp @@ -19,8 +19,11 @@ class Runc : public CommonCLI { using CommonCLI::CommonCLI; public: + static auto New(const std::filesystem::path &bin) noexcept + -> tl::expected, std::exception_ptr>; + static auto New(const std::filesystem::path &bin, - const std::unique_ptr &logger) noexcept + const std::shared_ptr &logger) noexcept -> tl::expected, std::exception_ptr>; }; diff --git a/include/ocppi/cli/youki/Youki.hpp b/include/ocppi/cli/youki/Youki.hpp index 8d1f8f3..34dd27e 100644 --- a/include/ocppi/cli/youki/Youki.hpp +++ b/include/ocppi/cli/youki/Youki.hpp @@ -19,8 +19,10 @@ class Youki : public CommonCLI { using CommonCLI::CommonCLI; public: + static auto New(const std::filesystem::path &bin) noexcept + -> tl::expected, std::exception_ptr>; static auto New(const std::filesystem::path &bin, - const std::unique_ptr &logger) noexcept + const std::shared_ptr &logger) noexcept -> tl::expected, std::exception_ptr>; }; diff --git a/src/ocppi/cli/CommonCLI.cpp b/src/ocppi/cli/CommonCLI.cpp index 1c0f926..eeb5663 100644 --- a/src/ocppi/cli/CommonCLI.cpp +++ b/src/ocppi/cli/CommonCLI.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include "ocppi/types/ContainerListItem.hpp" #include "ocppi/types/Generators.hpp" // IWYU pragma: keep #include "spdlog/fmt/ranges.h" // IWYU pragma: keep +#include "spdlog/sinks/null_sink.h" #include "spdlog/spdlog.h" namespace spdlog @@ -51,7 +53,7 @@ namespace template auto doCommand(const std::string &bin, - [[maybe_unused]] const std::unique_ptr &logger, + [[maybe_unused]] const std::shared_ptr &logger, std::vector &&globalOption, const std::string &command, std::vector &&options, std::vector &&arguments) -> Result @@ -90,9 +92,11 @@ auto doCommand(const std::string &bin, } CommonCLI::CommonCLI(std::filesystem::path bin, - const std::unique_ptr &logger) + const std::shared_ptr &logger) : bin_(std::move(bin)) - , logger_(std::move(logger)) + , logger_(logger != nullptr ? + logger : + spdlog::create("")) { if (std::filesystem::exists(bin_)) { return; @@ -105,8 +109,9 @@ auto CommonCLI::bin() const noexcept -> const std::filesystem::path & return this->bin_; } -auto CommonCLI::logger() const -> const std::unique_ptr & +auto CommonCLI::logger() const -> const std::shared_ptr & { + assert(this->logger_ != nullptr); return this->logger_; } diff --git a/src/ocppi/cli/crun/Crun.cpp b/src/ocppi/cli/crun/Crun.cpp index 36c7fe7..f003a12 100644 --- a/src/ocppi/cli/crun/Crun.cpp +++ b/src/ocppi/cli/crun/Crun.cpp @@ -10,8 +10,16 @@ class logger; namespace ocppi::cli::crun { +auto Crun::New(const std::filesystem::path &bin) noexcept + -> tl::expected, std::exception_ptr> +try { + return std::unique_ptr(new Crun(bin, spdlog::default_logger())); +} catch (...) { + return tl::unexpected(std::current_exception()); +} + auto Crun::New(const std::filesystem::path &bin, - const std::unique_ptr &logger) noexcept + const std::shared_ptr &logger) noexcept -> tl::expected, std::exception_ptr> try { return std::unique_ptr(new Crun(bin, logger)); diff --git a/src/ocppi/cli/runc/Runc.cpp b/src/ocppi/cli/runc/Runc.cpp index 826b1cb..168d8d9 100644 --- a/src/ocppi/cli/runc/Runc.cpp +++ b/src/ocppi/cli/runc/Runc.cpp @@ -1 +1,3 @@ #include "ocppi/cli/runc/Runc.hpp" // IWYU pragma: keep + +// TODO: not implemented yet diff --git a/src/ocppi/cli/youki/Youki.cpp b/src/ocppi/cli/youki/Youki.cpp index e4241e1..ae963d2 100644 --- a/src/ocppi/cli/youki/Youki.cpp +++ b/src/ocppi/cli/youki/Youki.cpp @@ -1 +1,3 @@ #include "ocppi/cli/youki/Youki.hpp" // IWYU pragma: keep + +// TODO: not implemented yet