expressions_channel_severity_filter.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <iostream>
  9. #include <boost/log/expressions.hpp>
  10. #include <boost/log/sources/severity_channel_logger.hpp>
  11. #include <boost/log/sources/record_ostream.hpp>
  12. #include <boost/log/utility/setup/console.hpp>
  13. #include <boost/log/utility/setup/common_attributes.hpp>
  14. namespace logging = boost::log;
  15. namespace src = boost::log::sources;
  16. namespace expr = boost::log::expressions;
  17. namespace keywords = boost::log::keywords;
  18. //[ example_expressions_channel_severity_filter
  19. // We define our own severity levels
  20. enum severity_level
  21. {
  22. normal,
  23. notification,
  24. warning,
  25. error,
  26. critical
  27. };
  28. // Define the attribute keywords
  29. BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
  30. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  31. BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
  32. //<-
  33. std::ostream& operator<< (std::ostream& strm, severity_level level)
  34. {
  35. static const char* strings[] =
  36. {
  37. "normal",
  38. "notification",
  39. "warning",
  40. "error",
  41. "critical"
  42. };
  43. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  44. strm << strings[level];
  45. else
  46. strm << static_cast< int >(level);
  47. return strm;
  48. }
  49. //->
  50. void init()
  51. {
  52. // Create a minimal severity table filter
  53. typedef expr::channel_severity_filter_actor< std::string, severity_level > min_severity_filter;
  54. min_severity_filter min_severity = expr::channel_severity_filter(channel, severity);
  55. // Set up the minimum severity levels for different channels
  56. min_severity["general"] = notification;
  57. min_severity["network"] = warning;
  58. min_severity["gui"] = error;
  59. logging::add_console_log
  60. (
  61. std::clog,
  62. keywords::filter = min_severity || severity >= critical,
  63. keywords::format =
  64. (
  65. expr::stream
  66. << line_id
  67. << ": <" << severity
  68. << "> [" << channel << "] "
  69. << expr::smessage
  70. )
  71. );
  72. }
  73. // Define our logger type
  74. typedef src::severity_channel_logger< severity_level, std::string > logger_type;
  75. void test_logging(logger_type& lg, std::string const& channel_name)
  76. {
  77. BOOST_LOG_CHANNEL_SEV(lg, channel_name, normal) << "A normal severity level message";
  78. BOOST_LOG_CHANNEL_SEV(lg, channel_name, notification) << "A notification severity level message";
  79. BOOST_LOG_CHANNEL_SEV(lg, channel_name, warning) << "A warning severity level message";
  80. BOOST_LOG_CHANNEL_SEV(lg, channel_name, error) << "An error severity level message";
  81. BOOST_LOG_CHANNEL_SEV(lg, channel_name, critical) << "A critical severity level message";
  82. }
  83. //]
  84. int main(int, char*[])
  85. {
  86. init();
  87. logging::add_common_attributes();
  88. logger_type lg;
  89. test_logging(lg, "general");
  90. test_logging(lg, "network");
  91. test_logging(lg, "gui");
  92. test_logging(lg, "filesystem");
  93. return 0;
  94. }