expressions_keyword_fmt_tag.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_logger.hpp>
  11. #include <boost/log/sources/record_ostream.hpp>
  12. #include <boost/log/utility/formatting_ostream.hpp>
  13. #include <boost/log/utility/manipulators/to_log.hpp>
  14. #include <boost/log/utility/setup/console.hpp>
  15. #include <boost/log/utility/setup/common_attributes.hpp>
  16. namespace logging = boost::log;
  17. namespace src = boost::log::sources;
  18. namespace expr = boost::log::expressions;
  19. namespace keywords = boost::log::keywords;
  20. //[ example_expressions_keyword_formatter_stream_tag
  21. // We define our own severity levels
  22. enum severity_level
  23. {
  24. normal,
  25. notification,
  26. warning,
  27. error,
  28. critical
  29. };
  30. // Define the attribute keywords
  31. BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
  32. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  33. // The operator is used for regular stream formatting
  34. std::ostream& operator<< (std::ostream& strm, severity_level level)
  35. {
  36. static const char* strings[] =
  37. {
  38. "normal",
  39. "notification",
  40. "warning",
  41. "error",
  42. "critical"
  43. };
  44. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  45. strm << strings[level];
  46. else
  47. strm << static_cast< int >(level);
  48. return strm;
  49. }
  50. // The operator is used when putting the severity level to log
  51. logging::formatting_ostream& operator<<
  52. (
  53. logging::formatting_ostream& strm,
  54. logging::to_log_manip< severity_level, tag::severity > const& manip
  55. )
  56. {
  57. static const char* strings[] =
  58. {
  59. "NORM",
  60. "NTFY",
  61. "WARN",
  62. "ERRR",
  63. "CRIT"
  64. };
  65. severity_level level = manip.get();
  66. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  67. strm << strings[level];
  68. else
  69. strm << static_cast< int >(level);
  70. return strm;
  71. }
  72. void init()
  73. {
  74. logging::add_console_log
  75. (
  76. std::clog,
  77. // This makes the sink to write log records that look like this:
  78. // 1: <NORM> A normal severity message
  79. // 2: <ERRR> An error severity message
  80. keywords::format =
  81. (
  82. expr::stream
  83. << line_id
  84. << ": <" << severity
  85. << "> " << expr::smessage
  86. )
  87. );
  88. }
  89. //]
  90. //[ example_expressions_keyword_lookup
  91. void print_severity(logging::record_view const& rec)
  92. {
  93. logging::value_ref< severity_level, tag::severity > level = rec[severity];
  94. std::cout << level << std::endl;
  95. }
  96. //]
  97. int main(int, char*[])
  98. {
  99. init();
  100. logging::add_common_attributes();
  101. src::severity_logger< severity_level > lg;
  102. // These messages will be written with CAPS severity levels
  103. BOOST_LOG_SEV(lg, normal) << "A normal severity message";
  104. BOOST_LOG_SEV(lg, notification) << "A notification severity message";
  105. BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  106. BOOST_LOG_SEV(lg, error) << "An error severity message";
  107. BOOST_LOG_SEV(lg, critical) << "A critical severity message";
  108. // This line will still use lower-case severity levels
  109. std::cout << "The regular output still uses lower-case formatting: " << normal << std::endl;
  110. return 0;
  111. }