<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bicosyes - since evermore... &#187; C++</title>
	<atom:link href="http://bicosyes.com/category/programacion/cplusplus/feed/" rel="self" type="application/rss+xml" />
	<link>http://bicosyes.com</link>
	<description></description>
	<lastBuildDate>Sat, 06 Mar 2010 20:03:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>boost::asio, synchronous read with timeout</title>
		<link>http://bicosyes.com/boostasio-synchronous-read-with-timeout/</link>
		<comments>http://bicosyes.com/boostasio-synchronous-read-with-timeout/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 20:48:25 +0000</pubDate>
		<dc:creator>blaxter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[boost::asio]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[english]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://bicosyes.com/?p=833</guid>
		<description><![CDATA[The boost::asio (which means asynchronous input/output) library, is quite powerful library for asynchronous i/o, but it could be a bit difficult at first to figure out how to do a normal synchronous read. So, as a reminder for my future-me, and for you, this snippet it'll be very useful to accomplish that. Probably there will [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong><a href="http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio.html">boost::asio</a></strong> (which means asynchronous input/output) library, is quite powerful library for asynchronous i/o, but it could be a bit difficult at first to figure out how to do a normal <strong>synchronous</strong> read. So, as a reminder for my future-me, and for you, this snippet it'll be very useful to accomplish that. Probably there will be another ways for doing that, but this is how I managed to do it:</p>
<pre class="cpp"><span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> boost::<span style="color: #00eeff;">asio</span>;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> boost::<span style="color: #0000dd;">system</span>;
<span style="color: #0000ff;">using</span> boost::<span style="color: #00eeff;">optional</span>;
&nbsp;
ip::<span style="color: #00eeff;">tcp</span>::<span style="color: #00eeff;">socket</span> _socket; <span style="color: #ff0000;">// it could be another kind of socket, not only ip::tcp</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/**
 * Dumb function to be used as handler argument and save the error_code
 * into a pointer
 *
 * e.g.: boost::bind( &amp;set_result, some_pointer, _1 )
 */</span>
<span style="color: #0000ff;">void</span> set_result<span style="color: #000000;">&#40;</span> optional&lt;error_code&gt;* a, error_code b <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  a-&gt;reset<span style="color: #000000;">&#40;</span> b <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#define TIMEOUT 60</span>
<span style="color: #ff0000; font-style: italic;">/**
 * it uses _socket
 * if timeout happends throw a system_error exception
 */</span>
template&lt;typename MutableBufferSequence&gt;
optional&lt;error_code&gt; read_with_timeout<span style="color: #000000;">&#40;</span>
    <span style="color: #0000ff;">const</span> MutableBufferSequence&amp; buffer
  <span style="color: #000000;">&#41;</span> throw<span style="color: #000000;">&#40;</span> system_error <span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  optional&lt;error_code&gt; timer_result;
  optional&lt;error_code&gt; read_result;
&nbsp;
  deadline_timer timer<span style="color: #000000;">&#40;</span> _socket.<span style="color: #00eeff;">io_service</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
  timer.<span style="color: #00eeff;">expires_from_now</span><span style="color: #000000;">&#40;</span> seconds<span style="color: #000000;">&#40;</span>TIMEOUT<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
  timer.<span style="color: #00eeff;">async_wait</span><span style="color: #000000;">&#40;</span> boost::<span style="color: #00eeff;">bind</span><span style="color: #000000;">&#40;</span>&amp;set_result, &amp;timer_result, _1<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
  boost::<span style="color: #00eeff;">asio</span>::<span style="color: #00eeff;">async_read</span><span style="color: #000000;">&#40;</span>
      _socket,
      buffer,
      boost::<span style="color: #00eeff;">asio</span>::<span style="color: #00eeff;">transfer_at_least</span><span style="color: #000000;">&#40;</span> buffer_size_helper<span style="color: #000000;">&#40;</span>buffer<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>,
      boost::<span style="color: #00eeff;">bind</span><span style="color: #000000;">&#40;</span> &amp;set_result, &amp;read_result, _1 <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#41;</span>;
&nbsp;
  _socket.<span style="color: #00eeff;">io_service</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">reset</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #0000ff;">while</span> <span style="color: #000000;">&#40;</span> _socket.<span style="color: #00eeff;">io_service</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">run_one</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> read_result <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      timer.<span style="color: #00eeff;">cancel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> timer_result <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      _socket.<span style="color: #00eeff;">cancel</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
      throw system_error<span style="color: #000000;">&#40;</span>
          error_code<span style="color: #000000;">&#40;</span> errc::<span style="color: #00eeff;">timed_out</span>, get_generic_category<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#41;</span>;
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
  <span style="color: #0000ff;">return</span> read_result;
<span style="color: #000000;">&#125;</span>
&nbsp;</pre>
<p>I hope it will be useful, have fun.</p>
<img src="http://bicosyes.com/?ak_action=api_record_view&id=833&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://bicosyes.com/boostasio-synchronous-read-with-timeout/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C++, cadenas multilínea de forma clara</title>
		<link>http://bicosyes.com/c-cadenas-multilinea-de-forma-clara/</link>
		<comments>http://bicosyes.com/c-cadenas-multilinea-de-forma-clara/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 14:05:05 +0000</pubDate>
		<dc:creator>blaxter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[multiline]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://bicosyes.com/?p=846</guid>
		<description><![CDATA[Según el estándar c++98 (sección 2.13.4) las cadenas literales tienen como propiedad que durante el análisis léxico del código fuente (la primera fase que realiza el compilador, convirtiendo texto de entrada a tokens y símbolos) se concatenarán cadenas adyacentes.
In translation phase 6 (2.1), adjacent narrow string literals are concatenated and adjacent wide string literals are [...]]]></description>
			<content:encoded><![CDATA[<p>Según el <a href="http://en.wikipedia.org/wiki/ISO/IEC_14882#Language_standard">estándar c++98</a> (sección 2.13.4) las cadenas literales tienen como propiedad que durante el análisis léxico del código fuente (la primera fase que realiza el compilador, convirtiendo texto de entrada a tokens y símbolos) se concatenarán cadenas adyacentes.</p>
<blockquote><p>In translation phase 6 (2.1), adjacent narrow string literals are concatenated and adjacent wide string literals are concatenated. </p></blockquote>
<p>Esto viene a decir que podemos partir nuestras cadenas en cualquier momento sin poner ningún signo de concatenación entre ellas, de esta forma:</p>
<pre class="cpp">&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> s1* = <span style="color: #666666;">&quot;dum&quot;</span> <span style="color: #666666;">&quot;de&quot;</span> <span style="color: #666666;">&quot;dum&quot;</span>;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> s2* = <span style="color: #666666;">&quot;dum&quot;</span>
                 <span style="color: #666666;">&quot;de&quot;</span>
                 <span style="color: #666666;">&quot;dum&quot;</span>
;</pre>
<p>Lo cual viene realmente bien para formar código bien indentado y formateado, pues aunque también podemos usar "\" para saltar de línea, el texto a continuación no puede estar indentado pues significaría incluir esos espacios/tabuladores en la cadena que estamos definiendo. Y lo realmente interesante es que todo esto está en la propia definición del lenguaje, no es ninguna ayuda concreta del compilador, a los ojos del programa todas las siguientes definiciones son idénticas:</p>
<pre class="cpp">&nbsp;
<span style="color: #339900;">#include &lt;string&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> * cmp<span style="color: #000000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> *s1, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> *s2 <span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> * s1 = <span style="color: #666666;">&quot;My spoon is too big&quot;</span>
                      <span style="color: #666666;">&quot;My spoon is TOO BIG&quot;</span>
                      <span style="color: #666666;">&quot;I AM A BANANA&quot;</span>;
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> * s2 = <span style="color: #666666;">&quot;My spoon is too big<span style="color: #666666; font-weight: bold;">\</span>
My spoon is TOO BIG<span style="color: #666666; font-weight: bold;">\</span>
I AM A BANANA&quot;</span>;
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> * s3 = <span style="color: #000000;">&#40;</span> std::<span style="color: #00eeff;">string</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;My spoon is too big&quot;</span><span style="color: #000000;">&#41;</span> +
                        std::<span style="color: #00eeff;">string</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;My spoon is TOO BIG&quot;</span><span style="color: #000000;">&#41;</span> +
                        std::<span style="color: #00eeff;">string</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;I AM A BANANA&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">c_str</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    std::<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;s1 and s2 are &quot;</span> &lt;&lt; cmp<span style="color: #000000;">&#40;</span> s1, s2 <span style="color: #000000;">&#41;</span> &lt;&lt; std::<span style="color: #00eeff;">endl</span>;
    std::<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;s1 and s3 are &quot;</span> &lt;&lt; cmp<span style="color: #000000;">&#40;</span> s1, s3 <span style="color: #000000;">&#41;</span> &lt;&lt; std::<span style="color: #00eeff;">endl</span>;
    std::<span style="color: #0000dd;">cout</span> &lt;&lt; <span style="color: #666666;">&quot;s2 and s3 are &quot;</span> &lt;&lt; cmp<span style="color: #000000;">&#40;</span> s2, s3 <span style="color: #000000;">&#41;</span> &lt;&lt; std::<span style="color: #00eeff;">endl</span>;
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> * cmp<span style="color: #000000;">&#40;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> *s1, <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> *s2 <span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#123;</span>
    <span style="color: #0000ff;">while</span><span style="color: #000000;">&#40;</span> *s1 &amp;&amp; *s2 &amp;&amp; *s1++ == *s2++ <span style="color: #000000;">&#41;</span>;
    <span style="color: #0000ff;">if</span> <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span> *s1 != *s2 <span style="color: #000000;">&#41;</span> || <span style="color: #000000;">&#40;</span> *s1 || *s2 <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #666666;">&quot;different&quot;</span>;
    <span style="color: #0000ff;">return</span> <span style="color: #666666;">&quot;equal&quot;</span>;
<span style="color: #000000;">&#125;</span></pre>
<pre class="bash">$ <span style="color: #c20cb9; font-weight: bold;">c++</span> const_char_declaration_sample.<span style="color: #c20cb9; font-weight: bold;">cpp</span> &amp;&amp; ./a.out
s1 and s2 are equal
s1 and s3 are equal
s2 and s3 are equal</pre>
<img src="http://bicosyes.com/?ak_action=api_record_view&id=846&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://bicosyes.com/c-cadenas-multilinea-de-forma-clara/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Librería de logging para C++, boost::logging</title>
		<link>http://bicosyes.com/libreria-de-logging-para-c-boostlogging/</link>
		<comments>http://bicosyes.com/libreria-de-logging-para-c-boostlogging/#comments</comments>
		<pubDate>Sun, 17 May 2009 19:58:31 +0000</pubDate>
		<dc:creator>blaxter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[boost::logging]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://bicosyes.com/?p=789</guid>
		<description><![CDATA[En C++  tienes diversas librerías de logging, la mayoría son clones de log4j realmente, u otras con un estilo diferente bastante interesante; pero como suele ser costumbre en el mundo C++, tienes una alternativa relacionada con las librerías boost (en realidad boost::logging no es de boost oficialmente, pero es más que probable que en [...]]]></description>
			<content:encoded><![CDATA[<p>En C++  tienes diversas librerías de <em>logging</em>, la mayoría son <a href="http://logging.apache.org/log4cxx/index.html">clones</a> <a href="http://log4c.sourceforge.net/">de</a> <a href="http://logging.apache.org/log4j/">log4j</a> <a href="http://log4cpp.sourceforge.net/">realmente</a>, u otras con un <a href="http://www.pantheios.org/">estilo</a> diferente <a href="http://www.arg0.net/rlog">bastante interesante</a>; pero como suele ser costumbre en el mundo C++, tienes una alternativa relacionada con las <a href="http://www.boost.org/">librerías <strong>boost</strong></a> (en realidad <em>boost::logging</em> no es de boost oficialmente, pero es <a href="http://archives.free.net.ph/message/20080218.000757.d0fe0ce5.en.html">más que probable</a> que en el futuro lo sea) que suele ser la ganadora por méritos propios.</p>
<p>Estoy hablando de <a href="http://torjo.com/log2/index.html">la implementación de John Torjo</a>. Muy flexible a la hora de configurarla unido a un uso trivial de la misma (como tiene que ser, tampoco es que, la labor de logging, sea algo muy complejo que digamos...). Para conocer todos los detalles puedes leerte la extensa y útil <a href="http://torjo.com/log2/doc/html/index.html">documentación</a>, aunque de primeras puede ser un tanto compleja debido a nuevos conceptos que se usan a diestro y siniestro.</p>
<p>Principalmente debemos de conocer dos cosas:</p>
<ul>
<li>Qué tipo de <strong>filtro</strong> queremos: el cual será el encargado decidir si un mensaje se escribe o no, dependiendo tanto de si el logging está activado, como si cumplimos la restricción de nivel (debug, warn, error, etc...)</li>
<li>Qué tipo de <strong>log</strong> queremos: ¿cómo debe de ser el formato de salida?, ¿cuál es la salida/s? ¿cómo será su comportamiento?.</li>
</ul>
<p>Una vez definido esto, podemos escribirnos un par de ficheros en los cuales definiremos el log que podrá ser usado desde cualquier parte de la aplicación, incluyendo el header, y siempre y cuando se haya inicializado previamente. </p>
<pre class="cpp">&nbsp;
<span style="color: #339900;">#ifndef __LOGGING_HPP__</span>
<span style="color: #339900;">#define __LOGGING_HPP__</span>
&nbsp;
<span style="color: #339900;">#include &lt;boost/logging/format_fwd.hpp&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> boost::<span style="color: #00eeff;">logging</span>;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> boost::<span style="color: #00eeff;">logging</span>::<span style="color: #00eeff;">scenario</span>::<span style="color: #00eeff;">usage</span>;
&nbsp;
<span style="color: #0000ff;">typedef</span> use&lt;
    filter_::<span style="color: #00eeff;">change</span>::<span style="color: #00eeff;">single_thread</span>, <span style="color: #ff0000;">// how often does the filter change?</span>
    filter_::<span style="color: #00eeff;">level</span>::<span style="color: #00eeff;">no_levels</span>,      <span style="color: #ff0000;">// does the filter use levels?</span>
    logger_::<span style="color: #00eeff;">change</span>::<span style="color: #00eeff;">single_thread</span>, <span style="color: #ff0000;">// how often does the logger change?</span>
    logger_::<span style="color: #00eeff;">favor</span>::<span style="color: #00eeff;">correctness</span>     <span style="color: #ff0000;">// what does the logger favor?</span>
  &gt; finder;
&nbsp;
BOOST_DECLARE_LOG_FILTER<span style="color: #000000;">&#40;</span> g_log_filter, finder::<span style="color: #00eeff;">filter</span> <span style="color: #000000;">&#41;</span>
BOOST_DECLARE_LOG<span style="color: #000000;">&#40;</span> g_log, finder::<span style="color: #00eeff;">logger</span> <span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #339900;">#define L_ BOOST_LOG_USE_LOG_IF_FILTER( g_log(), g_log_filter()-&gt;is_enabled() )</span>
&nbsp;
<span style="color: #0000ff;">void</span> initialize_logs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
<span style="color: #339900;">#endif // __LOGGING_HPP__</span></pre>
<p>Aquí acabamos de declarar un log, sin niveles, para una aplicación simple sin usar un thread separado para el logging. Para ver otras alternativas puedes echarle un ojo a los namespaces <a href="http://torjo.com/log2/doc/html/namespaceboost_1_1logging_1_1scenario_1_1usage_1_1filter__.html">boost::logging::scenario::usage::filter_</a> y <a href="http://torjo.com/log2/doc/html/namespaceboost_1_1logging_1_1scenario_1_1usage_1_1logger__.html">boost::logging::scenario::usage::logger_</a>. </p>
<p>Finalmente solo nos quedaría inicializar el log (o logs, recuerda que nada te impide tener tantos como quieras):</p>
<pre class="cpp">&nbsp;
<span style="color: #339900;">#include &quot;logging.hpp&quot;</span>
<span style="color: #339900;">#include &lt;boost/logging/format_ts.hpp&gt;</span>
<span style="color: #339900;">#include &lt;boost/thread/xtime.hpp&gt;</span>
&nbsp;
BOOST_DEFINE_LOG_FILTER<span style="color: #000000;">&#40;</span> g_log_filter, finder::<span style="color: #00eeff;">filter</span> <span style="color: #000000;">&#41;</span>
&nbsp;
BOOST_DEFINE_LOG<span style="color: #000000;">&#40;</span> g_log, finder::<span style="color: #00eeff;">logger</span> <span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #0000ff;">void</span> initialize_logs<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;writer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">add_formatter</span><span style="color: #000000;">&#40;</span> formatter::<span style="color: #00eeff;">idx</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;[%] &quot;</span>  <span style="color: #000000;">&#41;</span>;
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;writer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">add_formatter</span><span style="color: #000000;">&#40;</span> formatter::<span style="color: #0000dd;">time</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;$hh:$mm.$ss &quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;writer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">add_formatter</span><span style="color: #000000;">&#40;</span> formatter::<span style="color: #00eeff;">append_newline</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span>;
&nbsp;
  <span style="color: #0000ff;">typedef</span> detail::<span style="color: #00eeff;">flag</span>&lt;destination::<span style="color: #00eeff;">file_settings</span>&gt; flag;
  destination::<span style="color: #00eeff;">file_settings</span> file_settings;
  file_settings.<span style="color: #00eeff;">initial_overwrite</span> = flag::<span style="color: #00eeff;">t</span>&lt;bool&gt;<span style="color: #000000;">&#40;</span> &amp;file_settings, <span style="color: #0000ff;">true</span> <span style="color: #000000;">&#41;</span>;
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;writer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">add_destination</span><span style="color: #000000;">&#40;</span>
      destination::<span style="color: #0000ff;">file</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;app_debug.txt&quot;</span>, file_settings <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#41;</span>;
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;writer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #00eeff;">add_destination</span><span style="color: #000000;">&#40;</span>
      destination::<span style="color: #0000dd;">cerr</span>
    <span style="color: #000000;">&#41;</span>;
&nbsp;
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;turn_cache_off<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; <span style="color: #ff0000;">// for showing output immediately</span>
  g_log<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>-&gt;mark_as_initialized<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span></pre>
<p>Aquí tenemos que hacer la definición del <em>log</em> y del <em>filter</em> de la misma forma que hemos hecho su declaración anteriormente. Definimos <a href="http://torjo.com/log2/doc/html/namespaceboost_1_1logging_1_1formatter.html">el formato de salida</a> como más nos guste y finalmente solo nos queda indicar dónde se deben de escribir los mensajes. Se puede tener varios destinos, aunque básicamente podemos usar desde ficheros hasta cualquier <em><a href="http://www.cplusplus.com/reference/iostream/">stream</a></em>, pasando por las salidas estándar (fíjate en los typedef de <a href="http://torjo.com/log2/doc/html/namespaceboost_1_1logging_1_1destination.html">boost::logging:destination</a>).</p>
<p>Una cosa importante que merece la pena mencionar es la llamada al método <em>turn_cache_off()</em>, especialmente importante si usas una salida a consola, pues por defecto el logging se cachea y solo se escribe al destino cada cierto tiempo, por lo que si monitorizas el log en tiempo real es crucial desactivar esta caché.</p>
<p>De esta forma, ya tendremos en cualquier parte de nuestro código donde incluyamos la declaración una macro <em>L_</em> (o como la hayamos definido) lista para ser usada:</p>
<pre class="cpp">&nbsp;
L_ &lt;&lt; <span style="color: #666666;">&quot;Here we are&quot;</span>;
&nbsp;
<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> n = <span style="color: #0000dd;">42</span>;
L_ &lt;&lt; <span style="color: #666666;">&quot;The meaning of life is &quot;</span> &lt;&lt; n;
&nbsp;
L_ &lt;&lt; boost::<span style="color: #00eeff;">format</span><span style="color: #000000;">&#40;</span> <span style="color: #666666;">&quot;We can even use the awesome %s library versión %d.%d.%d&quot;</span> <span style="color: #000000;">&#41;</span>
    % <span style="color: #666666;">&quot;boost::format&quot;</span>
    % <span style="color: #000000;">&#40;</span> BOOST_VERSION / <span style="color: #0000dd;">100000</span> <span style="color: #000000;">&#41;</span>
    % <span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span> BOOST_VERSION / <span style="color: #0000dd;">100</span> <span style="color: #000000;">&#41;</span> % <span style="color: #0000dd;">1000</span> <span style="color: #000000;">&#41;</span>
    % <span style="color: #000000;">&#40;</span> BOOST_VERSION % <span style="color: #0000dd;">100</span> <span style="color: #000000;">&#41;</span>;</pre>
<img src="http://bicosyes.com/?ak_action=api_record_view&id=789&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://bicosyes.com/libreria-de-logging-para-c-boostlogging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

