Menu

Logging function

Victor H
2014-03-18
2014-03-18
  • Victor H

    Victor H - 2014-03-18

    Hello all.

    After a while using the logger I developed using boost::log I have new needs and I'm not sure how to implement them. The basic point is that I do not want setting the logger before some specific variables are set. That is, in the beggining of the program, some parser reads a config file in which are defined the output directories (included the logger output directory).

    So, my logger is defined using BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT and, inside this macro, I made the checks needed to set the proper directory, but it happens that when the logger is initialized, the config file is still not read. It seems to me that the log is initialized just after the include sentence. So I though could be possible if I used a init_function like the examples in settings containers and then I could use this function after the config file was read and that way, having everything properly set. However I don't know, in the case my previous explanation were rigth, how could I use the setting elements to configure different sinks I defined inside the macro. I mean,

        // Subsections can be referred to with a single path
        setts["Sinks.cout_sink"]["Destination"] = "Console";
        setts["Sinks.cout_sink"]["Filter"] = "%Rank% == 0";
        setts["Sinks.cout_sink"]["AutoFlush"] = true;
    

    would the previous code work for a sink called cout_sink? It is not clear for me how to use it properly. Any hint or help would be very apreciated. Best regards.

     
  • Andrey Semashev

    Andrey Semashev - 2014-03-18

    If the logger is declared with the BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT macro, its initialization routine is invoked when the logger is first requested. If you see it's being called before main() then you use this logger in some global constructor.

    You can warrant that the logging system is initialized before this logger is created by actually initializing logging inside this initialization routine before creating the logger. If you have multiple global loggers, you will have to do it in each logger initialization routine and also make sure you initialize logging only once. That also includes main(), if you initialize logging there too. Simply put:

    // init.cpp
    
    // Loads logging settings, sets up attributes, etc.
    void do_init_logging();
    
    boost::once_flag g_logging_initialized = BOOST_ONCE_INIT;
    
    void init_logging()
    {
        boost::call_once(g_logging_initialized, &do_init_logging);
    }
    
    // main.cpp
    
    int main()
    {
        init_logging();
        // ...
    }
    
    // Logger declarations
    
    BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(my_logger, boost::log::logger_mt)
    {
        init_logging();
        return boost::log::logger_mt();
    }
    
     
  • Victor H

    Victor H - 2014-03-18

    By the way, I don't know which header files are needed because I included the boost/log/utility/setup/settings.hpp and boost/log/utility/setup/formatter_parser.hpp but the compiler still says to me "error: ‘init_from_settings’ is not a member of ‘boost::log’" in the final line in which I wrote the same as in the examples:

    boost::log::init_from_settings(setts);
    

    where setts is the

    boost::log::settings setts 
    

    object

     
  • Andrey Semashev

    Andrey Semashev - 2014-03-18

    The associated headers are given for each component at the beginning of the corresponding section of the docs.

     

Log in to post a comment.