我正在尝试在 pybind11 模块中打开一个文件并向其中写入一些内容。
但FILE *fp = fopen("log/module_cc.log", "w");
会回来NULL
。
你能帮我解决这个问题吗?
实际上,我正在尝试使用 spdlog 登录 pybind11 模块,但似乎效果不佳。我知道 stdout/stderr 没有重定向到 python 模块的控制台,但是文件 fd 是否也被禁用了?
这是我的 pybind11 模块中的 spdlog 初始化程序:
#include <pybind11/pybind11.h>
PYBIND11_MODULE(my_module, m) {
// def my module here
}
#include <fmt/format.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/rotating_file_sink.h>
const int MAX_ROTATE_SIZE = 50 * 1024 * 1024;
const int MAX_ROTATE_FILES = 10;
const spdlog::level::level_enum LOG_LEVEL = spdlog::level::info;
struct ModuleInitializer {
ModuleInitializer() {
init_logger();
}
static void init_logger() {
auto rotate_logger = spdlog::rotating_logger_mt(
"my_module", "my_module.log", MAX_ROTATE_SIZE, MAX_ROTATE_FILES);
spdlog::set_default_logger(rotate_logger);
spdlog::set_level(LOG_LEVEL);
spdlog::set_pattern("%Y-%m-%d %H:%M%S.%f | %l | p-%P t-%t | %!:%@ - %v");
SPDLOG_DEBUG("logging initialized");
}
};
static ModuleInitializer module_initializer;
你能帮我解决这个问题吗?