C++ boost日志中的分段错误

C++ boost日志中的分段错误,c++,boost-log,C++,Boost Log,我无法重现这个bug,它似乎与多线程问题有关。以下是堆芯转储: Program terminated with signal SIGSEGV, Segmentation fault. #0 0x0000000000678c98 in boost::log::v2s_mt_posix::attribute_value_set::~attribute_value_set() () [Current thread is 1 (Thread 0x7f40c3731700 (LWP 24642))] (

我无法重现这个bug,它似乎与多线程问题有关。以下是堆芯转储:

Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000678c98 in boost::log::v2s_mt_posix::attribute_value_set::~attribute_value_set() ()
[Current thread is 1 (Thread 0x7f40c3731700 (LWP 24642))]
(gdb) bt
#0  0x0000000000678c98 in boost::log::v2s_mt_posix::attribute_value_set::~attribute_value_set() ()
#1  0x000000000067d4ec in boost::log::v2s_mt_posix::core::open_record(boost::log::v2s_mt_posix::attribute_set const&) ()
#2  0x000000000041c65c in boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_channel_logger_mt<Logger::severity_level, ChannelType>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, Logger::severity_level const> > (this=0x7f40b4005148) at /usr/local/include/boost/log/sources/basic_logger.hpp:259
#3  boost::log::v2s_mt_posix::sources::basic_channel_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_channel_logger_mt<Logger::severity_level, ChannelType>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >, ChannelType>::open_record_with_channel_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, Logger::severity_level const> > (args=..., this=0x7f40b4005148) at /usr/local/include/boost/log/sources/channel_feature.hpp:195
#4  boost::log::v2s_mt_posix::sources::basic_channel_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_channel_logger_mt<Logger::severity_level, ChannelType>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >, ChannelType>::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, Logger::severity_level const> > (args=..., this=0x7f40b4005148) at /usr/local/include/boost/log/sources/channel_feature.hpp:171
#5  boost::log::v2s_mt_posix::sources::basic_severity_logger<boost::log::v2s_mt_posix::sources::basic_channel_logger<boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_channel_logger_mt<Logger::severity_level, ChannelType>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex> >, ChannelType>, Logger::severity_level>::open_record_unlocked<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, Logger::severity_level const> > (args=..., this=0x7f40b4005148)
    at /usr/local/include/boost/log/sources/severity_feature.hpp:253
#6  boost::log::v2s_mt_posix::sources::basic_composite_logger<char, boost::log::v2s_mt_posix::sources::severity_channel_logger_mt<Logger::severity_level, ChannelType>, boost::log::v2s_mt_posix::sources::multi_thread_model<boost::log::v2s_mt_posix::aux::light_rw_mutex>, boost::log::v2s_mt_posix::sources::features<boost::log::v2s_mt_posix::sources::severity<Logger::severity_level>, boost::log::v2s_mt_posix::sources::channel<ChannelType> > >::open_record<boost::parameter::aux::tagged_argument<boost::log::v2s_mt_posix::keywords::tag::severity, Logger::severity_level const> > (this=0x7f40b4005148, args=...)
    at /usr/local/include/boost/log/sources/basic_logger.hpp:459
程序以信号SIGSEGV终止,分段故障。
#boost::log::v2s_mt_posix::attribute_value_set::~ attribute_value_set()()
[当前线程为1(线程0x7f40c3731700(LWP 24642))]
(gdb)英国电信
#boost::log::v2s_mt_posix::attribute_value_set::~ attribute_value_set()()
#boost::log::v2s\u mt\u posix::core::open\u记录中的1 0x000000000067d4ec(boost::log::v2s\u mt\u posix::attribute\u set const&)()
#boost::log::v2s_mt_posix::sources::basic_logger::open_record_unlocked(this=0x7f40b4005148)at/usr/local/include/boost/log/sources/basic_logger.hpp:259
#3 boost::log::v2s_mt_posix::sources::basic_channel_logger::open_record_,在/usr/local/include/boost/log/sources/channel_功能中解锁(args=…,this=0x7f40b4005148)并打开_channel___记录。hpp:195
#4 boost::log::v2s_mt_posix::sources::basic_channel_logger::open_record_unlocked(args=…,this=0x7f40b4005148)at/usr/local/include/boost/log/sources/channel_功能。hpp:171
#5 boost::log::v2s\u mt\u posix::sources::basic\u severity\u logger::open\u record\u unlocked(args=…,this=0x7f40b4005148)
at/usr/local/include/boost/log/sources/severity_feature.hpp:253
#6 boost::log::v2s_mt_posix::sources::basic_composite_logger::open_record(this=0x7f40b4005148,args=…)
at/usr/local/include/boost/log/sources/basic_logger.hpp:459
这是我的代码:Logger.h

