Search code examples
c++boostboost-log

Boost Log to File not working


I can't figure out why this example code doesn't work right. My compiler says that the functions being called are not members of the declared namespace. This is the example code for Boost log so why doesn't it work? What do I need to do?

I've already defined BOOST_LOG_DYN_LINK, and I have all of the headers included, that should need to be included. Additionally boost was installed via yum from the fedora repos, according to yum, the boost version is 1.55.0.

Example: http://www.boost.org/doc/libs/1_55_0/libs/log/example/doc/tutorial_file.cpp

Errors

main.cpp:33:5: error: ‘add_file_log’ is not a member of ‘logging’
     logging::add_file_log(
     ^
main.cpp:34:10: error: ‘file_name’ is not a member of ‘keywords’
          keywords::file_name = "sample_%N.log",                                        /*< file name pattern >*/
          ^
main.cpp:35:10: error: ‘rotation_size’ is not a member of ‘keywords’
          keywords::rotation_size = 10 * 1024 * 1024,                                   /*< rotate files every 10 MiB... >*/
          ^
main.cpp:36:10: error: ‘time_based_rotation’ is not a member of ‘keywords’
          keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
          ^
main.cpp:36:49: error: ‘sinks::file’ has not been declared
          keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/

Code

#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>

namespace logging = boost::log;
namespace src = boost::log::sources;
namespace sinks = boost::log::sinks;
namespace keywords = boost::log::keywords;

void init()
{
    logging::add_file_log
    (
        keywords::file_name = "sample_%N.log",
        keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %Message%"
    );

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );
}

Make invocation

g++ -c -DBOOST_LOG_DYN_LINK -o main.cpp.o main.cpp

Linker flags: -lboost_program_options -lboost_log -lboost_filesystem -lboost_system -lboost_thread -lpthread

Verbose Log: https://gist.github.com/HSchmale16/d4dd5656a47ce82c63b2


Solution

  • Check your header files for anything missing and also include -lboost_log_setup to your compiler/linker invocation.