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