Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2005-06-21 05:32:54


Hi Andy,

Sorry for not explaining the role of flush_log_cache() in the docs.

The flush_log_cache() should be called after you've *initialized* all
the logs (in other words, after you've added all appenders and/or
modifiers).

In your code, it should be:

// note: no need for the below call
// set_log_caching(true);
        logger l_lgArray("lgArray");
        logger l_lgCout("lgCout");
        add_appender("lgCout", write_to_cout);

        add_appender("lgArray",
                ts_appender(
                        appender_array()
                                .add(write_to_file("btArrayFile1.txt"))
                                .add(write_to_file("btArrayFile2.txt"))
                                .add(write_to_file("btArrayFile3.txt"))
                                .add(write_to_file("btArrayFile4.txt"))));
        flush_log_cache();
        ThreadTest(l_lgArray, 1, 20000);

The purpose of flush_log_cache() is to flush everything you've written
to logs, before they were ***fully initialized***. This is especially
useful in case you do logging when static variables are initialized --
without this mechanism, logging would happen too soon, and would end up
writing nowhere.

Example:

BOOST_DEFINE_LOG(app,"app");
BOOST_LOG(app) << "where does this get written?"; // [1]
add_appender("app", write_to_cout);
add_appender("app", write_to_debug);
// here, it knows where to log [1]
flush_log_cache();

As for making all pending writes complete -- this is a defect of
ts_appender() class -- which I want to correct.

Best,
John

> I'm not sure I understand correctly, but it appears that flush_log_cache
> in the logging library doesn't actually force everything in the pipeline
> to be written by the logging thread to be written. Empirically, it seems
> that I had to add a sleep to the end of my program to cause everything
> to be written. Code-review-wise, and here I'm pretty sure I don't
> completely understand, it appears that flush_log_cache pushes things
> further down the pipeline but doesn't actually synchronize (wait for)
> the writing thread to complete. It seems like there maybe should be some
> kind of routine that "blocks" until all pending writes are complete,
> maybe just a wrapper around the ability to request the writing thread to
> finish, and then wait for it to complete. Maybe this mechanism is
> already present? I suppose that brings up other complications, such as
> what if some other thread logs something after we've asked it to stop.
>
> In the code I was testing, I had a ts_appender, to which I added a
> appender_array, to which I added 4 write_to_files. For ~ 10 seconds I
> wrote to the log. Then I call flush_log_cache. If I don't then sleep a
> few seconds, the files are not all identical.
>

>

-- 
John Torjo,    Contributing editor, C/C++ Users Journal
-- "Win32 GUI Generics" -- generics & GUI do mix, after all
-- http://www.torjo.com/win32gui/ -v1.6.3 (Resource Splitter)
-- http://www.torjo.com/cb/ - Click, Build, Run!

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk