sinks_ostream.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <string>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <boost/smart_ptr/shared_ptr.hpp>
  11. #include <boost/core/null_deleter.hpp>
  12. #include <boost/log/core.hpp>
  13. #include <boost/log/sinks/sync_frontend.hpp>
  14. #include <boost/log/sinks/text_ostream_backend.hpp>
  15. #include <boost/log/sources/severity_channel_logger.hpp>
  16. #include <boost/log/sources/record_ostream.hpp>
  17. namespace logging = boost::log;
  18. namespace src = boost::log::sources;
  19. namespace sinks = boost::log::sinks;
  20. namespace keywords = boost::log::keywords;
  21. //[ example_sinks_ostream
  22. void init_logging()
  23. {
  24. boost::shared_ptr< logging::core > core = logging::core::get();
  25. // Create a backend and attach a couple of streams to it
  26. boost::shared_ptr< sinks::text_ostream_backend > backend =
  27. boost::make_shared< sinks::text_ostream_backend >();
  28. backend->add_stream(
  29. boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
  30. backend->add_stream(
  31. boost::shared_ptr< std::ostream >(new std::ofstream("sample.log")));
  32. // Enable auto-flushing after each log record written
  33. backend->auto_flush(true);
  34. // Wrap it into the frontend and register in the core.
  35. // The backend requires synchronization in the frontend.
  36. typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
  37. boost::shared_ptr< sink_t > sink(new sink_t(backend));
  38. core->add_sink(sink);
  39. }
  40. //]
  41. int main(int, char*[])
  42. {
  43. init_logging();
  44. src::severity_channel_logger< > lg(keywords::channel = "net");
  45. BOOST_LOG_SEV(lg, 3) << "Hello world!";
  46. return 0;
  47. }