tutorial_attributes.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <iomanip>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/date_time/posix_time/posix_time.hpp>
  15. #include <boost/log/core.hpp>
  16. #include <boost/log/expressions.hpp>
  17. #include <boost/log/attributes.hpp>
  18. #include <boost/log/sources/basic_logger.hpp>
  19. #include <boost/log/sources/severity_logger.hpp>
  20. #include <boost/log/sources/record_ostream.hpp>
  21. #include <boost/log/sinks/sync_frontend.hpp>
  22. #include <boost/log/sinks/text_ostream_backend.hpp>
  23. #include <boost/log/attributes/scoped_attribute.hpp>
  24. #include <boost/log/utility/setup/common_attributes.hpp>
  25. namespace logging = boost::log;
  26. namespace src = boost::log::sources;
  27. namespace expr = boost::log::expressions;
  28. namespace sinks = boost::log::sinks;
  29. namespace attrs = boost::log::attributes;
  30. namespace keywords = boost::log::keywords;
  31. // We define our own severity levels
  32. enum severity_level
  33. {
  34. normal,
  35. notification,
  36. warning,
  37. error,
  38. critical
  39. };
  40. BOOST_LOG_ATTRIBUTE_KEYWORD(line_id, "LineID", unsigned int)
  41. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  42. BOOST_LOG_ATTRIBUTE_KEYWORD(tag_attr, "Tag", std::string)
  43. BOOST_LOG_ATTRIBUTE_KEYWORD(scope, "Scope", attrs::named_scope::value_type)
  44. BOOST_LOG_ATTRIBUTE_KEYWORD(timeline, "Timeline", attrs::timer::value_type)
  45. void logging_function()
  46. {
  47. src::severity_logger< severity_level > slg;
  48. BOOST_LOG_SEV(slg, normal) << "A regular message";
  49. BOOST_LOG_SEV(slg, warning) << "Something bad is going on but I can handle it";
  50. BOOST_LOG_SEV(slg, critical) << "Everything crumbles, shoot me now!";
  51. }
  52. //[ example_tutorial_attributes_named_scope
  53. void named_scope_logging()
  54. {
  55. BOOST_LOG_NAMED_SCOPE("named_scope_logging");
  56. src::severity_logger< severity_level > slg;
  57. BOOST_LOG_SEV(slg, normal) << "Hello from the function named_scope_logging!";
  58. }
  59. //]
  60. //[ example_tutorial_attributes_tagged_logging
  61. void tagged_logging()
  62. {
  63. src::severity_logger< severity_level > slg;
  64. slg.add_attribute("Tag", attrs::constant< std::string >("My tag value"));
  65. BOOST_LOG_SEV(slg, normal) << "Here goes the tagged record";
  66. }
  67. //]
  68. //[ example_tutorial_attributes_timed_logging
  69. void timed_logging()
  70. {
  71. BOOST_LOG_SCOPED_THREAD_ATTR("Timeline", attrs::timer());
  72. src::severity_logger< severity_level > slg;
  73. BOOST_LOG_SEV(slg, normal) << "Starting to time nested functions";
  74. logging_function();
  75. BOOST_LOG_SEV(slg, normal) << "Stopping to time nested functions";
  76. }
  77. //]
  78. // The operator puts a human-friendly representation of the severity level to the stream
  79. std::ostream& operator<< (std::ostream& strm, severity_level level)
  80. {
  81. static const char* strings[] =
  82. {
  83. "normal",
  84. "notification",
  85. "warning",
  86. "error",
  87. "critical"
  88. };
  89. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  90. strm << strings[level];
  91. else
  92. strm << static_cast< int >(level);
  93. return strm;
  94. }
  95. void init()
  96. {
  97. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  98. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  99. sink->locked_backend()->add_stream(
  100. boost::make_shared< std::ofstream >("sample.log"));
  101. sink->set_formatter
  102. (
  103. expr::stream
  104. << std::hex << std::setw(8) << std::setfill('0') << line_id << std::dec << std::setfill(' ')
  105. << ": <" << severity << ">\t"
  106. << "(" << scope << ") "
  107. << expr::if_(expr::has_attr(tag_attr))
  108. [
  109. expr::stream << "[" << tag_attr << "] "
  110. ]
  111. << expr::if_(expr::has_attr(timeline))
  112. [
  113. expr::stream << "[" << timeline << "] "
  114. ]
  115. << expr::smessage
  116. );
  117. logging::core::get()->add_sink(sink);
  118. // Add attributes
  119. logging::add_common_attributes();
  120. logging::core::get()->add_global_attribute("Scope", attrs::named_scope());
  121. }
  122. int main(int, char*[])
  123. {
  124. init();
  125. named_scope_logging();
  126. tagged_logging();
  127. timed_logging();
  128. return 0;
  129. }