• 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 #include <stdexcept>
9 #include <string>
10 #include <iostream>
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/date_time/posix_time/posix_time.hpp>
13 
14 #include <boost/log/common.hpp>
15 
16 #if defined(BOOST_WINDOWS) && !defined(BOOST_LOG_WITHOUT_EVENT_LOG)
17 
18 #include <boost/log/attributes.hpp>
19 #include <boost/log/expressions.hpp>
20 #include <boost/log/sinks/sync_frontend.hpp>
21 #include <boost/log/sinks/event_log_backend.hpp>
22 
23 namespace logging = boost::log;
24 namespace attrs = boost::log::attributes;
25 namespace src = boost::log::sources;
26 namespace sinks = boost::log::sinks;
27 namespace expr = boost::log::expressions;
28 namespace keywords = boost::log::keywords;
29 
30 //[ example_sinks_simple_event_log
31 // Complete sink type
32 typedef sinks::synchronous_sink< sinks::simple_event_log_backend > sink_t;
33 
34 // Define application-specific severity levels
35 enum severity_level
36 {
37     normal,
38     warning,
39     error
40 };
41 
init_logging()42 void init_logging()
43 {
44     // Create an event log sink
45     boost::shared_ptr< sink_t > sink(new sink_t());
46 
47     sink->set_formatter
48     (
49         expr::format("%1%: [%2%] - %3%")
50             % expr::attr< unsigned int >("LineID")
51             % expr::attr< boost::posix_time::ptime >("TimeStamp")
52             % expr::smessage
53     );
54 
55     // We'll have to map our custom levels to the event log event types
56     sinks::event_log::custom_event_type_mapping< severity_level > mapping("Severity");
57     mapping[normal] = sinks::event_log::info;
58     mapping[warning] = sinks::event_log::warning;
59     mapping[error] = sinks::event_log::error;
60 
61     sink->locked_backend()->set_event_type_mapper(mapping);
62 
63     // Add the sink to the core
64     logging::core::get()->add_sink(sink);
65 }
66 //]
67 
main(int argc,char * argv[])68 int main(int argc, char* argv[])
69 {
70     try
71     {
72         // Initialize logging library
73         init_logging();
74 
75         // Add some attributes too
76         logging::core::get()->add_global_attribute("TimeStamp", attrs::local_clock());
77         logging::core::get()->add_global_attribute("LineID", attrs::counter< unsigned int >());
78 
79         // Do some logging
80         src::severity_logger< severity_level > lg(keywords::severity = normal);
81         BOOST_LOG_SEV(lg, normal) << "Some record for NT event log with normal level";
82         BOOST_LOG_SEV(lg, warning) << "Some record for NT event log with warning level";
83         BOOST_LOG_SEV(lg, error) << "Some record for NT event log with error level";
84 
85         return 0;
86     }
87     catch (std::exception& e)
88     {
89         std::cout << "FAILURE: " << e.what() << std::endl;
90         return 1;
91     }
92 }
93 
94 #else
95 
main(int,char * [])96 int main(int, char*[])
97 {
98     return 0;
99 }
100 
101 #endif
102