Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have posted this on different forums and here, I KNOW what is the issue ( no need to repeat that here) , but I really do not have a solution.

I am using cout to track code process, works fine.
I am using perror to identify code erros - where applicable.
Even when perror returns "Invalid argument" it helps with debugging.

perror / cerr DO NOT follow the code flow.

According to responses I have received both cout and perror/cerr are BUFFERED , but perror/cerr actual outputs to console (terminal) is NEVER put in same "place" , but mostly at the end of the code run.

perror/cerr are output in red - that helps , but I really would like to have them in correct code sequence.

I DO NOT KNOW HOW TO buffer/ unbuffer perror/ cerr so it does output in correct code sequence.


I have been putting off the solution for long time, now I am open to suggestions HOW to fix this,

Attached code and output demonstrate the issue.

<pre>	if (FileDescriptor_socket) {
		cout << "Synchronize  @line " << __LINE__ << endl;
		perror("socket allocated SUCCESS");
		cerr << "socket allocated SUCCESS" << endl;
		cout << "Synchronize  @line " << __LINE__ << endl;
		return 0;


STOP  @line 1437
Synchronize  @line 1445

I like to see perror/cerr here 


Synchronize  @line 1448
STOP  @line 744
SUCCESS SERVER.AllocateSocket() 
function Socket_RX_Composite
STOP  @line 749
TASK @line 753
function Socket_RX_Composite
STOP  @line 755
Not here 
socket allocated SUCCESS: Success
socket allocated SUCCESS


What I have tried:

Posted in efferent forums, this is just 30 characters fill as required.
Posted
Updated 21-Jan-20 6:37am
Comments
Richard MacCutchan 21-Jan-20 12:01pm    
When I run the above code I get the following output:
Synchronize  @line 31
socket allocated SUCCESS: No error
socket allocated SUCCESS
Synchronize  @line 34

So where is all the other text generated?

1 solution

In addition to using flush, have you considered using strerror(), instead of perror()

e.g.
C++
#include <cstring>
#include <cerrno>
#include <iostream>

void perror_cpp(const char *message)
{
    std::cerr << message << ": " << strerror(errno) << std::endl << std::flush;
}



You could add you colorizing in there, too, to clean up your debug code, and is simple enough it could be marked inline.
 
Share this answer
 
v2
Comments
Vaclav_ 21-Jan-20 12:40pm    
I am sorry, since this has been a long time issue, I totally forgot about "flush". Yes, I did use it and it did not help. But I'll try it again.
I'll give strerror a try too. Thanks
k5054 21-Jan-20 12:47pm    
I haven't tried this, but maybe turn off buffering for both cerr and cout: more info here https://stackoverflow.com/a/156413
Vaclav_ 21-Jan-20 18:21pm    
I feel this may shed some light on the issue.
Here are quotes from elsewhere:
"by default cout is buffered " and "new line at the end of cout will clear the buffer"
With that - the issue is NOT with cout anyway , but with cerr / perror.

I can add endl or /n to cerr or add flush to whole cerr line.

Not sure how to "add" such to perror - since it prints the optional message first then the actual text of the "errno" .
k5054 21-Jan-20 21:21pm    
well, you could fflush(stderr).
Or you could use the code snippet above. sterror() returns the error string associated with the current errno, so my perror_cpp duplicates the functionality of perror(), but sends the output through std::cerr. Note that in the code given, the final << std::flush probably isn't strictly necessary, since std::endl is supposed to do a flush, too.
Vaclav_ 21-Jan-20 23:04pm    
SOLVED
Purfect...perror_cpp prints EXACTLY where it does most good.
Tracking socket behaviour in REAL time!
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900