Why Boost Log write first message to the 1st log-file? (rotation in Boost Log)

Viewed 54

These flags are used: "keywords::open_mode = std::ios_base::app" and "keywords::rotation_size = <some_value>"
After running application few times, I get two log files: sample_0.log and sample_1.log
When I run application again I get strange behaviour (and problem):
Observed behaviour: the 1st log message goes to the 1st log-file (sample_0.log) and the rest goes to the 2nd log-file.
Expected behaviour: All the log-messages should go to the 2nd log-file (sample_1.log)

Here is the code:

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;

void init_logging()
{
    logging::register_simple_formatter_factory<logging::trivial::severity_level, char>("Severity");

    logging::add_file_log(
            keywords::file_name = "sample_%N.log",
            keywords::open_mode = std::ios_base::app,
            keywords::auto_flush = true,
            keywords::rotation_size = 1 * 1024,
            keywords::max_size = 3 * 1024,
            keywords::format = "%LineID%. [%TimeStamp%][%ThreadID%] [%Severity%] - %Message%");

    logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);

    logging::add_common_attributes();
}

int main(int, char*[])
{
    init_logging();

    BOOST_LOG_TRIVIAL(trace) << "This is a trace severity message";
    BOOST_LOG_TRIVIAL(debug) << "This is a debug severity message";
    BOOST_LOG_TRIVIAL(info) << "This is an informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "This is a warning severity message";
    BOOST_LOG_TRIVIAL(error) << "This is an error severity message";
    BOOST_LOG_TRIVIAL(fatal) << "This is a fatal severity message";

    return 0;
}

Where am I wrong? (I use gcc 9.3.0 and Boost 1.71)

1 Answers

Since then I've reported this with a reproducing example, and it has been fixed in Boost 1.79.0

https://github.com/boostorg/log/issues/179#issuecomment-1052623458

The reproducer

#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>

namespace logging = boost::log;
namespace keywords = boost::log::keywords;

void init_logging()
{
    logging::register_simple_formatter_factory<logging::trivial::severity_level, char>("Severity");

    auto file = logging::add_file_log(
        keywords::file_name        = "sample_%N.log",    //
        keywords::target           = "./",               //
        keywords::target_file_name = "sample_%N.log",    //
        keywords::open_mode        = std::ios_base::app, //
        keywords::auto_flush       = true,               //
        keywords::rotation_size    = 1024,               //
        keywords::max_size         = 300 * 1024,         //
        keywords::format =
            "%LineID%. [%TimeStamp%][%ThreadID%] [%Severity%] - %Message%");

    file->locked_backend()->scan_for_files();

    logging::core::get()->set_filter(logging::trivial::severity >= logging::trivial::info);

    logging::add_common_attributes();
}

int main()
{
    init_logging();

    BOOST_LOG_TRIVIAL(trace)   << "This is a  trace         severity message";
    BOOST_LOG_TRIVIAL(debug)   << "This is a  debug         severity message";
    BOOST_LOG_TRIVIAL(info)    << "This is an informational severity message";
    BOOST_LOG_TRIVIAL(warning) << "This is a  warning       severity message";
    BOOST_LOG_TRIVIAL(error)   << "This is an error         severity message";
    BOOST_LOG_TRIVIAL(fatal)   << "This is a  fatal         severity message";
}

Now, when testing with

rm -rf sample*.log
for a in {1..100}
do 
    ./build/sotest
done 
wc -l $(ls -tr sample_*.log)

Now prints the expectable

9 sample_0.log
9 sample_1.log
9 sample_2.log
...
9 sample_42.log
9 sample_43.log
4 sample_44.log
Related