Search code examples
loggingconfigurationruntimetraceboost-log

C++ Boost libraries


I'm using boost log:

How to change (configure) severity level for logging during RUNTIME.


Solution

  • The answer is on the page you linked to. You can set a filter at any time in run time. The new filter can check against an updated severity threshold.

    Alternatively, you can set a custom filter which will use an external variable holding the level threshold. An example is given here, see the phoenix::bind part. Note that in this case you should be prepared that your filter will be called concurrently from multiple threads, and you must provide the necessary synchronization.

    std::atomic< logging::trivial::severity_level > g_threshold;
    
    void update_threshold(logging::trivial::severity_level level)
    {
        g_threshold.store(level, std::memory_order_relaxed);
    }
    
    bool my_filter(logging::value_ref< logging::trivial::severity_level > const& level)
    {
        logging::trivial::severity_level threshold =
            g_threshold.load(std::memory_order_relaxed);
        return level >= threshold;
    }
    
    // ...
    
    logging::core::get()->set_filter(
        phoenix::bind(&my_filter, expr::attr< logging::trivial::severity_level >("Severity")));