-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from paulsengroup/feat/py-logging
Support interfacing spdlog with Python's logging stdlib
- Loading branch information
Showing
12 changed files
with
216 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (C) 2024 Roberto Rossini <roberros@uio.no> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#pragma once | ||
|
||
#include <spdlog/spdlog-inl.h> | ||
|
||
#include <memory> | ||
#include <string_view> | ||
|
||
#include "hictkpy/nanobind.hpp" | ||
|
||
namespace hictkpy { | ||
|
||
class Logger { | ||
nanobind::object _py_logger{}; | ||
std::shared_ptr<spdlog::logger> _logger{}; | ||
|
||
public: | ||
explicit Logger(spdlog::level::level_enum level_ = spdlog::level::warn); | ||
explicit Logger(std::string_view level_); | ||
|
||
[[nodiscard]] std::shared_ptr<spdlog::logger> get_logger(); | ||
|
||
private: | ||
[[nodiscard]] static nanobind::object init_py_logger(); | ||
[[nodiscard]] static std::shared_ptr<spdlog::logger> init_cpp_logger( | ||
spdlog::level::level_enum level_, nanobind::object py_logger); | ||
}; | ||
|
||
} // namespace hictkpy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (C) 2024 Roberto Rossini <roberros@uio.no> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "hictkpy/logger.hpp" | ||
|
||
#include <spdlog/sinks/callback_sink.h> | ||
#include <spdlog/spdlog.h> | ||
|
||
#include "hictkpy/common.hpp" | ||
#include "hictkpy/nanobind.hpp" | ||
|
||
namespace nb = nanobind; | ||
|
||
namespace hictkpy { | ||
|
||
[[nodiscard]] static std::int32_t to_py_lvl(spdlog::level::level_enum level) { | ||
// https://docs.python.org/3/library/logging.html#logging-levels | ||
// NOLINTBEGIN(*-avoid-magic-numbers) | ||
switch (level) { | ||
case spdlog::level::trace: | ||
[[fallthrough]]; | ||
case spdlog::level::debug: | ||
return 10; | ||
case spdlog::level::info: | ||
return 20; | ||
case spdlog::level::warn: | ||
return 30; | ||
case spdlog::level::err: | ||
return 40; | ||
case spdlog::level::critical: | ||
[[fallthrough]]; | ||
case spdlog::level::off: | ||
return 50; | ||
default: | ||
unreachable_code(); | ||
} | ||
// NOLINTEND(*-avoid-magic-numbers) | ||
} | ||
|
||
Logger::Logger(spdlog::level::level_enum level_) | ||
: _py_logger(init_py_logger()), _logger(init_cpp_logger(level_, _py_logger)) {} | ||
|
||
Logger::Logger(std::string_view level_) : Logger(spdlog::level::from_str(std::string{level_})) {} | ||
|
||
nb::object Logger::init_py_logger() { | ||
const auto logging = nb::module_::import_("logging"); | ||
return logging.attr("getLogger")("hictkpy"); | ||
} | ||
|
||
std::shared_ptr<spdlog::logger> Logger::get_logger() { return _logger; } | ||
|
||
std::shared_ptr<spdlog::logger> Logger::init_cpp_logger( | ||
[[maybe_unused]] spdlog::level::level_enum level_, [[maybe_unused]] nb::object py_logger) { | ||
#ifndef _WIN32 | ||
auto sink = std::make_shared<spdlog::sinks::callback_sink_mt>( | ||
// NOLINTNEXTLINE(*-unnecessary-value-param) | ||
[logger = py_logger](const spdlog::details::log_msg& msg) { | ||
logger.attr("log")(to_py_lvl(msg.level), | ||
std::string_view{msg.payload.data(), msg.payload.size()}); | ||
}); | ||
|
||
sink->set_pattern("%v"); | ||
sink->set_level(level_); | ||
|
||
auto logger = std::make_shared<spdlog::logger>("hictkpy", std::move(sink)); | ||
logger->set_level(level_); | ||
|
||
return logger; | ||
#else | ||
return nullptr; | ||
#endif | ||
} | ||
|
||
} // namespace hictkpy |
Oops, something went wrong.