core_record.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/core.hpp>
  10. #include <boost/log/expressions.hpp>
  11. #include <boost/log/attributes.hpp>
  12. #include <boost/log/utility/value_ref.hpp>
  13. namespace logging = boost::log;
  14. namespace expr = boost::log::expressions;
  15. namespace sinks = boost::log::sinks;
  16. namespace attrs = boost::log::attributes;
  17. namespace keywords = boost::log::keywords;
  18. // We define our own severity levels
  19. enum severity_level
  20. {
  21. normal,
  22. notification,
  23. warning,
  24. error,
  25. critical
  26. };
  27. // The operator puts a human-friendly representation of the severity level to the stream
  28. std::ostream& operator<< (std::ostream& strm, severity_level level)
  29. {
  30. static const char* strings[] =
  31. {
  32. "normal",
  33. "notification",
  34. "warning",
  35. "error",
  36. "critical"
  37. };
  38. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  39. strm << strings[level];
  40. else
  41. strm << static_cast< int >(level);
  42. return strm;
  43. }
  44. //[ example_core_record_visitation_extraction
  45. //=enum severity_level { ... };
  46. //=std::ostream& operator<< (std::ostream& strm, severity_level level);
  47. struct print_visitor
  48. {
  49. typedef void result_type;
  50. result_type operator() (severity_level level) const
  51. {
  52. std::cout << level << std::endl;
  53. };
  54. };
  55. // Prints severity level through visitation API
  56. void print_severity_visitation(logging::record const& rec)
  57. {
  58. logging::visit< severity_level >("Severity", rec, print_visitor());
  59. }
  60. // Prints severity level through extraction API
  61. void print_severity_extraction(logging::record const& rec)
  62. {
  63. logging::value_ref< severity_level > level = logging::extract< severity_level >("Severity", rec);
  64. std::cout << level << std::endl;
  65. }
  66. //]
  67. //[ example_core_record_attr_value_lookup
  68. // Prints severity level by searching the attribute values
  69. void print_severity_lookup(logging::record const& rec)
  70. {
  71. logging::attribute_value_set const& values = rec.attribute_values();
  72. logging::attribute_value_set::const_iterator it = values.find("Severity");
  73. if (it != values.end())
  74. {
  75. logging::attribute_value const& value = it->second;
  76. // A single attribute value can also be visited or extracted
  77. std::cout << value.extract< severity_level >() << std::endl;
  78. }
  79. }
  80. //]
  81. //[ example_core_record_subscript
  82. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  83. // Prints severity level by using the subscript operator
  84. void print_severity_subscript(logging::record const& rec)
  85. {
  86. // Use the attribute keyword to communicate the name and type of the value
  87. logging::value_ref< severity_level, tag::severity > level = rec[severity];
  88. std::cout << level << std::endl;
  89. }
  90. //]
  91. int main(int, char*[])
  92. {
  93. logging::attribute_set attrs;
  94. attrs.insert("Severity", attrs::make_constant(notification));
  95. logging::record rec = logging::core::get()->open_record(attrs);
  96. if (rec)
  97. {
  98. print_severity_visitation(rec);
  99. print_severity_extraction(rec);
  100. print_severity_lookup(rec);
  101. print_severity_subscript(rec);
  102. }
  103. return 0;
  104. }