1

当目标路径以点('.')开头时,我对 boost::log 有一些麻烦。这是我的代码(简单但已编译):

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup.hpp>
#include <boost/log/sources/logger.hpp>

void SetupLog()
{
    static bool alreadyInitialized = false;
    if( alreadyInitialized )
        return;

    alreadyInitialized = true;

    auto sink = boost::log::add_file_log(
        boost::log::keywords::target = ".log",
        boost::log::keywords::file_name = "sample_%2N.log"
        );

    boost::log::add_common_attributes();
}

typedef ::boost::log::sources::logger Logger;

class Engine {
public:
    Engine() {
        SetupLog();
        BOOST_LOG( logger ) << "Engine initialized";
    }

private:
    Logger logger;
};


int main(int argc, char* argv[])
{
    Engine engine;

    return 0;
}

如果我在创建引擎实例之前(以及在创建记录器之前)调用 SetupLog,那么它将正常工作。如果我将目标更改为“_log”(开头没有点),或者即使我完全省略与目标的行并包含关键字::file_name 的路径,也会更正创建日志:

auto sink = boost::log::add_file_log(
    boost::log::keywords::file_name = ".log/sample_%2N.log"
    );

但是当我尝试运行第一个变体时,然后:

1)如果目录“.log”不存在或为空,则将创建该文件夹,但日志将保存到工作目录,但不在该路径中(没有任何未处理的异常);

2)如果该目录已经包含一些日志,那么运行程序后我会遇到奇怪的异常:

    msvcp120d.dll!6ff0732b()    Unknown
    [Frames below may be incorrect and/or missing, no symbols loaded for msvcp120d.dll] 
>   JustSandbox.exe!`anonymous namespace'::convert_aux(const wchar_t * from, const wchar_t * from_end, char * to, char * to_end, std::basic_string<char,std::char_traits<char>,std::allocator<char> > & target, const std::codecvt<wchar_t,char,int> & cvt) Line 111    C++
    JustSandbox.exe!boost::filesystem::path_traits::convert(const wchar_t * from, const wchar_t * from_end, std::basic_string<char,std::char_traits<char>,std::allocator<char> > & to, const std::codecvt<wchar_t,char,int> & cvt) Line 197 C++
    JustSandbox.exe!boost::filesystem::path::string(const std::codecvt<wchar_t,char,int> & cvt) Line 373    C++
    JustSandbox.exe!boost::filesystem::path::string() Line 367  C++
    JustSandbox.exe!`anonymous namespace'::make_permissions(const boost::filesystem::path & p, unsigned long attr) Line 514 C++
    JustSandbox.exe!boost::filesystem::detail::status(const boost::filesystem::path & p, boost::system::error_code * ec) Line 1677  C++
    JustSandbox.exe!boost::filesystem::status(const boost::filesystem::path & p, boost::system::error_code & ec) Line 282   C++
    JustSandbox.exe!boost::filesystem::detail::create_directories(const boost::filesystem::path & p, boost::system::error_code * ec) Line 920   C++
    JustSandbox.exe!boost::filesystem::create_directories(const boost::filesystem::path & p) Line 399   C++
    JustSandbox.exe!boost::log::v2s_mt_nt5::sinks::`anonymous namespace'::file_collector::store_file(const boost::filesystem::path & src_path) Line 666 C++
    JustSandbox.exe!boost::log::v2s_mt_nt5::sinks::text_file_backend::rotate_file() Line 1262   C++
    JustSandbox.exe!boost::log::v2s_mt_nt5::sinks::text_file_backend::~text_file_backend() Line 1083    C++
    [External Code] 
    JustSandbox.exe!boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::text_file_backend>::destroy() Line 65   C++
    JustSandbox.exe!boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::text_file_backend>::operator()(boost::log::v2s_mt_nt5::sinks::text_file_backend * __formal) Line 88 C++
    JustSandbox.exe!boost::detail::sp_counted_impl_pd<boost::log::v2s_mt_nt5::sinks::text_file_backend *,boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::text_file_backend> >::dispose() Line 154   C++
    JustSandbox.exe!boost::detail::sp_counted_base::release() Line 103  C++
    JustSandbox.exe!boost::detail::shared_count::~shared_count() Line 375   C++
    [External Code] 
    JustSandbox.exe!boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::synchronous_sink<boost::log::v2s_mt_nt5::sinks::text_file_backend> >::destroy() Line 61 C++
    JustSandbox.exe!boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::synchronous_sink<boost::log::v2s_mt_nt5::sinks::text_file_backend> >::operator()(boost::log::v2s_mt_nt5::sinks::synchronous_sink<boost::log::v2s_mt_nt5::sinks::text_file_backend> * __formal) Line 88  C++
    JustSandbox.exe!boost::detail::sp_counted_impl_pd<boost::log::v2s_mt_nt5::sinks::synchronous_sink<boost::log::v2s_mt_nt5::sinks::text_file_backend> *,boost::detail::sp_ms_deleter<boost::log::v2s_mt_nt5::sinks::synchronous_sink<boost::log::v2s_mt_nt5::sinks::text_file_backend> > >::dispose() Line 154    C++
    JustSandbox.exe!boost::detail::sp_counted_base::release() Line 103  C++
    JustSandbox.exe!boost::detail::shared_count::~shared_count() Line 375   C++
    [External Code] 
    JustSandbox.exe!boost::log::v2s_mt_nt5::core::~core() Line 415  C++
    [External Code] 
    JustSandbox.exe!boost::checked_delete<boost::log::v2s_mt_nt5::core>(boost::log::v2s_mt_nt5::core * x) Line 34   C++
    JustSandbox.exe!boost::detail::sp_counted_impl_p<boost::log::v2s_mt_nt5::core>::dispose() Line 78   C++
    JustSandbox.exe!boost::detail::sp_counted_base::release() Line 103  C++
    JustSandbox.exe!boost::detail::shared_count::~shared_count() Line 375   C++
    [External Code] 

任何想法这段代码有什么问题?

PS:我使用 boost 1.54.0 和 Visual Studio Express 2013 Preview for Windows Desktop。

编辑:我认为这可能只是一些错误,但在将其提交给跟踪器之前决定查找,可能是我的代码有问题而且我不知道一些重要的事情(我只是对 boost 很陌生)。

4

2 回答 2

2

哇。我在退出时有完全相同的错误和堆栈跟踪。我认为这是我的代码中的一个错误。(使用 Visual Studio 2013 和 boost 1_55_0)

只需更改“。” 在目标名称中到其他一些字符使其工作。谢谢你的提示。

如:

//This does not work:
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
    keywords::target = "/log/%Y_%m_%d-%H_%M_%S_dbg.log",
...
));

//This works:
sink->locked_backend()->set_file_collector(sinks::file::make_collector(
    keywords::target = "/log/%Y_%m_%d-%H_%M_%S_dbg_log",
...
));

您可能应该提交该错误报告。

于 2014-01-19T13:18:19.993 回答
0

我在文档中找到了这个参考,它是一种解决方法。

它对我有用的是:

int main(int argc, char* argv[])
{
    // ...

    logging::core::get()->remove_all_sinks();

    return 0;
}
于 2017-01-02T15:03:34.323 回答