Home/Support/Support Forum/C++, printf() and flushing
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

C++, printf() and flushing

0 votes
I'm using the C++ sample from NET+OS 7.1.

When I use cout it works correctly during debugging (if I step over the statement the whole line is printed on the serial console).

When I use printf it doesn't print the whole line on the serial console. How can I make printf automatically flush its output?


From the root.cxx:

#ifndef NETOS_GNU_TOOLS
printf(..);
#else
cout << ..;
#endif

So the sample uses cout by default, but I need printf.
asked May 29, 2007 in NET+OS by compie Community Contributor (55 points)

Please log in or register to answer this question.

7 Answers

0 votes
this is odd. because cout shouldn't flush it's cache also. for flushing you can append "<< endl;" for cout and "\n" for printf
answered May 29, 2007 by DanielS New to the Community (2 points)
0 votes
You should be able to call \n as Daniel states to see the whole output. Also, if you're calling printf from multiple threads you'll need to mutex protect the calls to printf, otherwise they'll stomp on eachother.
answered May 29, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
In the sample code both printf and cout are using a end of line at the end. (\n and endl). So both should flush.

And how do you know that printf needs mutexes if I call it from multiple threads? Can I find that in the documentation somewhere?
answered May 30, 2007 by compie Community Contributor (55 points)
0 votes
I'm still having problems with flushing. I have a breakpoint on the first line in applicationStart() and this is what I get on the serial port:

IAM:Got AUTO-CONFIGURED IPv6 address FE80::240:9DFF:FE43:3597 on interface eth0:3
IAM:Got DHCP IPv4 address 1

I first have to printf() something myself before it continues with
"30.144.45.14 on interface eth0
Network IP configured."
answered May 30, 2007 by compie Community Contributor (55 points)
0 votes
I'm not aware of any place in the documentation that specifies this (and a quick search of the API reference confirms this). I know this, however, from speaking with Digi's Engineering team about a case a few months back.
answered May 30, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
Does this happen with one of the template apps or just your own custom app? If only on yours, what changes have you made?
answered May 30, 2007 by charliek Veteran of the Digi Community (408 points)
0 votes
It happens with the C++ sample.
The breakpoint is on the line

app_down.Name(app_name);
answered May 31, 2007 by compie Community Contributor (55 points)
...