升压日志中的分段故障

时间:2017-05-10 09:51:45

标签: c++ boost-log

我无法重现这个bug,似乎与多线程问题有关。这是核心转储:

// storing the key in the session
req.session.rsa = JSON.stringify(cryptico.generateRSAKey(passPhrase, bits).toJSON());

// retrieving the key from the session
let RSAKey = cryptico.RSAKey.parse(req.session.rsa);

我的代码是:Logger.h

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

Logger.cpp

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";
}

我使用的是MT版本记录器,因此多线程问题应该由log lib本身来处理,但这种崩溃是随机发生的(我的app是多线程的)

0 个答案:

没有答案