title: How to trigger action on end-of-statement?
body: <p>Suppose that I want to create a stream that would perform an action at the end of the statement, so that</p>
  
  <pre><code>myStream &lt;&lt; "Hello, " &lt;&lt; "World!";
  </code></pre>
  
  <p>would print "Hello, World!\n" <strong>in one shot</strong>. Not "Hello, \nWorld!\n" and not "Hello, World!", but "Hello, World\n", as if <code>;</code> would trigger appending <code>\n</code> and flushing the buffers. </p>
  
  <p>The rationale for that is a stream class that writes to both <code>stdout</code> and a logfile, with the logfile entries having certain prefixes and suffixes. </p>
  
  <p>For example, if my target was HTML I would want this code:</p>
  
  <pre><code>myStream &lt;&lt; "Hello, " &lt;&lt; "World!";
  myStream &lt;&lt; "Good bye, " &lt;&lt; "cruel World!";
  </code></pre>
  
  <p>to print like this:</p>
  
  <pre><code>&lt;p&gt;Hello, World!&lt;/p&gt;
  &lt;p&gt;Good bye, cruel World!&lt;/p&gt;
  </code></pre>
  
  <p>and <strong>not</strong> like this:</p>
  
  <pre><code>&lt;p&gt;Hello, &lt;/p&gt;&lt;p&gt;World!&lt;/p&gt;
  &lt;p&gt;Good bye, &lt;/p&gt;&lt;p&gt;cruel World!&lt;/p&gt;
  </code></pre>
  
  <p>Now, if I implement <code>LogStream</code> sort of like this:</p>
  
  <pre><code>LogStream &amp; LogStream::operator&lt;&lt;( const std::string &amp; text );
  </code></pre>
  
  <p>I won't be able to distinguish between <code>&lt;&lt;</code> in the middle of the statements from the ones in the beginning/ending of the statements.</p>
  
  <p>If I implement <code>LogStream</code> sort of like this:</p>
  
  <pre><code>LogStream LogStream::operator&lt;&lt;( const std::string &amp; text );
  </code></pre>
  
  <p>and try to massage the input in the destructor I would get multiple destructors at once at the end of the block.</p>
  
  <p>Finally, I can implement this my requiring <code>endl</code> at the end of each statement, but I'd rather not bother the caller with the necessity to do so.</p>
  
  <p>Thus the question: how one implements such a stream in a caller-transparent fashion? </p>