Search code examples
c++boostboost-log

Boost.log - Get current file name when using file rotation


I know that this question may be a duplicate but I want to know whether there is still no possibility to get the current log file name when there is a file backend with filename rotation?!

Thanks in advance.

Edit:

With filename rotation I mean something like the following where the filename changes when the log file gets rotated:

auto fileSink = logging::add_file_log(
    keywords::file_name = "..\\Logs\\%Y%m%d_Tracing_%H%M%S.log",
    keywords::rotation_size = 2 * 1024 * 1024,
    keywords::min_free_space = 15 * 1024 * 1024,
    keywords::open_mode = std::ios_base::app);

Solution

  • As sehe mentioned:

    It's a property of the backend.

    Therefor I managed to separate the sink initialization from the corresponding backend:

    typedef sinks::synchronous_sink<sinks::text_file_backend> fileSink;
    boost::shared_ptr<fileSink> fileSinkSink;
    fileSinkSink = logging::add_file_log(
        keywords::file_name = "..\\Logs\\%Y%m%d_Tracing_%H%M%S.log",
        keywords::rotation_size = 2* 1024 * 1024,
        keywords::min_free_space = 15 * 1024 * 1024,
        keywords::open_mode = std::ios_base::app);
    

    Now I figured out that I can access the backend and thereby the current file name:

    char currentFileName[200] = "";
    strcpy(currentFileName, fileSinkSink->locked_backend().get()->get_current_file_name().string().c_str());
    

    Thanks for that hint! It solved my problem!