[Rcpp-devel] rout and rerr instead of cout and cerr in Rcpp

Sean Robert McGuffee sean.mcguffee at gmail.com
Thu Apr 28 04:27:06 CEST 2011


Just to follow up myself, it turns out that it¹s easy to redirect cerr and
cout to files, and that solves all my issues.
In case anyone is interested, here is how I did it:


#include<ostream>
#include<fstream>

RcppExport SEXP myfunction(SEXP params)
{
    try
    { // or use BEGIN_RCPP macro

        //start off by redirecting
        streambuf* save_sbuf_fout;
        streambuf* save_sbuf_ferr;
        streambuf* save_sbuf_cout;
        streambuf* save_sbuf_cerr;
        ofstream fout;
        ofstream ferr;
        fout.open("cout.txt");
        ferr.open("cerr.txt");
        save_sbuf_cout = cout.rdbuf();
        save_sbuf_cerr = cerr.rdbuf();
        save_sbuf_fout = fout.rdbuf();
        save_sbuf_ferr = ferr.rdbuf();
        cout.rdbuf(save_sbuf_fout);
        cerr.rdbuf(save_sbuf_ferr);

        /*do whatever I normally do and write to cerr and/or cout all I
want*/

        //revert to original state
        cout.rdbuf(save_sbuf_cout);
        cerr.rdbuf(save_sbuf_cerr);
        fout.close();
        ferr.close();

    }
}



On 4/27/11 9:26 PM, "Sean Robert McGuffee" <sean.mcguffee at gmail.com> wrote:

>>            >On 11/10/2009 12:23 PM, Dirk Eddelbuettel wrote:
>>> >
>>> > On 10 November 2009 at 09:47, Romain François wrote:
>>> > | Hi,
>>> > |
>>> > | Would it make sense to have an rout and rerr so that we could do :
>>> > |
>>> > | rout<<  "bla bla"<<  endl ;
>>> > |
>>> > | and
>>> > |
>>> > | rerr<<  "bla bla"<<  endl ;
>>> > |
>>> > | and this would go into Rprintf and REprintf ? or maybe we can directly
>>> > | redefine cout and cerr
>>> >
>>> > Hm. That never really arose in my use. cout was mostly for debugging.
>>> >
>>> > I think a C++-ish way is to create<<  operators (that eg RcppDate and
>>> > RcppDatetime have).
>>> >
>>> > Where you thinking of something other than debugging help?
>> 
>> |Not really, but just sort of making sure debugging is consistent with
>> |the recommendation from WRE of using Rprintf instead of printf
> 
> Has anyone followed up on this? I¹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¹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?
> Thanks,
> Sean


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110427/11044a42/attachment.htm>


More information about the Rcpp-devel mailing list