[Rcpp-devel] Rostream.h header
Alexey Stukalov
astukalov at gmail.com
Sun May 13 16:45:33 CEST 2012
Hi Dirk,
thanks for the quick response, there are some clarifications below.
Hi Alexey,
> On 12 May 2012 at 19:03, Alexey Stukalov wrote:
> |* Dear Rcpp developers,
> *|
> * *|* ATM to define the Rcpp::Rostream I have to fix the
> iostream/Rostream.h header
> *|* directly and add
> *|
> * *|* #include "Rstreambuf.h"
> *|
> * *|* before its declarations.
> *|
> * *|* Including "Rstreambuf.h" in my source file before "Rstreambuf.h"
> doesn't help,
> *|* it seems the headers are processed in a way that Rostream declaration
> comes
> *|* before Rstreambuf
> *|* (due to RcppCommon.h that is included by Rstreambuf.h and not by
> Rostream.h).
> *|
> * *|* Is it possible to fix Rostream.h in the mainline or there's another
> proper way
> *|* to include it?
> * Can you restate your problem a little differently? I don't quite
> understand
> what you are trying to do, and what is not working.
> What do you mean by "define the Rcpp::Rostream" ? You would not have to
> include that explicitly as our header structure already does it. See the
> example blog post from a few months ago:
> http://dirk.eddelbuettel.com/blog/2012/02/18#rcout_armadillo_example
> That works the same way with Rcpp alone:
> R> library(inline)
> R>
> R> fun <- cxxfunction(signature(), plugin="Rcpp", body='
> + Rcpp::Rcout << "Hello, world" << std::endl;
> + ')
> R>
> R> cat("Before Rcpp-created function\n")
> Before Rcpp-created function
> R> fun()
> Hello, world
> NULL
> R> cat("After Rcpp-created function\n")
> After Rcpp-created function
> R>
> R>
> That doesn't work so impressively with inline etc as we come back to the
> prompt anyway, but by using Rcpp::Rcout all your stdout access from C++ is
> passed through the output stream controlled by R thanks to the work Jelmer
> did.
> Or did I misunderstand and you wanted to do something different ?
>
I'm trying to use Rcpp::Rostream from my C++ library, not from the inline
code,
so I have to deal with the headers inclusion on my own :)
Basically, I want to use Rostream as a log sink for boost::log (this
library not in the mainline boost yet).
Because of boost-log API I have to create new Rostream object, using
Rcpp::Rcout is not an option.
The minimal code that directs log to R console once the library is loaded
looks like this:
#include <Rcpp/iostream/Rostream.h>
#include <boost/log/trivial.hpp>
#include <boost/log/sinks.hpp>
#include <boost/log/core.hpp>
// Called when the library is loaded and before dlopen() returns
void __attribute__ ((constructor)) test_load()
{
// initialize boost-log
// Construct the R sink
typedef
boost::log::sinks::synchronous_sink<boost::log::sinks::text_ostream_backend>
text_sink;
boost::shared_ptr< text_sink > pRSink = boost::make_shared< text_sink
>();
// Add a stream to write log to
pRSink->locked_backend()->add_stream(
boost::make_shared<Rcpp::Rostream>() );
// Register the sink in the logging core
boost::log::core::get()->add_sink(pRSink);
BOOST_LOG_TRIVIAL(info) << "R test library loaded";
}
// Called when the library is unloaded and before dlclose() returns
void __attribute__ ((destructor)) test_unload()
{
}
To make this code work I had to add #include "Rstreambuf.h" into
<Rostream.h> for the reasons described above.
I hope now it's more clear.
> Dirk
WBR,
Alexey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120513/e50a7142/attachment.html>
More information about the Rcpp-devel
mailing list