我尝试这样做:在使用 Boost::Log 的 Windows (*.dll) 上创建 1 个共享库 (我想将 Boost::Log 静态链接到该库,因为我希望所有内容都只打包在 1 个 DLL 文件中)但是不成功。
我的项目有 4 个文件,如下所示:
我的CMakeLists.txt:(还有另一个 CMakeLists.txt 可以找到 Boost 库find_package(Boost 1.54.0 REQUIRED thread log log_setup filesystem date_time system)
)
cmake_minimum_required(VERSION 2.6)
add_library(mylog SHARED
mylog.cpp
)
target_link_libraries(mylog ${Boost_LIBRARIES})
if(UNIX)
target_link_libraries(mylog rt)
endif(UNIX)
add_executable(testlog
main.cpp
)
target_link_libraries(testlog mylog)
我的mylog.cpp:
#include "mylog.h"
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
__declspec( dllexport ) void initLog()
{
logging::add_file_log(
keywords::file_name = "testlog.log",
keywords::format = expr::format("%1% [%2%] %3% ")
% expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
% expr::attr< logging::trivial::severity_level >("Severity")
% expr::smessage
);
logging::add_common_attributes();
}
和mylog.h:
#ifndef _MYLOG_H
#define _MYLOG_H
#include <boost/log/common.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/log/utility/empty_deleter.hpp>
__declspec( dllexport ) void initLog();
#endif //_MYLOG_H
我的main.cpp:
#include "mylog.h"
int main(int, char*[])
{
using namespace boost::log::trivial;
initLog();
BOOST_LOG_TRIVIAL(trace) << "A trace severity message";
BOOST_LOG_TRIVIAL(debug) << "A debug severity message";
BOOST_LOG_TRIVIAL(info) << "An informational severity message";
BOOST_LOG_TRIVIAL(warning) << "A warning severity message";
BOOST_LOG_TRIVIAL(error) << "An error severity message";
BOOST_LOG_TRIVIAL(fatal) << "A fatal severity message";
return 0;
}
当我将库mylog编译为静态库(没有 SHARED 关键字)并运行程序testlog时,日志消息保存在文件testlog.log中,并且没有任何内容打印到屏幕上,如代码所述:
2013-12-20, 15:05:36.741156 [trace] A trace severity message
2013-12-20, 15:05:36.742156 [debug] A debug severity message
2013-12-20, 15:05:36.743156 [info] An informational severity message
2013-12-20, 15:05:36.743156 [warning] A warning severity message
2013-12-20, 15:05:36.743156 [error] An error severity message
2013-12-20, 15:05:36.743156 [fatal] A fatal severity message
但是如果我把它编译成一个 DLL共享库(使用 SHARED 关键字),编译成功但结果却不如预期。没有创建日志文件,消息以不同的格式显示在屏幕上。看起来函数initLog没有运行:
[2013-12-20 15:06:17.195469] [0x00000e6c] [trace] A trace severity message
[2013-12-20 15:06:17.198470] [0x00000e6c] [debug] A debug severity message
[2013-12-20 15:06:17.198470] [0x00000e6c] [info] An informational severity message
[2013-12-20 15:06:17.199470] [0x00000e6c] [warning] A warning severity message
[2013-12-20 15:06:17.199470] [0x00000e6c] [error] An error severity message
[2013-12-20 15:06:17.200470] [0x00000e6c] [fatal] A fatal severity message
请帮助我。谢谢。
P/S:我也尝试过为日志创建自定义接收器,而不是使用 Boost::Log::Trivial 但结果是一样的。Boost 1.54.0 和 1.55.0 都经过测试。