2

我从https://github.com/boostorg/log/blob/master/example/basic_usage/main.cpp复制了一个程序。我将 main.cpp 重命名为 mylog.cpp。一开始我用 -std=c++0x 编译它(详细信息如下所示)。它构建得很好,但是当我运行可执行文件时出现分段错误。没有 -std=c++0x,程序运行良好。请帮忙。我对 c++ 很陌生,因此,我可能会错过一些调试信息。让我知道您是否需要任何信息来调试此问题。谢谢

注意:我在这个 main.cpp 上取消了前三行的注释

#define BOOST_LOG_USE_CHAR
#define BOOST_ALL_DYN_LINK 1
#define BOOST_LOG_DYN_LINK 1

从 Eclipse 构建设置(为了构建这个程序,我认为需要提升日志库):

make all 
Building file: ../mylog.cpp
Invoking: GCC C++ Compiler
g++ -I/home/help/usr/local/boost_1_54_0 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"mylog.d" -MT"mylog.d" -o "mylog.o" "../mylog.cpp"
Finished building: ../mylog.cpp

Building target: test
Invoking: GCC C++ Linker
g++ -L/home/help/usr/local/lib -o "test"  ./mylog.o   -lpthread -lboost_thread -lboost_system -lboost_log_setup -lboost_log
Finished building target: test

建成后,我运行这个程序并得到分段错误。

$ ./mylog
Segmentation fault (core dumped)

使用 gdb。它指向一个图书馆

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7100c0a in boost::log::v2_mt_posix::attribute_value_set::find(boost::log::v2_mt_posix::attribute_name) const () from /home/fcai/usr/local/lib/libboost_log.so.1.54.0
(gdb) list
73      return strm;
74  }
75  
76  int main(int argc, char* argv[])
77  {
78      // This is a simple tutorial/example of Boost.Log usage
79  
80      // The first thing we have to do to get using the library is
81      // to set up the logging sinks - i.e. where the logs will be written to.
82      logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%");
(gdb) bt
#0  0x00007ffff7100c0a in boost::log::v2_mt_posix::attribute_value_set::find(boost::log::v2_mt_posix::attribute_name) const () from /home/fcai/usr/local/lib/libboost_log.so.1.54.0
#1  0x000000000042713f in boost::log::v2_mt_posix::value_extractor<boost::mpl::vector2<std::string, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::expressions::tag::message>::operator() (this=0x7fffffffc6b0, name=..., attrs=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/attributes/value_extraction.hpp:232
#2  0x00007ffff74c379d in boost::log::v2_mt_posix::aux::light_function<void (boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >&)>::impl<boost::log::v2_mt_posix::(anonymous namespace)::chained_formatter<char, boost::phoenix::actor<boost::proto::exprns_::expr<boost::proto::tagns_::tag::shift_left, boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<2> >, 0l> >, boost::log::v2_mt_posix::expressions::attribute_actor<boost::mpl::vector2<std::string, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > >, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::expressions::tag::message, boost::phoenix::actor> >, 2l> > > >::invoke_impl(boost::log::v2_mt_posix::aux::light_function<void (boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >&)>::impl_base*, boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >&) () from /home/fcai/usr/local/lib/libboost_log_setup.so.1.54.0
#3  0x0000000000431b64 in boost::log::v2_mt_posix::aux::light_function<void (boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >&)>::operator()(boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >&) const (this=0x652ab8, 
    args#0=..., args#1=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/detail/light_function.hpp:444
#4  0x00000000004306db in boost::log::v2_mt_posix::basic_formatter<char>::operator() (this=0x652ab8, rec=..., strm=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/expressions/formatter.hpp:157
#5  0x0000000000430806 in boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::log::v2_mt_posix::aux::fake_mutex, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> > (this=0x6507c0, rec=..., backend_mutex=..., backend=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/sinks/basic_sink_frontend.hpp:440
#6  0x000000000042f505 in boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::try_feed_record<boost::mutex, boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> > (this=0x6507c0, 
    rec=..., backend_mutex=..., backend=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/sinks/basic_sink_frontend.hpp:489
#7  0x000000000042d28e in boost::log::v2_mt_posix::sinks::synchronous_sink<boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> >::try_consume (this=0x6507c0, rec=...)
    at /home/fcai/usr/local/boost_1_54_0/boost/log/sinks/sync_frontend.hpp:145
#8  0x00007ffff7106053 in boost::log::v2_mt_posix::core::push_record_move(boost::log::v2_mt_posix::record&) () from /home/fcai/usr/local/lib/libboost_log.so.1.54.0
#9  0x000000000041f8f1 in push_record (rec=..., this=0x651050) at /home/fcai/usr/local/boost_1_54_0/boost/log/core/core.hpp:308
#10 boost::log::v2_mt_posix::sources::basic_logger<char, boost::log::v2_mt_posix::sources::logger, boost::log::v2_mt_posix::sources::single_thread_model>::push_record_unlocked(boost::log::v2_mt_posix::record&&) (this=0x7fffffffccc0, rec=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/sources/basic_logger.hpp:268
#11 0x000000000041d2e6 in boost::log::v2_mt_posix::sources::basic_composite_logger<char, boost::log::v2_mt_posix::sources::logger, boost::log::v2_mt_posix::sources::single_thread_model, boost::log::v2_mt_posix::sources::features<> >::push_record(boost::log::v2_mt_posix::record&&) (this=0x7fffffffccc0, rec=...) at /home/fcai/usr/local/boost_1_54_0/boost/log/sources/basic_logger.hpp:582
#12 0x000000000041a9b4 in boost::log::v2_mt_posix::aux::record_pump<boost::log::v2_mt_posix::sources::logger>::~record_pump (this=0x7fffffffd170, __in_chrg=<optimized out>)
    at /home/fcai/usr/local/boost_1_54_0/boost/log/sources/record_ostream.hpp:279
#13 0x0000000000414de3 in main (argc=1, argv=0x7fffffffd7b8) at ../mylog.cpp:116
(gdb) 
4

1 回答 1

0

我遇到了同样的问题。不知何故,当我从下一行中删除 %Message% 时,它至少可以工作。但是,显然现在日志没有出现在控制台上。

logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %Message%");

将其更改为以下内容,您将看到预期的结果。

logging::add_console_log(std::clog, keywords::format = expr::stream
        << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d, %H:%M:%S.%f")
        << " --> " << expr::message );
于 2013-08-27T09:45:26.863 回答