Question

I am using the C++ Boost Log library (1.54) to do logging for my Windows application. I have defined a simple class to setup the logging macros & take a Boost config file that defines the sinks for my application.

LoggerClass.h

class LoggerClass {
  public:
    static void Initialization(const std::string& configurationFile)
}

BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, boost::log::sources::severity_logger_mt<boost::log_trivial::severity_level>);

#define LOG_INFO_MESSAGE BOOST_LOG_SEV(my_logger::get(), boost::log::trivial::severity_level::info)

LoggerClass.cpp

void LoggerClass::Initialization(const std::string& configurationFile)
{
    boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
    boost::log::register_simple_filter_factory< boost::log::trivial::severity_level, char >("Severity");

    boost::log::register_simple_formatter_factory< unsigned int, char >("Line");
    boost::log::register_simple_filter_factory< unsigned int, char >("Line");

    boost::log::register_formatter_factory("TimeStamp", boost::make_shared< time_formatter_factory >());

    std::ifstream file(configurationFile);
    boost::log::init_from_stream(file);

    boost::log::add_common_attributes();

    my_boost::get();
}

My config file:

[Core]
DisableLogging=false

[Sinks.Sink1]

Destination=Console

Asynchronous=false

AutoFlush=true

If I call these macros before setting up any sinks, the default sink writes the first message to STDOUT, as it should.

int main(int argc, char* argv[] {
  LOG_INFO_MESSAGE << "Before Initialization()"; //writes to STDOUT

  LoggerClass::Initialization("log.conf"); //sets up the console sink

  LOG_INFO_MESSAGE << "After Initialization()"; //writes to STDERR???
}

However, once I have defined a Console sink in my config file & called boost::log::init_from_stream() method, all messages end up in STDERR, not STDOUT as I would expect.

C:\Test\TestLogger.exe > std.txt 2> err.txt

std.txt

[2014-05-08 12:57:17.293972] [0x00004b58] [info]    Before Initialization()

err.txt

After Initialization()

Why don't messages go to STDOUT by default after the Console sink is setup?

Was it helpful?

Solution

According to the Boost source header file (Boost\log\utility\setup\console.hpp), the Console logger writes to std::clog, which is associated with the C stream STDERR.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top