tutorial_file_manual.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 <fstream>
  8. #include <boost/smart_ptr/shared_ptr.hpp>
  9. #include <boost/smart_ptr/make_shared_object.hpp>
  10. #include <boost/log/core.hpp>
  11. #include <boost/log/trivial.hpp>
  12. #include <boost/log/sinks/sync_frontend.hpp>
  13. #include <boost/log/sinks/text_ostream_backend.hpp>
  14. #include <boost/log/sources/logger.hpp>
  15. #include <boost/log/sources/record_ostream.hpp>
  16. namespace logging = boost::log;
  17. namespace src = boost::log::sources;
  18. namespace sinks = boost::log::sinks;
  19. //[ example_tutorial_file_manual
  20. void init()
  21. {
  22. // Construct the sink
  23. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  24. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  25. // Add a stream to write log to
  26. sink->locked_backend()->add_stream(
  27. boost::make_shared< std::ofstream >("sample.log"));
  28. // Register the sink in the logging core
  29. logging::core::get()->add_sink(sink);
  30. }
  31. //]
  32. int main(int, char*[])
  33. {
  34. init();
  35. src::logger lg;
  36. BOOST_LOG(lg) << "Hello world!";
  37. return 0;
  38. }