0

I'm using boost-log in my application. I want to both write logs to file and to send the logged strings over the network (for replication purposes).

For file-logging, I'm using a text_file_backend (together with a synchronous_sink<text_file_backend>); this works good, and logs to file information with a certain format. I want to obtain the same formatted string, to be able to send it over the network.

How can I squeeze out from the text_file_backend the actual string that it will write down to file?

Paolo M
  • 12,403
  • 6
  • 52
  • 73

1 Answers1

0

You can try to add multiple stream to the backend:

void init_logging()
{
    boost::shared_ptr< logging::core > core = logging::core::get();

    // Create a backend and attach a couple of streams to it
    boost::shared_ptr< sinks::text_ostream_backend > backend =
        boost::make_shared< sinks::text_ostream_backend >();
    backend->add_stream(
        boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
    backend->add_stream(
        boost::shared_ptr< std::ostream >(new std::ofstream("sample.log")));

    /*** Add your network stream here ***/

    typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
    boost::shared_ptr< sink_t > sink(new sink_t(backend));
    core->add_sink(sink);
}

Update

Stick to text_file_backend, you can use the void set_close_handler(close_handler_type const & handler) to set a callback function which reads the closed log file and send it to your log server.

If you cannot stand with the delay, then use set_open_handler, and start a new thread to read the file.

Ultimately, you can subclass text_file_backend and overload the consume method which I think the fontend calls. Refer to this page for the backend API

gdlmx
  • 6,479
  • 1
  • 21
  • 39
  • It's the first thing I've thought, too. But it seems that the `text_file_backend` does not provide an `add_stream` feature. – Paolo M Apr 28 '16 at 14:40
  • Don't use `text_file_backend`, use `text_ostream_backend` instead. Or do you need the file separation (rotation) function of `text_file_backend`? – gdlmx Apr 28 '16 at 14:42
  • Yes, I need file rotation because the application is supposed to run for an indefinitely long period of time. I've solved adding a `text_ostream_backend`, but I can't believe that there is no way to avoid this *duplication*... – Paolo M Apr 28 '16 at 14:56