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)
12 #include <boost/smart_ptr/shared_ptr.hpp>
13 #include <boost/phoenix.hpp>
14 #include <boost/log/trivial.hpp>
15 #include <boost/log/core.hpp>
16 #include <boost/log/expressions.hpp>
17 #include <boost/log/attributes/current_process_name.hpp>
18 #include <boost/log/sinks/sync_frontend.hpp>
19 #include <boost/log/sinks/basic_sink_backend.hpp>
20 #include <boost/log/sources/record_ostream.hpp>
21 #include <boost/log/utility/value_ref.hpp>
22 #include <boost/log/utility/formatting_ostream.hpp>
23 #include <boost/log/utility/manipulators/add_value.hpp>
25 namespace logging
= boost::log
;
26 namespace attrs
= boost::log::attributes
;
27 namespace src
= boost::log::sources
;
28 namespace expr
= boost::log::expressions
;
29 namespace sinks
= boost::log::sinks
;
30 namespace keywords
= boost::log::keywords
;
32 //[ example_extension_app_launcher_definition
33 // The backend starts an external application to display notifications
35 public sinks::basic_formatted_sink_backend
<
36 char, /*< target character type >*/
37 sinks::synchronized_feeding
/*< in order not to spawn too many application instances we require records to be processed serial >*/
41 // The function consumes the log records that come from the frontend
42 void consume(logging::record_view
const& rec
, string_type
const& command_line
);
46 //[ example_extension_app_launcher_consume
47 // The function consumes the log records that come from the frontend
48 void app_launcher::consume(logging::record_view
const& rec
, string_type
const& command_line
)
50 std::system(command_line
.c_str());
54 //[ example_extension_app_launcher_formatting
55 BOOST_LOG_ATTRIBUTE_KEYWORD(process_name
, "ProcessName", std::string
)
56 BOOST_LOG_ATTRIBUTE_KEYWORD(caption
, "Caption", std::string
)
58 // Custom severity level formatting function
59 std::string
severity_level_as_urgency(
60 logging::value_ref
< logging::trivial::severity_level
, logging::trivial::tag::severity
> const& level
)
62 if (!level
|| level
.get() == logging::trivial::info
)
64 logging::trivial::severity_level lvl
= level
.get();
65 if (lvl
< logging::trivial::info
)
71 // The function initializes the logging library
74 boost::shared_ptr
< logging::core
> core
= logging::core::get();
76 typedef sinks::synchronous_sink
< app_launcher
> sink_t
;
77 boost::shared_ptr
< sink_t
> sink(new sink_t());
79 const std::pair
< const char*, const char* > shell_decorations
[] =
81 std::pair
< const char*, const char* >("\"", "\\\""),
82 std::pair
< const char*, const char* >("$", "\\$"),
83 std::pair
< const char*, const char* >("!", "\\!")
86 // Make the formatter generate the command line for notify-send
89 expr::stream
<< "notify-send -t 2000 -u "
90 << boost::phoenix::bind(&severity_level_as_urgency
, logging::trivial::severity
.or_none())
91 << expr::if_(expr::has_attr(process_name
))
93 expr::stream
<< " -a '" << process_name
<< "'"
95 << expr::if_(expr::has_attr(caption
))
97 expr::stream
<< " \"" << expr::char_decor(shell_decorations
)[ expr::stream
<< caption
] << "\""
99 << " \"" << expr::char_decor(shell_decorations
)[ expr::stream
<< expr::message
] << "\""
102 core
->add_sink(sink
);
104 // Add attributes that we will use
105 core
->add_global_attribute("ProcessName", attrs::current_process_name());
109 //[ example_extension_app_launcher_logging
110 void test_notifications()
112 BOOST_LOG_TRIVIAL(debug
) << "Hello, it's a simple notification";
113 BOOST_LOG_TRIVIAL(info
) << logging::add_value(caption
, "Caption text") << "And this notification has caption as well";
117 int main(int, char*[])
120 test_notifications();