【问题标题】:Unhandled exception from boost::log when target path file begins with dot ('.')当目标路径文件以点 ('.') 开头时,来自 boost::log 的未处理异常
【发布时间】:2013-08-08 19:52:45
【问题描述】:

当目标路径以点 ('.') 开头时,我在使用 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;
}

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

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 )。

【问题讨论】:

    标签: c++ path unhandled-exception boost-log


    【解决方案1】:

    哇。我在退出时有完全相同的错误和堆栈跟踪。我认为这是我的代码中的一个错误。 (使用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",
    ...
    ));
    

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

    【讨论】:

      【解决方案2】:

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

      对我有用的是:

      int main(int argc, char* argv[])
      {
          // ...
      
          logging::core::get()->remove_all_sinks();
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-03-29
        • 1970-01-01
        • 1970-01-01
        • 2013-09-14
        • 2017-11-26
        • 2022-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多