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