main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /*!
  8. * \file main.cpp
  9. * \author Andrey Semashev
  10. * \date 01.12.2012
  11. *
  12. * \brief An example of using attribute keywords.
  13. */
  14. // #define BOOST_LOG_USE_CHAR
  15. // #define BOOST_ALL_DYN_LINK 1
  16. // #define BOOST_LOG_DYN_LINK 1
  17. #include <iostream>
  18. #include <boost/log/common.hpp>
  19. #include <boost/log/expressions.hpp>
  20. #include <boost/log/utility/setup/file.hpp>
  21. #include <boost/log/utility/setup/console.hpp>
  22. #include <boost/log/utility/setup/common_attributes.hpp>
  23. #include <boost/log/attributes/timer.hpp>
  24. #include <boost/log/attributes/named_scope.hpp>
  25. #include <boost/log/sources/logger.hpp>
  26. #include <boost/log/support/date_time.hpp>
  27. namespace logging = boost::log;
  28. namespace sinks = boost::log::sinks;
  29. namespace attrs = boost::log::attributes;
  30. namespace src = boost::log::sources;
  31. namespace expr = boost::log::expressions;
  32. namespace keywords = boost::log::keywords;
  33. using boost::shared_ptr;
  34. // Here we define our application severity levels.
  35. enum severity_level
  36. {
  37. normal,
  38. notification,
  39. warning,
  40. error,
  41. critical
  42. };
  43. // The formatting logic for the severity level
  44. template< typename CharT, typename TraitsT >
  45. inline std::basic_ostream< CharT, TraitsT >& operator<< (
  46. std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
  47. {
  48. static const char* const str[] =
  49. {
  50. "normal",
  51. "notification",
  52. "warning",
  53. "error",
  54. "critical"
  55. };
  56. if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
  57. strm << str[lvl];
  58. else
  59. strm << static_cast< int >(lvl);
  60. return strm;
  61. }
  62. // Declare attribute keywords
  63. BOOST_LOG_ATTRIBUTE_KEYWORD(_severity, "Severity", severity_level)
  64. BOOST_LOG_ATTRIBUTE_KEYWORD(_timestamp, "TimeStamp", boost::posix_time::ptime)
  65. BOOST_LOG_ATTRIBUTE_KEYWORD(_uptime, "Uptime", attrs::timer::value_type)
  66. BOOST_LOG_ATTRIBUTE_KEYWORD(_scope, "Scope", attrs::named_scope::value_type)
  67. int main(int argc, char* argv[])
  68. {
  69. // This is a simple tutorial/example of Boost.Log usage
  70. // The first thing we have to do to get using the library is
  71. // to set up the logging sinks - i.e. where the logs will be written to.
  72. logging::add_console_log(std::clog, keywords::format = "%TimeStamp%: %_%");
  73. // One can also use lambda expressions to setup filters and formatters
  74. logging::add_file_log
  75. (
  76. "sample.log",
  77. keywords::filter = _severity >= warning,
  78. keywords::format = expr::stream
  79. << expr::format_date_time(_timestamp, "%Y-%m-%d, %H:%M:%S.%f")
  80. << " [" << expr::format_date_time(_uptime, "%O:%M:%S")
  81. << "] [" << expr::format_named_scope(_scope, keywords::format = "%n (%f:%l)")
  82. << "] <" << _severity
  83. << "> " << expr::message
  84. /*
  85. keywords::format = expr::format("%1% [%2%] [%3%] <%4%> %5%")
  86. % expr::format_date_time(_timestamp, "%Y-%m-%d, %H:%M:%S.%f")
  87. % expr::format_date_time(_uptime, "%O:%M:%S")
  88. % expr::format_named_scope(_scope, keywords::format = "%n (%f:%l)")
  89. % _severity
  90. % expr::message
  91. */
  92. );
  93. // Also let's add some commonly used attributes, like timestamp and record counter.
  94. logging::add_common_attributes();
  95. logging::core::get()->add_thread_attribute("Scope", attrs::named_scope());
  96. BOOST_LOG_FUNCTION();
  97. // Now our logs will be written both to the console and to the file.
  98. // Let's do a quick test and output something. We have to create a logger for this.
  99. src::logger lg;
  100. // And output...
  101. BOOST_LOG(lg) << "Hello, World!";
  102. // Now, let's try logging with severity
  103. src::severity_logger< severity_level > slg;
  104. // Let's pretend we also want to profile our code, so add a special timer attribute.
  105. slg.add_attribute("Uptime", attrs::timer());
  106. BOOST_LOG_SEV(slg, normal) << "A normal severity message, will not pass to the file";
  107. BOOST_LOG_SEV(slg, warning) << "A warning severity message, will pass to the file";
  108. BOOST_LOG_SEV(slg, error) << "An error severity message, will pass to the file";
  109. return 0;
  110. }