<HTML>
<HEAD>
<TITLE>Re: [Rcpp-devel] rout and rerr instead of cout and cerr in Rcpp </TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>Just to follow up myself, it turns out that it&#8217;s easy to redirect cerr and cout to files, and that solves all my issues.<BR>
In case anyone is interested, here is how I did it:<BR>
<BR>
<BR>
#include&lt;ostream&gt;<BR>
#include&lt;fstream&gt;<BR>
<BR>
RcppExport SEXP myfunction(SEXP params)<BR>
{<BR>
&nbsp;&nbsp;&nbsp;&nbsp;try<BR>
&nbsp;&nbsp;&nbsp;&nbsp;{ // or use BEGIN_RCPP macro &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//start off by redirecting<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;streambuf* save_sbuf_fout;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;streambuf* save_sbuf_ferr;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;streambuf* save_sbuf_cout;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;streambuf* save_sbuf_cerr;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ofstream fout;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ofstream ferr;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fout.open(&quot;cout.txt&quot;); &nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ferr.open(&quot;cerr.txt&quot;);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save_sbuf_cout = cout.rdbuf();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save_sbuf_cerr = cerr.rdbuf();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save_sbuf_fout = fout.rdbuf();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;save_sbuf_ferr = ferr.rdbuf();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout.rdbuf(save_sbuf_fout);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cerr.rdbuf(save_sbuf_ferr);<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*do whatever I normally do and write to cerr and/or cout all I want*/<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//revert to original state<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout.rdbuf(save_sbuf_cout);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cerr.rdbuf(save_sbuf_cerr);<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fout.close();<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ferr.close();<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;}<BR>
}<BR>
<BR>
<BR>
<BR>
On 4/27/11 9:26 PM, &quot;Sean Robert McGuffee&quot; &lt;<a href="sean.mcguffee@gmail.com">sean.mcguffee@gmail.com</a>&gt; wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;On 11/10/2009 12:23 PM, Dirk Eddelbuettel wrote:<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>&gt;<BR>
&gt;<I> On 10 November 2009 at 09:47, Romain Fran&ccedil;ois wrote:<BR>
</I>&gt;<I> | Hi,<BR>
</I>&gt;<I> |<BR>
</I>&gt;<I> | Would it make sense to have an rout and rerr so that we could do :<BR>
</I>&gt;<I> |<BR>
</I>&gt;<I> | rout&lt;&lt; &nbsp;&quot;bla bla&quot;&lt;&lt; &nbsp;endl ;<BR>
</I>&gt;<I> |<BR>
</I>&gt;<I> | and<BR>
</I>&gt;<I> |<BR>
</I>&gt;<I> | rerr&lt;&lt; &nbsp;&quot;bla bla&quot;&lt;&lt; &nbsp;endl ;<BR>
</I>&gt;<I> |<BR>
</I>&gt;<I> | and this would go into Rprintf and REprintf ? or maybe we can directly<BR>
</I>&gt;<I> | redefine cout and cerr<BR>
</I>&gt;<BR>
&gt;<I> Hm. That never really arose in my use. cout was mostly for debugging.<BR>
</I>&gt;<BR>
&gt;<I> I think a C++-ish way is to create&lt;&lt; &nbsp;operators (that eg RcppDate and<BR>
</I>&gt;<I> RcppDatetime have).<BR>
</I>&gt;<BR>
&gt;<I> Where you thinking of something other than debugging help?<BR>
</I><BR>
|Not really, but just sort of making sure debugging is consistent with <BR>
|the recommendation from WRE of using Rprintf instead of printf<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
Has anyone followed up on this? I&#8217;m wondering if there might be a simple way to redirect cerr and cout to something like an rerr and rout? I for one have thousands of lines of code that use cerr and cout in libraries that I would like to use in R. It seems that they currently are causing a crash at run-time when I leave those lines in my packages. I won&#8217;t know for sure until I take the time to remove all of them, but who knows how long that might take. I think it would probably be faster and easier for me to redirect these streams to R somehow. Has that been worked out yet? If so, how? If not, I think maybe I can redirect them to some log files or something. Has anyone done that?<BR>
Thanks,<BR>
Sean<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'><BR>
</SPAN></FONT>
</BODY>
</HTML>