3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Utilities</title>
5 <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7 <link rel="home" href="../../index.html" title="Chapter 1. Boost.Log v2">
8 <link rel="up" href="../detailed.html" title="Detailed features description">
9 <link rel="prev" href="attributes.html" title="Attributes">
10 <link rel="next" href="../extension.html" title="Extending the library">
12 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13 <table cellpadding="2" width="100%"><tr><td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td></tr></table>
15 <div class="spirit-nav">
16 <a accesskey="p" href="attributes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../detailed.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../extension.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
19 <div class="titlepage"><div><div><h3 class="title">
20 <a name="log.detailed.utilities"></a><a class="link" href="utilities.html" title="Utilities">Utilities</a>
21 </h3></div></div></div>
22 <div class="toc"><dl class="toc">
23 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.string_literal">String literals</a></span></dt>
24 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.type_dispatch">Type dispatchers</a></span></dt>
25 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.predef_types">Predefined type
26 sequences</a></span></dt>
27 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.value_ref">Value reference wrapper</a></span></dt>
28 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.record_ordering">Log record
29 ordering</a></span></dt>
30 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.exception_handlers">Exception
31 handlers</a></span></dt>
32 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.manipulators">Output manipulators</a></span></dt>
33 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.ipc">Interprocess communication
35 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.setup">Simplified library initialization
39 <div class="titlepage"><div><div><h4 class="title">
40 <a name="log.detailed.utilities.string_literal"></a><a class="link" href="utilities.html#log.detailed.utilities.string_literal" title="String literals">String literals</a>
41 </h4></div></div></div>
42 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.string_literal_hpp" title="Header <boost/log/utility/string_literal.hpp>">boost/log/utility/string_literal.hpp</a></code><span class="special">></span>
45 String literals are used in several places throughout the library. However,
46 this component can be successfully used outside of the library in users'
47 code. It is header-only and does not require linking with the library binary.
48 String literals can improve performance significantly if there is no need
49 to modify stored strings. What is also important, since string literals
50 do not dynamically allocate memory, it is easier to maintain exception
51 safety when using string literals instead of regular strings.
54 The functionality is implemented in the <code class="computeroutput"><a class="link" href="../../boost/log/basic_string_literal.html" title="Class template basic_string_literal">basic_string_literal</a></code>
55 class template, which is parametrized with the character and character
56 traits, similar to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code>.
57 There are also two convenience typedefs provided: <code class="computeroutput"><span class="identifier">string_literal</span></code>
58 and <code class="computeroutput"><span class="identifier">wstring_literal</span></code>, for
59 narrow and wide character types, respectively. In order to ease string
60 literal construction in generic code there is also a <code class="computeroutput"><span class="identifier">str_literal</span></code>
61 function template that accepts a string literal and returns a <code class="computeroutput"><a class="link" href="../../boost/log/basic_string_literal.html" title="Class template basic_string_literal">basic_string_literal</a></code>
62 instance for the appropriate character type.
65 String literals support interface similar to STL strings, except for string
66 modification functions. However, it is possible to assign to or clear string
67 literals, as long as only string literals involved. Relational and stream
68 output operators are also supported.
72 <div class="titlepage"><div><div><h4 class="title">
73 <a name="log.detailed.utilities.type_dispatch"></a><a class="link" href="utilities.html#log.detailed.utilities.type_dispatch" title="Type dispatchers">Type dispatchers</a>
74 </h4></div></div></div>
75 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.type_dispatch.type_dispatcher_hpp" title="Header <boost/log/utility/type_dispatch/type_dispatcher.hpp>">boost/log/utility/type_dispatch/type_dispatcher.hpp</a></code><span class="special">></span>
78 Type dispatchers are used throughout the library in order to work with
79 attribute values. Dispatchers allow acquiring the stored attribute value
80 using the Visitor concept. The most notable places where the functionality
81 is used are filters and formatters. However, this mechanism is orthogonal
82 to attributes and can be used for other purposes as well. Most of the time
83 users won't need to dig into the details of type dispatchers, but this
84 information may be useful for those who intend to extend the library and
85 wants to understand what's under the hood.
88 Every type dispatcher supports the <code class="computeroutput"><a class="link" href="../../boost/log/type_dispatcher.html" title="Class type_dispatcher">type_dispatcher</a></code>
89 interface. When an attribute value needs to be extracted, this interface
90 is passed to the attribute value object, which then tries to acquire the
91 callback for the actual type of the value. All callbacks are objects of
92 the [class_type_dispatcher_callback] class template, instantiated on the
93 actual type of the value. If the dispatcher is able to consume the value
94 of the requested type, it must return a non-empty callback object. When
95 (and if) the corresponding callback is acquired, the attribute value object
96 only has to pass the contained value to its <code class="computeroutput"><span class="keyword">operator</span>
97 <span class="special">()</span></code>.
100 Happily, there is no need to write type dispatchers from scratch. The library
101 provides two kinds of type dispatchers that implement the <code class="computeroutput"><a class="link" href="../../boost/log/type_dispatcher.html" title="Class type_dispatcher">type_dispatcher</a></code>
102 and [class_type_dispatcher_callback] interfaces and encapsulate the callback
106 <a name="log.detailed.utilities.type_dispatch.h0"></a>
107 <span class="phrase"><a name="log.detailed.utilities.type_dispatch.static_type_dispatcher"></a></span><a class="link" href="utilities.html#log.detailed.utilities.type_dispatch.static_type_dispatcher">Static
110 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.type_dispatch.static_type_dispatcher_hpp" title="Header <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>">boost/log/utility/type_dispatch/static_type_dispatcher.hpp</a></code><span class="special">></span>
113 Static type dispatchers are used when the set of types that needs to be
114 supported for extraction is known at compile time. The <code class="computeroutput"><a class="link" href="../../boost/log/static_type_dispatcher.html" title="Class template static_type_dispatcher">static_type_dispatcher</a></code>
115 class template is parametrized with an MPL type sequence of types that
116 need to be supported. The dispatcher inherits from the <code class="computeroutput"><a class="link" href="../../boost/log/type_dispatcher.html" title="Class type_dispatcher">type_dispatcher</a></code>
117 interface which provides the <code class="computeroutput"><span class="identifier">get_callback</span></code>
118 method for acquiring the function object to invoke on the stored value.
119 All you need to do is provide a visitor function object to the dispatcher
120 at construction point and invoke the callback when dispatching the stored
125 <pre class="programlisting"><span class="comment">// Base interface for the custom opaque value</span>
126 <span class="keyword">struct</span> <span class="identifier">my_value_base</span>
127 <span class="special">{</span>
128 <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">my_value_base</span><span class="special">()</span> <span class="special">{}</span>
129 <span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">type_dispatcher</span><span class="special">&</span> <span class="identifier">dispatcher</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
130 <span class="special">};</span>
132 <span class="comment">// A simple attribute value</span>
133 <span class="keyword">template</span><span class="special"><</span> <span class="keyword">typename</span> <span class="identifier">T</span> <span class="special">></span>
134 <span class="keyword">struct</span> <span class="identifier">my_value</span> <span class="special">:</span>
135 <span class="keyword">public</span> <span class="identifier">my_value_base</span>
136 <span class="special">{</span>
137 <span class="identifier">T</span> <span class="identifier">m_value</span><span class="special">;</span>
139 <span class="keyword">explicit</span> <span class="identifier">my_value</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">m_value</span><span class="special">(</span><span class="identifier">value</span><span class="special">)</span> <span class="special">{}</span>
141 <span class="comment">// The function passes the contained type into the dispatcher</span>
142 <span class="keyword">bool</span> <span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">type_dispatcher</span><span class="special">&</span> <span class="identifier">dispatcher</span><span class="special">)</span> <span class="keyword">const</span>
143 <span class="special">{</span>
144 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">type_dispatcher</span><span class="special">::</span><span class="identifier">callback</span><span class="special"><</span> <span class="identifier">T</span> <span class="special">></span> <span class="identifier">cb</span> <span class="special">=</span> <span class="identifier">dispatcher</span><span class="special">.</span><span class="identifier">get_callback</span><span class="special"><</span> <span class="identifier">T</span> <span class="special">>();</span>
145 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">cb</span><span class="special">)</span>
146 <span class="special">{</span>
147 <span class="identifier">cb</span><span class="special">(</span><span class="identifier">m_value</span><span class="special">);</span>
148 <span class="keyword">return</span> <span class="keyword">true</span><span class="special">;</span>
149 <span class="special">}</span>
150 <span class="keyword">else</span>
151 <span class="keyword">return</span> <span class="keyword">false</span><span class="special">;</span>
152 <span class="special">}</span>
153 <span class="special">};</span>
155 <span class="comment">// Value visitor for the supported types</span>
156 <span class="keyword">struct</span> <span class="identifier">print_visitor</span>
157 <span class="special">{</span>
158 <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">result_type</span><span class="special">;</span>
160 <span class="comment">// Implement visitation logic for all supported types</span>
161 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="keyword">int</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span> <span class="keyword">const</span>
162 <span class="special">{</span>
163 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received int value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
164 <span class="special">}</span>
165 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="keyword">double</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span> <span class="keyword">const</span>
166 <span class="special">{</span>
167 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received double value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
168 <span class="special">}</span>
169 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span> <span class="keyword">const</span>
170 <span class="special">{</span>
171 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received string value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
172 <span class="special">}</span>
173 <span class="special">};</span>
175 <span class="comment">// Prints the supplied value</span>
176 <span class="keyword">bool</span> <span class="identifier">print</span><span class="special">(</span><span class="identifier">my_value_base</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">val</span><span class="special">)</span>
177 <span class="special">{</span>
178 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">></span> <span class="identifier">types</span><span class="special">;</span>
180 <span class="identifier">print_visitor</span> <span class="identifier">visitor</span><span class="special">;</span>
181 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">static_type_dispatcher</span><span class="special"><</span> <span class="identifier">types</span> <span class="special">></span> <span class="identifier">disp</span><span class="special">(</span><span class="identifier">visitor</span><span class="special">);</span>
183 <span class="keyword">return</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">disp</span><span class="special">);</span>
184 <span class="special">}</span>
189 <a href="../../../../../../libs/log/example/doc/util_static_type_disp.cpp" target="_top">See
190 the complete code</a>.
193 <a name="log.detailed.utilities.type_dispatch.h1"></a>
194 <span class="phrase"><a name="log.detailed.utilities.type_dispatch.dynamic_type_dispatcher"></a></span><a class="link" href="utilities.html#log.detailed.utilities.type_dispatch.dynamic_type_dispatcher">Dynamic
197 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.type_dispatch.dynamic_type_dispatcher_hpp" title="Header <boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp>">boost/log/utility/type_dispatch/dynamic_type_dispatcher.hpp</a></code><span class="special">></span>
200 If the set of types that have to be supported is not available at compile
201 time, the <code class="computeroutput"><a class="link" href="../../boost/log/dynamic_type_dispatcher.html" title="Class dynamic_type_dispatcher">dynamic_type_dispatcher</a></code>
202 class is there to help. One can use its <code class="computeroutput"><span class="identifier">register_type</span></code>
203 method to add support for a particular type. The user has to pass a function
204 object along with the type, this functor will be called when a visitor
205 for the specified type is invoked. Considering the <code class="computeroutput"><span class="identifier">my_value</span></code>
206 from the code sample for static type dispatcher is intact, the code can
207 be rewritten as follows:
211 <pre class="programlisting"><span class="comment">// Visitor functions for the supported types</span>
212 <span class="keyword">void</span> <span class="identifier">on_int</span><span class="special">(</span><span class="keyword">int</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span>
213 <span class="special">{</span>
214 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received int value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
215 <span class="special">}</span>
217 <span class="keyword">void</span> <span class="identifier">on_double</span><span class="special">(</span><span class="keyword">double</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span>
218 <span class="special">{</span>
219 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received double value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
220 <span class="special">}</span>
222 <span class="keyword">void</span> <span class="identifier">on_string</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">value</span><span class="special">)</span>
223 <span class="special">{</span>
224 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Received string value = "</span> <span class="special"><<</span> <span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
225 <span class="special">}</span>
227 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">dynamic_type_dispatcher</span> <span class="identifier">disp</span><span class="special">;</span>
229 <span class="comment">// The function initializes the dispatcher object</span>
230 <span class="keyword">void</span> <span class="identifier">init_disp</span><span class="special">()</span>
231 <span class="special">{</span>
232 <span class="comment">// Register type visitors</span>
233 <span class="identifier">disp</span><span class="special">.</span><span class="identifier">register_type</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>(&</span><span class="identifier">on_int</span><span class="special">);</span>
234 <span class="identifier">disp</span><span class="special">.</span><span class="identifier">register_type</span><span class="special"><</span> <span class="keyword">double</span> <span class="special">>(&</span><span class="identifier">on_double</span><span class="special">);</span>
235 <span class="identifier">disp</span><span class="special">.</span><span class="identifier">register_type</span><span class="special"><</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">>(&</span><span class="identifier">on_string</span><span class="special">);</span>
236 <span class="special">}</span>
238 <span class="comment">// Prints the supplied value</span>
239 <span class="keyword">bool</span> <span class="identifier">print</span><span class="special">(</span><span class="identifier">my_value_base</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">val</span><span class="special">)</span>
240 <span class="special">{</span>
241 <span class="keyword">return</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">dispatch</span><span class="special">(</span><span class="identifier">disp</span><span class="special">);</span>
242 <span class="special">}</span>
247 <a href="../../../../../../libs/log/example/doc/util_dynamic_type_disp.cpp" target="_top">See
248 the complete code</a>.
251 Of course, complex function objects, like those provided by <a href="http://www.boost.org/doc/libs/release/libs/bind/bind.html" target="_top">Boost.Bind</a>,
255 <div class="section">
256 <div class="titlepage"><div><div><h4 class="title">
257 <a name="log.detailed.utilities.predef_types"></a><a class="link" href="utilities.html#log.detailed.utilities.predef_types" title="Predefined type sequences">Predefined type
259 </h4></div></div></div>
260 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.type_dispatch.standard_types_hpp" title="Header <boost/log/utility/type_dispatch/standard_types.hpp>">boost/log/utility/type_dispatch/standard_types.hpp</a></code><span class="special">></span>
261 <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.type_dispatch.date_time_types_hpp" title="Header <boost/log/utility/type_dispatch/date_time_types.hpp>">boost/log/utility/type_dispatch/date_time_types.hpp</a></code><span class="special">></span>
264 One may notice that when using type dispatchers and defining filters and
265 formatters it may be convenient to have some predefined type sequences
266 to designate frequently used sets of types. The library provides several
270 <a name="log.detailed.utilities.predef_types.standard_types__standard_types_hpp_"></a><p class="title"><b>Table 1.6. Standard types (standard_types.hpp)</b></p>
271 <div class="table-contents"><table class="table" summary="Standard types (standard_types.hpp)">
292 <code class="computeroutput"><span class="identifier">integral_types</span></code>
297 All integral types, including <code class="computeroutput"><span class="keyword">bool</span></code>,
298 character and 64 bit integral types, if available
305 <code class="computeroutput"><span class="identifier">floating_point_types</span></code>
317 <code class="computeroutput"><span class="identifier">numeric_types</span></code>
322 Includes <code class="computeroutput"><span class="identifier">integral_types</span></code>
323 and <code class="computeroutput"><span class="identifier">floating_point_types</span></code>
330 <code class="computeroutput"><span class="identifier">string_types</span></code>
335 Narrow and wide string types. Currently only includes STL string
336 types and <a class="link" href="utilities.html#log.detailed.utilities.string_literal" title="String literals">string
344 <br class="table-break"><p>
345 There are also a number of time-related type sequences available:
348 <a name="log.detailed.utilities.predef_types.time_related_types__date_time_types_hpp_"></a><p class="title"><b>Table 1.7. Time-related types (date_time_types.hpp)</b></p>
349 <div class="table-contents"><table class="table" summary="Time-related types (date_time_types.hpp)">
370 <code class="computeroutput"><span class="identifier">native_date_time_types</span></code>
375 All types defined in C/C++ standard that have both date and time
383 <code class="computeroutput"><span class="identifier">boost_date_time_types</span></code>
388 All types defined in <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>
389 that have both date and time portions
396 <code class="computeroutput"><span class="identifier">date_time_types</span></code>
401 Includes <code class="computeroutput"><span class="identifier">native_date_time_types</span></code>
402 and <code class="computeroutput"><span class="identifier">boost_date_time_types</span></code>
409 <code class="computeroutput"><span class="identifier">native_date_types</span></code>
414 All types defined in C/C++ standard that have date portion. Currently
415 equivalent to <code class="computeroutput"><span class="identifier">native_date_time_types</span></code>.
422 <code class="computeroutput"><span class="identifier">boost_date_types</span></code>
427 All types defined in <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>
428 that have date portion
435 <code class="computeroutput"><span class="identifier">date_types</span></code>
440 Includes <code class="computeroutput"><span class="identifier">native_date_types</span></code>
441 and <code class="computeroutput"><span class="identifier">boost_date_types</span></code>
448 <code class="computeroutput"><span class="identifier">native_time_types</span></code>
453 All types defined in C/C++ standard that have time portion. Currently
454 equivalent to <code class="computeroutput"><span class="identifier">native_date_time_types</span></code>.
461 <code class="computeroutput"><span class="identifier">boost_time_types</span></code>
466 All types defined in <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>
467 that have time portion. Currently equivalent to <code class="computeroutput"><span class="identifier">boost_date_time_types</span></code>.
474 <code class="computeroutput"><span class="identifier">time_types</span></code>
479 Includes <code class="computeroutput"><span class="identifier">native_time_types</span></code>
480 and <code class="computeroutput"><span class="identifier">boost_time_types</span></code>
487 <code class="computeroutput"><span class="identifier">native_time_duration_types</span></code>
492 All types defined in C/C++ standard that are used to represent
493 time duration. Currently only includes <code class="computeroutput"><span class="keyword">double</span></code>,
494 as the result type of the <code class="computeroutput"><span class="identifier">difftime</span></code>
502 <code class="computeroutput"><span class="identifier">boost_time_duration_types</span></code>
507 All time duration types defined in <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>
514 <code class="computeroutput"><span class="identifier">time_duration_types</span></code>
519 Includes <code class="computeroutput"><span class="identifier">native_time_duration_types</span></code>
520 and <code class="computeroutput"><span class="identifier">boost_time_duration_types</span></code>
527 <code class="computeroutput"><span class="identifier">boost_time_period_types</span></code>
532 All time period types defined in <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>
539 <code class="computeroutput"><span class="identifier">time_period_types</span></code>
544 Currently equivalent to <code class="computeroutput"><span class="identifier">boost_time_period_types</span></code>
551 <br class="table-break">
553 <div class="section">
554 <div class="titlepage"><div><div><h4 class="title">
555 <a name="log.detailed.utilities.value_ref"></a><a class="link" href="utilities.html#log.detailed.utilities.value_ref" title="Value reference wrapper">Value reference wrapper</a>
556 </h4></div></div></div>
557 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.value_ref_hpp" title="Header <boost/log/utility/value_ref.hpp>">boost/log/utility/value_ref.hpp</a></code><span class="special">></span>
560 The <code class="computeroutput"><a class="link" href="../../boost/log/value_ref.html" title="Class template value_ref">value_ref</a></code> class
561 template is an optional reference wrapper which is used by the library
562 to refer to the stored attribute values. To a certain degree it shares
563 features of <a href="http://www.boost.org/doc/libs/release/libs/optional/index.html" target="_top">Boost.Optional</a>
564 and <a href="http://www.boost.org/doc/libs/release/doc/html/variant.html" target="_top">Boost.Variant</a>
568 The template has two type parameters. The first is the referred type. It
569 can also be specified as a <a href="http://www.boost.org/doc/libs/release/libs/mpl/doc/index.html" target="_top">Boost.MPL</a>
570 type sequence, in which case the <code class="computeroutput"><a class="link" href="../../boost/log/value_ref.html" title="Class template value_ref">value_ref</a></code>
571 wrapper may refer to either type in the sequence. In this case, the <code class="computeroutput"><span class="identifier">which</span></code> method will return the index of
572 the referred type within the sequence. The second template parameter is
573 an optional tag type which can be used to customize formatting behavior.
574 This tag is forwarded to the <a class="link" href="utilities.html#log.detailed.utilities.manipulators.to_log" title="Customized logging manipulator"><code class="computeroutput"><span class="identifier">to_log</span></code></a> manipulator when the wrapper
575 is put to a <code class="computeroutput"><a class="link" href="../../boost/log/basic_formatting_ostream.html" title="Class template basic_formatting_ostream">basic_formatting_ostream</a></code>
576 stream, which is used by the library for record formatting. For an example
577 see how attribute value extraction is implemented:
581 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">print_value_multiple_types</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">attribute_value</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">attr</span><span class="special">)</span>
582 <span class="special">{</span>
583 <span class="comment">// Define the set of expected types of the stored value</span>
584 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">></span> <span class="identifier">types</span><span class="special">;</span>
586 <span class="comment">// Extract a reference to the stored value</span>
587 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">value_ref</span><span class="special"><</span> <span class="identifier">types</span> <span class="special">></span> <span class="identifier">val</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">extract</span><span class="special"><</span> <span class="identifier">types</span> <span class="special">>(</span><span class="identifier">attr</span><span class="special">);</span>
589 <span class="comment">// Check the result</span>
590 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">val</span><span class="special">)</span>
591 <span class="special">{</span>
592 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Extraction succeeded"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
593 <span class="keyword">switch</span> <span class="special">(</span><span class="identifier">val</span><span class="special">.</span><span class="identifier">which</span><span class="special">())</span>
594 <span class="special">{</span>
595 <span class="keyword">case</span> <span class="number">0</span><span class="special">:</span>
596 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"int: "</span> <span class="special"><<</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">get</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
597 <span class="keyword">break</span><span class="special">;</span>
599 <span class="keyword">case</span> <span class="number">1</span><span class="special">:</span>
600 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"string: "</span> <span class="special"><<</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">get</span><span class="special"><</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">>()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
601 <span class="keyword">break</span><span class="special">;</span>
602 <span class="special">}</span>
603 <span class="special">}</span>
604 <span class="keyword">else</span>
605 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Extraction failed"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
606 <span class="special">}</span>
611 <a href="../../../../../../libs/log/example/doc/attr_value_extraction.cpp" target="_top">See
612 the complete code</a>.
615 The <code class="computeroutput"><a class="link" href="../../boost/log/value_ref.html" title="Class template value_ref">value_ref</a></code> wrapper
616 also supports applying a visitor function object to the referred object.
617 This can be done by calling one of the following methods:
619 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
620 <li class="listitem">
621 <code class="computeroutput"><span class="identifier">apply_visitor</span></code>. This
622 method should only be used on a valid (non-empty) reference. The method
623 returns the visitor result.
625 <li class="listitem">
626 <code class="computeroutput"><span class="identifier">apply_visitor_optional</span></code>.
627 The method checks if the reference is valid and applies the visitor
628 to the referred value if it is. The method returns the visitor result
629 wrapped into <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span></code>
630 which will be filled only if the reference is valid.
632 <li class="listitem">
633 <code class="computeroutput"><span class="identifier">apply_visitor_or_default</span></code>.
634 If the reference is valid, the method applies the visitor on the referred
635 value and returns its result. Otherwise the method returns a default
636 value passed as the second argument.
639 <div class="note"><table border="0" summary="Note">
641 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
642 <th align="left">Note</th>
644 <tr><td align="left" valign="top"><p>
645 Regardless of the method used, the visitor function object <span class="underline">must</span> define the <code class="computeroutput"><span class="identifier">result_type</span></code>
646 typedef. Polymorphic visitors are not supported as this would complicate
647 the <code class="computeroutput"><a class="link" href="../../boost/log/value_ref.html" title="Class template value_ref">value_ref</a></code> interface
648 too much. This requirement also precludes free functions and C++11 lambda
649 functions from being used as visitors. Please, use <a href="http://www.boost.org/doc/libs/release/libs/bind/bind.html" target="_top">Boost.Bind</a>
650 or similar wrappers in such cases.
654 Here is an example of applying a visitor:
658 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">hash_visitor</span>
659 <span class="special">{</span>
660 <span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">result_type</span><span class="special">;</span>
662 <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="keyword">int</span> <span class="identifier">val</span><span class="special">)</span> <span class="keyword">const</span>
663 <span class="special">{</span>
664 <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">h</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">;</span>
665 <span class="identifier">h</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">h</span> <span class="special"><<</span> <span class="number">15</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">h</span><span class="special">;</span>
666 <span class="identifier">h</span> <span class="special">^=</span> <span class="special">(</span><span class="identifier">h</span> <span class="special">>></span> <span class="number">6</span><span class="special">)</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">h</span> <span class="special"><<</span> <span class="number">7</span><span class="special">);</span>
667 <span class="keyword">return</span> <span class="identifier">h</span><span class="special">;</span>
668 <span class="special">}</span>
670 <span class="identifier">result_type</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">val</span><span class="special">)</span> <span class="keyword">const</span>
671 <span class="special">{</span>
672 <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">h</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
673 <span class="keyword">for</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">::</span><span class="identifier">const_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span>
674 <span class="identifier">h</span> <span class="special">+=</span> <span class="special">*</span><span class="identifier">it</span><span class="special">;</span>
676 <span class="identifier">h</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">h</span> <span class="special"><<</span> <span class="number">15</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">h</span><span class="special">;</span>
677 <span class="identifier">h</span> <span class="special">^=</span> <span class="special">(</span><span class="identifier">h</span> <span class="special">>></span> <span class="number">6</span><span class="special">)</span> <span class="special">+</span> <span class="special">(</span><span class="identifier">h</span> <span class="special"><<</span> <span class="number">7</span><span class="special">);</span>
678 <span class="keyword">return</span> <span class="identifier">h</span><span class="special">;</span>
679 <span class="special">}</span>
680 <span class="special">};</span>
682 <span class="keyword">void</span> <span class="identifier">hash_value</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">attribute_value</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">attr</span><span class="special">)</span>
683 <span class="special">{</span>
684 <span class="comment">// Define the set of expected types of the stored value</span>
685 <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">mpl</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="special">></span> <span class="identifier">types</span><span class="special">;</span>
687 <span class="comment">// Extract the stored value</span>
688 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">value_ref</span><span class="special"><</span> <span class="identifier">types</span> <span class="special">></span> <span class="identifier">val</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">extract</span><span class="special"><</span> <span class="identifier">types</span> <span class="special">>(</span><span class="identifier">attr</span><span class="special">);</span>
690 <span class="comment">// Check the result</span>
691 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">val</span><span class="special">)</span>
692 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Extraction succeeded, hash value: "</span> <span class="special"><<</span> <span class="identifier">val</span><span class="special">.</span><span class="identifier">apply_visitor</span><span class="special">(</span><span class="identifier">hash_visitor</span><span class="special">())</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
693 <span class="keyword">else</span>
694 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Extraction failed"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
695 <span class="special">}</span>
700 <div class="section">
701 <div class="titlepage"><div><div><h4 class="title">
702 <a name="log.detailed.utilities.record_ordering"></a><a class="link" href="utilities.html#log.detailed.utilities.record_ordering" title="Log record ordering">Log record
704 </h4></div></div></div>
705 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.record_ordering_hpp" title="Header <boost/log/utility/record_ordering.hpp>">boost/log/utility/record_ordering.hpp</a></code><span class="special">></span>
708 There are cases when log records need to be ordered. One possible use case
709 is storing records in a container or a priority queue. The library provides
710 two types of record ordering predicates out of the box:
713 <a name="log.detailed.utilities.record_ordering.h0"></a>
714 <span class="phrase"><a name="log.detailed.utilities.record_ordering.abstract_record_ordering"></a></span><a class="link" href="utilities.html#log.detailed.utilities.record_ordering.abstract_record_ordering">Abstract
718 The <code class="computeroutput"><a class="link" href="../../boost/log/abstract_ordering.html" title="Class template abstract_ordering">abstract_ordering</a></code>
719 class allows application of a quick opaque ordering. The result of this
720 ordering is not stable between different runs of the application and in
721 general cannot be predicted before the predicate is applied, however it
722 provides the best performance. The <code class="computeroutput"><a class="link" href="../../boost/log/abstract_ordering.html" title="Class template abstract_ordering">abstract_ordering</a></code>
723 class is a template that is specialized with an optional predicate function
724 that will be able to compare <code class="computeroutput"><span class="keyword">const</span>
725 <span class="keyword">void</span><span class="special">*</span></code>
726 pointers. By default an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span></code>
729 <pre class="programlisting"><span class="comment">// A set of unique records</span>
730 <span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span><span class="special"><</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">record_view</span><span class="special">,</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">abstract_ordering</span><span class="special"><</span> <span class="special">></span> <span class="special">></span> <span class="identifier">m_Records</span><span class="special">;</span>
733 This kind of ordering can be useful if the particular order of log records
734 is not important but nevertheless some order is required.
737 <a name="log.detailed.utilities.record_ordering.h1"></a>
738 <span class="phrase"><a name="log.detailed.utilities.record_ordering.attribute_value_based_ordering"></a></span><a class="link" href="utilities.html#log.detailed.utilities.record_ordering.attribute_value_based_ordering">Attribute
739 value based ordering</a>
742 This kind of ordering is implemented with the <code class="computeroutput"><a class="link" href="../../boost/log/attribute_value_ordering.html" title="Class template attribute_value_ordering">attribute_value_ordering</a></code>
743 class and is based on the attribute values attached to the record. The
744 predicate will seek for an attribute value with the specified name in both
745 records being ordered and attempt to compare the attribute values.
747 <pre class="programlisting"><span class="comment">// Ordering type definition</span>
748 <span class="keyword">typedef</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">attribute_value_ordering</span><span class="special"><</span>
749 <span class="keyword">int</span> <span class="comment">// attribute value type</span>
750 <span class="special">></span> <span class="identifier">ordering</span><span class="special">;</span>
752 <span class="comment">// Records organized into a queue based on the "Severity" attribute value</span>
753 <span class="identifier">std</span><span class="special">::</span><span class="identifier">priority_queue</span><span class="special"><</span>
754 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">record_view</span><span class="special">,</span>
755 <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">record_view</span> <span class="special">>,</span>
756 <span class="identifier">ordering</span>
757 <span class="special">></span> <span class="identifier">m_Records</span><span class="special">(</span><span class="identifier">ordering</span><span class="special">(</span><span class="string">"Severity"</span><span class="special">));</span>
760 Like the <code class="computeroutput"><a class="link" href="../../boost/log/abstract_ordering.html" title="Class template abstract_ordering">abstract_ordering</a></code>,
761 <code class="computeroutput"><a class="link" href="../../boost/log/attribute_value_ordering.html" title="Class template attribute_value_ordering">attribute_value_ordering</a></code>
762 also accepts the second optional template parameter, which should be the
763 predicate to compare attribute values (<code class="computeroutput"><span class="keyword">int</span></code>s
764 in the example above). By default, an <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">less</span></code>
768 You can also use the <code class="computeroutput"><a class="link" href="../../boost/log/make_att_idm45711343139040.html" title="Function template make_attr_ordering">make_attr_ordering</a></code> generator
769 function to automatically generate the <code class="computeroutput"><a class="link" href="../../boost/log/attribute_value_ordering.html" title="Class template attribute_value_ordering">attribute_value_ordering</a></code>
770 instance based on the attribute value name and the ordering function. This
771 might be useful if the ordering function has a non-trivial type, like the
772 ones <a href="http://www.boost.org/doc/libs/release/libs/bind/bind.html" target="_top">Boost.Bind</a>
776 <div class="section">
777 <div class="titlepage"><div><div><h4 class="title">
778 <a name="log.detailed.utilities.exception_handlers"></a><a class="link" href="utilities.html#log.detailed.utilities.exception_handlers" title="Exception handlers">Exception
780 </h4></div></div></div>
781 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.exception_handler_hpp" title="Header <boost/log/utility/exception_handler.hpp>">boost/log/utility/exception_handler.hpp</a></code><span class="special">></span>
784 The library provides exception handling hooks in different places. Tools,
785 defined in this header, provide an easy way of implementing function objects
786 suitable for such hooks.
789 An exception handler is a function object that accepts no arguments. The
790 result of the exception handler is ignored and thus should generally be
791 <code class="computeroutput"><span class="keyword">void</span></code>. Exception handlers are
792 called from within <code class="computeroutput"><span class="keyword">catch</span></code> sections
793 by the library, therefore in order to reacquire the exception object it
794 has to rethrow it. The header defines an <code class="computeroutput"><a class="link" href="../../boost/log/exception_handler.html" title="Class template exception_handler">exception_handler</a></code>
795 template functor that does just that and then forwards the exception object
796 to a unary user-defined functional object. The <code class="computeroutput"><a class="link" href="../../boost/log/make_exc_idm45711349902752.html" title="Function template make_exception_handler">make_exception_handler</a></code>
797 function can be used to simplify the handler construction. All expected
798 exception types should be specified explicitly in the call, in the order
799 they would appear in the <code class="computeroutput"><span class="keyword">catch</span></code>
800 sections (i.e. from most specific ones to the most general ones).
804 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_handler</span>
805 <span class="special">{</span>
806 <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">result_type</span><span class="special">;</span>
808 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> <span class="keyword">const</span>
809 <span class="special">{</span>
810 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"std::runtime_error: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
811 <span class="special">}</span>
812 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">logic_error</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> <span class="keyword">const</span>
813 <span class="special">{</span>
814 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"std::logic_error: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
815 <span class="keyword">throw</span><span class="special">;</span>
816 <span class="special">}</span>
817 <span class="special">};</span>
819 <span class="keyword">void</span> <span class="identifier">init_exception_handler</span><span class="special">()</span>
820 <span class="special">{</span>
821 <span class="comment">// Setup a global exception handler that will call my_handler::operator()</span>
822 <span class="comment">// for the specified exception types</span>
823 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span><span class="special">::</span><span class="identifier">get</span><span class="special">()-></span><span class="identifier">set_exception_handler</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">make_exception_handler</span><span class="special"><</span>
824 <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">,</span>
825 <span class="identifier">std</span><span class="special">::</span><span class="identifier">logic_error</span>
826 <span class="special">>(</span><span class="identifier">my_handler</span><span class="special">()));</span>
827 <span class="special">}</span>
832 As you can see, you can either suppress the exception by returning normally
833 from <code class="computeroutput"><span class="keyword">operator</span><span class="special">()</span></code>
834 in the user-defined handler functor, or rethrow the exception, in which
835 case it will propagate further. If it appears that the exception handler
836 is invoked for an exception type that cannot be caught by any of the specified
837 types, the exception will be propagated without any processing. In order
838 to catch such situations, there exists the <code class="computeroutput"><a class="link" href="../../boost/log/nothrow_exception_handler.html" title="Class template nothrow_exception_handler">nothrow_exception_handler</a></code>
839 class. It invokes the user-defined functor with no arguments if it cannot
840 determine the exception type.
844 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">my_handler_nothrow</span>
845 <span class="special">{</span>
846 <span class="keyword">typedef</span> <span class="keyword">void</span> <span class="identifier">result_type</span><span class="special">;</span>
848 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> <span class="keyword">const</span>
849 <span class="special">{</span>
850 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"std::runtime_error: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
851 <span class="special">}</span>
852 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">logic_error</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span> <span class="keyword">const</span>
853 <span class="special">{</span>
854 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"std::logic_error: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
855 <span class="keyword">throw</span><span class="special">;</span>
856 <span class="special">}</span>
857 <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()</span> <span class="special">()</span> <span class="keyword">const</span>
858 <span class="special">{</span>
859 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"unknown exception"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
860 <span class="special">}</span>
861 <span class="special">};</span>
863 <span class="keyword">void</span> <span class="identifier">init_exception_handler_nothrow</span><span class="special">()</span>
864 <span class="special">{</span>
865 <span class="comment">// Setup a global exception handler that will call my_handler::operator()</span>
866 <span class="comment">// for the specified exception types. Note the std::nothrow argument that</span>
867 <span class="comment">// specifies that all other exceptions should also be passed to the functor.</span>
868 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span><span class="special">::</span><span class="identifier">get</span><span class="special">()-></span><span class="identifier">set_exception_handler</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">make_exception_handler</span><span class="special"><</span>
869 <span class="identifier">std</span><span class="special">::</span><span class="identifier">runtime_error</span><span class="special">,</span>
870 <span class="identifier">std</span><span class="special">::</span><span class="identifier">logic_error</span>
871 <span class="special">>(</span><span class="identifier">my_handler_nothrow</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">nothrow</span><span class="special">));</span>
872 <span class="special">}</span>
877 It is sometimes convenient to completely suppress all exceptions at a certain
878 library level. The <code class="computeroutput"><a class="link" href="../../boost/log/make_exception_suppressor.html" title="Function make_exception_suppressor">make_exception_suppressor</a></code>
879 function creates an exception handler that simply does nothing upon exception
880 being caught. For example, this way we can disable all exceptions from
883 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">init_logging</span><span class="special">()</span>
884 <span class="special">{</span>
885 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special"><</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span> <span class="special">></span> <span class="identifier">core</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">core</span><span class="special">::</span><span class="identifier">get</span><span class="special">();</span>
887 <span class="comment">// Disable all exceptions</span>
888 <span class="identifier">core</span><span class="special">-></span><span class="identifier">set_exception_handler</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">make_exception_suppressor</span><span class="special">());</span>
889 <span class="special">}</span>
892 <div class="section">
893 <div class="titlepage"><div><div><h4 class="title">
894 <a name="log.detailed.utilities.manipulators"></a><a class="link" href="utilities.html#log.detailed.utilities.manipulators" title="Output manipulators">Output manipulators</a>
895 </h4></div></div></div>
896 <div class="toc"><dl class="toc">
897 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.manipulators.to_log">Customized
898 logging manipulator</a></span></dt>
899 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.manipulators.add_value">Attribute
900 value attaching manipulator</a></span></dt>
901 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.manipulators.dump">Binary
902 dump manipulator</a></span></dt>
903 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.manipulators.auto_newline">Automatic
904 newline insertion</a></span></dt>
907 The library provides a number of stream manipulators that may be useful
910 <div class="section">
911 <div class="titlepage"><div><div><h5 class="title">
912 <a name="log.detailed.utilities.manipulators.to_log"></a><a class="link" href="utilities.html#log.detailed.utilities.manipulators.to_log" title="Customized logging manipulator">Customized
913 logging manipulator</a>
914 </h5></div></div></div>
915 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.manipulators.to_log_hpp" title="Header <boost/log/utility/manipulators/to_log.hpp>">boost/log/utility/manipulators/to_log.hpp</a></code><span class="special">></span>
918 The <code class="computeroutput"><a class="link" href="../../boost/log/to_log_idm45711343255184.html" title="Function template to_log">to_log</a></code>
919 function creates a stream manipulator that simply outputs the adopted
920 value to the stream. By default its behavior is equivalent to simply
921 putting the value to the stream. However, the user is able to overload
922 the <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code>
923 for the adopted value to override formatting behavior when values are
924 formatted for logging purposes. This is typically desired when the regular
925 <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code>
926 is employed for other tasks (such as serialization) and its behavior
927 is neither suitable for logging nor can be easily changed. For example:
931 <pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="keyword">operator</span><span class="special"><<</span>
932 <span class="special">(</span>
933 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="identifier">strm</span><span class="special">,</span>
934 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log_manip</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">manip</span>
935 <span class="special">)</span>
936 <span class="special">{</span>
937 <span class="identifier">strm</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setw</span><span class="special">(</span><span class="number">4</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setfill</span><span class="special">(</span><span class="char">'0'</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span> <span class="special"><<</span> <span class="identifier">manip</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">dec</span><span class="special">;</span>
938 <span class="keyword">return</span> <span class="identifier">strm</span><span class="special">;</span>
939 <span class="special">}</span>
941 <span class="keyword">void</span> <span class="identifier">test_manip</span><span class="special">()</span>
942 <span class="special">{</span>
943 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Regular output: "</span> <span class="special"><<</span> <span class="number">1010</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
944 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Log output: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log</span><span class="special">(</span><span class="number">1010</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
945 <span class="special">}</span>
950 The second streaming statement in the <code class="computeroutput"><span class="identifier">test_manip</span></code>
951 function will invoke our custom stream insertion operator which defines
952 special formatting rules.
955 It is also possible to define different formatting rules for different
956 value contexts as well. The library uses this feature to allow different
957 formatting ruled for different attribute values, even if the stored value
958 type is the same. To do so one has to specify an explicit template argument
959 for <code class="computeroutput"><a class="link" href="../../boost/log/to_log_idm45711343255184.html" title="Function template to_log">to_log</a></code>,
960 a tag type, which will be embedded into the manipulator type and thus
961 will allow to define different insertion operators:
965 <pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">tag_A</span><span class="special">;</span>
966 <span class="keyword">struct</span> <span class="identifier">tag_B</span><span class="special">;</span>
968 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="keyword">operator</span><span class="special"><<</span>
969 <span class="special">(</span>
970 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="identifier">strm</span><span class="special">,</span>
971 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log_manip</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">tag_A</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">manip</span>
972 <span class="special">)</span>
973 <span class="special">{</span>
974 <span class="identifier">strm</span> <span class="special"><<</span> <span class="string">"A["</span> <span class="special"><<</span> <span class="identifier">manip</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="string">"]"</span><span class="special">;</span>
975 <span class="keyword">return</span> <span class="identifier">strm</span><span class="special">;</span>
976 <span class="special">}</span>
978 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="keyword">operator</span><span class="special"><<</span>
979 <span class="special">(</span>
980 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&</span> <span class="identifier">strm</span><span class="special">,</span>
981 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log_manip</span><span class="special"><</span> <span class="keyword">int</span><span class="special">,</span> <span class="identifier">tag_B</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">manip</span>
982 <span class="special">)</span>
983 <span class="special">{</span>
984 <span class="identifier">strm</span> <span class="special"><<</span> <span class="string">"B["</span> <span class="special"><<</span> <span class="identifier">manip</span><span class="special">.</span><span class="identifier">get</span><span class="special">()</span> <span class="special"><<</span> <span class="string">"]"</span><span class="special">;</span>
985 <span class="keyword">return</span> <span class="identifier">strm</span><span class="special">;</span>
986 <span class="special">}</span>
988 <span class="keyword">void</span> <span class="identifier">test_manip_with_tag</span><span class="special">()</span>
989 <span class="special">{</span>
990 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Regular output: "</span> <span class="special"><<</span> <span class="number">1010</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
991 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Log output A: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log</span><span class="special"><</span> <span class="identifier">tag_A</span> <span class="special">>(</span><span class="number">1010</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
992 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Log output B: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">to_log</span><span class="special"><</span> <span class="identifier">tag_B</span> <span class="special">>(</span><span class="number">1010</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
993 <span class="special">}</span>
998 <a href="../../../../../../libs/log/example/doc/util_manip_to_log.cpp" target="_top">See the
1001 <div class="note"><table border="0" summary="Note">
1003 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1004 <th align="left">Note</th>
1006 <tr><td align="left" valign="top"><p>
1007 The library uses <code class="computeroutput"><a class="link" href="../../boost/log/basic_formatting_ostream.html" title="Class template basic_formatting_ostream">basic_formatting_ostream</a></code>
1008 stream type for record formatting, so when customizing attribute value
1009 formatting rules the <code class="computeroutput"><span class="keyword">operator</span><span class="special"><<</span></code> must use <code class="computeroutput"><a class="link" href="../../boost/log/basic_formatting_ostream.html" title="Class template basic_formatting_ostream">basic_formatting_ostream</a></code>
1010 instead of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span></code>.
1014 <div class="section">
1015 <div class="titlepage"><div><div><h5 class="title">
1016 <a name="log.detailed.utilities.manipulators.add_value"></a><a class="link" href="utilities.html#log.detailed.utilities.manipulators.add_value" title="Attribute value attaching manipulator">Attribute
1017 value attaching manipulator</a>
1018 </h5></div></div></div>
1019 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.manipulators.add_value_hpp" title="Header <boost/log/utility/manipulators/add_value.hpp>">boost/log/utility/manipulators/add_value.hpp</a></code><span class="special">></span>
1022 The <code class="computeroutput"><a class="link" href="../../boost/log/add_value.html" title="Function add_value">add_value</a></code>
1023 function creates a manipulator that attaches an attribute value to a
1024 log record. This manipulator can only be used in streaming expressions
1025 with the <code class="computeroutput"><a class="link" href="../../boost/log/basic_record_ostream.html" title="Class template basic_record_ostream">basic_record_ostream</a></code>
1026 stream type (which is the case when log record message is formatted).
1027 Since the message text is only formatted after filtering, attribute values
1028 attached with this manipulator do not affect filtering and can only be
1029 used in formatters and sinks themselves.
1032 In addition to the value itself, the manipulator also requires the attribute
1033 name to be provided. For example:
1035 <pre class="programlisting"><span class="comment">// Creates a log record with attribute value "MyAttr" of type int attached</span>
1036 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_value</span><span class="special">(</span><span class="string">"MyAttr"</span><span class="special">,</span> <span class="number">10</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello world!"</span><span class="special">;</span>
1039 <div class="section">
1040 <div class="titlepage"><div><div><h5 class="title">
1041 <a name="log.detailed.utilities.manipulators.dump"></a><a class="link" href="utilities.html#log.detailed.utilities.manipulators.dump" title="Binary dump manipulator">Binary
1042 dump manipulator</a>
1043 </h5></div></div></div>
1044 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.manipulators.dump_hpp" title="Header <boost/log/utility/manipulators/dump.hpp>">boost/log/utility/manipulators/dump.hpp</a></code><span class="special">></span>
1047 The <code class="computeroutput"><a class="link" href="../../boost/log/dump_idm45711343298304.html" title="Function template dump">dump</a></code>
1048 function creates a manipulator that outputs binary contents of a contiguous
1049 memory region. This can be useful for logging some low level binary data,
1050 such as encoded network packets or entries of a binary file. The use
1051 is quite straightforward:
1053 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">on_receive</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">unsigned</span> <span class="keyword">char</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">packet</span><span class="special">)</span>
1054 <span class="special">{</span>
1055 <span class="comment">// Outputs something like "Packet received: 00 01 02 0a 0b 0c"</span>
1056 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Packet received: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">dump</span><span class="special">(</span><span class="identifier">packet</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">packet</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span>
1057 <span class="special">}</span>
1060 The manipulator also allows to limit the amount of data to be output,
1061 in case if the input data can be too large. Just specify the maximum
1062 number of bytes of input to dump as the last argument:
1064 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">on_receive</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">unsigned</span> <span class="keyword">char</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">packet</span><span class="special">)</span>
1065 <span class="special">{</span>
1066 <span class="comment">// Outputs something like "Packet received: 00 01 02 03 04 05 06 07 and 67 bytes more"</span>
1067 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Packet received: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">dump</span><span class="special">(</span><span class="identifier">packet</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">packet</span><span class="special">.</span><span class="identifier">size</span><span class="special">(),</span> <span class="number">8</span><span class="special">);</span>
1068 <span class="special">}</span>
1071 There is another manipulator called <code class="computeroutput"><a class="link" href="../../boost/log/dump_ele_idm45711343292832.html" title="Function template dump_elements">dump_elements</a></code> for printing
1072 binary representation of non-byte array elements. The special manipulator
1073 for this case is necessary because the units of the size argument of
1074 <code class="computeroutput"><a class="link" href="../../boost/log/dump_idm45711343298304.html" title="Function template dump">dump</a></code>
1075 can be confusing (is it in bytes or in elements?). Therefore <code class="computeroutput"><a class="link" href="../../boost/log/dump_idm45711343298304.html" title="Function template dump">dump</a></code>
1076 will not compile when used for non-byte input data. <code class="computeroutput"><a class="link" href="../../boost/log/dump_ele_idm45711343292832.html" title="Function template dump_elements">dump_elements</a></code> accepts
1077 the same arguments, and its size-related arguments always designate the
1078 number of elements to process.
1080 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">process</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span> <span class="keyword">double</span> <span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">matrix</span><span class="special">)</span>
1081 <span class="special">{</span>
1082 <span class="comment">// Note that dump_elements accepts the number of elements in the matrix, not its size in bytes</span>
1083 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Matrix dump: "</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">dump_elements</span><span class="special">(</span><span class="identifier">matrix</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">matrix</span><span class="special">.</span><span class="identifier">size</span><span class="special">());</span>
1084 <span class="special">}</span>
1086 <div class="tip"><table border="0" summary="Tip">
1088 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td>
1089 <th align="left">Tip</th>
1091 <tr><td align="left" valign="top"><p>
1092 Both these manipulators can also be used with regular output streams,
1093 not necessarily loggers.
1097 <div class="section">
1098 <div class="titlepage"><div><div><h5 class="title">
1099 <a name="log.detailed.utilities.manipulators.auto_newline"></a><a class="link" href="utilities.html#log.detailed.utilities.manipulators.auto_newline" title="Automatic newline insertion">Automatic
1100 newline insertion</a>
1101 </h5></div></div></div>
1102 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.manipulators.auto_newline_hpp" title="Header <boost/log/utility/manipulators/auto_newline.hpp>">boost/log/utility/manipulators/auto_newline.hpp</a></code><span class="special">></span>
1105 Sometimes it can be useful to be able to insert a newline character in
1106 the output stream, but only if it hasn't been inserted as part of the
1107 previous output. For example, if a string may or may not end with a newline,
1108 and we cannot easily tell which one it is each time. The <code class="computeroutput"><span class="identifier">auto_newline</span></code> manipulator can be used
1109 to ensure that all such strings are reliably terminated with a newline
1110 and there are no duplicate newline characters. The manipulator will insert
1111 a newline unless the last character inserted into the stream before it
1112 was a newline. Its use is similar to standard stream manipulators:
1114 <pre class="programlisting"><span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Parameter: "</span> <span class="special"><<</span> <span class="identifier">param</span><span class="special">.</span><span class="identifier">name</span> <span class="special"><<</span> <span class="string">", value: "</span> <span class="special"><<</span> <span class="identifier">param</span><span class="special">.</span><span class="identifier">value</span> <span class="special"><<</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">auto_newline</span><span class="special">;</span>
1116 <div class="note"><table border="0" summary="Note">
1118 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1119 <th align="left">Note</th>
1121 <tr><td align="left" valign="top"><p>
1122 This manipulator inspects previous output to the stream, and therefore
1123 can only be used with Boost.Log streams based on <code class="computeroutput"><a class="link" href="../../boost/log/basic_formatting_ostream.html" title="Class template basic_formatting_ostream">basic_formatting_ostream</a></code>.
1128 <div class="section">
1129 <div class="titlepage"><div><div><h4 class="title">
1130 <a name="log.detailed.utilities.ipc"></a><a class="link" href="utilities.html#log.detailed.utilities.ipc" title="Interprocess communication tools">Interprocess communication
1132 </h4></div></div></div>
1133 <div class="toc"><dl class="toc">
1134 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.ipc.object_name">Resource
1135 name wrapper</a></span></dt>
1136 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue">Reliable
1137 message queue</a></span></dt>
1139 <div class="section">
1140 <div class="titlepage"><div><div><h5 class="title">
1141 <a name="log.detailed.utilities.ipc.object_name"></a><a class="link" href="utilities.html#log.detailed.utilities.ipc.object_name" title="Resource name wrapper">Resource
1143 </h5></div></div></div>
1144 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.ipc.object_name_hpp" title="Header <boost/log/utility/ipc/object_name.hpp>">boost/log/utility/ipc/object_name.hpp</a></code><span class="special">></span>
1147 In modern operating systems process-shared system resources are typically
1148 identified with names. Unfortunately, different systems have different
1149 requirements on the name syntax and allowed character set. Additionally,
1150 some systems offer support for namespaces in order to avoid name clashes.
1151 The <code class="computeroutput"><a class="link" href="../../boost/log/ipc/object_name.html" title="Class object_name">object_name</a></code>
1152 class is intended to hide these differences.
1155 An object name can be constructed from a UTF-8 string identifier and
1156 a scope. A portable identifier can contain the following characters:
1158 <pre class="programlisting">A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1159 a b c d e f g h i j k l m n o p q r s t u v w x y z
1160 0 1 2 3 4 5 6 7 8 9 . _ -
1162 <div class="note"><table border="0" summary="Note">
1164 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1165 <th align="left">Note</th>
1167 <tr><td align="left" valign="top"><p>
1168 The character set corresponds to <a href="http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278" target="_top">POSIX
1169 Portable Filename Character Set</a>.
1173 Use of other characters may result in non-portable system-specific behavior.
1176 The scope is identified with the <code class="computeroutput"><span class="identifier">object_name</span><span class="special">::</span><span class="identifier">scope</span></code>
1179 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1180 <li class="listitem">
1181 <code class="computeroutput"><span class="identifier">global</span></code> - The name
1182 has global scope; any process in the system has the potential to
1183 open the resource identified by the name. On some systems this scope
1184 may not be available or require elevated privileges.
1186 <li class="listitem">
1187 <code class="computeroutput"><span class="identifier">user</span></code> - The name is
1188 limited to processes running under the current user.
1190 <li class="listitem">
1191 <code class="computeroutput"><span class="identifier">session</span></code> - The name
1192 is limited to processes running in the current login session.
1194 <li class="listitem">
1195 <code class="computeroutput"><span class="identifier">process_group</span></code> - The
1196 name is limited to processes running in the current process group.
1197 Currently, on Windows all processes running in the current session
1198 are considered members of the same process group. This may change
1203 The scopes are not overlapping. For instance, if an object is created
1204 in the global scope, the object cannot be opened with the same name but
1205 in user's scope. Some of the scopes may require elevated privileges to
1206 create or open objects in.
1208 <div class="warning"><table border="0" summary="Warning">
1210 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../doc/src/images/warning.png"></td>
1211 <th align="left">Warning</th>
1213 <tr><td align="left" valign="top"><p>
1214 Object name scoping should not be considered a security measure. Objects
1215 may still be accessible by processes outside of their name scopes.
1216 The main purpose of the scopes is to avoid name clashes between different
1217 processes using <code class="computeroutput"><a class="link" href="../../boost/log/ipc/object_name.html" title="Class object_name">object_name</a></code>.
1218 Use access permissions for security control.
1222 <div class="section">
1223 <div class="titlepage"><div><div><h5 class="title">
1224 <a name="log.detailed.utilities.ipc.reliable_message_queue"></a><a class="link" href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue" title="Reliable message queue">Reliable
1226 </h5></div></div></div>
1227 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.ipc.reliable_message_queue_hpp" title="Header <boost/log/utility/ipc/reliable_message_queue.hpp>">boost/log/utility/ipc/reliable_message_queue.hpp</a></code><span class="special">></span>
1230 The <code class="computeroutput"><a class="link" href="../../boost/log/ipc/reliable_message_queue.html" title="Class reliable_message_queue">reliable_message_queue</a></code>
1231 class implements a reliable one-way channel of passing messages from
1232 one or multiple writers to a single reader. The format of the messages
1233 is user-defined and must be consistent across all writers and the reader.
1234 The queue does not enforce any specific format of the messages, other
1235 than they should be supplied as a contiguous array of bytes. The queue
1236 internally uses a process-shared storage identified by an <a class="link" href="utilities.html#log.detailed.utilities.ipc.object_name" title="Resource name wrapper">object
1237 name</a> (the queue name).
1240 The queue storage is organized as a fixed number of blocks of a fixed
1241 size. The block size must be an integer power of 2 and is expressed in
1242 bytes. Each written message, together with some metadata added by the
1243 queue, consumes an integer number of blocks. Each read message received
1244 by the reader releases the blocks allocated for that message. As such
1245 the maximum size of a message is slightly less than block size times
1246 capacity of the queue. For efficiency, it is recommended to choose block
1247 size large enough to accommodate most of the messages to be passed through
1248 the queue. The queue is considered empty when no messages are enqueued
1249 (all blocks are free). The queue is considered full at the point of enqueueing
1250 a message when there is not enough free blocks to accommodate the message.
1253 The queue is reliable in that it will not drop successfully sent messages
1254 that are not received by the reader, other than the case when a non-empty
1255 queue is destroyed by the last user. If a message cannot be enqueued
1256 by the writer because the queue is full, the queue will act depending
1257 on the overflow policy specified at the queue creation:
1259 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1260 <li class="listitem">
1261 <code class="computeroutput"><span class="identifier">block_on_overflow</span></code>
1262 - Block the thread until there is enough space to enqueue the message
1263 or the operation is aborted by calling <code class="computeroutput"><span class="identifier">stop_local</span></code>.
1265 <li class="listitem">
1266 <code class="computeroutput"><span class="identifier">fail_on_overflow</span></code>
1267 - Return an error code from the send operation. The error code is
1268 <code class="computeroutput"><span class="identifier">operation_result</span><span class="special">::</span><span class="identifier">no_space</span></code>.
1270 <li class="listitem">
1271 <code class="computeroutput"><span class="identifier">throw_on_overflow</span></code>
1272 - Throw an exception from the send operation. The exception is <code class="computeroutput"><a class="link" href="../../boost/log/capacity_limit_reached.html" title="Class capacity_limit_reached">capacity_limit_reached</a></code>.
1276 The policy is object local, i.e. different writers and the reader can
1277 have different overflow policies. Here is an example of writing to the
1282 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
1283 <span class="special">{</span>
1284 <span class="keyword">typedef</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">reliable_message_queue</span> <span class="identifier">queue_t</span><span class="special">;</span>
1286 <span class="comment">// Create a message_queue_type object that is associated with the interprocess</span>
1287 <span class="comment">// message queue named "ipc_message_queue".</span>
1288 <span class="identifier">queue_t</span> <span class="identifier">queue</span>
1289 <span class="special">(</span>
1290 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">name</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">object_name</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">object_name</span><span class="special">::</span><span class="identifier">user</span><span class="special">,</span> <span class="string">"ipc_message_queue"</span><span class="special">),</span>
1291 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">open_mode</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">open_mode</span><span class="special">::</span><span class="identifier">open_or_create</span><span class="special">,</span> <a class="co" name="log.detailed.utilities.ipc.reliable_message_queue.c0" href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue.c1"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>
1292 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">capacity</span> <span class="special">=</span> <span class="number">256</span><span class="special">,</span> <a class="co" name="log.detailed.utilities.ipc.reliable_message_queue.c2" href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue.c3"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a>
1293 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">block_size</span> <span class="special">=</span> <span class="number">1024</span><span class="special">,</span> <a class="co" name="log.detailed.utilities.ipc.reliable_message_queue.c4" href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue.c5"><img src="../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a>
1294 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">overflow_policy</span> <span class="special">=</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">fail_on_overflow</span> <a class="co" name="log.detailed.utilities.ipc.reliable_message_queue.c6" href="utilities.html#log.detailed.utilities.ipc.reliable_message_queue.c7"><img src="../../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a>
1295 <span class="special">);</span>
1297 <span class="comment">// Send a message through the queue</span>
1298 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">message</span> <span class="special">=</span> <span class="string">"Hello, Viewer!"</span><span class="special">;</span>
1299 <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">operation_result</span> <span class="identifier">result</span> <span class="special">=</span> <span class="identifier">queue</span><span class="special">.</span><span class="identifier">send</span><span class="special">(</span><span class="identifier">message</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="keyword">static_cast</span><span class="special"><</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">size_type</span> <span class="special">>(</span><span class="identifier">message</span><span class="special">.</span><span class="identifier">size</span><span class="special">()));</span>
1301 <span class="comment">// See if the message was sent</span>
1302 <span class="keyword">switch</span> <span class="special">(</span><span class="identifier">result</span><span class="special">)</span>
1303 <span class="special">{</span>
1304 <span class="keyword">case</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">operation_result</span><span class="special">::</span><span class="identifier">succeeded</span><span class="special">:</span>
1305 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Message sent successfully"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1306 <span class="keyword">break</span><span class="special">;</span>
1308 <span class="keyword">case</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">operation_result</span><span class="special">::</span><span class="identifier">no_space</span><span class="special">:</span>
1309 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Message could not be sent because the queue is full"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1310 <span class="keyword">break</span><span class="special">;</span>
1312 <span class="keyword">case</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">operation_result</span><span class="special">::</span><span class="identifier">aborted</span><span class="special">:</span>
1313 <span class="comment">// This can happen is overflow_policy is block_on_overflow</span>
1314 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Message sending operation has been interrupted"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1315 <span class="keyword">break</span><span class="special">;</span>
1316 <span class="special">}</span>
1318 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1319 <span class="special">}</span>
1323 <div class="calloutlist"><table border="0" summary="Callout list">
1325 <td width="5%" valign="top" align="left"><p><a name="log.detailed.utilities.ipc.reliable_message_queue.c1"></a><a href="#log.detailed.utilities.ipc.reliable_message_queue.c0"><img src="../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
1326 <td valign="top" align="left"><p>
1327 create the queue, if not yet created
1331 <td width="5%" valign="top" align="left"><p><a name="log.detailed.utilities.ipc.reliable_message_queue.c3"></a><a href="#log.detailed.utilities.ipc.reliable_message_queue.c2"><img src="../../../../../../doc/src/images/callouts/2.png" alt="2" border="0"></a> </p></td>
1332 <td valign="top" align="left"><p>
1333 if the queue has to be created, allocate 256 blocks...
1337 <td width="5%" valign="top" align="left"><p><a name="log.detailed.utilities.ipc.reliable_message_queue.c5"></a><a href="#log.detailed.utilities.ipc.reliable_message_queue.c4"><img src="../../../../../../doc/src/images/callouts/3.png" alt="3" border="0"></a> </p></td>
1338 <td valign="top" align="left"><p>
1339 ... of 1 KiB each for messages
1343 <td width="5%" valign="top" align="left"><p><a name="log.detailed.utilities.ipc.reliable_message_queue.c7"></a><a href="#log.detailed.utilities.ipc.reliable_message_queue.c6"><img src="../../../../../../doc/src/images/callouts/4.png" alt="4" border="0"></a> </p></td>
1344 <td valign="top" align="left"><p>
1345 if the queue is full, return error to the writer
1350 <a href="../../../../../../libs/log/example/doc/util_ipc_reliable_mq_writer.cpp" target="_top">See
1351 the complete code</a>.
1354 Typically, the queue would be used for sending log records to a different
1355 process. As such, instead of using the queue for writing directly, one
1356 would use a special sink backend for that. See <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_ipc_message_queue" title="Text IPC message queue backend"><code class="computeroutput"><span class="identifier">text_ipc_message_queue_backend</span></code></a>
1360 Receiving messages from the queue is similar. Here is an example of a
1361 log viewer that receives messages from the queue and displays them on
1366 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
1367 <span class="special">{</span>
1368 <span class="keyword">try</span>
1369 <span class="special">{</span>
1370 <span class="keyword">typedef</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">reliable_message_queue</span> <span class="identifier">queue_t</span><span class="special">;</span>
1372 <span class="comment">// Create a message_queue_type object that is associated with the interprocess</span>
1373 <span class="comment">// message queue named "ipc_message_queue".</span>
1374 <span class="identifier">queue_t</span> <span class="identifier">queue</span>
1375 <span class="special">(</span>
1376 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">name</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">object_name</span><span class="special">(</span><span class="identifier">logging</span><span class="special">::</span><span class="identifier">ipc</span><span class="special">::</span><span class="identifier">object_name</span><span class="special">::</span><span class="identifier">user</span><span class="special">,</span> <span class="string">"ipc_message_queue"</span><span class="special">),</span>
1377 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">open_mode</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">open_mode</span><span class="special">::</span><span class="identifier">open_or_create</span><span class="special">,</span>
1378 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">capacity</span> <span class="special">=</span> <span class="number">256</span><span class="special">,</span>
1379 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">block_size</span> <span class="special">=</span> <span class="number">1024</span><span class="special">,</span>
1380 <span class="identifier">keywords</span><span class="special">::</span><span class="identifier">overflow_policy</span> <span class="special">=</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">block_on_overflow</span>
1381 <span class="special">);</span>
1383 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Viewer process running..."</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1385 <span class="comment">// Keep reading log messages from the associated message queue and print them on the console.</span>
1386 <span class="comment">// queue.receive() will block if the queue is empty.</span>
1387 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">message</span><span class="special">;</span>
1388 <span class="keyword">while</span> <span class="special">(</span><span class="identifier">queue</span><span class="special">.</span><span class="identifier">receive</span><span class="special">(</span><span class="identifier">message</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">queue_t</span><span class="special">::</span><span class="identifier">succeeded</span><span class="special">)</span>
1389 <span class="special">{</span>
1390 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">message</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1392 <span class="comment">// Clear the buffer for the next message</span>
1393 <span class="identifier">message</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
1394 <span class="special">}</span>
1395 <span class="special">}</span>
1396 <span class="keyword">catch</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span><span class="special">&</span> <span class="identifier">e</span><span class="special">)</span>
1397 <span class="special">{</span>
1398 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Failure: "</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">()</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
1399 <span class="special">}</span>
1401 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1402 <span class="special">}</span>
1407 <a href="../../../../../../libs/log/example/doc/sinks_ipc_receiver.cpp" target="_top">See the
1410 <div class="note"><table border="0" summary="Note">
1412 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1413 <th align="left">Note</th>
1415 <tr><td align="left" valign="top"><p>
1416 The queue does not guarantee any particular order of received messages
1417 from different writer threads. Messages sent by a particular writer
1418 thread will be received in the order of sending.
1422 A blocked reader or writer can be unblocked by calling <code class="computeroutput"><span class="identifier">stop_local</span></code>. After this method is called,
1423 all threads blocked on this particular object are released and return
1424 <code class="computeroutput"><span class="identifier">operation_result</span><span class="special">::</span><span class="identifier">aborted</span></code>. The other instances of the
1425 queue (in the current or other processes) are unaffected. In order to
1426 restore the normal functioning of the queue instance after the <code class="computeroutput"><span class="identifier">stop_local</span></code> call the user has to invoke
1427 <code class="computeroutput"><span class="identifier">reset_local</span></code>.
1431 <div class="section">
1432 <div class="titlepage"><div><div><h4 class="title">
1433 <a name="log.detailed.utilities.setup"></a><a class="link" href="utilities.html#log.detailed.utilities.setup" title="Simplified library initialization tools">Simplified library initialization
1435 </h4></div></div></div>
1436 <div class="toc"><dl class="toc">
1437 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.setup.convenience">Convenience
1438 functions</a></span></dt>
1439 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.setup.filter_formatter">Filter
1440 and formatter parsers</a></span></dt>
1441 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.setup.settings">Library initialization
1442 from a settings container</a></span></dt>
1443 <dt><span class="section"><a href="utilities.html#log.detailed.utilities.setup.settings_file">Library
1444 initialization from a settings file</a></span></dt>
1447 This part of the library is provided in order to simplify logging initialization
1448 and provide basic tools to develop user-specific initialization mechanisms.
1449 It is known that setup capabilities and preferences may vary widely from
1450 application to application, therefore the library does not attempt to provide
1451 a universal solution for this task. The provided tools are mostly intended
1452 to serve as a quick drop-in support for logging setup and a set of instruments
1453 to implement something more elaborate and more fitting users' needs.
1456 Some of the features described in this section will require the separate
1457 library binary, with name based on "boost_log_setup" substring.
1458 This binary depends on the main library.
1460 <div class="section">
1461 <div class="titlepage"><div><div><h5 class="title">
1462 <a name="log.detailed.utilities.setup.convenience"></a><a class="link" href="utilities.html#log.detailed.utilities.setup.convenience" title="Convenience functions">Convenience
1464 </h5></div></div></div>
1465 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.console_hpp" title="Header <boost/log/utility/setup/console.hpp>">boost/log/utility/setup/console.hpp</a></code><span class="special">></span>
1466 <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.file_hpp" title="Header <boost/log/utility/setup/file.hpp>">boost/log/utility/setup/file.hpp</a></code><span class="special">></span>
1467 <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.common_attributes_hpp" title="Header <boost/log/utility/setup/common_attributes.hpp>">boost/log/utility/setup/common_attributes.hpp</a></code><span class="special">></span>
1470 The library provides a number of functions that simplify some common
1471 initialization procedures, like sink and commonly used attributes registration.
1472 This is not much functionality. However, it saves a couple of minutes
1473 of learning the library for a newcomer.
1476 Logging to the application console is the simplest way to see the logging
1477 library in action. To achieve this, one can initialize the library with
1478 a single function call, like this:
1480 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
1481 <span class="special">{</span>
1482 <span class="comment">// Initialize logging to std::clog</span>
1483 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_console_log</span><span class="special">();</span>
1485 <span class="comment">// Here we go, we can write logs right away</span>
1486 <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger</span> <span class="identifier">lg</span><span class="special">;</span>
1487 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello world!"</span><span class="special">;</span>
1489 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1490 <span class="special">}</span>
1493 Pretty easy, isn't it? There is also the <code class="computeroutput"><span class="identifier">wadd_console_log</span></code>
1494 function for wide-character console. If you want to put logs to some
1495 other standard stream, you can pass the stream to the <code class="computeroutput"><a class="link" href="../../boost/log/add_cons_idm45711343115184.html" title="Function template add_console_log">add_console_log</a></code> function
1496 as an argument. E.g. enabling logging to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span></code>
1497 instead of <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">clog</span></code> would look like this:
1499 <pre class="programlisting"><span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_console_log</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">);</span>
1502 What's important, is that you can further manage the console sink if
1503 you save the <code class="computeroutput"><span class="identifier">shared_ptr</span></code>
1504 to the sink that this function returns. This allows you to set up things
1505 like filter, formatter and auto-flush flag.
1507 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
1508 <span class="special">{</span>
1509 <span class="comment">// Initialize logging to std::clog</span>
1510 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special"><</span>
1511 <span class="identifier">sinks</span><span class="special">::</span><span class="identifier">synchronous_sink</span><span class="special"><</span> <span class="identifier">sinks</span><span class="special">::</span><span class="identifier">text_ostream_backend</span> <span class="special">></span>
1512 <span class="special">></span> <span class="identifier">sink</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_console_log</span><span class="special">();</span>
1514 <span class="identifier">sink</span><span class="special">-></span><span class="identifier">set_filter</span><span class="special">(</span><span class="identifier">expr</span><span class="special">::</span><span class="identifier">attr</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>(</span><span class="string">"Severity"</span><span class="special">)</span> <span class="special">>=</span> <span class="number">3</span><span class="special">);</span>
1515 <span class="identifier">sink</span><span class="special">-></span><span class="identifier">locked_backend</span><span class="special">()-></span><span class="identifier">auto_flush</span><span class="special">(</span><span class="keyword">true</span><span class="special">);</span>
1517 <span class="comment">// Here we go, we can write logs right away</span>
1518 <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger</span> <span class="identifier">lg</span><span class="special">;</span>
1519 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello world!"</span><span class="special">;</span>
1521 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1522 <span class="special">}</span>
1525 Similarly to console, one can use a single function call to enable logging
1526 to a file. All you have to do is to provide the file name:
1528 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
1529 <span class="special">{</span>
1530 <span class="comment">// Initialize logging to the "test.log" file</span>
1531 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_file_log</span><span class="special">(</span><span class="string">"test.log"</span><span class="special">);</span>
1533 <span class="comment">// Here we go, we can write logs right away</span>
1534 <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger</span> <span class="identifier">lg</span><span class="special">;</span>
1535 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello world!"</span><span class="special">;</span>
1537 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1538 <span class="special">}</span>
1541 The <code class="computeroutput"><a class="link" href="../../boost/log/add_cons_idm45711343115184.html" title="Function template add_console_log">add_console_log</a></code>
1542 and <code class="computeroutput"><a class="link" href="../../boost/log/add_file_log.html" title="Function template add_file_log">add_file_log</a></code>
1543 functions do not conflict and may be combined freely, so it is possible
1544 to set up logging to the console and a couple of files, including filtering
1545 and formatting, in about 10 lines of code.
1548 Lastly, there is an <code class="computeroutput"><a class="link" href="../../boost/log/add_common_attributes.html" title="Function add_common_attributes">add_common_attributes</a></code>
1549 function that registers two frequently used attributes: "LineID"
1550 and "TimeStamp". The former counts log record being made and
1551 has attribute value <code class="computeroutput"><span class="keyword">unsigned</span> <span class="keyword">int</span></code>. The latter, as its name implies,
1552 provides the current time for each log record, in the form of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">ptime</span></code> (see <a href="http://www.boost.org/doc/libs/release/doc/html/date_time.html" target="_top">Boost.DateTime</a>).
1553 These two attributes are registered globally, so they will remain available
1554 in all threads and loggers. This makes the final version of our code
1555 sample look something like this:
1557 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
1558 <span class="special">{</span>
1559 <span class="comment">// Initialize sinks</span>
1560 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_console_log</span><span class="special">()-></span><span class="identifier">set_filter</span><span class="special">(</span><span class="identifier">expr</span><span class="special">::</span><span class="identifier">attr</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>(</span><span class="string">"Severity"</span><span class="special">)</span> <span class="special">>=</span> <span class="number">4</span><span class="special">);</span>
1562 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">formatter</span> <span class="identifier">formatter</span> <span class="special">=</span>
1563 <span class="identifier">expr</span><span class="special">::</span><span class="identifier">stream</span>
1564 <span class="special"><<</span> <span class="identifier">expr</span><span class="special">::</span><span class="identifier">attr</span><span class="special"><</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="special">>(</span><span class="string">"LineID"</span><span class="special">)</span> <span class="special"><<</span> <span class="string">": "</span>
1565 <span class="special"><<</span> <span class="identifier">expr</span><span class="special">::</span><span class="identifier">format_date_time</span><span class="special"><</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">posix_time</span><span class="special">::</span><span class="identifier">ptime</span> <span class="special">>(</span><span class="string">"TimeStamp"</span><span class="special">,</span> <span class="string">"%Y-%m-%d %H:%M:%S"</span><span class="special">)</span> <span class="special"><<</span> <span class="string">" *"</span>
1566 <span class="special"><<</span> <span class="identifier">expr</span><span class="special">::</span><span class="identifier">attr</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>(</span><span class="string">"Severity"</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"* "</span>
1567 <span class="special"><<</span> <span class="identifier">expr</span><span class="special">::</span><span class="identifier">message</span><span class="special">;</span>
1569 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_file_log</span><span class="special">(</span><span class="string">"complete.log"</span><span class="special">)-></span><span class="identifier">set_formatter</span><span class="special">(</span><span class="identifier">formatter</span><span class="special">);</span>
1571 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">shared_ptr</span><span class="special"><</span>
1572 <span class="identifier">sinks</span><span class="special">::</span><span class="identifier">synchronous_sink</span><span class="special"><</span> <span class="identifier">sinks</span><span class="special">::</span><span class="identifier">text_ostream_backend</span> <span class="special">></span>
1573 <span class="special">></span> <span class="identifier">sink</span> <span class="special">=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_file_log</span><span class="special">(</span><span class="string">"essential.log"</span><span class="special">);</span>
1574 <span class="identifier">sink</span><span class="special">-></span><span class="identifier">set_formatter</span><span class="special">(</span><span class="identifier">formatter</span><span class="special">);</span>
1575 <span class="identifier">sink</span><span class="special">-></span><span class="identifier">set_filter</span><span class="special">(</span><span class="identifier">expr</span><span class="special">::</span><span class="identifier">attr</span><span class="special"><</span> <span class="keyword">int</span> <span class="special">>(</span><span class="string">"Severity"</span><span class="special">)</span> <span class="special">>=</span> <span class="number">1</span><span class="special">);</span>
1577 <span class="comment">// Register common attributes</span>
1578 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_common_attributes</span><span class="special">();</span>
1580 <span class="comment">// Here we go, we can write logs</span>
1581 <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger</span> <span class="identifier">lg</span><span class="special">;</span>
1582 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">lg</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello world!"</span><span class="special">;</span>
1584 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
1585 <span class="special">}</span>
1588 <div class="section">
1589 <div class="titlepage"><div><div><h5 class="title">
1590 <a name="log.detailed.utilities.setup.filter_formatter"></a><a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">Filter
1591 and formatter parsers</a>
1592 </h5></div></div></div>
1593 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.filter_parser_hpp" title="Header <boost/log/utility/setup/filter_parser.hpp>">boost/log/utility/setup/filter_parser.hpp</a></code><span class="special">></span>
1594 <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.formatter_parser_hpp" title="Header <boost/log/utility/setup/formatter_parser.hpp>">boost/log/utility/setup/formatter_parser.hpp</a></code><span class="special">></span>
1597 Filter and formatter parsers allow constructing filters and formatters
1598 from a descriptive string. The function <code class="computeroutput"><span class="identifier">parse_filter</span></code>
1599 is responsible for recognizing filters and <code class="computeroutput"><span class="identifier">parse_formatter</span></code>
1600 - for recognizing formatters.
1603 In the case of filters the string is formed of a sequence of condition
1604 expressions, interconnected with boolean operations. There are two operations
1605 supported: conjunction (designated as "&" or "and")
1606 and disjunction ("|" or "or"). Each condition itself
1607 may be either a single condition or a sub-filter, taken in round brackets.
1608 Each condition can be negated with the "!" sign or "not"
1609 keyword. The condition, if it's not a sub-filter, usually consists of
1610 an attribute name enclosed in percent characters ("%"), a relation
1611 keyword and an operand. The relation and operand may be omitted, in which
1612 case the condition is assumed to be the requirement of the attribute
1613 presence (with any type).
1615 <pre class="programlisting">filter:
1616 condition { op condition }
1629 %attribute_name% relation operand
1644 Below are some examples of filters:
1647 <a name="log.detailed.utilities.setup.filter_formatter.examples_of_filters"></a><p class="title"><b>Table 1.8. Examples of filters</b></p>
1648 <div class="table-contents"><table class="table" summary="Examples of filters">
1669 <code class="computeroutput"><span class="special">%</span><span class="identifier">Severity</span><span class="special">%</span></code>
1674 The filter returns <code class="computeroutput"><span class="keyword">true</span></code>
1675 if an attribute value with name "Severity" is found
1683 <code class="computeroutput"><span class="special">%</span><span class="identifier">Severity</span><span class="special">%</span> <span class="special">></span>
1684 <span class="number">3</span></code>
1689 The filter returns <code class="computeroutput"><span class="keyword">true</span></code>
1690 if an attribute value with name "Severity" is found
1691 and it is greater than 3. The attribute value must be of one
1692 of the <a class="link" href="utilities.html#log.detailed.utilities.predef_types" title="Predefined type sequences">integral
1700 !(<code class="computeroutput"><span class="special">%</span><span class="identifier">Ratio</span><span class="special">%</span> <span class="special">></span>
1701 <span class="number">0.0</span> <span class="special">&</span>
1702 <span class="special">%</span><span class="identifier">Ratio</span><span class="special">%</span> <span class="special"><=</span>
1703 <span class="number">0.5</span><span class="special">)</span></code>
1708 The filter returns <code class="computeroutput"><span class="keyword">true</span></code>
1709 if an attribute value with name "Ratio" of one of
1710 the <a class="link" href="utilities.html#log.detailed.utilities.predef_types" title="Predefined type sequences">floating
1711 point types</a> is not found or it is not between 0 and
1719 <code class="computeroutput"><span class="special">%</span><span class="identifier">Tag</span><span class="special">%</span> <span class="identifier">contains</span>
1720 <span class="string">"net"</span> <span class="keyword">or</span>
1721 <span class="special">%</span><span class="identifier">Tag</span><span class="special">%</span> <span class="identifier">contains</span>
1722 <span class="string">"io"</span> <span class="keyword">and</span>
1723 <span class="keyword">not</span> <span class="special">%</span><span class="identifier">StatFlow</span><span class="special">%</span></code>
1728 The filter returns <code class="computeroutput"><span class="keyword">true</span></code>
1729 if an attribute value with name "Tag" is found and
1730 contains words "net" or "io" and if an
1731 attribute value "StatFlow" is not found. The "Tag"
1732 attribute value must be of one of the <a class="link" href="utilities.html#log.detailed.utilities.predef_types" title="Predefined type sequences">string
1733 types</a>, the "StatFlow" attribute value type
1741 <br class="table-break"><p>
1742 The formatter string syntax is even simpler and pretty much resembles
1743 <a href="http://www.boost.org/doc/libs/release/libs/format/index.html" target="_top">Boost.Format</a>
1744 format string syntax. The string is interpreted as a template which can
1745 contain attribute names enclosed with percent signs ("%").
1746 The corresponding attribute values will replace these placeholders when
1747 the formatter is applied. The placeholder "%Message%" will
1748 be replaced with the log record text. For instance, the following formatter
1751 <pre class="programlisting">[%TimeStamp%] *%Severity%* %Message%
1754 will make log records look like this:
1756 <pre class="programlisting">[2008-07-05 13:44:23] *0* Hello world
1758 <div class="note"><table border="0" summary="Note">
1760 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1761 <th align="left">Note</th>
1763 <tr><td align="left" valign="top"><p>
1764 Previous releases of the library also supported the "%_%"
1765 placeholder for the message text. This placeholder is deprecated now,
1766 although it still works for backward compatibility. Its support will
1767 be removed in future releases.
1771 It must be noted that by default the library only supports those attribute
1772 value types <a class="link" href="utilities.html#log.detailed.utilities.predef_types" title="Predefined type sequences">which
1773 are known</a> at the library build time. User-defined types will not
1774 work properly in parsed filters and formatters until registered in the
1775 library. It is also possible to override formatting rules of the known
1776 types, including support for additional formatting parameters in the
1777 string template. More on this is available in the <a class="link" href="../extension/settings.html" title="Extending library settings support">Extending
1778 the library</a> section.
1780 <div class="note"><table border="0" summary="Note">
1782 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1783 <th align="left">Note</th>
1785 <tr><td align="left" valign="top"><p>
1786 The parsed formatters and filters are generally less optimal than the
1787 equivalent ones written in code with <a class="link" href="expressions.html" title="Lambda expressions">template
1788 expressions</a>. This is because of two reasons: (*) the programmer
1789 usually knows more about types of the attribute values that may be
1790 involved in formatting or filtering and (*) the compiler has a better
1791 chance to optimize the formatter or filter if it is known in compile
1792 time. Therefore, when performance matters, it is advised to avoid parsed
1793 filters and formatters.
1797 <div class="section">
1798 <div class="titlepage"><div><div><h5 class="title">
1799 <a name="log.detailed.utilities.setup.settings"></a><a class="link" href="utilities.html#log.detailed.utilities.setup.settings" title="Library initialization from a settings container">Library initialization
1800 from a settings container</a>
1801 </h5></div></div></div>
1802 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.settings_hpp" title="Header <boost/log/utility/setup/settings.hpp>">boost/log/utility/setup/settings.hpp</a></code><span class="special">></span>
1803 <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.from_settings_hpp" title="Header <boost/log/utility/setup/from_settings.hpp>">boost/log/utility/setup/from_settings.hpp</a></code><span class="special">></span>
1806 The headers define components for library initialization from a settings
1807 container. The settings container is basically a set of named parameters
1808 divided into sections. The container is implemented with the <code class="computeroutput"><a class="link" href="../../boost/log/basic_settings.html" title="Class template basic_settings">basic_settings</a></code> class template.
1809 There are several constraints on how parameters are stored in the container:
1811 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
1812 <li class="listitem">
1813 Every parameter must reside in a section. There can be no parameters
1814 that do not belong to a section.
1816 <li class="listitem">
1817 Parameters must have names unique within the section they belong
1818 to. Parameters from different sections may have the same name.
1820 <li class="listitem">
1821 Sections can nest. When read from a file or accessed from the code,
1822 section names can express arbitrary hierarchy by separating the parent
1823 and child section names with '.' (e.g. "[Parent.Child.ChildChild]").
1825 <li class="listitem">
1826 Sections must have names unique within the enclosing section (or
1827 global scope, if the section is top level).
1831 So basically, settings container is a layered associative container,
1832 with string keys and values. In some respect it is similar to <a href="http://www.boost.org/doc/libs/release/doc/html/property_tree.html" target="_top">Boost.PropertyTree</a>,
1833 and in fact it supports construction from <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">ptree</span></code>.
1834 The supported parameters are described below.
1836 <div class="tip"><table border="0" summary="Tip">
1838 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td>
1839 <th align="left">Tip</th>
1841 <tr><td align="left" valign="top"><p>
1842 In the tables below, the <code class="computeroutput"><span class="identifier">CharT</span></code>
1843 type denotes the character type that is used with the settings container.
1847 <a name="log.detailed.utilities.setup.settings.section__core___logging_core_settings_"></a><p class="title"><b>Table 1.9. Section "Core". Logging core settings.</b></p>
1848 <div class="table-contents"><table class="table" summary='Section "Core". Logging core settings.'>
1880 Filter string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
1885 Global filter to be installed to the core. If not specified,
1886 the global filter is not set.
1903 If <code class="computeroutput"><span class="keyword">true</span></code>, results
1904 in calling <code class="computeroutput"><span class="identifier">set_logging_enabled</span><span class="special">(</span><span class="keyword">false</span><span class="special">)</span></code> on the core. By default, value
1905 <code class="computeroutput"><span class="keyword">false</span></code> is assumed.
1912 <br class="table-break"><p>
1913 Sink settings are divided into separate subsections within the common
1914 top-level section "Sinks" - one subsection for each sink. The
1915 subsection names denote a user-defined sink name. For example, "MyFile".
1917 <div class="note"><table border="0" summary="Note">
1919 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
1920 <th align="left">Note</th>
1922 <tr><td align="left" valign="top"><p>
1923 Previous versions of the library also supported top-level sections
1924 starting with the "Sink:" prefix to describe sink parameters.
1925 This syntax is deprecated now, although it still works when parsing
1926 a settings file for backward compatibility. The parser will automatically
1927 put these sections under the "Sinks" top-level section in
1928 the resulting settings container. Support for this syntax will be removed
1933 <a name="log.detailed.utilities.setup.settings.sections_under_the__sinks__section__common_sink_settings_"></a><p class="title"><b>Table 1.10. Sections under the "Sinks" section. Common sink settings.</b></p>
1934 <div class="table-contents"><table class="table" summary='Sections under the "Sinks" section. Common sink settings.'>
1966 Sink target, see description
1971 Sink backend type. Mandatory parameter. May have one of these
1972 values: <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_ostream" title="Text stream backend">Console</a>,
1973 <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file" title="Text file backend">TextFile</a>,
1974 <a class="link" href="sink_backends.html#log.detailed.sink_backends.syslog" title="Syslog backend">Syslog</a>.
1975 On Windows the following values are additionally supported:
1976 <a class="link" href="sink_backends.html#log.detailed.sink_backends.event_log" title="Windows event log backends">SimpleEventLog</a>,
1977 <a class="link" href="sink_backends.html#log.detailed.sink_backends.debugger" title="Windows debugger output backend">Debugger</a>.
1978 Also, user-defined sink names may also be supported if registered
1979 by calling <code class="computeroutput"><a class="link" href="../../boost/log/register_idm45711342847040.html" title="Function template register_sink_factory">register_sink_factory</a></code>.
1980 See <a class="link" href="../extension/settings.html#log.extension.settings.sinks" title="Adding support for user-defined sinks">this</a>
1981 section on how to add support for new sinks.
1993 Filter string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
1998 Sink-specific filter. If not specified, the filter is not set.
2015 If <code class="computeroutput"><span class="keyword">true</span></code>, the
2016 <a class="link" href="sink_frontends.html#log.detailed.sink_frontends.async" title="Asynchronous sink frontend">asynchronous
2017 sink frontend</a> will be used. Otherwise the <a class="link" href="sink_frontends.html#log.detailed.sink_frontends.sync" title="Synchronous sink frontend">synchronous
2018 sink frontend</a> will be used. By default, value <code class="computeroutput"><span class="keyword">false</span></code> is assumed. In single-threaded
2019 builds this parameter is not used, as <a class="link" href="sink_frontends.html#log.detailed.sink_frontends.unlocked" title="Unlocked sink frontend">unlocked
2020 sink frontend</a> is always used.
2027 <br class="table-break"><p>
2028 Besides the common settings that all sinks support, some sink backends
2029 also accept a number of specific parameters. These parameters should
2030 be specified in the same section.
2033 <a name="log.detailed.utilities.setup.settings._console__sink_settings"></a><p class="title"><b>Table 1.11. "Console" sink settings</b></p>
2034 <div class="table-contents"><table class="table" summary='"Console" sink settings'>
2066 Format string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
2071 Log record formatter to be used by the sink. If not specified,
2072 the default formatter is used.
2084 "Disabled", "AlwaysInsert" or "InsertIfMissing"
2089 Controls whether the backend should automatically insert a
2090 trailing newline after every log record, see <code class="computeroutput"><a class="link" href="../../sinks.html#boost.log.sinks.auto_newline_mode">auto_newline_mode</a></code>.
2091 If not specified, the default value is "InsertIfMissing".
2108 Enables or disables the auto-flush feature of the backend.
2109 If not specified, the default value <code class="computeroutput"><span class="keyword">false</span></code>
2117 <br class="table-break"><div class="table">
2118 <a name="log.detailed.utilities.setup.settings._textfile__sink_settings"></a><p class="title"><b>Table 1.12. "TextFile" sink settings</b></p>
2119 <div class="table-contents"><table class="table" summary='"TextFile" sink settings'>
2156 The active file name pattern for the sink backend. This parameter
2174 The target file name pattern for the sink backend. If not specified,
2175 active file name is preserved after rotation.
2187 Format string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
2192 Log record formatter to be used by the sink. If not specified,
2193 the default formatter is used.
2205 "Disabled", "AlwaysInsert" or "InsertIfMissing"
2210 Controls whether the backend should automatically insert a
2211 trailing newline after every log record, see <code class="computeroutput"><a class="link" href="../../sinks.html#boost.log.sinks.auto_newline_mode">auto_newline_mode</a></code>.
2212 If not specified, the default value is "InsertIfMissing".
2229 Enables or disables the auto-flush feature of the backend.
2230 If not specified, the default value <code class="computeroutput"><span class="keyword">false</span></code>
2248 Enables or disables <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.appending" title="Appending to the previously written files">appending</a>
2249 to the existing file instead of overwriting it. If not specified,
2250 the default value <code class="computeroutput"><span class="keyword">false</span></code>
2268 File size, in bytes, upon which <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.file_rotation" title="File rotation">file
2269 rotation</a> will be performed. If not specified, no size-based
2270 rotation will be made.
2287 Time interval, in seconds, upon which <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.file_rotation" title="File rotation">file
2288 rotation</a> will be performed. See also the RotationTimePoint
2289 parameter and the note below.
2301 Time point format string, see below
2306 Time point or a predicate that detects at what moment of time
2307 to perform log <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.file_rotation" title="File rotation">file
2308 rotation</a>. See also the RotationInterval parameter and
2326 Enables or disables final file rotation on sink destruction,
2327 which typically happens on program termination. If not specified,
2328 the default value <code class="computeroutput"><span class="keyword">true</span></code>
2341 File system path to a directory
2346 Target directory name, in which the rotated files will be stored.
2347 If this parameter is specified, rotated <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.file_collection" title="Managing rotated files">file
2348 collection</a> is enabled. Otherwise the feature is not
2349 enabled and all corresponding parameters are ignored.
2366 Total size of files in the target directory, in bytes, upon
2367 which the oldest file will be deleted. If not specified, no
2368 size-based file cleanup will be performed.
2385 Minimum free space in the target directory, in bytes, upon
2386 which the oldest file will be deleted. If not specified, no
2387 space-based file cleanup will be performed.
2404 Total number of files in the target directory, upon which the
2405 oldest file will be deleted. If not specified, no count-based
2406 file cleanup will be performed.
2423 Mode of <a class="link" href="sink_backends.html#log.detailed.sink_backends.text_file.file_scanning" title="Scanning for rotated files">scanning</a>
2424 for old files in the target directory, see <code class="computeroutput"><a class="link" href="../../sinks.html#boost.log.sinks.file.scan_method">scan_method</a></code>. If
2425 not specified, no scanning will be performed.
2432 <br class="table-break"><div class="warning"><table border="0" summary="Warning">
2434 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../doc/src/images/warning.png"></td>
2435 <th align="left">Warning</th>
2437 <tr><td align="left" valign="top"><p>
2438 The text file sink uses <a href="http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm" target="_top">Boost.Filesystem</a>
2439 internally, which may cause problems on process termination. See <a class="link" href="../rationale/why_crash_on_term.html" title="Why my application crashes on process termination when file sinks are used?">here</a> for more details.
2443 The time-based rotation can be set up with one of the two parameters:
2444 RotationInterval or RotationTimePoint. Not more than one of these parameters
2445 should be specified for a given sink. If none is specified, no time-based
2446 rotation will be performed.
2449 The RotationTimePoint parameter should have one of the following formats,
2450 according to the <a href="http://www.boost.org/doc/libs/release/doc/html/date_time/date_time_io.html#date_time.format_flags" target="_top">Boost.DateTime</a>
2453 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
2454 <li class="listitem">
2455 "%H:%M:%S". In this case, file rotation will be performed
2456 on a daily basis, at the specified time. For example, "12:00:00".
2458 <li class="listitem">
2459 "%a %H:%M:%S" or "%A %H:%M:%S". File rotation
2460 takes place every week, on the weekday specified in the long or short
2461 form, at the specified time. For example, "Saturday 09:00:00".
2463 <li class="listitem">
2464 "%d %H:%M:%S". File rotation takes place every month, on
2465 the specified day of month, at the specified time. For example, "01
2470 <a name="log.detailed.utilities.setup.settings._syslog__sink_settings"></a><p class="title"><b>Table 1.13. "Syslog" sink settings</b></p>
2471 <div class="table-contents"><table class="table" summary='"Syslog" sink settings'>
2503 Format string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
2508 Log record formatter to be used by the sink. If not specified,
2509 the default formatter is used.
2526 Local address to initiate connection to the syslog server.
2527 If not specified, the default local address will be used.
2544 Remote address of the syslog server. If not specified, the
2545 local address will be used.
2552 <br class="table-break"><div class="table">
2553 <a name="log.detailed.utilities.setup.settings._simpleeventlog__sink_settings"></a><p class="title"><b>Table 1.14. "SimpleEventLog" sink settings</b></p>
2554 <div class="table-contents"><table class="table" summary='"SimpleEventLog" sink settings'>
2586 Format string as described <a class="link" href="utilities.html#log.detailed.utilities.setup.filter_formatter" title="Filter and formatter parsers">here</a>
2591 Log record formatter to be used by the sink. If not specified,
2592 the default formatter is used.
2609 Log name to write events into. If not specified, the default
2610 log name will be used.
2627 Log source to write events from. If not specified, the default
2628 source will be used.
2640 "Never", "OnDemand" or "Forced"
2645 Mode of log source registration in Windows registry, see <code class="computeroutput"><a class="link" href="../../sinks.html#boost.log.sinks.event_log.registration_mode">registration_mode</a></code>.
2646 If not specified, on-demand registration will be performed.
2653 <br class="table-break"><p>
2654 The user is free to fill the settings container from whatever settings
2655 source he needs. The usage example is below:
2659 <pre class="programlisting"><span class="identifier">logging</span><span class="special">::</span><span class="identifier">settings</span> <span class="identifier">setts</span><span class="special">;</span>
2661 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Core"</span><span class="special">][</span><span class="string">"Filter"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"%Severity% >= warning"</span><span class="special">;</span>
2662 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Core"</span><span class="special">][</span><span class="string">"DisableLogging"</span><span class="special">]</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">;</span>
2664 <span class="comment">// Subsections can be referred to with a single path</span>
2665 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.Console"</span><span class="special">][</span><span class="string">"Destination"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"Console"</span><span class="special">;</span>
2666 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.Console"</span><span class="special">][</span><span class="string">"Filter"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"%Severity% >= critical"</span><span class="special">;</span>
2667 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.Console"</span><span class="special">][</span><span class="string">"Format"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"%TimeStamp% [%Severity%] %Message%"</span><span class="special">;</span>
2668 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.Console"</span><span class="special">][</span><span class="string">"AutoFlush"</span><span class="special">]</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
2670 <span class="comment">// ...as well as the individual parameters</span>
2671 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.File.Destination"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"TextFile"</span><span class="special">;</span>
2672 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.File.FileName"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"MyApp_%3N.log"</span><span class="special">;</span>
2673 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.File.AutoFlush"</span><span class="special">]</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">;</span>
2674 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.File.RotationSize"</span><span class="special">]</span> <span class="special">=</span> <span class="number">10</span> <span class="special">*</span> <span class="number">1024</span> <span class="special">*</span> <span class="number">1024</span><span class="special">;</span> <span class="comment">// 10 MiB</span>
2675 <span class="identifier">setts</span><span class="special">[</span><span class="string">"Sinks.File.Format"</span><span class="special">]</span> <span class="special">=</span> <span class="string">"%TimeStamp% [%Severity%] %Message%"</span><span class="special">;</span>
2677 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">init_from_settings</span><span class="special">(</span><span class="identifier">setts</span><span class="special">);</span>
2682 <a href="../../../../../../libs/log/example/doc/util_setup_settings.cpp" target="_top">See
2683 the complete code</a>.
2685 <div class="note"><table border="0" summary="Note">
2687 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
2688 <th align="left">Note</th>
2690 <tr><td align="left" valign="top"><p>
2691 Initialization from settings does not automatically create any attributes
2692 or loggers, the application developer is still responsible for creating
2693 those. It can be said that loggers and attributes define the data that
2694 is exported by the application and the settings only describe how that
2695 data is going to be presented to the application user.
2699 The settings reader also can be extended to support custom sink types.
2700 See the <a class="link" href="../extension/settings.html" title="Extending library settings support">Extending the library</a>
2701 section for more information.
2704 <div class="section">
2705 <div class="titlepage"><div><div><h5 class="title">
2706 <a name="log.detailed.utilities.setup.settings_file"></a><a class="link" href="utilities.html#log.detailed.utilities.setup.settings_file" title="Library initialization from a settings file">Library
2707 initialization from a settings file</a>
2708 </h5></div></div></div>
2709 <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a class="link" href="../../utilities.html#header.boost.log.utility.setup.from_stream_hpp" title="Header <boost/log/utility/setup/from_stream.hpp>">boost/log/utility/setup/from_stream.hpp</a></code><span class="special">></span>
2712 Support for configuration files is a frequently requested feature of
2713 the library. And despite the fact there is no ultimately convenient and
2714 flexible format of the library settings, the library provides preliminary
2715 support for this feature. The functionality is implemented with a simple
2716 function <code class="computeroutput"><a class="link" href="../../boost/log/init_from_stream.html" title="Function template init_from_stream">init_from_stream</a></code>, which
2717 accepts a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span></code> and reads the library settings
2718 from it. The function then passes on the read settings to the <code class="computeroutput"><a class="link" href="../../boost/log/init_from_settings.html" title="Function template init_from_settings">init_from_settings</a></code>
2719 function, described <a class="link" href="utilities.html#log.detailed.utilities.setup.settings" title="Library initialization from a settings container">above</a>.
2720 Therefore the parameter names and their meaning is the same as for the
2721 <code class="computeroutput"><a class="link" href="../../boost/log/init_from_settings.html" title="Function template init_from_settings">init_from_settings</a></code>
2725 The settings format is quite simple and widely used. Below is the description
2726 of syntax and parameters.
2728 <pre class="programlisting"># Comments are allowed. Comment line begins with the '#' character
2729 # and spans until the end of the line.
2731 # Logging core settings section. May be omitted if no parameters specified within it.
2733 DisableLogging=false
2734 Filter="%Severity% > 3"
2736 # Sink settings sections
2739 # Sink destination type
2742 # Sink-specific filter. Optional, by default no filter is applied.
2743 Filter="%Target% contains \"MySink1\""
2745 # Formatter string. Optional, by default only log record message text is written.
2746 Format="<%TimeStamp%> - %Message%"
2748 # The flag shows whether the sink should be asynchronous
2751 # Enables automatic stream flush after each log record.
2755 Here's the usage example:
2757 <pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*[])</span>
2758 <span class="special">{</span>
2759 <span class="comment">// Read logging settings from a file</span>
2760 <span class="identifier">std</span><span class="special">::</span><span class="identifier">ifstream</span> <span class="identifier">file</span><span class="special">(</span><span class="string">"settings.ini"</span><span class="special">);</span>
2761 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">init_from_stream</span><span class="special">(</span><span class="identifier">file</span><span class="special">);</span>
2763 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
2764 <span class="special">}</span>
2767 See a <a href="../../../../../../libs/log/example/settings_file/main.cpp" target="_top">more
2768 complete example</a>.
2773 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
2774 <td align="left"></td>
2775 <td align="right"><div class="copyright-footer">Copyright © 2007-2016 Andrey Semashev<p>
2776 Distributed under the Boost Software License, Version 1.0. (See accompanying
2777 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>).
2782 <div class="spirit-nav">
2783 <a accesskey="p" href="attributes.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../detailed.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../extension.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>