tutorial_file.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/log/core.hpp>
  8. #include <boost/log/trivial.hpp>
  9. #include <boost/log/expressions.hpp>
  10. #include <boost/log/sinks/text_file_backend.hpp>
  11. #include <boost/log/utility/setup/file.hpp>
  12. #include <boost/log/utility/setup/common_attributes.hpp>
  13. #include <boost/log/sources/severity_logger.hpp>
  14. #include <boost/log/sources/record_ostream.hpp>
  15. namespace logging = boost::log;
  16. namespace src = boost::log::sources;
  17. namespace sinks = boost::log::sinks;
  18. namespace keywords = boost::log::keywords;
  19. #if 0
  20. //[ example_tutorial_file_simple
  21. void init()
  22. {
  23. logging::add_file_log("sample.log");
  24. logging::core::get()->set_filter
  25. (
  26. logging::trivial::severity >= logging::trivial::info
  27. );
  28. }
  29. //]
  30. // We need this due to this bug: https://svn.boost.org/trac/boost/ticket/4416
  31. //[ example_tutorial_file_advanced_no_callouts
  32. void init()
  33. {
  34. logging::add_file_log
  35. (
  36. keywords::file_name = "sample_%N.log",
  37. keywords::rotation_size = 10 * 1024 * 1024,
  38. keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
  39. keywords::format = "[%TimeStamp%]: %Message%"
  40. );
  41. logging::core::get()->set_filter
  42. (
  43. logging::trivial::severity >= logging::trivial::info
  44. );
  45. }
  46. //]
  47. #else
  48. //[ example_tutorial_file_advanced
  49. void init()
  50. {
  51. logging::add_file_log
  52. (
  53. keywords::file_name = "sample_%N.log", /*< file name pattern >*/
  54. keywords::rotation_size = 10 * 1024 * 1024, /*< rotate files every 10 MiB... >*/
  55. keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0), /*< ...or at midnight >*/
  56. keywords::format = "[%TimeStamp%]: %Message%" /*< log record format >*/
  57. );
  58. logging::core::get()->set_filter
  59. (
  60. logging::trivial::severity >= logging::trivial::info
  61. );
  62. }
  63. //]
  64. #endif
  65. int main(int, char*[])
  66. {
  67. init();
  68. logging::add_common_attributes();
  69. using namespace logging::trivial;
  70. src::severity_logger< severity_level > lg;
  71. BOOST_LOG_SEV(lg, trace) << "A trace severity message";
  72. BOOST_LOG_SEV(lg, debug) << "A debug severity message";
  73. BOOST_LOG_SEV(lg, info) << "An informational severity message";
  74. BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  75. BOOST_LOG_SEV(lg, error) << "An error severity message";
  76. BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
  77. return 0;
  78. }