tutorial_logging.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 <boost/move/utility_core.hpp>
  8. #include <boost/log/sources/logger.hpp>
  9. #include <boost/log/sources/record_ostream.hpp>
  10. #include <boost/log/sources/global_logger_storage.hpp>
  11. #include <boost/log/utility/setup/file.hpp>
  12. #include <boost/log/utility/setup/common_attributes.hpp>
  13. namespace logging = boost::log;
  14. namespace src = boost::log::sources;
  15. namespace keywords = boost::log::keywords;
  16. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(my_logger, src::logger_mt)
  17. void logging_function1()
  18. {
  19. src::logger lg;
  20. //[ example_tutorial_logging_manual_logging
  21. logging::record rec = lg.open_record();
  22. if (rec)
  23. {
  24. logging::record_ostream strm(rec);
  25. strm << "Hello, World!";
  26. strm.flush();
  27. lg.push_record(boost::move(rec));
  28. }
  29. //]
  30. }
  31. void logging_function2()
  32. {
  33. src::logger_mt& lg = my_logger::get();
  34. BOOST_LOG(lg) << "Greetings from the global logger!";
  35. }
  36. int main(int, char*[])
  37. {
  38. logging::add_file_log("sample.log");
  39. logging::add_common_attributes();
  40. logging_function1();
  41. logging_function2();
  42. return 0;
  43. }