Skip to content

Commit

Permalink
feat: use without link to spdlog
Browse files Browse the repository at this point in the history
Signed-off-by: black-desk <me@black-desk.cn>
  • Loading branch information
black-desk committed Mar 8, 2024
1 parent ca0344a commit 11a3180
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 18 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ pfl_add_library(
EXAMPLES
parse-config
using-crun
use-default-logger
LINK_LIBRARIES
PUBLIC
nlohmann_json::nlohmann_json
Expand Down
6 changes: 3 additions & 3 deletions examples/parse-config/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class sink;
} // namespace sinks
} // namespace spdlog

void printException(const std::unique_ptr<spdlog::logger> &logger,
void printException(const std::shared_ptr<spdlog::logger> &logger,
std::string_view msg, std::exception_ptr ptr) noexcept
try {
std::rethrow_exception(ptr);
Expand All @@ -42,7 +42,7 @@ try {

int main()
{
std::unique_ptr<spdlog::logger> logger;
std::shared_ptr<spdlog::logger> logger;
{
auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>(
{ std::make_shared<spdlog::sinks::systemd_sink_mt>(
Expand All @@ -52,7 +52,7 @@ int main()
spdlog::sinks::stderr_color_sink_mt>());
}

logger = std::make_unique<spdlog::logger>(
logger = std::make_shared<spdlog::logger>(
"ocppi", sinks.begin(), sinks.end());

logger->set_level(spdlog::level::trace);
Expand Down
7 changes: 7 additions & 0 deletions examples/use-default-logger/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pfl_add_executable(

Check warning on line 1 in examples/use-default-logger/CMakeLists.txt

View workflow job for this annotation

GitHub Actions / Run black-desk/checks

[cmake-format] reported by reviewdog 🐶 Raw Output: examples/use-default-logger/CMakeLists.txt:1:-pfl_add_executable( examples/use-default-logger/CMakeLists.txt:2:- INTERNAL examples/use-default-logger/CMakeLists.txt:3:- SOURCES examples/use-default-logger/CMakeLists.txt:4:- src/main.cpp examples/use-default-logger/CMakeLists.txt:5:- LINK_LIBRARIES examples/use-default-logger/CMakeLists.txt:6:- PUBLIC examples/use-default-logger/CMakeLists.txt:7:- ocppi::ocppi) examples/use-default-logger/CMakeLists.txt:1:+pfl_add_executable(INTERNAL SOURCES src/main.cpp LINK_LIBRARIES PUBLIC examples/use-default-logger/CMakeLists.txt:2:+ ocppi::ocppi)
INTERNAL
SOURCES
src/main.cpp
LINK_LIBRARIES
PUBLIC
ocppi::ocppi)
108 changes: 108 additions & 0 deletions examples/use-default-logger/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#include <unistd.h>

#include <algorithm>
#include <cstdio>
#include <exception>
#include <filesystem>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <bits/types/struct_FILE.h>

#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<ocppi::cli::CLI> 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;
}
8 changes: 4 additions & 4 deletions examples/using-crun/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class sink;
} // namespace sinks
} // namespace spdlog

void printException(const std::unique_ptr<spdlog::logger> &logger,
void printException(const std::shared_ptr<spdlog::logger> &logger,
std::string_view msg, std::exception_ptr ptr) noexcept
try {
std::rethrow_exception(std::move(ptr));
Expand All @@ -50,7 +50,7 @@ try {

auto main() -> int
{
std::unique_ptr<spdlog::logger> logger;
std::shared_ptr<spdlog::logger> logger;
{
auto sinks = std::vector<std::shared_ptr<spdlog::sinks::sink>>(
{ std::make_shared<spdlog::sinks::systemd_sink_mt>(
Expand All @@ -60,7 +60,7 @@ auto main() -> int
spdlog::sinks::stderr_color_sink_mt>());
}

logger = std::make_unique<spdlog::logger>(
logger = std::make_shared<spdlog::logger>(
"ocppi", sinks.begin(), sinks.end());

logger->set_level(spdlog::level::trace);
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions include/ocppi/cli/CommonCLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ namespace ocppi::cli
class CommonCLI : public virtual CLI {
protected:
CommonCLI(std::filesystem::path,
const std::unique_ptr<spdlog::logger> &);
const std::shared_ptr<spdlog::logger> &);

[[nodiscard]]
auto logger() const -> const std::unique_ptr<spdlog::logger> &;
auto logger() const -> const std::shared_ptr<spdlog::logger> &;

[[nodiscard]]
auto generateGlobalOptions(const runtime::GlobalOption &option)
Expand Down Expand Up @@ -185,7 +185,7 @@ class CommonCLI : public virtual CLI {

private:
std::filesystem::path bin_;
const std::unique_ptr<spdlog::logger> &logger_;
std::shared_ptr<spdlog::logger> logger_;
};

}
5 changes: 4 additions & 1 deletion include/ocppi/cli/crun/Crun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::unique_ptr<Crun>, std::exception_ptr>;

static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>;
};
}
5 changes: 4 additions & 1 deletion include/ocppi/cli/runc/Runc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class Runc : public CommonCLI {
using CommonCLI::CommonCLI;

public:
static auto New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Runc>, std::exception_ptr>;

static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Runc>, std::exception_ptr>;
};

Expand Down
4 changes: 3 additions & 1 deletion include/ocppi/cli/youki/Youki.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class Youki : public CommonCLI {
using CommonCLI::CommonCLI;

public:
static auto New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Youki>, std::exception_ptr>;
static auto New(const std::filesystem::path &bin,
const std::unique_ptr<spdlog::logger> &logger) noexcept
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Youki>, std::exception_ptr>;
};

Expand Down
13 changes: 9 additions & 4 deletions src/ocppi/cli/CommonCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <istream>
#include <iterator>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
Expand Down Expand Up @@ -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
Expand All @@ -51,7 +53,7 @@ namespace

template <typename Result>
auto doCommand(const std::string &bin,
[[maybe_unused]] const std::unique_ptr<spdlog::logger> &logger,
[[maybe_unused]] const std::shared_ptr<spdlog::logger> &logger,
std::vector<std::string> &&globalOption,
const std::string &command, std::vector<std::string> &&options,
std::vector<std::string> &&arguments) -> Result
Expand Down Expand Up @@ -90,9 +92,11 @@ auto doCommand(const std::string &bin,
}

CommonCLI::CommonCLI(std::filesystem::path bin,
const std::unique_ptr<spdlog::logger> &logger)
const std::shared_ptr<spdlog::logger> &logger)
: bin_(std::move(bin))
, logger_(std::move(logger))
, logger_(logger != nullptr ?
logger :
spdlog::create<spdlog::sinks::null_sink_st>(""))
{
if (std::filesystem::exists(bin_)) {
return;
Expand All @@ -105,8 +109,9 @@ auto CommonCLI::bin() const noexcept -> const std::filesystem::path &
return this->bin_;
}

auto CommonCLI::logger() const -> const std::unique_ptr<spdlog::logger> &
auto CommonCLI::logger() const -> const std::shared_ptr<spdlog::logger> &
{
assert(this->logger_ != nullptr);
return this->logger_;
}

Expand Down
10 changes: 9 additions & 1 deletion src/ocppi/cli/crun/Crun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ class logger;
namespace ocppi::cli::crun
{

auto Crun::New(const std::filesystem::path &bin) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>
try {
return std::unique_ptr<Crun>(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<spdlog::logger> &logger) noexcept
const std::shared_ptr<spdlog::logger> &logger) noexcept
-> tl::expected<std::unique_ptr<Crun>, std::exception_ptr>
try {
return std::unique_ptr<Crun>(new Crun(bin, logger));
Expand Down
2 changes: 2 additions & 0 deletions src/ocppi/cli/runc/Runc.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "ocppi/cli/runc/Runc.hpp" // IWYU pragma: keep

// TODO: not implemented yet
2 changes: 2 additions & 0 deletions src/ocppi/cli/youki/Youki.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#include "ocppi/cli/youki/Youki.hpp" // IWYU pragma: keep

// TODO: not implemented yet

0 comments on commit 11a3180

Please sign in to comment.