0

I try to log with Boost library 1.61, rotating file once size exceeded 5Ko. But, every time program runs, it begins from the first log file, exceeding max size !

That is, it appends text to flog000.txt, then to flog001.txt.

But the desired behaviour was: append to the last file (flog002.txt) then continue with flog003.txt...

Here is my code:

#include <boost/log/common.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/core.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>

void init()
{
   boost::log::add_file_log
      (
      boost::log::keywords::file_name = "flog%3N.txt",
      boost::log::keywords::open_mode = std::ios_base::app,
      boost::log::keywords::rotation_size = 5 * 1024,
      boost::log::keywords::auto_flush = true
      );
}

int main()
{
   init();
   boost::log::sources::logger lg;
   boost::log::add_common_attributes();

   for (int i(0); i < 1000; ++i)
   {
      BOOST_LOG(lg) << "Hello : " << i << std::endl;
   }
    return 0;
}
yO_
  • 1,135
  • 2
  • 12
  • 18
  • Already saw these 3 questions: https://stackoverflow.com/questions/28020692/boost-log-clears-log-file , https://stackoverflow.com/questions/25505734/boost-logger-append-to-file and https://stackoverflow.com/questions/34037325/problems-with-file-rotation-after-adding-append-flag-in-boost-1-59-log-v2 . – yO_ Jun 05 '18 at 13:01
  • Possible duplicate of [Boost.Log - how to configure a text sink backend to append to rotated files](https://stackoverflow.com/questions/8418917/boost-log-how-to-configure-a-text-sink-backend-to-append-to-rotated-files) – Alan Birtles Jun 05 '18 at 13:09
  • @AlanBirtles Not exactly, because here there is no file collecting. Also didn't see a working solution there... – yO_ Jun 05 '18 at 13:13
  • looks the same to me, I agree that there isn't a satisfactory answer in that question but that doesn't mean it isn't a duplicate. boost log just doesn't seem to support this behaviour. – Alan Birtles Jun 05 '18 at 13:16
  • @AlanBirtles Mmm ... maybe, yes. But it would be very disappointing not to be able to do something so easy ... – yO_ Jun 05 '18 at 13:28
  • This similar question is also unanswered (after the comment): https://stackoverflow.com/questions/10581405/how-to-configure-boost-log-for-writing-with-rotating-and-appending?noredirect=1&lq=1 – yO_ Jun 05 '18 at 13:30

1 Answers1

1

"There's no file collecting" - this is at least a part of the problem.

It does seem impossible to have scanning and append to an existing as well.

However, if you do scanning, you can at least prevent extra log messages from being wrongly appended to the older log files:

Live On Coliru

#include <boost/log/common.hpp>
#include <boost/log/core.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sources/logger.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/setup/file.hpp>

void init() {
    auto s = boost::log::add_file_log(
            boost::log::keywords::file_name = "flog%3N.txt",
            boost::log::keywords::open_mode = std::ios_base::app,
            boost::log::keywords::rotation_size = 5 * 1024,
            boost::log::keywords::auto_flush = true
        );

    auto be = s->locked_backend();
    be->set_file_collector(boost::log::sinks::file::make_collector(
                boost::log::keywords::target = "./", // log file & target have same dir
                boost::log::keywords::max_size = 50 * 1024,
                boost::log::keywords::min_free_space = 100000
                ));

    be->scan_for_files(boost::log::sinks::file::scan_method::scan_matching, true);
}

int main() {
    init();
    boost::log::sources::logger lg;
    boost::log::add_common_attributes();

    for (int i(0); i < 1000; ++i) {
        BOOST_LOG(lg) << "Hello : " << i << std::endl;
    }
}

After one run:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:27 flog000.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:27 flog001.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:27 flog002.txt

After two runs:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:27 flog003.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:27 flog004.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:27 flog005.txt

After three runs:

-rw-rw-r-- 1 sehe sehe 5116 jun  6 00:28 flog006.txt
-rw-rw-r-- 1 sehe sehe 5109 jun  6 00:28 flog007.txt
-rw-rw-r-- 1 sehe sehe 2665 jun  6 00:28 flog008.txt
sehe
  • 374,641
  • 47
  • 450
  • 633