1

I'm learning the Boost.Log library. I want to send messages to a file and std::clog. I have the following class:

class logger
{
    public:
        explicit logger(
            const std::string& tag,
            const std::string& file,
            bool console
        )
        {
            boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");

            std::string the_format = "[%TimeStamp%] (%LineID%) [%Severity%]";
            if(!tag.empty()) {
                m_log_.add_attribute(
                    "Tag",
                    boost::log::attributes::constant< std::string >( tag )
                );
                the_format += " [%Tag%]";
            }

            the_format += ": %Message%";

            if(console) {
                boost::log::add_console_log(
                    std::clog,
                    boost::log::keywords::auto_flush = true,
                    boost::log::keywords::format = the_format
                );
            }

            if(!file.empty()) {
                boost::log::add_file_log(
                    boost::log::keywords::file_name = file,
                    boost::log::keywords::auto_flush = true,
                    boost::log::keywords::open_mode = (std::ios::out | std::ios::app),
                    boost::log::keywords::format = the_format
                );
            }

        }

        ~logger(void)
        { }

        void log(
            const std::string& msg
        )
        {
            BOOST_LOG_SEV ( m_log_, boost::log::trivial::info ) << msg;
        }

    private:
        boost::log::sources::severity_logger< boost::log::trivial::severity_level > m_log_;

}; // logger

I have the following main() function:

void x()
{
    logger lg("omega", "", true);
    lg.log( "Goodbye" );
}

int main(int argc, char** argv)
{
    logger lg( "alfa", "", true );
    lg.log( "Hello world!!!");
    x();
    return 0;
}

I do not understand why the duplicate message is displayed: "Hello world!!!":

[2016-Aug-23 17:51:36.852912] (1) [info] [alfa]: Hello world!!!
[2016-Aug-23 17:51:36.853359] (2) [info] [omega]: Goodbye
[2016-Aug-23 17:51:36.853359] (2) [info] [omega]: Goodbye

UPDATE: Sorry, the example was incomplete.

Juan Solo
  • 359
  • 1
  • 5
  • 17

1 Answers1

3

logger's constructor is calling add_console_log() and add_file_log(). If you construct logger twice, these functions will also be called twice. Since they add global sinks, every log entry will be duplicated twice on the console and in your file.

int main(int argc, char** argv)
{
    logger lg1("1", "", true);
    logger lg2("2", "", true);
    logger lg3("3", "", true);
    logger lg4("4", "", true);

    lg1.log("test");
}

This will output:

[2016-Aug-23 13:25:56.468382] (1) [info] [1]: test
[2016-Aug-23 13:25:56.468382] (1) [info] [1]: test
[2016-Aug-23 13:25:56.468382] (1) [info] [1]: test
[2016-Aug-23 13:25:56.468382] (1) [info] [1]: test

You probably want some sort of reference count or flag to only call these functions once.

isanae
  • 3,253
  • 1
  • 22
  • 47
  • @isanea: Thanks, the problem was fixed. One more thing, if I use streams in the example, How do to prevent that output will be duplicated in every streams? – Juan Solo Aug 25 '16 at 08:44
  • @JuanSolo (Make sure you type usernames correctly, or select them from the list that pops up. I didn't get a notification about your message.) What do you mean? The problem is with `add_console_log()` being called multiple times, not streams. – isanae Aug 28 '16 at 01:45
  • Now, I use files in the example. In this case, `Goodbye` appears at the output of the first logger. The documentation says that this behavior is correct. Can I change this behavior? – Juan Solo Aug 29 '16 at 16:55
  • @JuanSolo I don't understand what behaviour you want to change. If you only call `add_console_log()` once, the output will only appear once. – isanae Aug 29 '16 at 17:40
  • : I configure the logger to use files (I don't use the `add_console_log()` function, I use only the `add_file_log()` function). [Here](http://coliru.stacked-crooked.com/a/3d7de5f63e654493), you can see an example (It is not executable because I do not know how to write files in coliru but you can understand what I ask). – Juan Solo Aug 30 '16 at 10:54
  • @JuanSolo I don't understand your question. Since it seems unrelated to your original one, I suggest you ask a new one with more details. You can add a comment linking to the new question here, I'll try to help more. – isanae Aug 30 '16 at 11:56
  • http://stackoverflow.com/questions/39247778/boost-log-how-to-prevent-the-output-will-be-duplicated-to-all-added-streams-whe – Juan Solo Aug 31 '16 at 11:01