1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Trivial logging with filters</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Log v2">
<link rel="up" href="../tutorial.html" title="Tutorial">
<link rel="prev" href="../tutorial.html" title="Tutorial">
<link rel="next" href="sinks.html" title="Setting up sinks">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td></tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.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="sinks.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="log.tutorial.trivial_filtering"></a><a class="link" href="trivial_filtering.html" title="Trivial logging with filters">Trivial logging with
filters</a>
</h3></div></div></div>
<p>
While severity levels can be used for informative purposes, you will normally
want to apply filters to output only significant records and ignore the rest.
It is easy to do so by setting a global filter in the library core, like
this:
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">init</span><span class="special">()</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><span class="identifier">set_filter</span>
<span class="special">(</span>
<span class="identifier">logging</span><span class="special">::</span><span class="identifier">trivial</span><span class="special">::</span><span class="identifier">severity</span> <span class="special">>=</span> <span class="identifier">logging</span><span class="special">::</span><span class="identifier">trivial</span><span class="special">::</span><span class="identifier">info</span>
<span class="special">);</span>
<span class="special">}</span>
<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>
<span class="special">{</span>
<span class="identifier">init</span><span class="special">();</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">trace</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"A trace severity message"</span><span class="special">;</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">debug</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"A debug severity message"</span><span class="special">;</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">info</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"An informational severity message"</span><span class="special">;</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">warning</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"A warning severity message"</span><span class="special">;</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">error</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"An error severity message"</span><span class="special">;</span>
<span class="identifier">BOOST_LOG_TRIVIAL</span><span class="special">(</span><span class="identifier">fatal</span><span class="special">)</span> <span class="special"><<</span> <span class="string">"A fatal severity message"</span><span class="special">;</span>
<span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
<a href="../../../../../../libs/log/example/doc/tutorial_trivial_flt.cpp" target="_top">See the
complete code</a>.
</p>
<p>
Now, if we run this code sample, the first two log records will be ignored,
while the remaining four will pass on to the console.
</p>
<div class="important"><table border="0" summary="Important">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
<th align="left">Important</th>
</tr>
<tr><td align="left" valign="top"><p>
Remember that the streaming expression is only executed if the record passed
filtering. Don't specify business-critical calls in the streaming expression,
as these calls may not get invoked if the record is filtered away.
</p></td></tr>
</table></div>
<p>
A few words must be said about the filter setup expression. Since we're setting
up a global filter, we have to acquire the <a class="link" href="../detailed.html#log.detailed.core.core" title="Logging core">logging
core</a> instance. This is what <code class="computeroutput"><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></code>
does - it returns a pointer to the core singleton. The <code class="computeroutput"><span class="identifier">set_filter</span></code>
method of the logging core sets the global filtering function.
</p>
<p>
The filter in this example is built as a <a href="http://www.boost.org/doc/libs/release/libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>
lambda expression. In our case, this expression consists of a single logical
predicate, whose left argument is a placeholder that describes the attribute
to be checked, and the right argument is the value to be checked against.
The <code class="computeroutput"><span class="identifier">severity</span></code> keyword is a
placeholder provided by the library. This placeholder identifies the severity
attribute value in the template expressions; this value is expected to have
name "Severity" and type <code class="computeroutput"><a class="link" href="../reference.html#boost.log.trivial.severity_level">severity_level</a></code>. This attribute
is automatically provided by the library in case of trivial logging; the
user only has to supply its value in logging statements. The placeholder
along with the ordering operator creates a function object that will be called
by the logging core to filter log records. As a result, only log records
with severity level not less than <code class="computeroutput"><span class="identifier">info</span></code>
will pass the filter and end up on the console.
</p>
<p>
It is possible to build more complex filters, combining logical predicates
like this with each other, or even define your own function (including a
C++11 lambda function) that would act as a filter. We will return to filtering
in the following sections.
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2007-2014 Andrey Semashev<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
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>).
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../tutorial.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="sinks.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|