BOOST_LOG_TRIVIAL ( severity ) << … does not

2019-06-07 02:51发布

问题:

We are using the simplest version of boost.log v2 with BOOST_LOG_TRIVIAL ( severity ) << ...

For the moment we would not like to create a separate sink or output file, sometimes redirecting the console output to a file is good enough for our purposes. Under CentOs 7 with boost.log v1.59 we have noticed that many times the output is not redirected but instead lost completely when using various redirection options like <executable> 2>&1 out.txt etc. The file ends up with zero length and no screen output is present naturally.

Has anybody observed this behaviour? Is there a known fix? Many thanks, filimon

回答1:

thanks, I have seen that the trivial logging is basically std::cout this is why I am surprised it does not work as expected (and I also explained it to the colleagues who complained...). The logging output works perfectly for us on console. Not sure what you mean by "setup the standard output streams in the application or its parent", I only do in my main()

boost::log::core::get()->set_filter ( boost::log::trivial::severity >= vm["boost_log_level"].as() );

and this works as expected on the console for different command line settings of severity level when simply doing BOOST_LOG_TRIVIAL (...) << ...

Any specific pointer on where to look at in more detail to debug this? Could it be some thread-related issue, I do get my initialization messages from one thread but seem to miss the ones for another thread, as I said this only happens when redirecting, I can happily see the messages from all different threads in the console...



回答2:

This is explanation of that happens not what could be done. Output is buffered, that's why you can't see anything in file but can see in console. Just wait for it to reach 4096 bytes. Need some way of flushing the buffer...



回答3:

The syntax 2>&1 out.txt is not how you redirect output to a file. If you want to redirect both stdout and stderr to a file, you need to do either >& out.txt or >out.txt 2>&1. The first form is not supported by all shells, but the second form should be.