Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file event_log_backend.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 07.11.2008
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains a logging sink backend that uses Windows NT event log API
|
Chris@16
|
13 * for signaling application events.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_SINKS_EVENT_LOG_BACKEND_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_SINKS_EVENT_LOG_BACKEND_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
22 #pragma once
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 #ifndef BOOST_LOG_WITHOUT_EVENT_LOG
|
Chris@16
|
26
|
Chris@16
|
27 #include <map>
|
Chris@16
|
28 #include <vector>
|
Chris@16
|
29 #include <string>
|
Chris@16
|
30 #include <iosfwd>
|
Chris@16
|
31 #include <boost/filesystem/path.hpp>
|
Chris@16
|
32 #include <boost/log/detail/light_function.hpp>
|
Chris@16
|
33 #include <boost/log/detail/parameter_tools.hpp>
|
Chris@16
|
34 #include <boost/log/attributes/attribute_value_set.hpp>
|
Chris@16
|
35 #include <boost/log/keywords/message_file.hpp>
|
Chris@16
|
36 #include <boost/log/keywords/log_name.hpp>
|
Chris@16
|
37 #include <boost/log/keywords/log_source.hpp>
|
Chris@16
|
38 #include <boost/log/keywords/registration.hpp>
|
Chris@16
|
39 #include <boost/log/keywords/target.hpp>
|
Chris@16
|
40 #include <boost/log/sinks/basic_sink_backend.hpp>
|
Chris@16
|
41 #include <boost/log/sinks/frontend_requirements.hpp>
|
Chris@16
|
42 #include <boost/log/sinks/attribute_mapping.hpp>
|
Chris@16
|
43 #include <boost/log/sinks/event_log_constants.hpp>
|
Chris@16
|
44 #include <boost/log/core/record_view.hpp>
|
Chris@16
|
45 #include <boost/log/expressions/formatter.hpp>
|
Chris@16
|
46 #include <boost/log/detail/header.hpp>
|
Chris@16
|
47
|
Chris@16
|
48 namespace boost {
|
Chris@16
|
49
|
Chris@16
|
50 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
51
|
Chris@16
|
52 namespace sinks {
|
Chris@16
|
53
|
Chris@16
|
54 namespace event_log {
|
Chris@16
|
55
|
Chris@16
|
56 //! Event log source registration modes
|
Chris@16
|
57 enum registration_mode
|
Chris@16
|
58 {
|
Chris@16
|
59 never, //!< Never register event source, even if it's not registered
|
Chris@16
|
60 on_demand, //!< Register if the source is not registered yet
|
Chris@16
|
61 forced //!< Register always, event if the source is already registered
|
Chris@16
|
62 };
|
Chris@16
|
63
|
Chris@16
|
64 /*!
|
Chris@16
|
65 * \brief Straightforward event type mapping
|
Chris@16
|
66 *
|
Chris@16
|
67 * This type of mapping assumes that attribute with a particular name always
|
Chris@16
|
68 * provides values that map directly onto the native event types. The mapping
|
Chris@16
|
69 * simply returns the extracted attribute value converted to the native event type.
|
Chris@16
|
70 */
|
Chris@16
|
71 template< typename AttributeValueT = int >
|
Chris@16
|
72 class direct_event_type_mapping :
|
Chris@16
|
73 public basic_direct_mapping< event_type, AttributeValueT >
|
Chris@16
|
74 {
|
Chris@16
|
75 //! Base type
|
Chris@16
|
76 typedef basic_direct_mapping< event_type, AttributeValueT > base_type;
|
Chris@16
|
77
|
Chris@16
|
78 public:
|
Chris@16
|
79 /*!
|
Chris@16
|
80 * Constructor
|
Chris@16
|
81 *
|
Chris@16
|
82 * \param name Attribute name
|
Chris@16
|
83 */
|
Chris@16
|
84 explicit direct_event_type_mapping(attribute_name const& name) :
|
Chris@16
|
85 base_type(name, info)
|
Chris@16
|
86 {
|
Chris@16
|
87 }
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 /*!
|
Chris@16
|
91 * \brief Customizable event type mapping
|
Chris@16
|
92 *
|
Chris@16
|
93 * The class allows to setup a custom mapping between an attribute and native event types.
|
Chris@16
|
94 * The mapping should be initialized similarly to the standard \c map container, by using
|
Chris@16
|
95 * indexing operator and assignment.
|
Chris@16
|
96 */
|
Chris@16
|
97 template< typename AttributeValueT = int >
|
Chris@16
|
98 class custom_event_type_mapping :
|
Chris@16
|
99 public basic_custom_mapping< event_type, AttributeValueT >
|
Chris@16
|
100 {
|
Chris@16
|
101 //! Base type
|
Chris@16
|
102 typedef basic_custom_mapping< event_type, AttributeValueT > base_type;
|
Chris@16
|
103
|
Chris@16
|
104 public:
|
Chris@16
|
105 /*!
|
Chris@16
|
106 * Constructor
|
Chris@16
|
107 *
|
Chris@16
|
108 * \param name Attribute name
|
Chris@16
|
109 */
|
Chris@16
|
110 explicit custom_event_type_mapping(attribute_name const& name) :
|
Chris@16
|
111 base_type(name, info)
|
Chris@16
|
112 {
|
Chris@16
|
113 }
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 /*!
|
Chris@16
|
117 * \brief Straightforward event ID mapping
|
Chris@16
|
118 *
|
Chris@16
|
119 * This type of mapping assumes that attribute with a particular name always
|
Chris@16
|
120 * provides values that map directly onto the event identifiers. The mapping
|
Chris@16
|
121 * simply returns the extracted attribute value converted to the event ID.
|
Chris@16
|
122 */
|
Chris@16
|
123 template< typename AttributeValueT = int >
|
Chris@16
|
124 class direct_event_id_mapping :
|
Chris@16
|
125 public basic_direct_mapping< event_id, AttributeValueT >
|
Chris@16
|
126 {
|
Chris@16
|
127 //! Base type
|
Chris@16
|
128 typedef basic_direct_mapping< event_id, AttributeValueT > base_type;
|
Chris@16
|
129
|
Chris@16
|
130 public:
|
Chris@16
|
131 /*!
|
Chris@16
|
132 * Constructor
|
Chris@16
|
133 *
|
Chris@16
|
134 * \param name Attribute name
|
Chris@16
|
135 */
|
Chris@16
|
136 explicit direct_event_id_mapping(attribute_name const& name) :
|
Chris@16
|
137 base_type(name, make_event_id(0))
|
Chris@16
|
138 {
|
Chris@16
|
139 }
|
Chris@16
|
140 };
|
Chris@16
|
141
|
Chris@16
|
142 /*!
|
Chris@16
|
143 * \brief Customizable event ID mapping
|
Chris@16
|
144 *
|
Chris@16
|
145 * The class allows to setup a custom mapping between an attribute and event identifiers.
|
Chris@16
|
146 * The mapping should be initialized similarly to the standard \c map container, by using
|
Chris@16
|
147 * indexing operator and assignment.
|
Chris@16
|
148 */
|
Chris@16
|
149 template< typename AttributeValueT = int >
|
Chris@16
|
150 class custom_event_id_mapping :
|
Chris@16
|
151 public basic_custom_mapping< event_id, AttributeValueT >
|
Chris@16
|
152 {
|
Chris@16
|
153 //! Base type
|
Chris@16
|
154 typedef basic_custom_mapping< event_id, AttributeValueT > base_type;
|
Chris@16
|
155
|
Chris@16
|
156 public:
|
Chris@16
|
157 /*!
|
Chris@16
|
158 * Constructor
|
Chris@16
|
159 *
|
Chris@16
|
160 * \param name Attribute name
|
Chris@16
|
161 */
|
Chris@16
|
162 explicit custom_event_id_mapping(attribute_name const& name) :
|
Chris@16
|
163 base_type(name, make_event_id(0))
|
Chris@16
|
164 {
|
Chris@16
|
165 }
|
Chris@16
|
166 };
|
Chris@16
|
167
|
Chris@16
|
168 /*!
|
Chris@16
|
169 * \brief Straightforward event category mapping
|
Chris@16
|
170 *
|
Chris@16
|
171 * This type of mapping assumes that attribute with a particular name always
|
Chris@16
|
172 * provides values that map directly onto the event categories. The mapping
|
Chris@16
|
173 * simply returns the extracted attribute value converted to the event category.
|
Chris@16
|
174 */
|
Chris@16
|
175 template< typename AttributeValueT = int >
|
Chris@16
|
176 class direct_event_category_mapping :
|
Chris@16
|
177 public basic_direct_mapping< event_category, AttributeValueT >
|
Chris@16
|
178 {
|
Chris@16
|
179 //! Base type
|
Chris@16
|
180 typedef basic_direct_mapping< event_category, AttributeValueT > base_type;
|
Chris@16
|
181
|
Chris@16
|
182 public:
|
Chris@16
|
183 /*!
|
Chris@16
|
184 * Constructor
|
Chris@16
|
185 *
|
Chris@16
|
186 * \param name Attribute name
|
Chris@16
|
187 */
|
Chris@16
|
188 explicit direct_event_category_mapping(attribute_name const& name) :
|
Chris@16
|
189 base_type(name, make_event_category(0))
|
Chris@16
|
190 {
|
Chris@16
|
191 }
|
Chris@16
|
192 };
|
Chris@16
|
193
|
Chris@16
|
194 /*!
|
Chris@16
|
195 * \brief Customizable event category mapping
|
Chris@16
|
196 *
|
Chris@16
|
197 * The class allows to setup a custom mapping between an attribute and event categories.
|
Chris@16
|
198 * The mapping should be initialized similarly to the standard \c map container, by using
|
Chris@16
|
199 * indexing operator and assignment.
|
Chris@16
|
200 */
|
Chris@16
|
201 template< typename AttributeValueT = int >
|
Chris@16
|
202 class custom_event_category_mapping :
|
Chris@16
|
203 public basic_custom_mapping< event_category, AttributeValueT >
|
Chris@16
|
204 {
|
Chris@16
|
205 //! Base type
|
Chris@16
|
206 typedef basic_custom_mapping< event_category, AttributeValueT > base_type;
|
Chris@16
|
207
|
Chris@16
|
208 public:
|
Chris@16
|
209 /*!
|
Chris@16
|
210 * Constructor
|
Chris@16
|
211 *
|
Chris@16
|
212 * \param name Attribute name
|
Chris@16
|
213 */
|
Chris@16
|
214 explicit custom_event_category_mapping(attribute_name const& name) :
|
Chris@16
|
215 base_type(name, make_event_category(0))
|
Chris@16
|
216 {
|
Chris@16
|
217 }
|
Chris@16
|
218 };
|
Chris@16
|
219
|
Chris@16
|
220 /*!
|
Chris@16
|
221 * \brief An event composer
|
Chris@16
|
222 *
|
Chris@16
|
223 * This class is a function object that extracts event identifier from the attribute values set
|
Chris@16
|
224 * and formats insertion strings for the particular event. Each insertion string is formatted with
|
Chris@16
|
225 * a distinct formatter, which can be created just like regular sinks formatters.
|
Chris@16
|
226 *
|
Chris@16
|
227 * Before using, the composer must be initialized with the following information:
|
Chris@16
|
228 * \li Event identifier extraction logic. One can use \c basic_direct_event_id_mapping or
|
Chris@16
|
229 * \c basic_custom_event_id_mapping classes in order to create such extractor and pass it
|
Chris@16
|
230 * to the composer constructor.
|
Chris@16
|
231 * \li Event identifiers and insertion string formatters. The composer provides the following
|
Chris@16
|
232 * syntax to provide this information:
|
Chris@16
|
233 *
|
Chris@16
|
234 * \code
|
Chris@16
|
235 * event_composer comp;
|
Chris@16
|
236 * comp[MY_EVENT_ID1] % formatter1 % ... % formatterN;
|
Chris@16
|
237 * comp[MY_EVENT_ID2] % formatter1 % ... % formatterN;
|
Chris@16
|
238 * ...
|
Chris@16
|
239 * \endcode
|
Chris@16
|
240 *
|
Chris@16
|
241 * The event identifiers in square brackets are provided by the message compiler generated
|
Chris@16
|
242 * header (the actual names are specified in the .mc file). The formatters represent
|
Chris@16
|
243 * the insertion strings that will be used to replace placeholders in event messages,
|
Chris@16
|
244 * thus the number and the order of the formatters must correspond to the message definition.
|
Chris@16
|
245 */
|
Chris@16
|
246 template< typename CharT >
|
Chris@16
|
247 class BOOST_LOG_API basic_event_composer
|
Chris@16
|
248 {
|
Chris@16
|
249 public:
|
Chris@16
|
250 //! Character type
|
Chris@16
|
251 typedef CharT char_type;
|
Chris@16
|
252 //! String type to be used as a message text holder
|
Chris@16
|
253 typedef std::basic_string< char_type > string_type;
|
Chris@16
|
254
|
Chris@16
|
255 //! Event identifier mapper type
|
Chris@16
|
256 typedef boost::log::aux::light_function< event_id (record_view const&) > event_id_mapper_type;
|
Chris@16
|
257
|
Chris@16
|
258 //! Type of an insertion composer (a formatter)
|
Chris@16
|
259 typedef basic_formatter< char_type > formatter_type;
|
Chris@16
|
260 //! Type of the composed insertions list
|
Chris@16
|
261 typedef std::vector< string_type > insertion_list;
|
Chris@16
|
262
|
Chris@16
|
263 private:
|
Chris@16
|
264 //! \cond
|
Chris@16
|
265
|
Chris@16
|
266 //! The class that implements formatting of insertion strings
|
Chris@16
|
267 class insertion_composer;
|
Chris@16
|
268
|
Chris@16
|
269 //! Type of the events map
|
Chris@16
|
270 typedef std::map< event_id, insertion_composer > event_map;
|
Chris@16
|
271
|
Chris@16
|
272 //! A smart reference that puts formatters into the composer
|
Chris@16
|
273 class event_map_reference;
|
Chris@16
|
274 friend class event_map_reference;
|
Chris@16
|
275 class event_map_reference
|
Chris@16
|
276 {
|
Chris@16
|
277 private:
|
Chris@16
|
278 //! Event identifier
|
Chris@16
|
279 event_id m_ID;
|
Chris@16
|
280 //! A reference to the object that created the reference
|
Chris@16
|
281 basic_event_composer< char_type >& m_Owner;
|
Chris@16
|
282 //! A hint for the owner to optimize insertion
|
Chris@16
|
283 insertion_composer* m_Composer;
|
Chris@16
|
284
|
Chris@16
|
285 public:
|
Chris@16
|
286 //! Initializing constructor
|
Chris@16
|
287 explicit event_map_reference(event_id id, basic_event_composer< char_type >& owner) :
|
Chris@16
|
288 m_ID(id),
|
Chris@16
|
289 m_Owner(owner),
|
Chris@16
|
290 m_Composer(0)
|
Chris@16
|
291 {
|
Chris@16
|
292 }
|
Chris@16
|
293 //! The operator puts the formatter into the composer
|
Chris@16
|
294 template< typename FormatterT >
|
Chris@16
|
295 event_map_reference& operator% (FormatterT const& fmt)
|
Chris@16
|
296 {
|
Chris@16
|
297 m_Composer = m_Owner.add_formatter(m_ID, m_Composer, formatter_type(fmt));
|
Chris@16
|
298 return *this;
|
Chris@16
|
299 }
|
Chris@16
|
300 };
|
Chris@16
|
301
|
Chris@16
|
302 //! \endcond
|
Chris@16
|
303
|
Chris@16
|
304 private:
|
Chris@16
|
305 //! The mapper that will extract the event identifier
|
Chris@16
|
306 event_id_mapper_type m_EventIDMapper;
|
Chris@16
|
307 //! The map of event identifiers and their insertion composers
|
Chris@16
|
308 event_map m_EventMap;
|
Chris@16
|
309
|
Chris@16
|
310 public:
|
Chris@16
|
311 /*!
|
Chris@16
|
312 * Default constructor. Creates an empty map of events.
|
Chris@16
|
313 *
|
Chris@16
|
314 * \param id_mapper An event identifier mapping function that will be used to extract event ID from attribute values
|
Chris@16
|
315 */
|
Chris@16
|
316 explicit basic_event_composer(event_id_mapper_type const& id_mapper);
|
Chris@16
|
317 /*!
|
Chris@16
|
318 * Copy constructor. Performs a deep copy of the object.
|
Chris@16
|
319 */
|
Chris@16
|
320 basic_event_composer(basic_event_composer const& that);
|
Chris@16
|
321 /*!
|
Chris@16
|
322 * Destructor
|
Chris@16
|
323 */
|
Chris@16
|
324 ~basic_event_composer();
|
Chris@16
|
325
|
Chris@16
|
326 /*!
|
Chris@16
|
327 * Assignment. Provides strong exception guarantee.
|
Chris@16
|
328 */
|
Chris@16
|
329 basic_event_composer& operator= (basic_event_composer that);
|
Chris@16
|
330 /*!
|
Chris@16
|
331 * Swaps \c *this and \c that objects.
|
Chris@16
|
332 */
|
Chris@16
|
333 void swap(basic_event_composer& that);
|
Chris@16
|
334 /*!
|
Chris@16
|
335 * Initiates creation of a new event description. The result of the operator can be used to
|
Chris@16
|
336 * add formatters for insertion strings construction. The returned reference type is implementation detail.
|
Chris@16
|
337 *
|
Chris@16
|
338 * \param id Event identifier.
|
Chris@16
|
339 */
|
Chris@16
|
340 event_map_reference operator[] (event_id id);
|
Chris@16
|
341 /*!
|
Chris@16
|
342 * Initiates creation of a new event description. The result of the operator can be used to
|
Chris@16
|
343 * add formatters for insertion strings construction. The returned reference type is implementation detail.
|
Chris@16
|
344 *
|
Chris@16
|
345 * \param id Event identifier.
|
Chris@16
|
346 */
|
Chris@16
|
347 event_map_reference operator[] (int id);
|
Chris@16
|
348 /*!
|
Chris@16
|
349 * Event composition operator. Extracts an event identifier from the attribute values by calling event ID mapper.
|
Chris@16
|
350 * Then runs all formatters that were registered for the event with the extracted ID. The results of formatting
|
Chris@16
|
351 * are returned in the \a insertions parameter.
|
Chris@16
|
352 *
|
Chris@16
|
353 * \param rec Log record view
|
Chris@16
|
354 * \param insertions A sequence of formatted insertion strings
|
Chris@16
|
355 * \return An event identifier that was extracted from \c attributes
|
Chris@16
|
356 */
|
Chris@16
|
357 event_id operator() (record_view const& rec, insertion_list& insertions) const;
|
Chris@16
|
358
|
Chris@16
|
359 private:
|
Chris@16
|
360 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
361 //! Adds a formatter to the insertion composers list
|
Chris@16
|
362 insertion_composer* add_formatter(event_id id, insertion_composer* composer, formatter_type const& fmt);
|
Chris@16
|
363 #endif // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
364 };
|
Chris@16
|
365
|
Chris@16
|
366 #ifdef BOOST_LOG_USE_CHAR
|
Chris@16
|
367 typedef basic_event_composer< char > event_composer; //!< Convenience typedef for narrow-character logging
|
Chris@16
|
368 #endif
|
Chris@16
|
369 #ifdef BOOST_LOG_USE_WCHAR_T
|
Chris@16
|
370 typedef basic_event_composer< wchar_t > wevent_composer; //!< Convenience typedef for wide-character logging
|
Chris@16
|
371 #endif
|
Chris@16
|
372
|
Chris@16
|
373 } // namespace event_log
|
Chris@16
|
374
|
Chris@16
|
375 /*!
|
Chris@16
|
376 * \brief An implementation of a simple logging sink backend that emits events into Windows NT event log
|
Chris@16
|
377 *
|
Chris@16
|
378 * The sink uses Windows NT 5 (Windows 2000) and later event log API to emit events
|
Chris@16
|
379 * to an event log. The sink acts as an event source in terms of the API, it implements all needed resources
|
Chris@16
|
380 * and source registration in the Windows registry that is needed for the event delivery.
|
Chris@16
|
381 *
|
Chris@16
|
382 * The backend performs message text formatting. The composed text is then passed as the first
|
Chris@16
|
383 * and only string parameter of the event. The resource embedded into the backend describes the event
|
Chris@16
|
384 * so that the parameter is inserted into the event description text, thus making it visible
|
Chris@16
|
385 * in the event log.
|
Chris@16
|
386 *
|
Chris@16
|
387 * The backend allows to customize mapping of application severity levels to the native Windows event types.
|
Chris@16
|
388 * This allows to write portable code even if OS-specific sinks, such as this one, are used.
|
Chris@16
|
389 *
|
Chris@16
|
390 * \note Since the backend registers itself into Windows registry as the resource file that contains
|
Chris@16
|
391 * event description, it is important to keep the library binary in a stable place of the filesystem.
|
Chris@16
|
392 * Otherwise Windows might not be able to load event resources from the library and display
|
Chris@16
|
393 * events correctly.
|
Chris@16
|
394 *
|
Chris@16
|
395 * \note It is known that Windows is not able to find event resources in the application executable,
|
Chris@16
|
396 * which is linked against the static build of the library. Users are advised to use dynamic
|
Chris@16
|
397 * builds of the library to solve this problem.
|
Chris@16
|
398 */
|
Chris@16
|
399 template< typename CharT >
|
Chris@16
|
400 class basic_simple_event_log_backend :
|
Chris@16
|
401 public basic_formatted_sink_backend< CharT, concurrent_feeding >
|
Chris@16
|
402 {
|
Chris@16
|
403 //! Base type
|
Chris@16
|
404 typedef basic_formatted_sink_backend< CharT, concurrent_feeding > base_type;
|
Chris@16
|
405 //! Implementation type
|
Chris@16
|
406 struct implementation;
|
Chris@16
|
407
|
Chris@16
|
408 public:
|
Chris@16
|
409 //! Character type
|
Chris@16
|
410 typedef typename base_type::char_type char_type;
|
Chris@16
|
411 //! String type to be used as a message text holder
|
Chris@16
|
412 typedef typename base_type::string_type string_type;
|
Chris@16
|
413
|
Chris@16
|
414 //! Mapper type for the event type
|
Chris@16
|
415 typedef boost::log::aux::light_function< event_log::event_type (record_view const&) > event_type_mapper_type;
|
Chris@16
|
416
|
Chris@16
|
417 private:
|
Chris@16
|
418 //! Pointer to the backend implementation that hides various types from windows.h
|
Chris@16
|
419 implementation* m_pImpl;
|
Chris@16
|
420
|
Chris@16
|
421 public:
|
Chris@16
|
422 /*!
|
Chris@16
|
423 * Default constructor. Registers event source with name based on the application
|
Chris@16
|
424 * executable file name in the Application log. If such a registration is already
|
Chris@16
|
425 * present, it is not overridden.
|
Chris@16
|
426 */
|
Chris@16
|
427 BOOST_LOG_API basic_simple_event_log_backend();
|
Chris@16
|
428 /*!
|
Chris@16
|
429 * Constructor. Registers event log source with the specified parameters.
|
Chris@16
|
430 * The following named parameters are supported:
|
Chris@16
|
431 *
|
Chris@16
|
432 * \li \c target - Specifies an UNC path to the remote server which log records should be sent to.
|
Chris@16
|
433 * The local machine will be used to process log records, if not specified.
|
Chris@16
|
434 * \li \c log_name - Specifies the log in which the source should be registered.
|
Chris@16
|
435 * The result of \c get_default_log_name is used, if the parameter is not specified.
|
Chris@16
|
436 * \li \c log_source - Specifies the source name. The result of \c get_default_source_name
|
Chris@16
|
437 * is used, if the parameter is not specified.
|
Chris@16
|
438 * \li \c registration - Specifies the event source registration mode in the Windows registry.
|
Chris@16
|
439 * Can have values of the \c registration_mode enum. Default value: \c on_demand.
|
Chris@16
|
440 *
|
Chris@16
|
441 * \param args A set of named parameters.
|
Chris@16
|
442 */
|
Chris@16
|
443 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
444 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(basic_simple_event_log_backend, construct)
|
Chris@16
|
445 #else
|
Chris@16
|
446 template< typename... ArgsT >
|
Chris@16
|
447 explicit basic_simple_event_log_backend(ArgsT... const& args);
|
Chris@16
|
448 #endif
|
Chris@16
|
449
|
Chris@16
|
450 /*!
|
Chris@16
|
451 * Destructor. Unregisters event source. The log source description is not removed from the Windows registry.
|
Chris@16
|
452 */
|
Chris@16
|
453 BOOST_LOG_API ~basic_simple_event_log_backend();
|
Chris@16
|
454
|
Chris@16
|
455 /*!
|
Chris@16
|
456 * The method installs the function object that maps application severity levels to WinAPI event types
|
Chris@16
|
457 */
|
Chris@16
|
458 BOOST_LOG_API void set_event_type_mapper(event_type_mapper_type const& mapper);
|
Chris@16
|
459
|
Chris@16
|
460 /*!
|
Chris@16
|
461 * \returns Default log name: Application
|
Chris@16
|
462 */
|
Chris@16
|
463 BOOST_LOG_API static string_type get_default_log_name();
|
Chris@16
|
464 /*!
|
Chris@16
|
465 * \returns Default log source name that is based on the application executable file name and the sink name
|
Chris@16
|
466 */
|
Chris@16
|
467 BOOST_LOG_API static string_type get_default_source_name();
|
Chris@16
|
468
|
Chris@16
|
469 /*!
|
Chris@16
|
470 * The method puts the formatted message to the event log
|
Chris@16
|
471 */
|
Chris@16
|
472 BOOST_LOG_API void consume(record_view const& rec, string_type const& formatted_message);
|
Chris@16
|
473
|
Chris@16
|
474 private:
|
Chris@16
|
475 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
476 //! Constructs backend implementation
|
Chris@16
|
477 template< typename ArgsT >
|
Chris@16
|
478 void construct(ArgsT const& args)
|
Chris@16
|
479 {
|
Chris@16
|
480 construct(
|
Chris@16
|
481 args[keywords::target | string_type()],
|
Chris@16
|
482 args[keywords::log_name || &basic_simple_event_log_backend::get_default_log_name],
|
Chris@16
|
483 args[keywords::log_source || &basic_simple_event_log_backend::get_default_source_name],
|
Chris@16
|
484 args[keywords::registration | event_log::on_demand]);
|
Chris@16
|
485 }
|
Chris@16
|
486 BOOST_LOG_API void construct(
|
Chris@16
|
487 string_type const& target,
|
Chris@16
|
488 string_type const& log_name,
|
Chris@16
|
489 string_type const& source_name,
|
Chris@16
|
490 event_log::registration_mode reg_mode);
|
Chris@16
|
491 #endif // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
492 };
|
Chris@16
|
493
|
Chris@16
|
494 /*!
|
Chris@16
|
495 * \brief An implementation of a logging sink backend that emits events into Windows NT event log
|
Chris@16
|
496 *
|
Chris@16
|
497 * The sink uses Windows NT 5 (Windows 2000) and later event log API to emit events
|
Chris@16
|
498 * to an event log. The sink acts as an event source. Unlike \c basic_simple_event_log_backend,
|
Chris@16
|
499 * this sink backend allows users to specify the custom event message file and supports
|
Chris@16
|
500 * mapping attribute values onto several insertion strings. Although it requires considerably
|
Chris@16
|
501 * more scaffolding than the simple backend, this allows to support localizable event descriptions.
|
Chris@16
|
502 *
|
Chris@16
|
503 * Besides the file name of the module with event resources, the backend provides the following
|
Chris@16
|
504 * customizations:
|
Chris@16
|
505 * \li Remote server UNC address, log name and source name. These parameters have similar meaning
|
Chris@16
|
506 * to \c basic_simple_event_log_backend.
|
Chris@16
|
507 * \li Event type and category mappings. These are function object that allow to map attribute
|
Chris@16
|
508 * values to the according event parameters. One can use mappings in the \c event_log namespace.
|
Chris@16
|
509 * \li Event composer. This function object extracts event identifier and formats string insertions,
|
Chris@16
|
510 * that will be used by the API to compose the final event message text.
|
Chris@16
|
511 */
|
Chris@16
|
512 template< typename CharT >
|
Chris@16
|
513 class basic_event_log_backend :
|
Chris@16
|
514 public basic_sink_backend< synchronized_feeding >
|
Chris@16
|
515 {
|
Chris@16
|
516 //! Base type
|
Chris@16
|
517 typedef basic_sink_backend< synchronized_feeding > base_type;
|
Chris@16
|
518 //! Implementation type
|
Chris@16
|
519 struct implementation;
|
Chris@16
|
520
|
Chris@16
|
521 public:
|
Chris@16
|
522 //! Character type
|
Chris@16
|
523 typedef CharT char_type;
|
Chris@16
|
524 //! String type
|
Chris@16
|
525 typedef std::basic_string< char_type > string_type;
|
Chris@16
|
526 //! Type of the composed insertions list
|
Chris@16
|
527 typedef std::vector< string_type > insertion_list;
|
Chris@16
|
528
|
Chris@16
|
529 //! Mapper type for the event type
|
Chris@16
|
530 typedef boost::log::aux::light_function< event_log::event_type (record_view const&) > event_type_mapper_type;
|
Chris@16
|
531 //! Mapper type for the event category
|
Chris@16
|
532 typedef boost::log::aux::light_function< event_log::event_category (record_view const&) > event_category_mapper_type;
|
Chris@16
|
533 //! Event composer type
|
Chris@16
|
534 typedef boost::log::aux::light_function< event_log::event_id (record_view const&, insertion_list&) > event_composer_type;
|
Chris@16
|
535
|
Chris@16
|
536 private:
|
Chris@16
|
537 //! Pointer to the backend implementation that hides various types from windows.h
|
Chris@16
|
538 implementation* m_pImpl;
|
Chris@16
|
539
|
Chris@16
|
540 public:
|
Chris@16
|
541 /*!
|
Chris@16
|
542 * Constructor. Registers event source with name based on the application
|
Chris@16
|
543 * executable file name in the Application log. If such a registration is already
|
Chris@16
|
544 * present, it is not overridden.
|
Chris@16
|
545 */
|
Chris@16
|
546 template< typename T >
|
Chris@16
|
547 explicit basic_event_log_backend(std::basic_string< T > const& message_file_name)
|
Chris@16
|
548 {
|
Chris@16
|
549 construct(keywords::message_file = message_file_name);
|
Chris@16
|
550 }
|
Chris@16
|
551 /*!
|
Chris@16
|
552 * Constructor. Registers event source with name based on the application
|
Chris@16
|
553 * executable file name in the Application log. If such a registration is already
|
Chris@16
|
554 * present, it is not overridden.
|
Chris@16
|
555 */
|
Chris@16
|
556 explicit basic_event_log_backend(filesystem::path const& message_file_name)
|
Chris@16
|
557 {
|
Chris@16
|
558 construct(keywords::message_file = message_file_name);
|
Chris@16
|
559 }
|
Chris@16
|
560 /*!
|
Chris@16
|
561 * Constructor. Registers event log source with the specified parameters.
|
Chris@16
|
562 * The following named parameters are supported:
|
Chris@16
|
563 *
|
Chris@16
|
564 * \li \c message_file - Specifies the file name that contains resources that
|
Chris@16
|
565 * describe events and categories.
|
Chris@16
|
566 * \li \c target - Specifies an UNC path to the remote server to which log records should be sent to.
|
Chris@16
|
567 * The local machine will be used to process log records, if not specified.
|
Chris@16
|
568 * \li \c log_name - Specifies the log in which the source should be registered.
|
Chris@16
|
569 * The result of \c get_default_log_name is used, if the parameter is not specified.
|
Chris@16
|
570 * \li \c log_source - Specifies the source name. The result of \c get_default_source_name
|
Chris@16
|
571 * is used, if the parameter is not specified.
|
Chris@16
|
572 * \li \c registration - Specifies the event source registration mode in the Windows registry.
|
Chris@16
|
573 * Can have values of the \c registration_mode enum. Default value: \c on_demand.
|
Chris@16
|
574 *
|
Chris@16
|
575 * \param args A set of named parameters.
|
Chris@16
|
576 */
|
Chris@16
|
577 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
578 BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(basic_event_log_backend, construct)
|
Chris@16
|
579 #else
|
Chris@16
|
580 template< typename... ArgsT >
|
Chris@16
|
581 explicit basic_event_log_backend(ArgsT... const& args);
|
Chris@16
|
582 #endif
|
Chris@16
|
583
|
Chris@16
|
584 /*!
|
Chris@16
|
585 * Destructor. Unregisters event source. The log source description is not removed from the Windows registry.
|
Chris@16
|
586 */
|
Chris@16
|
587 BOOST_LOG_API ~basic_event_log_backend();
|
Chris@16
|
588
|
Chris@16
|
589 /*!
|
Chris@16
|
590 * The method creates an event in the event log
|
Chris@16
|
591 *
|
Chris@16
|
592 * \param rec Log record to consume
|
Chris@16
|
593 */
|
Chris@16
|
594 BOOST_LOG_API void consume(record_view const& rec);
|
Chris@16
|
595
|
Chris@16
|
596 /*!
|
Chris@16
|
597 * The method installs the function object that maps application severity levels to WinAPI event types
|
Chris@16
|
598 */
|
Chris@16
|
599 BOOST_LOG_API void set_event_type_mapper(event_type_mapper_type const& mapper);
|
Chris@16
|
600
|
Chris@16
|
601 /*!
|
Chris@16
|
602 * The method installs the function object that extracts event category from attribute values
|
Chris@16
|
603 */
|
Chris@16
|
604 BOOST_LOG_API void set_event_category_mapper(event_category_mapper_type const& mapper);
|
Chris@16
|
605
|
Chris@16
|
606 /*!
|
Chris@16
|
607 * The method installs the function object that extracts event identifier from the attributes and creates
|
Chris@16
|
608 * insertion strings that will replace placeholders in the event message.
|
Chris@16
|
609 */
|
Chris@16
|
610 BOOST_LOG_API void set_event_composer(event_composer_type const& composer);
|
Chris@16
|
611
|
Chris@16
|
612 /*!
|
Chris@16
|
613 * \returns Default log name: Application
|
Chris@16
|
614 */
|
Chris@16
|
615 BOOST_LOG_API static string_type get_default_log_name();
|
Chris@16
|
616 /*!
|
Chris@16
|
617 * \returns Default log source name that is based on the application executable file name and the sink name
|
Chris@16
|
618 */
|
Chris@16
|
619 BOOST_LOG_API static string_type get_default_source_name();
|
Chris@16
|
620
|
Chris@16
|
621 private:
|
Chris@16
|
622 #ifndef BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
623 //! Constructs backend implementation
|
Chris@16
|
624 template< typename ArgsT >
|
Chris@16
|
625 void construct(ArgsT const& args)
|
Chris@16
|
626 {
|
Chris@16
|
627 construct(
|
Chris@16
|
628 filesystem::path(args[keywords::message_file]),
|
Chris@16
|
629 args[keywords::target | string_type()],
|
Chris@16
|
630 args[keywords::log_name || &basic_event_log_backend::get_default_log_name],
|
Chris@16
|
631 args[keywords::log_source || &basic_event_log_backend::get_default_source_name],
|
Chris@16
|
632 args[keywords::registration | event_log::on_demand]);
|
Chris@16
|
633 }
|
Chris@16
|
634 BOOST_LOG_API void construct(
|
Chris@16
|
635 filesystem::path const& message_file_name,
|
Chris@16
|
636 string_type const& target,
|
Chris@16
|
637 string_type const& log_name,
|
Chris@16
|
638 string_type const& source_name,
|
Chris@16
|
639 event_log::registration_mode reg_mode);
|
Chris@16
|
640 #endif // BOOST_LOG_DOXYGEN_PASS
|
Chris@16
|
641 };
|
Chris@16
|
642
|
Chris@16
|
643 #ifdef BOOST_LOG_USE_CHAR
|
Chris@16
|
644 typedef basic_simple_event_log_backend< char > simple_event_log_backend; //!< Convenience typedef for narrow-character logging
|
Chris@16
|
645 typedef basic_event_log_backend< char > event_log_backend; //!< Convenience typedef for narrow-character logging
|
Chris@16
|
646 #endif
|
Chris@16
|
647 #ifdef BOOST_LOG_USE_WCHAR_T
|
Chris@16
|
648 typedef basic_simple_event_log_backend< wchar_t > wsimple_event_log_backend; //!< Convenience typedef for wide-character logging
|
Chris@16
|
649 typedef basic_event_log_backend< wchar_t > wevent_log_backend; //!< Convenience typedef for wide-character logging
|
Chris@16
|
650 #endif
|
Chris@16
|
651
|
Chris@16
|
652 } // namespace sinks
|
Chris@16
|
653
|
Chris@16
|
654 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
655
|
Chris@16
|
656 } // namespace boost
|
Chris@16
|
657
|
Chris@16
|
658 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
659
|
Chris@16
|
660 #endif // BOOST_LOG_WITHOUT_EVENT_LOG
|
Chris@16
|
661
|
Chris@16
|
662 #endif // BOOST_LOG_SINKS_EVENT_LOG_BACKEND_HPP_INCLUDED_
|