tutorial_fmt_stream_manual.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 <iomanip>
  9. #include <boost/smart_ptr/shared_ptr.hpp>
  10. #include <boost/smart_ptr/make_shared_object.hpp>
  11. #include <boost/log/core.hpp>
  12. #include <boost/log/trivial.hpp>
  13. #include <boost/log/expressions.hpp>
  14. #include <boost/log/sinks/sync_frontend.hpp>
  15. #include <boost/log/sinks/text_ostream_backend.hpp>
  16. #include <boost/log/sources/severity_logger.hpp>
  17. #include <boost/log/sources/record_ostream.hpp>
  18. #include <boost/log/utility/setup/common_attributes.hpp>
  19. namespace logging = boost::log;
  20. namespace src = boost::log::sources;
  21. namespace expr = boost::log::expressions;
  22. namespace sinks = boost::log::sinks;
  23. namespace keywords = boost::log::keywords;
  24. //[ example_tutorial_formatters_stream_manual
  25. void init()
  26. {
  27. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  28. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  29. sink->locked_backend()->add_stream(
  30. boost::make_shared< std::ofstream >("sample.log"));
  31. sink->set_formatter
  32. (
  33. expr::stream
  34. // line id will be written in hex, 8-digits, zero-filled
  35. << std::hex << std::setw(8) << std::setfill('0') << expr::attr< unsigned int >("LineID")
  36. << ": <" << logging::trivial::severity
  37. << "> " << expr::smessage
  38. );
  39. logging::core::get()->add_sink(sink);
  40. }
  41. //]
  42. int main(int, char*[])
  43. {
  44. init();
  45. logging::add_common_attributes();
  46. using namespace logging::trivial;
  47. src::severity_logger< severity_level > lg;
  48. BOOST_LOG_SEV(lg, trace) << "A trace severity message";
  49. BOOST_LOG_SEV(lg, debug) << "A debug severity message";
  50. BOOST_LOG_SEV(lg, info) << "An informational severity message";
  51. BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  52. BOOST_LOG_SEV(lg, error) << "An error severity message";
  53. BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
  54. return 0;
  55. }