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