Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » cout and perror messages in wrong sequence (cout perror synchronization )
cout and perror messages in wrong sequence [message #1818967] Fri, 03 January 2020 11:31 Go to next message
Eclipse UserFriend
Need to synchronize cout and perror inTO correct code sequence.

I have been searching for an answer to this debugging dilemma.
I am using BOTH cout and perror to trace thru my code.

Both are output to Eclipse console in correct sequence when run on single core processor ( Raspberry Pi) so the opinion of another forum goes.

The messages outputs are hopelessly garbled / out of sequence when I run C++ code on 4 core , local , processor. Apparently cout and perror are run on different cores and not in the sequence expected.
I cannot argue with this multicore (solution) opinion -, but it seems odd.

I like to use perror since it outputs error messages , when available, so suggestion to ditch perror is not a solution. So is usage of cerr.

I did try suggested ios_base::sync_with_stdio(); , it changed the outputs just placing perror messages ahead of cout messages - again in one block ( see attached ).

The attached code snippet and full output of the app should clarify what I am against.

This is "under construction " code posted with intend to demonstrate the issue, I am not soliciting comments on my code style.



int result = -1;
ios_base::sync_with_stdio();
cout << "Server waits for a client request." << endl;
C_SOCKET SERVER;
if(SERVER.AllocateSocket() == 0 )
{
ios_base::sync_with_stdio();
cout << "STOP @line " << __LINE__ << endl;
ios_base::sync_with_stdio();
perror (" SUCCESS SERVER.AllocateSocket()");
}
else
perror (" FAILED SERVER.AllocateSocket()");












Server waits for a client request.
TRACE TODO Auto-generated constructor stub
TASK @line 49
function C_SOCKET
STOP @line 51
TRACE allocate a socket
TASK @line 252
function AllocateSocket
STOP @line 254

perror message should be here
socket allocated SUCCESS: Success
perror message should be here




STOP @line 114
TRACE set the connection parameters (who to connect to)
TASK @line 176
function Bind
STOP @line 178
TRACE bind socket to port 1 of the first available local bluetooth adapter
TASK @line 191
function Bind
STOP @line 193
TRACE bind socket to port 1 of the first available local bluetooth adapter
TASK @line 217
function Bind
STOP @line 219
TRACE put socket into listening mode - single queue
TASK @line 136
function SetupListen
STOP @line 138
TRACE STOP
TASK @line 133
function main
STOP @line 135
TRACE accept one connection - single queue
TASK @line 114
function SetupAccept
STOP @line 116

block of perror messages AFTER the run stops
DOES NOT BELONG HERE

socket allocated SUCCESS: Success
SUCCESS SERVER.AllocateSocket(): Invalid argument
bind to local SUCCESS: Invalid argument
SUCCESS SERVER.Bind(): Invalid argument
set into listen mode SUCCESS: Invalid argument
SUCCESS SERVER.SetupListen(): Invalid argument
Re: cout and perror messages in wrong sequence [message #1818975 is a reply to message #1818967] Fri, 03 January 2020 17:59 Go to previous messageGo to next message
Eclipse UserFriend
cout (stdout) and cerr (stderr) are separate process streams that are being buffered by Eclipse.
ios_base::sync_with_stdio() syncs stdout and cout within your program.
It does not sync stdout and stderr with each other.

#include <iostream>
#include <sstream>
using namespace std;

int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!

	std::stringstream ss;
	ss << 100 << ' ' << 200;
	std::string txt = ss.str();
	cout << txt << endl;

	for (int i=1; i<6; i++) {
		cout << "Line " << i << endl ; // << flush;
		cerr << "Error #" << i << endl;; // << flush;
	}
	cout << "All DONE!" << endl;
	return 0;
}

Output to Eclipse console is buffered:
index.php/fa/37077/0/

Output to a terminal is not buffered:
index.php/fa/37078/0/

I don't think there is any way to get Eclipse to tie them together.
If you want to run in the console then write a perror-like routine that sends output to cout.

[Updated on: Fri, 03 January 2020 18:03] by Moderator

Re: cout and perror messages in wrong sequence [message #1819006 is a reply to message #1818975] Sat, 04 January 2020 09:46 Go to previous messageGo to next message
Eclipse UserFriend
Thanks David.
Two follow up questions.
Call me dense , but how did you accomplish the buffering ?

Still do not get why it works fine running on remote hardware - Raspberry Pi .
The application is cross-complied AKA different complier / linker etc. , different CPU (single core ) and uses TCF.
The Eclipse console is then via TCF Debug Process Terminal .




Re: cout and perror messages in wrong sequence [message #1819018 is a reply to message #1819006] Sat, 04 January 2020 18:42 Go to previous message
Eclipse UserFriend
There are three basic streams associated with Linux processes: stdout, stderr, and stdin.
Every Linux process has them.
They are effectively file streams.
https://www.howtogeek.com/435903/what-are-stdin-stdout-and-stderr-on-linux/
There could be others depending on the program.
That's how I/O to files is done.

When Eclipse runs your program it

  • starts your program as a process and
  • connects to these streams and buffers stdout and stderr
  • the buffering is done within Eclipse

Don't confuse this with buffering cout and stdout within a C++ program.

When you are running in the Raspberry, the communication is via a serial port.
That's another device in Linux.
You are communicating with the device.

Eclipse TCF (Target Communication Framework) is through the Terminal View.

This is independent of the Eclipse console.
https://marketplace.eclipse.org/content/tm-terminal
When it is started, it can connect to different things.
For the Raspberry, it connects to your serial port (which could be a USB connection).


The displays look similar but are two different things.
The image labelled "Output to terminal" above is from a Terminal View.

Some examples (Local, Serial, and SSH)
index.php/fa/37084/0/
index.php/fa/37085/0/
index.php/fa/37086/0/

[Updated on: Sat, 04 January 2020 18:51] by Moderator

Previous Topic:Archiving / duplicating / copying entire Project workspace?
Next Topic:Cannot find Path & Symbols
Goto Forum:
  


Current Time: Wed Jun 18 12:03:57 EDT 2025

Powered by FUDForum. Page generated in 0.04804 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top