Shibli
Shibli

Reputation: 6149

Using boost::log in parallel environment

I ran the following code with two processes. In the output, there is only one entry. I get a different rank every time. Seems like each process overwrites the file. How can I make all processes to log a message without overwriting?

Expected result (order might be different):

0
1

Actual result:

0 // and sometimes 1

Code:

#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/mpi.hpp>

int main()
{
    boost::mpi::environment env;
    boost::mpi::communicator world;

    boost::log::add_file_log("sample.log");
    BOOST_LOG_TRIVIAL(info) << world.rank();
    return 0;
}

Upvotes: 2

Views: 293

Answers (2)

Andrey Semashev
Andrey Semashev

Reputation: 10614

In addition to what arturx64 suggested, you can also use an interprocess queue sink to publish log records in your multiple worker processes. These records can be received by a separate receiver process, which can then write the log records to a file. There is an example here.

Upvotes: 0

arturx64
arturx64

Reputation: 953

Boost log doesn't have such mechanism, but you can use separate process for logging data in the same file and provide access to this process outside. To achieve this approach you can use boost sockets or syslog backend

Upvotes: 1

Related Questions