main.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 remote syslog server.
  15. * The code creates a sink that will send syslog messages to local port 514.
  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. namespace logging = boost::log;
  32. namespace attrs = boost::log::attributes;
  33. namespace src = boost::log::sources;
  34. namespace sinks = boost::log::sinks;
  35. namespace expr = boost::log::expressions;
  36. namespace keywords = boost::log::keywords;
  37. using boost::shared_ptr;
  38. //! Define application-specific severity levels
  39. enum severity_levels
  40. {
  41. normal,
  42. warning,
  43. error
  44. };
  45. int main(int argc, char* argv[])
  46. {
  47. try
  48. {
  49. // Create a syslog sink
  50. shared_ptr< sinks::synchronous_sink< sinks::syslog_backend > > sink(
  51. new sinks::synchronous_sink< sinks::syslog_backend >());
  52. sink->set_formatter
  53. (
  54. expr::format("syslog.exe: %1%: %2%")
  55. % expr::attr< unsigned int >("RecordID")
  56. % expr::smessage
  57. );
  58. // We'll have to map our custom levels to the syslog levels
  59. sinks::syslog::custom_severity_mapping< severity_levels > mapping("Severity");
  60. mapping[normal] = sinks::syslog::info;
  61. mapping[warning] = sinks::syslog::warning;
  62. mapping[error] = sinks::syslog::critical;
  63. sink->locked_backend()->set_severity_mapper(mapping);
  64. #if !defined(BOOST_LOG_NO_ASIO)
  65. // Set the remote address to sent syslog messages to
  66. sink->locked_backend()->set_target_address("localhost");
  67. #endif
  68. // Add the sink to the core
  69. logging::core::get()->add_sink(sink);
  70. // Add some attributes too
  71. logging::core::get()->add_global_attribute("RecordID", attrs::counter< unsigned int >());
  72. // Do some logging
  73. src::severity_logger< severity_levels > lg(keywords::severity = normal);
  74. BOOST_LOG_SEV(lg, normal) << "A syslog record with normal level";
  75. BOOST_LOG_SEV(lg, warning) << "A syslog record with warning level";
  76. BOOST_LOG_SEV(lg, error) << "A syslog record with error level";
  77. return 0;
  78. }
  79. catch (std::exception& e)
  80. {
  81. std::cout << "FAILURE: " << e.what() << std::endl;
  82. return 1;
  83. }
  84. }