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