main.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 07.03.2009
  11. *
  12. * \brief An example of logging to a syslog server (syslogd, for example).
  13. *
  14. * The example shows how to initialize logging to a local syslog server.
  15. * The code creates a sink that will use native syslog API to emit messages.
  16. */
  17. // #define BOOST_LOG_DYN_LINK 1
  18. #include <boost/config.hpp>
  19. #if !defined(BOOST_WINDOWS)
  20. #define BOOST_LOG_USE_NATIVE_SYSLOG
  21. #endif
  22. #include <stdexcept>
  23. #include <string>
  24. #include <iostream>
  25. #include <boost/smart_ptr/shared_ptr.hpp>
  26. #include <boost/log/common.hpp>
  27. #include <boost/log/expressions.hpp>
  28. #include <boost/log/attributes.hpp>
  29. #include <boost/log/sinks/sync_frontend.hpp>
  30. #include <boost/log/sinks/syslog_backend.hpp>
  31. #if defined(BOOST_LOG_USE_NATIVE_SYSLOG)
  32. namespace logging = boost::log;
  33. namespace attrs = boost::log::attributes;
  34. namespace src = boost::log::sources;
  35. namespace sinks = boost::log::sinks;
  36. namespace expr = boost::log::expressions;
  37. namespace keywords = boost::log::keywords;
  38. using boost::shared_ptr;
  39. //! Define application-specific severity levels
  40. enum severity_levels
  41. {
  42. normal,
  43. warning,
  44. error
  45. };
  46. int main(int argc, char* argv[])
  47. {
  48. try
  49. {
  50. // Create a syslog sink
  51. shared_ptr< sinks::synchronous_sink< sinks::syslog_backend > > sink(
  52. new sinks::synchronous_sink< sinks::syslog_backend >(
  53. keywords::use_impl = sinks::syslog::native,
  54. keywords::facility = sinks::syslog::local7));
  55. sink->set_formatter
  56. (
  57. expr::format("native_syslog: %1%: %2%")
  58. % expr::attr< unsigned int >("RecordID")
  59. % expr::smessage
  60. );
  61. // We'll have to map our custom levels to the syslog levels
  62. sinks::syslog::custom_severity_mapping< severity_levels > mapping("Severity");
  63. mapping[normal] = sinks::syslog::info;
  64. mapping[warning] = sinks::syslog::warning;
  65. mapping[error] = sinks::syslog::critical;
  66. sink->locked_backend()->set_severity_mapper(mapping);
  67. // Add the sink to the core
  68. logging::core::get()->add_sink(sink);
  69. // Add some attributes too
  70. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  71. // Do some logging
  72. src::severity_logger< severity_levels > lg(keywords::severity = normal);
  73. BOOST_LOG_SEV(lg, normal) << "A syslog record with normal level";
  74. BOOST_LOG_SEV(lg, warning) << "A syslog record with warning level";
  75. BOOST_LOG_SEV(lg, error) << "A syslog record with error level";
  76. return 0;
  77. }
  78. catch (std::exception& e)
  79. {
  80. std::cout << "FAILURE: " << e.what() << std::endl;
  81. return 1;
  82. }
  83. }
  84. #else // defined(BOOST_LOG_USE_NATIVE_SYSLOG)
  85. int main (int, char*[])
  86. {
  87. std::cout << "Native syslog API is not supported on this platform" << std::endl;
  88. return 0;
  89. }
  90. #endif // defined(BOOST_LOG_USE_NATIVE_SYSLOG)