sources_severity.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <cstddef>
  8. #include <string>
  9. #include <ostream>
  10. #include <fstream>
  11. #include <boost/move/utility_core.hpp>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/log/core.hpp>
  15. #include <boost/log/expressions.hpp>
  16. #include <boost/log/sources/severity_logger.hpp>
  17. #include <boost/log/sources/record_ostream.hpp>
  18. #include <boost/log/sinks/sync_frontend.hpp>
  19. #include <boost/log/sinks/text_ostream_backend.hpp>
  20. #include <boost/log/utility/setup/common_attributes.hpp>
  21. namespace logging = boost::log;
  22. namespace src = boost::log::sources;
  23. namespace expr = boost::log::expressions;
  24. namespace sinks = boost::log::sinks;
  25. namespace attrs = boost::log::attributes;
  26. namespace keywords = boost::log::keywords;
  27. //[ example_sources_severity
  28. // We define our own severity levels
  29. enum severity_level
  30. {
  31. normal,
  32. notification,
  33. warning,
  34. error,
  35. critical
  36. };
  37. void logging_function()
  38. {
  39. // The logger implicitly adds a source-specific attribute 'Severity'
  40. // of type 'severity_level' on construction
  41. src::severity_logger< severity_level > slg;
  42. BOOST_LOG_SEV(slg, normal) << "A regular message";
  43. BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it";
  44. BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!";
  45. }
  46. //]
  47. //[ example_sources_default_severity
  48. void default_severity()
  49. {
  50. // The default severity can be specified in constructor.
  51. src::severity_logger< severity_level > error_lg(keywords::severity = error);
  52. BOOST_LOG(error_lg) << "An error level log record (by default)";
  53. // The explicitly specified level overrides the default
  54. BOOST_LOG_SEV(error_lg, warning) << "A warning level log record (overrode the default)";
  55. }
  56. //]
  57. //[ example_sources_severity_manual
  58. void manual_logging()
  59. {
  60. src::severity_logger< severity_level > slg;
  61. logging::record rec = slg.open_record(keywords::severity = normal);
  62. if (rec)
  63. {
  64. logging::record_ostream strm(rec);
  65. strm << "A regular message";
  66. strm.flush();
  67. slg.push_record(boost::move(rec));
  68. }
  69. }
  70. //]
  71. // The operator puts a human-friendly representation of the severity level to the stream
  72. std::ostream& operator<< (std::ostream& strm, severity_level level)
  73. {
  74. static const char* strings[] =
  75. {
  76. "normal",
  77. "notification",
  78. "warning",
  79. "error",
  80. "critical"
  81. };
  82. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  83. strm << strings[level];
  84. else
  85. strm << static_cast< int >(level);
  86. return strm;
  87. }
  88. void init()
  89. {
  90. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  91. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  92. sink->locked_backend()->add_stream(
  93. boost::make_shared< std::ofstream >("sample.log"));
  94. sink->set_formatter
  95. (
  96. expr::stream
  97. << expr::attr< unsigned int >("LineID")
  98. << ": <" << expr::attr< severity_level >("Severity")
  99. << ">\t"
  100. << expr::smessage
  101. );
  102. logging::core::get()->add_sink(sink);
  103. // Add attributes
  104. logging::add_common_attributes();
  105. }
  106. int main(int, char*[])
  107. {
  108. init();
  109. logging_function();
  110. default_severity();
  111. manual_logging();
  112. return 0;
  113. }