main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 wide character logging.
  13. */
  14. #include <iostream>
  15. #include <boost/locale/generator.hpp>
  16. #include <boost/date_time/posix_time/posix_time_types.hpp>
  17. #include <boost/log/common.hpp>
  18. #include <boost/log/expressions.hpp>
  19. #include <boost/log/utility/setup/file.hpp>
  20. #include <boost/log/utility/setup/console.hpp>
  21. #include <boost/log/utility/setup/common_attributes.hpp>
  22. #include <boost/log/sources/logger.hpp>
  23. #include <boost/log/support/date_time.hpp>
  24. namespace logging = boost::log;
  25. namespace sinks = boost::log::sinks;
  26. namespace attrs = boost::log::attributes;
  27. namespace src = boost::log::sources;
  28. namespace expr = boost::log::expressions;
  29. namespace keywords = boost::log::keywords;
  30. //[ example_wide_char_severity_level_definition
  31. enum severity_level
  32. {
  33. normal,
  34. notification,
  35. warning,
  36. error,
  37. critical
  38. };
  39. template< typename CharT, typename TraitsT >
  40. inline std::basic_ostream< CharT, TraitsT >& operator<< (
  41. std::basic_ostream< CharT, TraitsT >& strm, severity_level lvl)
  42. {
  43. static const char* const str[] =
  44. {
  45. "normal",
  46. "notification",
  47. "warning",
  48. "error",
  49. "critical"
  50. };
  51. if (static_cast< std::size_t >(lvl) < (sizeof(str) / sizeof(*str)))
  52. strm << str[lvl];
  53. else
  54. strm << static_cast< int >(lvl);
  55. return strm;
  56. }
  57. //]
  58. //[ example_wide_char_logging_initialization
  59. // Declare attribute keywords
  60. BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", severity_level)
  61. BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", boost::posix_time::ptime)
  62. void init_logging()
  63. {
  64. boost::shared_ptr< sinks::synchronous_sink< sinks::text_file_backend > > sink = logging::add_file_log
  65. (
  66. "sample.log",
  67. keywords::format = expr::stream
  68. << expr::format_date_time(timestamp, "%Y-%m-%d, %H:%M:%S.%f")
  69. << " <" << severity.or_default(normal)
  70. << "> " << expr::message
  71. );
  72. // The sink will perform character code conversion as needed, according to the locale set with imbue()
  73. std::locale loc = boost::locale::generator()("en_US.UTF-8");
  74. sink->imbue(loc);
  75. // Let's add some commonly used attributes, like timestamp and record counter.
  76. logging::add_common_attributes();
  77. }
  78. //]
  79. //[ example_wide_char_logging
  80. void test_narrow_char_logging()
  81. {
  82. // Narrow character logging still works
  83. src::logger lg;
  84. BOOST_LOG(lg) << "Hello, World! This is a narrow character message.";
  85. }
  86. void test_wide_char_logging()
  87. {
  88. src::wlogger lg;
  89. BOOST_LOG(lg) << L"Hello, World! This is a wide character message.";
  90. // National characters are also supported
  91. const wchar_t national_chars[] = { 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, L',', L' ', 0x043c, 0x0438, 0x0440, L'!', 0 };
  92. BOOST_LOG(lg) << national_chars;
  93. // Now, let's try logging with severity
  94. src::wseverity_logger< severity_level > slg;
  95. BOOST_LOG_SEV(slg, normal) << L"A normal severity message, will not pass to the file";
  96. BOOST_LOG_SEV(slg, warning) << L"A warning severity message, will pass to the file";
  97. BOOST_LOG_SEV(slg, error) << L"An error severity message, will pass to the file";
  98. }
  99. //]
  100. int main(int argc, char* argv[])
  101. {
  102. init_logging();
  103. test_narrow_char_logging();
  104. test_wide_char_logging();
  105. return 0;
  106. }