tutorial_fmt_stream.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/date_time/posix_time/posix_time_types.hpp>
  8. #include <boost/log/trivial.hpp>
  9. #include <boost/log/expressions.hpp>
  10. #include <boost/log/sources/severity_logger.hpp>
  11. #include <boost/log/sources/record_ostream.hpp>
  12. #include <boost/log/utility/setup/file.hpp>
  13. #include <boost/log/utility/setup/common_attributes.hpp>
  14. #include <boost/log/support/date_time.hpp>
  15. namespace logging = boost::log;
  16. namespace src = boost::log::sources;
  17. namespace expr = boost::log::expressions;
  18. namespace keywords = boost::log::keywords;
  19. #if 1
  20. //[ example_tutorial_formatters_stream
  21. void init()
  22. {
  23. logging::add_file_log
  24. (
  25. keywords::file_name = "sample_%N.log",
  26. // This makes the sink to write log records that look like this:
  27. // 1: <normal> A normal severity message
  28. // 2: <error> An error severity message
  29. keywords::format =
  30. (
  31. expr::stream
  32. << expr::attr< unsigned int >("LineID")
  33. << ": <" << logging::trivial::severity
  34. << "> " << expr::smessage
  35. )
  36. );
  37. }
  38. //]
  39. #else
  40. //[ example_tutorial_formatters_stream_date_time
  41. void init()
  42. {
  43. logging::add_file_log
  44. (
  45. keywords::file_name = "sample_%N.log",
  46. // This makes the sink to write log records that look like this:
  47. // YYYY-MM-DD HH:MI:SS: <normal> A normal severity message
  48. // YYYY-MM-DD HH:MI:SS: <error> An error severity message
  49. keywords::format =
  50. (
  51. expr::stream
  52. << expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
  53. << ": <" << logging::trivial::severity
  54. << "> " << expr::smessage
  55. )
  56. );
  57. }
  58. //]
  59. #endif
  60. int main(int, char*[])
  61. {
  62. init();
  63. logging::add_common_attributes();
  64. using namespace logging::trivial;
  65. src::severity_logger< severity_level > lg;
  66. BOOST_LOG_SEV(lg, trace) << "A trace severity message";
  67. BOOST_LOG_SEV(lg, debug) << "A debug severity message";
  68. BOOST_LOG_SEV(lg, info) << "An informational severity message";
  69. BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  70. BOOST_LOG_SEV(lg, error) << "An error severity message";
  71. BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
  72. return 0;
  73. }