Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Capturing Standard Output?

Capturing Standard Output?
Thread Tools
Mac Boy
Junior Member
Join Date: Dec 2000
Location: Boston, MA USA
Status: Offline
Reply With Quote
Nov 24, 2002, 01:11 AM
 
Is there any way for a Cocoa application to capture the standard output of itself? Specifically, I'd like to redirect any calls made to NSLog or printf to appear in a log window instead of in the Console?

I'm working with some cross-platform OpenGL code which uses standard printf calls to display useful information. Under Windows, the output is displayed in a DOS-looking window. Under OS X, the user would need to open up the Console, as far as I can tell. I would rather not modify the original codebase if possible.

One possible solution would be to somehow run the code as an NSTask, but I'm not sure how that would be possible as the code uses OpenGL in a window and I already have in incorporated into an OpenGLView.

Any ideas? Thanks.
( Last edited by Mac Boy; Nov 24, 2002 at 12:15 PM. )
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Nov 24, 2002, 01:15 PM
 
Mmm, that's gonna be tough just using printf. You could always pipe the output but that can get kludgy and messy.

The way apps (like Quake) usually print stuff to a custom console is to replace printf with something else. If I were you I'd go replace all calls to printf with a custom function, but I guess that's undesirable. To make it a bit more pleasing you could always not include stdio and define printf itself in your program.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
Mactoid
Grizzled Veteran
Join Date: Sep 2000
Location: Springfield, MA
Status: Offline
Reply With Quote
Nov 24, 2002, 05:05 PM
 
Why not just create a new file or pipe to write the output in, then use "dup2(fd, 1)" to redirect your programs stdout to your new file. Then you can just display the contents of that file in your log window.
We hope your rules and wisdom choke you / Now we are one in everlasting peace
-- Radiohead, Exit Music (for a film)
     
Mac Boy  (op)
Junior Member
Join Date: Dec 2000
Location: Boston, MA USA
Status: Offline
Reply With Quote
Nov 24, 2002, 10:43 PM
 
I have never used the dup2 function before. Is my assumption correct that the "1" is the description for standard output (and is this always the case?), and that by passing a new descriptor, 'fd', it will copy all of the content written into one into the other?

Sounds like this is heading in the right direction, thanks a lot.
- Ross
     
Mactoid
Grizzled Veteran
Join Date: Sep 2000
Location: Springfield, MA
Status: Offline
Reply With Quote
Nov 25, 2002, 12:10 AM
 
Originally posted by Mac Boy:
I have never used the dup2 function before. Is my assumption correct that the "1" is the description for standard output (and is this always the case?), and that by passing a new descriptor, 'fd', it will copy all of the content written into one into the other?

Sounds like this is heading in the right direction, thanks a lot.
- Ross
The man page is not very helpful, but yes. IIRC from my Operating Systems class, stdin, stdout, and stderr default to 0, 1, and 2 respectively.

Here's a simple example I put together before my original post to make sure I wasn't just making stuff up
Code:
#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main() { int fd; printf("to console\n"); fd = open("test.out", O_WRONLY|O_CREAT, S_IRWXU); if (fd != -1) { dup2(fd, 1); close(fd); printf("to file\n"); } else { printf("oops\n"); } return 0; }
We hope your rules and wisdom choke you / Now we are one in everlasting peace
-- Radiohead, Exit Music (for a film)
     
Mac Boy  (op)
Junior Member
Join Date: Dec 2000
Location: Boston, MA USA
Status: Offline
Reply With Quote
Nov 26, 2002, 08:17 PM
 
Well thanks for everyone's help. I put together a little reusable LogController class. Just connect it to a NSTextView and Bam! you have yourself a log of all Standard Output. You can also easily modify the class (in the header) to capture Standard Error instead (which includes calls to NSLog).

I figure this is a great resource for developers when they send out apps for beta testing. If an exception occurs... the user immediately knows to report an error to you, rather than hunting around in the Console.

Download Here

Enjoy,
- Ross
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 11:58 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,