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)
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/date_time/posix_time/posix_time.hpp>
14 #include <boost/log/common.hpp>
16 #if defined(BOOST_WINDOWS) && !defined(BOOST_LOG_WITHOUT_EVENT_LOG)
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>
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
;
30 //[ example_sinks_simple_event_log
32 typedef sinks::synchronous_sink
< sinks::simple_event_log_backend
> sink_t
;
34 // Define application-specific severity levels
44 // Create an event log sink
45 boost::shared_ptr
< sink_t
> sink(new sink_t());
49 expr::format("%1%: [%2%] - %3%")
50 % expr::attr
< unsigned int >("LineID")
51 % expr::attr
< boost::posix_time::ptime
>("TimeStamp")
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
;
61 sink
->locked_backend()->set_event_type_mapper(mapping
);
63 // Add the sink to the core
64 logging::core::get()->add_sink(sink
);
68 int main(int argc
, char* argv
[])
72 // Initialize logging library
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 >());
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";
87 catch (std::exception
& e
)
89 std::cout
<< "FAILURE: " << e
.what() << std::endl
;
96 int main(int, char*[])