0

I am trying since a few days to set up c++ boost to log different data in 2 different files . What I managed to get is different files , who all share the same content The code bellow creates in the log folder , 2 files file.txt and file2.txt with the same content:

file.txt 

0: [2019-Feb-14 19:39:01.997479] - thread 2
1: [2019-Feb-14 19:39:02.035582] - thread 1

file2.txt
0: [2019-Feb-14 19:39:01.997479] - thread 2
1: [2019-Feb-14 19:39:02.035582] - thread 1


class logger
{
    src::logger_mt lg;
public:
    logger(std::string filename)
    {
        // Create a text file sink
        typedef sinks::synchronous_sink< sinks::text_multifile_backend > file_sink;
        shared_ptr< file_sink > sink(new file_sink);


        // Set up how the file names will be generated
        sink->locked_backend()->set_file_name_composer(sinks::file::as_file_name_composer(
            expr::stream << "logs/" << filename));

        // Set the log record formatter
        sink->set_formatter
        (
            expr::format("%1%: [%2%] - %3%")
            % expr::attr< unsigned int >("RecordID")
            % expr::attr< boost::posix_time::ptime >("TimeStamp")
            % expr::smessage
        );

        // Add it to the core
        logging::core::get()->add_sink(sink);


        // Add some attributes too
        logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
        logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());

    }
    void log(std::string message)
    {
        BOOST_LOG(lg) << message;
    }    
};
logger logger1("file2.txt");
logger logger2("file.txt");
// This function is executed in a separate thread
int main(int argc, char* argv[])
{
    logger2.log("thread 2");
    logger1.log("thread 1");
    return 0;
}

I know is a basic question , but i read all the examples from the boost distribution libs\log\example and i couldnt find anything similar. The help is appreciated.

TraceKira
  • 281
  • 2
  • 13

1 Answers1

0

See this answer, in particular its first half about channels and filters.

Andrey Semashev
  • 10,046
  • 1
  • 17
  • 27