3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>Adding more information to log: Attributes</title>
5 <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
7 <link rel="home" href="../../index.html" title="Chapter 1. Boost.Log v2">
8 <link rel="up" href="../tutorial.html" title="Tutorial">
9 <link rel="prev" href="sources.html" title="Creating loggers and writing logs">
10 <link rel="next" href="formatters.html" title="Log record formatting">
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="sources.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="formatters.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
19 <div class="titlepage"><div><div><h3 class="title">
20 <a name="log.tutorial.attributes"></a><a class="link" href="attributes.html" title="Adding more information to log: Attributes">Adding more information to log:
22 </h3></div></div></div>
24 In previous sections we mentioned attributes and attribute values several
25 times. Here we will discover how attributes can be used to add more data
29 Each log record can have a number of named attribute values attached. Attributes
30 can represent any essential information about the conditions in which the
31 log record occurred, such as position in the code, executable module name,
32 current date and time, or any piece of data relevant to your particular application
33 and execution environment. An attribute may behave as a value generator,
34 in which case it would return a different value for each log record it's
35 involved in. As soon as the attribute generates the value, the latter becomes
36 independent from the creator and can be used by filters, formatters and sinks.
37 But in order to use the attribute value one has to know its name and type,
38 or at least a set of types it may have. There are a number of commonly used
39 attributes implemented in the library, you can find the types of their values
43 Aside from that, as described in the <a class="link" href="../design.html" title="Design overview">Design overview</a>
44 section, there are three possible scopes of attributes: source-specific,
45 thread-specific and global. When a log record is made, attribute values from
46 these three sets are joined into a single set and passed to sinks. This implies
47 that the origin of the attribute makes no difference for sinks. Any attribute
48 can be registered in any scope. When registered, an attribute is given a
49 unique name in order to make it possible to search for it. If it happens
50 that the same named attribute is found in several scopes, the attribute from
51 the most specific scope is taken into consideration in any further processing,
52 including filtering and formatting. Such behavior makes it possible to override
53 global or thread-scoped attributes with the ones registered in your local
54 logger, thus reducing thread interference.
57 Below is the description of the attribute registration process.
60 <a name="log.tutorial.attributes.h0"></a>
61 <span class="phrase"><a name="log.tutorial.attributes.commonly_used_attributes"></a></span><a class="link" href="attributes.html#log.tutorial.attributes.commonly_used_attributes">Commonly
65 There are attributes that are likely to be used in nearly any application.
66 Log record counter and a time stamp are good candidates. They can be added
67 with a single function call:
69 <pre class="programlisting"><span class="identifier">logging</span><span class="special">::</span><span class="identifier">add_common_attributes</span><span class="special">();</span>
72 With this call attributes "LineID", "TimeStamp", "ProcessID"
73 and "ThreadID" are registered globally. The "LineID"
74 attribute is a counter that increments for each record being made, the first
75 record gets identifier 1. The "TimeStamp" attribute always yields
76 the current time (i.e. the time when the log record is created, not the time
77 it was written to a sink). The last two attributes identify the process and
78 the thread in which every log record is emitted.
80 <div class="note"><table border="0" summary="Note">
82 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
83 <th align="left">Note</th>
85 <tr><td align="left" valign="top"><p>
86 In single-threaded builds the "ThreadID" attribute is not registered.
89 <div class="tip"><table border="0" summary="Tip">
91 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td>
92 <th align="left">Tip</th>
94 <tr><td align="left" valign="top"><p>
95 By default, when application starts, no attributes are registered in the
96 library. The application has to register all the necessary attributes in
97 the library before it starts writing logs. This can be done as a part of
98 the library initialization. A curious reader could have wondered how trivial
99 logging works then. The answer is that the default sink doesn't really
100 use any attribute values, except for the severity level, to compose its
101 output. This is done to avoid the need for any initialization for trivial
102 logging. Once you use filters or formatters and non-default sinks you will
103 have to register the attributes you need.
107 The <code class="computeroutput"><a class="link" href="../../boost/log/add_common_attributes.html" title="Function add_common_attributes">add_common_attributes</a></code>
108 function is one of the several convenience helpers described <a class="link" href="../detailed/utilities.html#log.detailed.utilities.setup.convenience" title="Convenience functions">here</a>.
111 Some attributes are registered automatically on loggers construction. For
112 example, <a class="link" href="../detailed/sources.html#log.detailed.sources.severity_level_logger" title="Loggers with severity level support"><code class="computeroutput"><span class="identifier">severity_logger</span></code></a> registers a source-specific
113 attribute "Severity" which can be used to add a level of emphasis
114 for different log records. For example:
118 <pre class="programlisting"><span class="comment">// We define our own severity levels</span>
119 <span class="keyword">enum</span> <span class="identifier">severity_level</span>
120 <span class="special">{</span>
121 <span class="identifier">normal</span><span class="special">,</span>
122 <span class="identifier">notification</span><span class="special">,</span>
123 <span class="identifier">warning</span><span class="special">,</span>
124 <span class="identifier">error</span><span class="special">,</span>
125 <span class="identifier">critical</span>
126 <span class="special">};</span>
128 <span class="keyword">void</span> <span class="identifier">logging_function</span><span class="special">()</span>
129 <span class="special">{</span>
130 <span class="comment">// The logger implicitly adds a source-specific attribute 'Severity'</span>
131 <span class="comment">// of type 'severity_level' on construction</span>
132 <span class="identifier">src</span><span class="special">::</span><span class="identifier">severity_logger</span><span class="special"><</span> <span class="identifier">severity_level</span> <span class="special">></span> <span class="identifier">slg</span><span class="special">;</span>
134 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">normal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"A regular message"</span><span class="special">;</span>
135 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">warning</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Something bad is going on but I can handle it"</span><span class="special">;</span>
136 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">critical</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Everything crumbles, shoot me now!"</span><span class="special">;</span>
137 <span class="special">}</span>
141 <div class="tip"><table border="0" summary="Tip">
143 <td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td>
144 <th align="left">Tip</th>
146 <tr><td align="left" valign="top"><p>
147 You can define your own formatting rules for the severity level by defining
148 <code class="computeroutput"><span class="keyword">operator</span> <span class="special"><<</span></code>
149 for this type. It will be automatically used by the library formatters.
150 See <a class="link" href="../detailed/expressions.html#log.detailed.expressions.attr" title="Generic attribute placeholder">this</a> section for
155 The <code class="computeroutput"><span class="identifier">BOOST_LOG_SEV</span></code> macro acts
156 pretty much like <code class="computeroutput"><span class="identifier">BOOST_LOG</span></code>
157 except that it takes an additional argument for the <code class="computeroutput"><span class="identifier">open_record</span></code>
158 method of the logger. The <code class="computeroutput"><span class="identifier">BOOST_LOG_SEV</span></code>
159 macro can be replaced with this equivalent:
163 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">manual_logging</span><span class="special">()</span>
164 <span class="special">{</span>
165 <span class="identifier">src</span><span class="special">::</span><span class="identifier">severity_logger</span><span class="special"><</span> <span class="identifier">severity_level</span> <span class="special">></span> <span class="identifier">slg</span><span class="special">;</span>
167 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">record</span> <span class="identifier">rec</span> <span class="special">=</span> <span class="identifier">slg</span><span class="special">.</span><span class="identifier">open_record</span><span class="special">(</span><span class="identifier">keywords</span><span class="special">::</span><span class="identifier">severity</span> <span class="special">=</span> <span class="identifier">normal</span><span class="special">);</span>
168 <span class="keyword">if</span> <span class="special">(</span><span class="identifier">rec</span><span class="special">)</span>
169 <span class="special">{</span>
170 <span class="identifier">logging</span><span class="special">::</span><span class="identifier">record_ostream</span> <span class="identifier">strm</span><span class="special">(</span><span class="identifier">rec</span><span class="special">);</span>
171 <span class="identifier">strm</span> <span class="special"><<</span> <span class="string">"A regular message"</span><span class="special">;</span>
172 <span class="identifier">strm</span><span class="special">.</span><span class="identifier">flush</span><span class="special">();</span>
173 <span class="identifier">slg</span><span class="special">.</span><span class="identifier">push_record</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">rec</span><span class="special">));</span>
174 <span class="special">}</span>
175 <span class="special">}</span>
180 You can see here that the <code class="computeroutput"><span class="identifier">open_record</span></code>
181 can take named arguments. Some logger types provided by the library have
182 support for such additional parameters and this approach can certainly be
183 used by users when writing their own loggers.
186 <a name="log.tutorial.attributes.h1"></a>
187 <span class="phrase"><a name="log.tutorial.attributes.more_attributes"></a></span><a class="link" href="attributes.html#log.tutorial.attributes.more_attributes">More
191 Let's see what's under the hood of that <a class="link" href="../detailed/utilities.html#log.detailed.utilities.setup.convenience" title="Convenience functions"><code class="computeroutput"><span class="identifier">add_common_attributes</span></code></a> function we
192 used in the simple form section. It might look something like this:
194 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">add_common_attributes</span><span class="special">()</span>
195 <span class="special">{</span>
196 <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>
197 <span class="identifier">core</span><span class="special">-></span><span class="identifier">add_global_attribute</span><span class="special">(</span><span class="string">"LineID"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">counter</span><span class="special"><</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> <span class="special">>(</span><span class="number">1</span><span class="special">));</span>
198 <span class="identifier">core</span><span class="special">-></span><span class="identifier">add_global_attribute</span><span class="special">(</span><span class="string">"TimeStamp"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">local_clock</span><span class="special">());</span>
200 <span class="comment">// other attributes skipped for brevity</span>
201 <span class="special">}</span>
204 Here the <a class="link" href="../detailed/attributes.html#log.detailed.attributes.counter" title="Counters"><code class="computeroutput"><span class="identifier">counter</span></code></a>
205 and <a class="link" href="../detailed/attributes.html#log.detailed.attributes.clock" title="Wall clock"><code class="computeroutput"><span class="identifier">local_clock</span></code></a>
206 components are attribute classes, they derive from the common attribute interface
207 <code class="computeroutput"><a class="link" href="../../boost/log/attribute.html" title="Class attribute">attribute</a></code>. The library
208 provides a number of other <a class="link" href="../detailed/attributes.html" title="Attributes">attribute
209 classes</a>, including the <a class="link" href="../detailed/attributes.html#log.detailed.attributes.function" title="Function objects as attributes"><code class="computeroutput"><span class="identifier">function</span></code></a> attribute that calls some
210 function object on value acquisition. For example, we can in a similar way
211 register a <a class="link" href="../detailed/attributes.html#log.detailed.attributes.named_scope" title="Named scopes"><code class="computeroutput"><span class="identifier">named_scope</span></code></a> attribute:
213 <pre class="programlisting"><span class="identifier">core</span><span class="special">-></span><span class="identifier">add_global_attribute</span><span class="special">(</span><span class="string">"Scope"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">named_scope</span><span class="special">());</span>
216 This will give us the ability to store scope names in log for every log record
217 the application makes. Here is how it's used:
221 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">named_scope_logging</span><span class="special">()</span>
222 <span class="special">{</span>
223 <span class="identifier">BOOST_LOG_NAMED_SCOPE</span><span class="special">(</span><span class="string">"named_scope_logging"</span><span class="special">);</span>
225 <span class="identifier">src</span><span class="special">::</span><span class="identifier">severity_logger</span><span class="special"><</span> <span class="identifier">severity_level</span> <span class="special">></span> <span class="identifier">slg</span><span class="special">;</span>
227 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">normal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Hello from the function named_scope_logging!"</span><span class="special">;</span>
228 <span class="special">}</span>
233 Logger-specific attributes are no less useful than global ones. Severity
234 levels and channel names are the most obvious candidates to be implemented
235 on the source level. Nothing prevents you from adding more attributes to
236 your loggers, like this:
240 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">tagged_logging</span><span class="special">()</span>
241 <span class="special">{</span>
242 <span class="identifier">src</span><span class="special">::</span><span class="identifier">severity_logger</span><span class="special"><</span> <span class="identifier">severity_level</span> <span class="special">></span> <span class="identifier">slg</span><span class="special">;</span>
243 <span class="identifier">slg</span><span class="special">.</span><span class="identifier">add_attribute</span><span class="special">(</span><span class="string">"Tag"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">constant</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="string">"My tag value"</span><span class="special">));</span>
245 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">normal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Here goes the tagged record"</span><span class="special">;</span>
246 <span class="special">}</span>
251 Now all log records made through this logger will be tagged with the specific
252 attribute. This attribute value may be used later in filtering and formatting.
255 Another good use of attributes is the ability to mark log records made by
256 different parts of application in order to highlight activity related to
257 a single process. One can even implement a rough profiling tool to detect
258 performance bottlenecks. For example:
262 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">timed_logging</span><span class="special">()</span>
263 <span class="special">{</span>
264 <span class="identifier">BOOST_LOG_SCOPED_THREAD_ATTR</span><span class="special">(</span><span class="string">"Timeline"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">timer</span><span class="special">());</span>
266 <span class="identifier">src</span><span class="special">::</span><span class="identifier">severity_logger</span><span class="special"><</span> <span class="identifier">severity_level</span> <span class="special">></span> <span class="identifier">slg</span><span class="special">;</span>
267 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">normal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Starting to time nested functions"</span><span class="special">;</span>
269 <span class="identifier">logging_function</span><span class="special">();</span>
271 <span class="identifier">BOOST_LOG_SEV</span><span class="special">(</span><span class="identifier">slg</span><span class="special">,</span> <span class="identifier">normal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"Stopping to time nested functions"</span><span class="special">;</span>
272 <span class="special">}</span>
277 Now every log record made from the <code class="computeroutput"><span class="identifier">logging_function</span></code>
278 function, or any other function it calls, will contain the "Timeline"
279 attribute with a high precision time duration passed since the attribute
280 was registered. Based on these readings, one will be able to detect which
281 parts of the code require more or less time to execute. The "Timeline"
282 attribute will be unregistered upon leaving the scope of function <code class="computeroutput"><span class="identifier">timed_logging</span></code>.
285 See the <a class="link" href="../detailed/attributes.html" title="Attributes">Attributes</a> section
286 for detailed description of attributes provided by the library. The complete
287 code for this section is available <a href="../../../../../../libs/log/example/doc/tutorial_attributes.cpp" target="_top">here</a>.
290 <a name="log.tutorial.attributes.h2"></a>
291 <span class="phrase"><a name="log.tutorial.attributes.defining_attribute_placeholders"></a></span><a class="link" href="attributes.html#log.tutorial.attributes.defining_attribute_placeholders">Defining
292 attribute placeholders</a>
295 As we will see in the coming sections, it is useful to define a keyword describing
296 a particular attribute the application uses. This keyword will be able to
297 participate in filtering and formatting expressions, like the <code class="computeroutput"><span class="identifier">severity</span></code> placeholder we have used in previous
298 sections. For example, to define placeholders for some of the attributes
299 we used in the previous examples we can write this:
301 <pre class="programlisting"><span class="identifier">BOOST_LOG_ATTRIBUTE_KEYWORD</span><span class="special">(</span><span class="identifier">line_id</span><span class="special">,</span> <span class="string">"LineID"</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="keyword">int</span><span class="special">)</span>
302 <span class="identifier">BOOST_LOG_ATTRIBUTE_KEYWORD</span><span class="special">(</span><span class="identifier">severity</span><span class="special">,</span> <span class="string">"Severity"</span><span class="special">,</span> <span class="identifier">severity_level</span><span class="special">)</span>
303 <span class="identifier">BOOST_LOG_ATTRIBUTE_KEYWORD</span><span class="special">(</span><span class="identifier">tag_attr</span><span class="special">,</span> <span class="string">"Tag"</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">)</span>
304 <span class="identifier">BOOST_LOG_ATTRIBUTE_KEYWORD</span><span class="special">(</span><span class="identifier">scope</span><span class="special">,</span> <span class="string">"Scope"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">named_scope</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">)</span>
305 <span class="identifier">BOOST_LOG_ATTRIBUTE_KEYWORD</span><span class="special">(</span><span class="identifier">timeline</span><span class="special">,</span> <span class="string">"Timeline"</span><span class="special">,</span> <span class="identifier">attrs</span><span class="special">::</span><span class="identifier">timer</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">)</span>
308 Each macro defines a keyword. The first argument is the placeholder name,
309 the second is the attribute name and the last parameter is the attribute
310 value type. Once defined, the placeholder can be used in template expressions
311 and some other contexts of the library. More details on defining attribute
312 keywords are available <a class="link" href="../detailed/expressions.html#log.detailed.expressions.attr_keywords" title="Defining attribute keywords">here</a>.
315 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
316 <td align="left"></td>
317 <td align="right"><div class="copyright-footer">Copyright © 2007-2014 Andrey Semashev<p>
318 Distributed under the Boost Software License, Version 1.0. (See accompanying
319 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>).
324 <div class="spirit-nav">
325 <a accesskey="p" href="sources.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.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="formatters.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>