3

apologies if I've missed a trick in the Boost log docs, but I'm really struggling to work out how best to use the Boost log throughout my program.

I'm not a complete C++ noob, but I'm far from competent in this language....!

In short:

What is the best practice to use the same Boost log in my other classes outside the main class?

My aims are to use a boost log which stores to a file as well as displays to the console.

The setup/main bit works fine:

Main entry point:

void init();

int main()
{
    /*start logging */

    init();
    logging::add_common_attributes();

    using namespace logging::trivial;
    src::severity_logger< severity_level > lg;
    BOOST_LOG_SEV(lg, info) << "KeyGeo started...";

    //rest of my program objects start here, omitted for brevity......

    return 0;
}

with an init function to setup the logger as per Boost docs

/* configuration for the boost log */
void init()
{
    logging::add_file_log
    (
        keywords::file_name = "KGLog_%N.log",
        keywords::rotation_size = 10 * 1024 * 1024,
        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
        keywords::format = "[%TimeStamp%]: %Message%",
        keywords::open_mode = std::ios_base::app,
        keywords::auto_flush = true
    );

    logging::core::get()->set_filter
    (
        logging::trivial::severity >= logging::trivial::info
    );

}

But what/how do I use this same log throughout my other classes. I could pass 'lg' as an argument as to the constructor or similar, but that seems like the hacky way. i tried various examples such as src::logger lg but those didnt work.

What do i put in here?

#include "KGSQLManager.h"
#include "KGResult.h"
#include "stdafx.h"

using namespace logging::trivial;

namespace KeyGeo
{
    class KGDataFactory
    {



       /* SOME BOOST LOG OBJECT IM GUESSING ???! */

        private:
            SQLWCHAR *connStr;
            std::vector< KGResult > results;

        public:
            KGDataFactory();
            KGSQLManager SQLManager;
            void parseSourceGeo();
            void initLogs();

    };
}

divbot
  • 41
  • 2

1 Answers1

1

I solved this myself...

I just needed to define the logger as the same type in the second class like this:

namespace KeyGeo
{
    class KGDataFactory
    {

        private:
            SQLWCHAR *connStr;
            std::vector< KGResult > results;

            /* added this.....*/
            src::severity_logger< severity_level > lg;

        public:
            KGDataFactory();
            KGSQLManager SQLManager;
            void parseSourceGeo();
            void initLogs();

    };
}

used like this:

namespace KeyGeo
{

    /* consturctor for main factory wrapper */
    KGDataFactory::KGDataFactory()
    {
        BOOST_LOG_SEV(lg, info) << "This worked in the class";
    };
divbot
  • 41
  • 2