sources_severity_channel.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <boost/smart_ptr/shared_ptr.hpp>
  12. #include <boost/smart_ptr/make_shared_object.hpp>
  13. #include <boost/log/core.hpp>
  14. #include <boost/log/expressions.hpp>
  15. #include <boost/log/sources/severity_channel_logger.hpp>
  16. #include <boost/log/sources/record_ostream.hpp>
  17. #include <boost/log/sources/global_logger_storage.hpp>
  18. #include <boost/log/sinks/sync_frontend.hpp>
  19. #include <boost/log/sinks/text_ostream_backend.hpp>
  20. #include <boost/log/utility/setup/common_attributes.hpp>
  21. namespace logging = boost::log;
  22. namespace src = boost::log::sources;
  23. namespace expr = boost::log::expressions;
  24. namespace sinks = boost::log::sinks;
  25. namespace attrs = boost::log::attributes;
  26. namespace keywords = boost::log::keywords;
  27. //[ example_sources_severity_channel
  28. enum severity_level
  29. {
  30. normal,
  31. notification,
  32. warning,
  33. error,
  34. critical
  35. };
  36. typedef src::severity_channel_logger_mt<
  37. severity_level, // the type of the severity level
  38. std::string // the type of the channel name
  39. > my_logger_mt;
  40. BOOST_LOG_INLINE_GLOBAL_LOGGER_INIT(my_logger, my_logger_mt)
  41. {
  42. // Specify the channel name on construction, similarly as with the channel_logger
  43. return my_logger_mt(keywords::channel = "my_logger");
  44. }
  45. void logging_function()
  46. {
  47. // Do logging with the severity level. The record will have both
  48. // the severity level and the channel name attached.
  49. BOOST_LOG_SEV(my_logger::get(), normal) << "Hello, world!";
  50. }
  51. //]
  52. // The operator puts a human-friendly representation of the severity level to the stream
  53. std::ostream& operator<< (std::ostream& strm, severity_level level)
  54. {
  55. static const char* strings[] =
  56. {
  57. "normal",
  58. "notification",
  59. "warning",
  60. "error",
  61. "critical"
  62. };
  63. if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
  64. strm << strings[level];
  65. else
  66. strm << static_cast< int >(level);
  67. return strm;
  68. }
  69. void init()
  70. {
  71. typedef sinks::synchronous_sink< sinks::text_ostream_backend > text_sink;
  72. boost::shared_ptr< text_sink > sink = boost::make_shared< text_sink >();
  73. sink->locked_backend()->add_stream(
  74. boost::make_shared< std::ofstream >("sample.log"));
  75. sink->set_formatter
  76. (
  77. expr::stream
  78. << expr::attr< unsigned int >("LineID")
  79. << ": <" << expr::attr< severity_level >("Severity")
  80. << ">\t"
  81. << "[" << expr::attr< std::string >("Channel") << "] "
  82. << expr::smessage
  83. );
  84. logging::core::get()->add_sink(sink);
  85. // Add attributes
  86. logging::add_common_attributes();
  87. }
  88. int main(int, char*[])
  89. {
  90. init();
  91. logging_function();
  92. return 0;
  93. }