1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Why scoped attributes don't override existing attributes?</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="../rationale.html" title="Rationale and FAQ"> 10<link rel="next" href="why_weak_record_ordering.html" title="Why log records are weakly ordered in a multithreaded application?"> 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="../rationale.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_weak_record_ordering.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_weak_scoped_attributes"></a><a class="link" href="why_weak_scoped_attributes.html" title="Why scoped attributes don't override existing attributes?">Why scoped 21 attributes don't override existing attributes?</a> 22</h3></div></div></div> 23<p> 24 Initially scoped attributes were able to override other attributes with the 25 same name if they were already registered by the time when a scoped attribute 26 encountered. This allowed some interesting use cases like this: 27 </p> 28<pre class="programlisting"><span class="identifier">BOOST_LOG_DECLARE_GLOBAL_LOGGER</span><span class="special">(</span><span class="identifier">my_logger</span><span class="special">,</span> <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger_mt</span><span class="special">)</span> 29 30<span class="keyword">void</span> <span class="identifier">foo</span><span class="special">()</span> 31<span class="special">{</span> 32 <span class="comment">// This scoped attribute would temporarily replace the existing tag</span> 33 <span class="identifier">BOOST_LOG_SCOPED_THREAD_TAG</span><span class="special">(</span><span class="string">"Section"</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">"In foo"</span><span class="special">);</span> 34 35 <span class="comment">// This log record will have a "Section" attribute with value "In foo"</span> 36 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in foo section"</span><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">BOOST_LOG_SCOPED_THREAD_TAG</span><span class="special">(</span><span class="string">"Section"</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">"In main"</span><span class="special">);</span> 42 43 <span class="comment">// This log record will have a "Section" attribute with value "In main"</span> 44 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in main section"</span><span class="special">;</span> 45 46 <span class="identifier">foo</span><span class="special">();</span> 47 48 <span class="comment">// This log record will have a "Section" attribute with value "In main" again</span> 49 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in main section again"</span><span class="special">;</span> 50 51 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 52<span class="special">}</span> 53</pre> 54<p> 55 However, this feature introduced a number of safety problems, including thread 56 safety issues, that could be difficult to track down. For example, it was 57 no longer safe to use logger-wide scoped attributes on the same logger from 58 different threads, because the resulting attribute would be undefined: 59 </p> 60<pre class="programlisting"><span class="identifier">BOOST_LOG_DECLARE_GLOBAL_LOGGER</span><span class="special">(</span><span class="identifier">my_logger</span><span class="special">,</span> <span class="identifier">src</span><span class="special">::</span><span class="identifier">logger_mt</span><span class="special">)</span> 61 62<span class="keyword">void</span> <span class="identifier">thread1</span><span class="special">()</span> 63<span class="special">{</span> 64 <span class="identifier">BOOST_LOG_SCOPED_LOGGER_TAG</span><span class="special">(</span><span class="identifier">get_my_logger</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> <span class="string">"thread1"</span><span class="special">);</span> 65 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in thread1"</span><span class="special">;</span> 66<span class="special">}</span> 67 68<span class="keyword">void</span> <span class="identifier">thread2</span><span class="special">()</span> 69<span class="special">{</span> 70 <span class="identifier">BOOST_LOG_SCOPED_LOGGER_TAG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">(),</span> <span class="string">"Tag"</span><span class="special">,</span> <span class="keyword">int</span><span class="special">,</span> <span class="number">10</span><span class="special">);</span> 71 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in thread2"</span><span class="special">;</span> 72<span class="special">}</span> 73 74<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> 75<span class="special">{</span> 76 <span class="identifier">BOOST_LOG_SCOPED_LOGGER_TAG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">(),</span> <span class="string">"Tag"</span><span class="special">,</span> <span class="keyword">double</span><span class="special">,</span> <span class="special">-</span><span class="number">2.2</span><span class="special">);</span> 77 78 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in main"</span><span class="special">;</span> 79 80 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">thread</span> <span class="identifier">t1</span><span class="special">(&</span><span class="identifier">thread1</span><span class="special">);</span> 81 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">thread</span> <span class="identifier">t2</span><span class="special">(&</span><span class="identifier">thread2</span><span class="special">);</span> 82 83 <span class="identifier">t1</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> 84 <span class="identifier">t2</span><span class="special">.</span><span class="identifier">join</span><span class="special">();</span> 85 86 <span class="comment">// Which "Tag" is registered here?</span> 87 <span class="identifier">BOOST_LOG</span><span class="special">(</span><span class="identifier">get_my_logger</span><span class="special">())</span> <span class="special"><<</span> <span class="string">"We're in main again"</span><span class="special">;</span> 88 89 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 90<span class="special">}</span> 91</pre> 92<p> 93 There were other issues, like having an attribute set iterator that points 94 to one attribute object, then suddenly without seemingly modifying it it 95 becomes pointing to a different attribute object (of, possibly, a different 96 type). Such behavior could lead to tricky failures that would be difficult 97 to investigate. Therefore this feature was eventually dropped, which simplified 98 the scoped attributes implementation significantly. 99 </p> 100</div> 101<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 102<td align="left"></td> 103<td align="right"><div class="copyright-footer">Copyright © 2007-2019 Andrey Semashev<p> 104 Distributed under the Boost Software License, Version 1.0. (See accompanying 105 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>). 106 </p> 107</div></td> 108</tr></table> 109<hr> 110<div class="spirit-nav"> 111<a accesskey="p" href="../rationale.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_weak_record_ordering.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 112</div> 113</body> 114</html> 115