enum ChannelType {
    main_channel,
    motion_check_channel
};
BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", ChannelType)

class Logger {
public:
    enum severity_level
    {
        debug,
        info,
        warning,
        error,
        fatal
    };
    static void init(Logger::severity_level level);
    static std::mutex mx;
};
#define LOGD BOOST_LOG_STREAM_WITH_PARAMS( gl::get(), (set_get_attrib("File", path_to_filename(__FILE__))) (set_get_attrib("Line", __LINE__))\
        (boost::log::keywords::severity = (Logger::debug)) )
typedef boost::log::sources::severity_channel_logger_mt< Logger::severity_level, ChannelType > logger_type1;
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(gl, logger_type1, (boost::log::keywords::channel = main_channel))
inline decltype(auto) set_get_attrib(const char* name, const std::string& value) {
    std::lock_guard<std::mutex> lk(Logger::mx);
    auto attr = boost::log::attribute_cast<boost::log::attributes::mutable_constant<std::string>>(boost::log::core::get()->get_global_attributes()[name]);
    attr.set(value);
    return attr.get();
}

inline decltype(auto) set_get_attrib(const char* name, const int value) {
    std::lock_guard<std::mutex> lk(Logger::mx);
    auto attr = boost::log::attribute_cast<boost::log::attributes::mutable_constant<int>>(boost::log::core::get()->get_global_attributes()[name]);
    attr.set(value);
    return attr.get();
}


inline std::string path_to_filename(const std::string& path)
{
    return path.substr(path.find_last_of("/\\") + 1);
}

