Redirect FooTypeSupport::print_data to stringstream

2 posts / 0 new
Last post
Offline
Last seen: 4 years 10 months ago
Joined: 01/26/2016
Posts: 11
Redirect FooTypeSupport::print_data to stringstream

Hi!

I'm trying to redirect the output of print_data into a stringstream. I wrote the following code:


Foo *myFoo = Foo::TypeSupport::create_data();

std::stringstream buffer;
std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
Foo::TypeSupport::print_data(myFoo);
std::string text = buffer.str(); // text will now contain "Bla\n"
std::cout.rdbuf(old);

Foo::TypeSupport::delete_data(status);

But the ouput of print_data is still written in the standard output.

Note that if I replace "Foo::TypeSupport::print_data(myFoo)" by "std::cout << "Print MyFoo;", the string "Print MyFoo" is correctly saved in my buffer stringstream.

 

Do you know how is printed the print_data output ? Using a printf? Something else?

 

Thank you for your help :-)

 

Lucie

 

ajimenez's picture
Offline
Last seen: 1 year 10 months ago
Joined: 09/29/2017
Posts: 21
Hi Lucie,
 
You are right, the output of TypeSupport::print_data() is written to the standard output.
 
As an alternative, you can use the method TypeSupport::data_to_string() , with the string you can redirect the output into a stringStream.
 
Example code: 
DDS_UnsignedLong size = 10;
char test [size];
DDS_PrintFormatProperty format;
retcode = testTypeSupport::data_to_string(&data_seq[i], test, size, format);
if (retcode != DDS_RETCODE_OK) {
    fprintf(stderr, "return data_to_string error %d\n", retcode);
} else {
    printf("Received data %s . Size %d\n", test, size);
}
 
Let me know if it suits your needs.
 
Best,
Antonio