Menu

Rotating file not appending

Help
Anonymous
2013-01-25
2013-06-03
  • Anonymous

    Anonymous - 2013-01-25

    Hi all

    Sorry again for newbie question but after spending one day playing with the library and search on the web, I just can't get my rotating log working to append to the last file written.

    Here is my code:

        // Create a text file sink
        typedef sinks::synchronous_sink< sinks::text_file_backend > file_sink;
        shared_ptr< file_sink > Fsink(new file_sink(
            keywords::file_name = "SampleTest_%2N.log",      // file name pattern
            keywords::open_mode = (std::ios::out | std::ios::app),
            keywords::rotation_size = 102240                     // rotation size, in characters
        ));
        // Set up where the rotated files will be stored
        Fsink->locked_backend()->set_file_collector(sinks::file::make_collector(
            keywords::target = "logs",                          // where to store rotated files
            keywords::max_size = 1000*1024,              // maximum total size of the stored files, in bytes
            keywords::min_free_space = 100 * 1024 * 1024        // minimum free space on the drive, in bytes
        ));
        // Upon restart, scan the target directory for files matching the file_name pattern
        Fsink->locked_backend()->scan_for_files();
        // Ok, we're ready to add the sink to the logging library
        logging::core::get()->add_sink(Fsink);
        // Now our logs will be written both to the console and to the file.
        // Let's do a quick test and output something. We have to create a logger for this.
        src::logger lg;
        BOOST_LOG(lg) << "Some log line with a counter"; // etc etc
    

    As you can tell I already set the open_mode = (std::ios::out | std::ios::app)

    as well as calling

      Fsink->locked_backend()->scan_for_files();

    before the sink the added to the core.

    But everytime I start my application, it will start produce a new SampleTest_0X.log file without appending to the last one.

    I am using MSVS10, Window 7, Boost 1.51 and the Log package would be the one comes with the package which would be v1.1

    Thanks for your time

     
  • Andrey Semashev

    Andrey Semashev - 2013-01-25

    When you scan_for_files, the backend selects the next available file counter. This makes the log file name unique each time you start your application, which prevents appending. This is expected.

     
  • Anonymous

    Anonymous - 2013-02-10

    Hi, same problem here, and I haven't found a solution yet.
    If this is the expected behaviour, is there any way to change it?
    For me the typical unix "logrotate-style" would be most interesting, is this somehow possible?

     
  • Andrey Semashev

    Andrey Semashev - 2013-02-10

    No, this cannot be customized, unless you reimplement file scanning and the file sink backend itself. I will probably change this behavior.

     
  • Andrey Semashev

    Andrey Semashev - 2013-02-10

    On the second thought, changing this will break the code when collected files are stored in a different directory than the sink target path. So I'm in favor of keeping the current behavior.

     
  • Anonymous

    Anonymous - 2013-02-10

    If you add it as an option, e.g. keywords::rotation_mode or something like that?

     
  • Andrey Semashev

    Andrey Semashev - 2013-02-10

    I don't think it will be as easy as that. The backend will create a new file with the same name as the file in the storage governed by the file collector, so the files will clash when it comes to file rotation. What you're asking requires a different file backend design, and this is a too radical change to do in v2. Maybe I will come up with another solution later.

     

Log in to post a comment.