int main() {
    Logger::init(Logger::debug);
    LOGD<<"something";
}
enum ChannelType{
主航道,
运动检查通道
};
BOOST\u LOG\u属性\u关键字(频道,“频道”,频道类型)
类记录器{
公众:
枚举严重性\u级别
{
调试,
信息,
警告
错误,
致命的
};
静态void init(记录器::严重性_级别);
静态std::互斥mx;
};
#使用参数(gl::get(),(设置get属性(“文件”,路径到文件名(“文件”)))(设置get属性(“行”),(行))\
(boost::log::keywords::severity=(记录器::调试)))
typedef boost::log::sources::severity_channel_logger_mtlogger_type1;
BOOST_LOG_INLINE_GLOBAL_LOGGER_CTOR_ARGS(gl,LOGGER_type1,(BOOST::LOG::keywords::channel=main_channel))
内联decltype(自动)set_get_attrib(常量字符*名称,常量标准::字符串和值){
std::锁紧保护lk(记录器::mx);
auto attr=boost::log::attribute_cast(boost::log::core::get()->get_global_attributes()[name]);
属性集(值);
返回attr.get();
}
内联decltype(自动)set_get_attrib(常量字符*名称,常量int值){
std::锁紧保护lk(记录器::mx);
auto attr=boost::log::attribute_cast(boost::log::core::get()->get_global_attributes()[name]);
属性集(值);
返回attr.get();
}
内联标准::字符串路径到文件名(常量标准::字符串和路径)
{
返回path.substr(path.find\u last\u of(“/\”)+1);
}
int main(){
Logger::init(Logger::debug);
LOGDget()->添加_全局_属性(“范围”,boost::log::attributes::named_Scope());
核心->获取()->添加全局属性(“文件”,boost::log::attributes::可变常量(“”);
core->get()->添加_全局_属性(“行”,boost::log::attributes::可变_常量(0));
//核心->设置过滤器(boost::log::expressions::attr(“严重性”)>=level);
#ifdef_调试
{
typedef boost::log::sinks::synchronous_sink cout_sink;
boost::shared_ptr sink=boost::make_shared();
boost::shared_ptr stream(&std::clog,boost::null_deleter());
sink->locked_backend()->添加_流(stream);
sink->set_filter(通道==主通道和boost::log::expressions::attr(“严重性”)>=级别);
核心->添加水槽(水槽);
}
boost::log::添加文件\u日志(
boost::log::keywords::filter=channel==main_channel&&boost::log::expressions::attr(“严重性”)>=level,
//boost::log::keywords::filter=通道==主通道,
boost::log::keywords::file_name=“./log/%Y%m%d.log”,
boost::log::keywords::auto_flush=true,
boost::log::keywords::open_mode=(std::ios::out | std::ios::app),
boost::log::keywords::time\u-based\u-rotation=boost::log::sinks::file::rotation\u在时间点(0,0,0),
//boost::log::keywords::format=“[%TimeStamp%][%\uuuuu-LINE\uuuuuu%](%LineID%){%ThreadID%}:%Message%”
boost::log::keywords::format=(
boost::log::expressions::stream

你介意删除水平滚动条吗?如果可能的话,请发布完整的堆栈跟踪。我只粘贴与boost日志相关的堆栈,其他的是我的代码,它们只是调用LOGDI。我不完全确定这是否是崩溃的原因,但我的第一个怀疑是你的
set\u get\u attrib
函数,它们可能不正确并且没有执行w首先,
core::get_global_attributes()
返回属性集的副本,因此您无法添加/删除核心使用的属性。接下来,下标运算符和
属性\u cast
都可能返回一个空属性,指示没有此类属性或它具有不同的类型。您可以对其调用
,而无需检查。接下来,从
set\u get\u attrib
未被日志记录器使用并被丢弃。我使用它们在日志中添加文件名和行号,它确实有效。坦率地说,我不知道它是如何工作的,这个库的某些部分对我来说太复杂了。因此我正在搜索并继续尝试,找出它的工作原理。也许你可以展示添加文件名和行号的正确用法电话号码?
void Logger::init(Logger::severity_level level)
{
    boost::log::add_common_attributes();
    boost::shared_ptr<boost::log::core> core = boost::log::core::get();
    core->set_exception_handler(boost::log::make_exception_suppressor()); //disable exception. in case file write permission problem

    core->get()->add_global_attribute("Scope", boost::log::attributes::named_scope()); 
    core->get()->add_global_attribute("File", boost::log::attributes::mutable_constant<std::string>(""));
    core->get()->add_global_attribute("Line", boost::log::attributes::mutable_constant<int>(0));
    //core->set_filter(boost::log::expressions::attr<Logger::severity_level>("Severity") >= level);
#ifdef _DEBUG
    {
        typedef boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend> cout_sink;
        boost::shared_ptr<cout_sink> sink = boost::make_shared<cout_sink>();
        boost::shared_ptr<std::ostream> stream(&std::clog, boost::null_deleter());
        sink->locked_backend()->add_stream(stream);
        sink->set_filter(channel == main_channel && boost::log::expressions::attr<Logger::severity_level>("Severity") >= level);
        core->add_sink(sink);
    }
    boost::log::add_file_log(
        boost::log::keywords::filter = channel == main_channel && boost::log::expressions::attr<Logger::severity_level>("Severity") >= level,
        //boost::log::keywords::filter = channel == main_channel,
        boost::log::keywords::file_name = "./log/%Y%m%d.log",
        boost::log::keywords::auto_flush = true,
        boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
        boost::log::keywords::time_based_rotation = boost::log::sinks::file::rotation_at_time_point(0, 0, 0), 
        //boost::log::keywords::format = "[%TimeStamp%] [%__LINE__%] (%LineID%) {%ThreadID%}: %Message%"
        boost::log::keywords::format = (
            boost::log::expressions::stream
            << "[" << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d_%H:%M:%S.%f") << "] "
            << "(" << boost::log::expressions::attr<unsigned int>("LineID") << ") "
            << "(" << boost::log::expressions::attr<boost::log::aux::thread::id>("ThreadID") << ") "
            << '[' << boost::log::expressions::attr<std::string>("File") << ":"
            << boost::log::expressions::attr<int>("Line") << "] "
            << boost::log::expressions::smessage
            )
        );
}