• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: stdout redirect in xcode and not
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stdout redirect in xcode and not


  • Subject: Re: stdout redirect in xcode and not
  • From: "Wesley Smith" <email@hidden>
  • Date: Wed, 24 Dec 2008 00:42:52 -0600

Just to follow up, especially since it's hard to find precise info on
this subject on the interwebs.  The problem I was trying to solve was
how to direct stdout and stderr to an arbitrary memory buffer inside
an application.  In my case, I'm directing to to a console window and
a log file.

In the end, I had to use a polling method as I couldn't find any way
to get callbacks when a pipe has data to read off the read endpoint.

The setup looks like this:

FILE *readStream;
int spipe[2];

if(pipe(spipe) != 0) {
    // error
}

for(int n=0; n < 2; n++) {
    int f = fcntl(spipe[n], F_GETFL, 0);
    f |= O_NONBLOCK;
    fcntl(spipe[n], F_SETFL, f);
}

readStream = fdopen(spipe[0], "r");
dup2(spipe[1], fileno(stdout));

Then, you have to poll the readStrem FILE structure as follows:


       std::string str;
	int res = 0;

       fflush(stdout);
	do {
		char buffer[128];
		res = fread(buffer, 1, 127, readStream);
		buffer[res] = '\0';

		str.append(buffer);
	} while(res > 0);


Obviously, the method of collecting chunks of data from the FILE can
differ, but it is essential yo call fflush on stdout (or stderr as the
case may be).  I've found that not calling fflush results in nothing
being read by fread.

wes
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >stdout redirect in xcode and not (From: "Wesley Smith" <email@hidden>)
 >Re: stdout redirect in xcode and not (From: Tommy Nordgren <email@hidden>)
 >Re: stdout redirect in xcode and not (From: "Wesley Smith" <email@hidden>)
 >Re: stdout redirect in xcode and not (From: Jason Foreman <email@hidden>)
 >Re: stdout redirect in xcode and not (From: "Wesley Smith" <email@hidden>)

  • Prev by Date: re: How do I link with copied libs?
  • Next by Date: Fwd: stdout redirect in xcode and not
  • Previous by thread: Re: stdout redirect in xcode and not
  • Next by thread: Fwd: stdout redirect in xcode and not
  • Index(es):
    • Date
    • Thread