I was completely confused by the following compilation error in the last three hours. Can someone tell me what is going on here?
I tried to define the log :: formatter format (indicated below) as my own variable, so it could be reused in several places. However, I get a compilation error when trying to reuse it.
However, if I completely get rid of this variable and copy and paste the code instead, it compiles. What the heck? Is there a way to do what I want?
boost::shared_ptr<log::core> logger = log::core::get(); logger->set_logging_enabled( enabled ); logger->set_filter(trivial::severity >= level); logger->add_global_attribute( "TimeStamp", attr::local_clock()); logger->add_global_attribute( "ProcessID", attr::current_process_id()); logger->add_global_attribute( "ThreadID", attr::current_thread_id()); // want this to be it own variable log::formatter fmt = expr::stream << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] " << "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] " << "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] " << "[" << expr::attr< std::string >("Channel") << "] " << "[" << expr::attr< severity_level >("Severity") << "]: " << expr::smessage ; // so it can be reused here, but this is a compiler error log::add_console_log( std::clog, keywords::format=fmt); // and here, too. But this is also a compiler error log::add_file_log( "test.log", keywords::format=fmt);
Compilation error (using clang ++):
In file included from ../src/util/logging/Logging.cpp:34: In file included from /opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:22: /opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:107:21: error: no matching function for call to 'acquire_formatter' s.set_formatter(aux::acquire_formatter(args[keywords::format])); ^~~~~~~~~~~~~~~~~~~~~~ /opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:76:5: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::aux::setup_format boost::log::v2_mt_posix::sinks::synchronous_sink<boost::log::v2_mt_posix::sinks::basic_text_ostream_backend<char> >, boost::parameter::aux::tagged_argument<boost::log::v2 posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here aux::setup_formatter(*pSink, args, ^ /opt/boost-1.54.0/include/boost/log/utility/setup/console.hpp:136:12: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::aux::add_consol g<char, boost::parameter::aux::tagged_argument<boost::log::v2_mt_posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here return aux::add_console_log(strm, arg1); ^ ../src/util/logging/Logging.cpp:121:2: note: in instantiation of function template specialization 'boost::log::v2_mt_posix::add_console_log<char, boost::parameter::aux::t d_argument<boost::log::v2_mt_posix::keywords::tag::format, boost::log::v2_mt_posix::basic_formatter<char> > >' requested here log::add_console_log( ^ /opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:80:33: note: candidate template ignored: failed template argument deduction inline basic_formatter< CharT > acquire_formatter(const CharT* formatter) ^ /opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:85:33: note: candidate template ignored: failed template argument deduction inline basic_formatter< CharT > acquire_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& formatter) ^ /opt/boost-1.54.0/include/boost/log/detail/sink_init_helpers.hpp:91:5: note: candidate template ignored: disabled by 'enable_if' [with FormatterT = boost::log::v2_mt_posi asic_formatter<char>] phoenix::is_actor< FormatterT >, ^
However, if I change it to this instead, it compiles:
boost::shared_ptr<log::core> logger = log::core::get(); logger->set_logging_enabled( enabled ); logger->set_filter(trivial::severity >= level); logger->add_global_attribute( "TimeStamp", attr::local_clock()); logger->add_global_attribute( "ProcessID", attr::current_process_id()); logger->add_global_attribute( "ThreadID", attr::current_thread_id()); // copy and paste the expression log::add_console_log( std::clog, keywords::format = ( expr::stream << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] " << "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] " << "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] " << "[" << expr::attr< std::string >("Channel") << "] " << "[" << expr::attr< severity_level >("Severity") << "]: " << expr::smessage ) ); // copy and paste the expression again log::add_file_log( "test.log", keywords::format = ( expr::stream << "[" << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%d.%m.%Y %H:%M:%S.%f") << "] " << "[" << expr::attr< attr::current_process_id::value_type >("ProcessID") << "] " << "[" << expr::attr< attr::current_thread_id::value_type >("ThreadID") << "] " << "[" << expr::attr< std::string >("Channel") << "] " << "[" << expr::attr< severity_level >("Severity") << "]: " << expr::smessage ) );