[logging] flushing logs

I'm just getting started with John Torjo's logging library. So far the library looks really useful, but I'm having problems understanding the timing/sequencing of output by the logging objects. I'm trying to get the logs to dump their output to std:cerr immediately: // ----------------------------- #include <iostream> #include <boost/log/log.hpp> #include <boost/log/functions.hpp> namespace logging = boost::logging; BOOST_DECLARE_LOG(cerrLog); BOOST_DEFINE_LOG(cerrLog,"cerrLog"); void write_to_cerr(const std::string &, const std::string &msg) { std::cerr << msg; std::cerr.flush();} int main(){ logging::add_modifier("cerrLog",logging::prepend_time("[$hh:$mm:$ss] "), logging::DEFAULT_INDEX-10); logging::add_appender("cerrLog",write_to_cerr,logging::DEFAULT_INDEX+1); std::cerr << "1" << std::endl; BOOST_LOG(cerrLog) << "2" << std::endl; std::cerr << "3" << std::endl; BOOST_LOG(cerrLog) << "4" << std::endl; } // ----------------------------- However, the output from running this is always something like: 1 3 [14:56:35] 2 [14:56:35] 4 So the logger output is "late". Is there some flag I can set (or appender I can use) that will force output to go to the target streams immediately or is this outside the design of the logging library? In the event that it matters: I'm using version 1.33 of the logging library and I've tested this on both Windows (vs .net 2003) and Linux (g++ v3.2). Thanks, -greg

Hello, Greg Landrum wrote:
I'm trying to get the logs to dump their output to std:cerr immediately: [snip] However, the output from running this is always something like: 1 3 [14:56:35] 2 [14:56:35] 4
So the logger output is "late". Is there some flag I can set (or appender I can use) that will force output to go to the target streams immediately or is this outside the design of the logging library?
I also happened to stumble accross this. The problem is that log caching is turned on initially and you have to explicitly turn it off. Basically, it should work if you change your code to this: int main(){ logging::add_modifier("cerrLog",logging::prepend_time("[$hh:$mm:$ss] "), logging::DEFAULT_INDEX-10); logging::add_appender("cerrLog",write_to_cerr,logging::DEFAULT_INDEX+1); // now flush the log cache; this also turns off log caching, so from now // on all log output is not cached, but written out immediately logging::flush_log_cache(); std::cerr << "1" << std::endl; BOOST_LOG(cerrLog) << "2" << std::endl; std::cerr << "3" << std::endl; BOOST_LOG(cerrLog) << "4" << std::endl; } See the docs for more details on this. Best Regards, Martin TAB Austria Haiderstraße 40 4052 Ansfelden Austria Phone: +43 7229 78040-218 Fax: +43 7229 78040-209 E-mail: martin.ecker@tab.at http://www.tab.at

martin.ecker@tab.at wrote:
Hello,
Greg Landrum wrote:
So the logger output is "late". Is there some flag I can set (or appender I can use) that will force output to go to the target streams immediately or is this outside the design of the logging library?
I also happened to stumble accross this. The problem is that log caching is turned on initially and you have to explicitly turn it off. Basically, it should work if you change your code to this:
This was exactly the problem; adding a call to boost::logging::set_log_caching(false) cleared up the ordering problems. I had misunderstood the purpose of log caching. Thanks for the quick answer, -greg

On 6/30/05, Greg Landrum <greg.landrum@gmail.com> wrote:
martin.ecker@tab.at wrote: This was exactly the problem; adding a call to boost::logging::set_log_caching(false) cleared up the ordering problems.
I had misunderstood the purpose of log caching.
It is a behaviour described in documentation. The caching is turned on by default, in order to allow the entries logged _before_ log initialisation not to be lost. -- Best regards, Zigmar
participants (3)
-
Greg Landrum
-
martin.ecker@tab.at
-
Pavel Antokolsky aka Zigmar