sinks_syslog.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <boost/config.hpp>
  8. #if !defined(BOOST_WINDOWS)
  9. #define BOOST_LOG_USE_NATIVE_SYSLOG
  10. #endif
  11. #include <string>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <boost/smart_ptr/make_shared_object.hpp>
  14. #include <boost/log/core.hpp>
  15. #include <boost/log/sinks/sync_frontend.hpp>
  16. #include <boost/log/sinks/syslog_backend.hpp>
  17. #include <boost/log/sources/severity_logger.hpp>
  18. #include <boost/log/sources/record_ostream.hpp>
  19. #include <boost/log/attributes/scoped_attribute.hpp>
  20. namespace logging = boost::log;
  21. namespace src = boost::log::sources;
  22. namespace sinks = boost::log::sinks;
  23. namespace keywords = boost::log::keywords;
  24. //[ example_sinks_syslog
  25. // Complete sink type
  26. typedef sinks::synchronous_sink< sinks::syslog_backend > sink_t;
  27. //<-
  28. #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
  29. //->
  30. void init_native_syslog()
  31. {
  32. boost::shared_ptr< logging::core > core = logging::core::get();
  33. // Create a backend
  34. boost::shared_ptr< sinks::syslog_backend > backend(new sinks::syslog_backend(
  35. keywords::facility = sinks::syslog::user, /*< the logging facility >*/
  36. keywords::use_impl = sinks::syslog::native /*< the native syslog API should be used >*/
  37. ));
  38. // Set the straightforward level translator for the "Severity" attribute of type int
  39. backend->set_severity_mapper(sinks::syslog::direct_severity_mapping< int >("Severity"));
  40. // Wrap it into the frontend and register in the core.
  41. // The backend requires synchronization in the frontend.
  42. core->add_sink(boost::make_shared< sink_t >(backend));
  43. }
  44. //<-
  45. #endif
  46. //->
  47. //<-
  48. #if !defined(BOOST_LOG_NO_ASIO)
  49. //->
  50. void init_builtin_syslog()
  51. {
  52. boost::shared_ptr< logging::core > core = logging::core::get();
  53. // Create a new backend
  54. boost::shared_ptr< sinks::syslog_backend > backend(new sinks::syslog_backend(
  55. keywords::facility = sinks::syslog::local0, /*< the logging facility >*/
  56. keywords::use_impl = sinks::syslog::udp_socket_based /*< the built-in socket-based implementation should be used >*/
  57. ));
  58. // Setup the target address and port to send syslog messages to
  59. backend->set_target_address("192.164.1.10", 514);
  60. // Create and fill in another level translator for "MyLevel" attribute of type string
  61. sinks::syslog::custom_severity_mapping< std::string > mapping("MyLevel");
  62. mapping["debug"] = sinks::syslog::debug;
  63. mapping["normal"] = sinks::syslog::info;
  64. mapping["warning"] = sinks::syslog::warning;
  65. mapping["failure"] = sinks::syslog::critical;
  66. backend->set_severity_mapper(mapping);
  67. // Wrap it into the frontend and register in the core.
  68. core->add_sink(boost::make_shared< sink_t >(backend));
  69. }
  70. //<-
  71. #endif
  72. //->
  73. //]
  74. int main(int, char*[])
  75. {
  76. #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
  77. init_native_syslog();
  78. #elif !defined(BOOST_LOG_NO_ASIO)
  79. init_builtin_syslog();
  80. #endif
  81. BOOST_LOG_SCOPED_THREAD_TAG("MyLevel", "warning");
  82. src::severity_logger< > lg;
  83. BOOST_LOG_SEV(lg, 3) << "Hello world!";
  84. return 0;
  85. }