我有一个 CMake 项目,它由一个库子项目和另一个构建可执行文件并将其链接到该库的子项目组成。
现在在我的库的源文件中(目前是我记录任何东西的唯一地方)我需要使用INITIALIZE_EASYLOGGINGPP
(否则我会得到典型的unresolved externals
链接器错误)。由于我从我的文件中加载 easylogging++ 配置,因此main()
我还需要放在INITIALIZE_EASYLOGGINGPP
那里。从 easylogging++ 的文档来看,这是正确的,因为它们是两个独立的翻译单元。
我将头文件和源文件(easylogging.cc
和easylogging.h
)都包含在两个目标中:
图书馆项目
include_directories(core
${INCLUDE_DIR}
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
${DEPS}/json/single_include
${DEPS}/easyloggingpp/src
)
add_library(core
SHARED
${SRC} # Contains my own source files for the library
${DEPS}/easyloggingpp/src/easylogging++.cc
)
链接到库的可执行文件
add_executable(playground
${SRC}
${DEPS}/easyloggingpp/src/easylogging++.cc
)
target_include_directories(playground
PUBLIC
${INCLUDE_DIR}
${DEPS}/easyloggingpp/src
${DEPS}/json/single_include
)
这会导致多个日志文件(可能是因为多个记录器?)。我正在使用从 git 存储库中获取的默认配置:
* GLOBAL:
FORMAT = "%datetime %msg"
FILENAME = "playground.log"
ENABLED = true
TO_FILE = true
TO_STANDARD_OUTPUT = true
SUBSECOND_PRECISION = 6
PERFORMANCE_TRACKING = true
MAX_LOG_FILE_SIZE = 2097152 ## 2MB - Comment starts with two hashes (##)
LOG_FLUSH_THRESHOLD = 100 ## Flush after every 100 logs
* DEBUG:
FORMAT = "%datetime{%d/%M} %func %msg"
在我的可执行文件运行的二进制文件中,我得到playground.log
了myeasylog.log
后者,后者包含我的库中的日志输出。
--
core.h
在下面的代码中,为了保持一致性,定义了以下命名空间别名:
project::core::logging
- 对于el
命名空间(easylogging++)project::core::configuration
对于nlohmann
命名空间 (JSON)
此外,我使用 JSON 来加载我的项目的运行时配置。目前它只包含
{
"logging": {
"info": "Absolute or relative path to the easylogging++ configuration file. Refer to https://github.com/amrayn/easyloggingpp#using-configuration-file for more information",
"configuration_file": "logging.conf"
}
}
上面已经列出了logging.conf
easylogging++的配置。
链接到库的可执行文件 - main.cpp
#include <core.h>
#include <iostream>
INITIALIZE_EASYLOGGINGPP
int main(int argc, char* argv[])
{
// TODO Globally configure all loggers to use the same easylogging++ configuration file
std::ifstream configFile("..\\projectconfig.json");
project::core::configuration::json config;
configFile >> config;
project::core::logging::Configurations loggingConfig(config["logging"]["configuration_file"]);
project::core::logging::Loggers::setDefaultConfigurations(loggingConfig, true);
//project::core::logging::Loggers::reconfigureAllLoggers(loggingConfig);
//std::string path = config["logging"]["configuration_file"]
//project::core::logging::Loggers::configureFromGlobal(std::to_string());
// Library related calls and data
int devIDMaxMem = 0;
int devIDMaxCudaVersion = 0;
auto res = project::core::cuda::listCudaDevices(devIDMaxMem, devIDMaxCudaVersion, true);
project::core::cuda::printCudaError(static_cast<cudaError_t>(res));
project::core::dx::printDirect3DInfo();
return 0;
}
如您所见,我尝试使用setDefaultConfigurations()
,但没有任何成功。configureFromGlobal()
reconfigureAllLoggers()
我也在考虑在我的库中加载配置(对于每个翻译单元),但这很丑陋,由于有更好的解决方案可用,我希望是错误的。