expressions_attr_fmt_tag.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_attr_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. // The operator is used for regular stream formatting
  31. std::ostream& operator<< (std::ostream& strm, severity_level level)
  32. {
  33. static const char* strings[] =
  34. {
  35. "normal",
  36. "notification",
  37. "warning",
  38. "error",
  39. "critical"
  40. };
  41. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  42. strm << strings[level];
  43. else
  44. strm << static_cast< int >(level);
  45. return strm;
  46. }
  47. // Attribute value tag type
  48. struct severity_tag;
  49. // The operator is used when putting the severity level to log
  50. logging::formatting_ostream& operator<<
  51. (
  52. logging::formatting_ostream& strm,
  53. logging::to_log_manip< severity_level, severity_tag > const& manip
  54. )
  55. {
  56. static const char* strings[] =
  57. {
  58. "NORM",
  59. "NTFY",
  60. "WARN",
  61. "ERRR",
  62. "CRIT"
  63. };
  64. severity_level level = manip.get();
  65. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  66. strm << strings[level];
  67. else
  68. strm << static_cast< int >(level);
  69. return strm;
  70. }
  71. void init()
  72. {
  73. logging::add_console_log
  74. (
  75. std::clog,
  76. // This makes the sink to write log records that look like this:
  77. // 1: <NORM> A normal severity message
  78. // 2: <ERRR> An error severity message
  79. keywords::format =
  80. (
  81. expr::stream
  82. << expr::attr< unsigned int >("LineID")
  83. << ": <" << expr::attr< severity_level, severity_tag >("Severity")
  84. << "> " << expr::smessage
  85. )
  86. );
  87. }
  88. //]
  89. int main(int, char*[])
  90. {
  91. init();
  92. logging::add_common_attributes();
  93. src::severity_logger< severity_level > lg;
  94. // These messages will be written with CAPS severity levels
  95. BOOST_LOG_SEV(lg, normal) << "A normal severity message";
  96. BOOST_LOG_SEV(lg, notification) << "A notification severity message";
  97. BOOST_LOG_SEV(lg, warning) << "A warning severity message";
  98. BOOST_LOG_SEV(lg, error) << "An error severity message";
  99. BOOST_LOG_SEV(lg, critical) << "A critical severity message";
  100. // This line will still use lower-case severity levels
  101. std::cout << "The regular output still uses lower-case formatting: " << normal << std::endl;
  102. return 0;
  103. }