1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Why not using lazy streaming?</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="../rationale.html" title="Rationale and FAQ"> 9<link rel="prev" href="why_attribute_manips_dont_affect_filters.html" title="Why attributes set with stream manipulators do not participate in filtering?"> 10<link rel="next" href="why_not_log4j.html" title="Why not using hierarchy of loggers, like in log4j? Why not Boost.Log4j? Etc."> 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="why_attribute_manips_dont_affect_filters.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rationale.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="why_not_log4j.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.rationale.why_not_lazy_streaming"></a><a class="link" href="why_not_lazy_streaming.html" title="Why not using lazy streaming?">Why not using lazy 21 streaming?</a> 22</h3></div></div></div> 23<p> 24 One of the possible library implementations would be using lazy expressions 25 to delay log record formatting. In essence, the expression: 26 </p> 27<pre class="programlisting"><span class="identifier">logger</span> <span class="special"><<</span> <span class="string">"Hello, world!"</span><span class="special">;</span> 28</pre> 29<p> 30 would become a lambda-expression that is only invoked if the filtering is 31 successful. Although this approach has advantages, it must be noted that 32 lazy expression construction is not zero-cost in terms of performance, code 33 size and compile times. The following expression: 34 </p> 35<pre class="programlisting"><span class="identifier">logger</span> <span class="special"><<</span> <span class="string">"Received packet from "</span> <span class="special"><<</span> <span class="identifier">ip</span> <span class="special"><<</span> <span class="string">" of "</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="special"><<</span> <span class="string">" bytes"</span><span class="special">;</span> 36</pre> 37<p> 38 would generate a considerable amount of code (proportional to the number 39 of streaming operators) to be executed before filtering takes place. Another 40 drawback is that the <code class="computeroutput"><span class="identifier">packet</span><span class="special">.</span><span class="identifier">size</span><span class="special">()</span></code> 41 is always called, whether or not the record is actually written to the log. 42 In order to delay this call, yet more scaffolding is needed, possibly involving 43 <a href="http://www.boost.org/doc/libs/release/libs/bind/bind.html" target="_top">Boost.Bind</a>, 44 <a href="http://www.boost.org/doc/libs/release/doc/html/lambda.html" target="_top">Boost.Lambda</a> 45 or <a href="http://www.boost.org/doc/libs/release/libs/phoenix/doc/html/index.html" target="_top">Boost.Phoenix</a>. 46 This complication is not acceptable for such a basic use case, like this. 47 </p> 48<p> 49 Although lazy streaming is not provided by the library out of the box, nothing 50 prevents developing it in a separate hierarchy of loggers. See the <a class="link" href="../extension/sources.html" title="Writing your own sources">Extending the library</a> section for 51 more information. 52 </p> 53</div> 54<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 55<td align="left"></td> 56<td align="right"><div class="copyright-footer">Copyright © 2007-2019 Andrey Semashev<p> 57 Distributed under the Boost Software License, Version 1.0. (See accompanying 58 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>). 59 </p> 60</div></td> 61</tr></table> 62<hr> 63<div class="spirit-nav"> 64<a accesskey="p" href="why_attribute_manips_dont_affect_filters.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../rationale.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="why_not_log4j.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 65</div> 66</body> 67</html> 68