1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Why my application crashes on process termination when file sinks are used?</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="init_term_support.html" title="Does Boost.Log support logging at process initialization and termination?"> 10<link rel="next" href="namespace_mangling.html" title="Why my application fails to link with Boost.Log? What's the fuss about library namespaces?"> 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="init_term_support.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="namespace_mangling.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_crash_on_term"></a><a class="link" href="why_crash_on_term.html" title="Why my application crashes on process termination when file sinks are used?">Why my application crashes 21 on process termination when file sinks are used?</a> 22</h3></div></div></div> 23<p> 24 There are known problems with <a href="http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm" target="_top">Boost.Filesystem</a> 25 (for example, <a href="https://svn.boost.org/trac/boost/ticket/8642" target="_top">#8642</a> and <a href="https://svn.boost.org/trac/boost/ticket/9219" target="_top">#9219</a>), which affect Boost.Log file sink backends. 26 When the file sink is destroyed, it attempts to perform a final log file 27 rotation, which involves <a href="http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm" target="_top">Boost.Filesystem</a> 28 for moving files. This typically happens when Boost.Log core is deinitialized, 29 at the global deinitialization stage, after leaving <code class="computeroutput"><span class="identifier">main</span><span class="special">()</span></code>. The crux of the problem is that <a href="http://www.boost.org/doc/libs/release/libs/filesystem/doc/index.htm" target="_top">Boost.Filesystem</a> 30 uses a global locale object internally to perform character code conversion 31 for <code class="computeroutput"><span class="identifier">path</span></code>s, and this locale 32 may get destroyed before Boost.Log is deinitialized, which results in a crash. 33 </p> 34<p> 35 There is no way for Boost.Log to influence the order of global deinitialization, 36 but the problem can be worked around on the user's side. One solution is 37 to make sure the locale is initialized <span class="emphasis"><em>before</em></span> Boost.Log. 38 This can be achieved by calling <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span><span class="special">::</span><span class="identifier">codecvt</span><span class="special">()</span></code> or <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span><span class="special">::</span><span class="identifier">imbue</span><span class="special">()</span></code> early during the application startup, before 39 performing any calls to Boost.Log. For example: 40 </p> 41<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">argv</span><span class="special">[])</span> 42<span class="special">{</span> 43 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span><span class="special">::</span><span class="identifier">imbue</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span><span class="special">(</span><span class="string">"C"</span><span class="special">));</span> 44 <span class="identifier">initialize_log</span><span class="special">();</span> 45 46 <span class="comment">// ...</span> 47<span class="special">}</span> 48</pre> 49<p> 50 Note that in this case you can't use Boost.Log in global constructors or 51 you have to make sure that <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">filesystem</span><span class="special">::</span><span class="identifier">path</span><span class="special">::</span><span class="identifier">imbue</span><span class="special">()</span></code> is still called first. 52 </p> 53<p> 54 Another solution is to remove and destroy file sinks from the logging core 55 before returning from <code class="computeroutput"><span class="identifier">main</span><span class="special">()</span></code>. This way file rotation will happen before 56 leaving <code class="computeroutput"><span class="identifier">main</span><span class="special">()</span></code>, 57 while the locale is still valid. The file sinks can be removed either individually 58 or as a part of the <code class="computeroutput"><span class="identifier">remove_all_sinks</span><span class="special">()</span></code> call: 59 </p> 60<pre class="programlisting"><span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">argv</span><span class="special">[])</span> 61<span class="special">{</span> 62 <span class="comment">// ...</span> 63 64 <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">remove_all_sinks</span><span class="special">();</span> 65 66 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 67<span class="special">}</span> 68</pre> 69<p> 70 Lastly, you can disable the final log file rotation in every file sink you 71 register in the logging core. For sinks added programmatically this can be 72 done by calling <code class="computeroutput"><span class="identifier">enable_final_rotation</span><span class="special">(</span><span class="keyword">false</span><span class="special">)</span></code> 73 on the sink backend. If the sink is created from <a class="link" href="../detailed/utilities.html#log.detailed.utilities.setup.settings" title="Library initialization from a settings container">settings</a>, 74 you can do this by setting EnableFinalRotation parameter to "false". 75 </p> 76</div> 77<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 78<td align="left"></td> 79<td align="right"><div class="copyright-footer">Copyright © 2007-2019 Andrey Semashev<p> 80 Distributed under the Boost Software License, Version 1.0. (See accompanying 81 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>). 82 </p> 83</div></td> 84</tr></table> 85<hr> 86<div class="spirit-nav"> 87<a accesskey="p" href="init_term_support.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="namespace_mangling.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 88</div> 89</body> 90</html> 91