Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Objective-c > Re: Redirecting...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 6 Topic 1003 of 1055
Post > Topic >>

Re: Redirecting standard output to a file

by Michael Hopkins <michael.hopkins@[EMAIL PROTECTED] > Jul 10, 2008 at 08:03 PM

On 2008-07-10 17:39:03 +0100, Gregory Weston <uce@[EMAIL PROTECTED]
> said:

> In article
> <2008071016194875249-michaelhopkins@[EMAIL PROTECTED]
>,
>  Michael Hopkins <michael.hopkins@[EMAIL PROTECTED]
> wrote:
> 
>> On 2008-07-10 13:56:10 +0100, Gregory Weston <uce@[EMAIL PROTECTED]
> said:
>> 
>>> In article
>>> <2008071010401816807-michaelhopkins@[EMAIL PROTECTED]
>,
>>> Michael Hopkins <michael.hopkins@[EMAIL PROTECTED]
> wrote:
>>> 
>>>> Hi all
>>>> 
>>>> I have a question about using the cl***** in Cocoa/GNUstep to
redirect
>>>> standard output to a file without too much hassle or re-writing of
>>>> code.  I have cl***** that print themselves to standard output using
>>>> printf() internally when sent messages like this:
>>>> 
>>>> 	[ someObject show ];
>>>> 
>>>> ..but would like to be able to redirect this behaviour within code
and
>>>> possibly in response to user input.  Something like...
>>>> 
>>>> 	[ outputFile appendedWith:[ someObject show ]];
>>>> 
>>>> I suspect that NSPipe and/or NSFileHandle might be involved but
welcome
>>>> any tips.
>>> 
>>> I think what I'd really recommend is replacing [someObject show]; with
>>> NSString* desc = [someObject description]); and then simply write desc
>>> to any stream when you want. Remove from the cl***** the knowledge
(aka
>>> brittleness) of what's going to be done with their description string.
>>> 
>>> Alternatively, if show is already just a wrapper around description,
I'd
>>> change it to take an argument indicating the output stream, and you
can
>>> use whatever type you want there as long as it works for you.
>> 
>> Thanks for the input, Gregory
>> 
>> The 'show' method is implemented directly as printf() or uses C
>> functions in a separate library that use printf(),
> 
> That last bit's the part that makes it tough. You're pretty much stuck
> using C's freopen(). I think the safest way to turn it off is to
> fclose(stdout) followed by fdopen() to get it back to its default state.
> Not sure if explicitly associating it with /dev/tty would be
> semantically the same.
> 
> 
>> Was hoping to use Cocoa/GNUstep goodness to make it a simple task.  Any
>> other ideas short of re-implementing the functions to use fprintf() and
>> parameterising the file to send to?
> 
> See, the thing is, that question is dangerously close to "Any other
> ideas short of doing it the right way?" Sometimes a little pain now
> byp***** a lot of pain tomorrow. Ultimately, redirecting stdout is a
> workaround for the brittleness of the original design (some of which,
> apparently, is beyond your control).
> 
> 
>> Would overriding the 'description'
>> method to call [ self show ] achieve this result?
> 
> Nope. The description method is supposed to return an NSString* that
> gives a (hopefully meaningful to someone) text representation of the
> object. It really shouldn't have any side effects such as writing to a
> file. The way you've described things, 'show' could invoke 'description'
> meaningfully, but not vice-versa.

Thanks Gregory and Sherman for the thoughts.

M

-- 
______________________________________________

    Hopkins Research      Touch the Future
______________________________________________
 




 6 Posts in Topic:
Redirecting standard output to a file
Michael Hopkins <micha  2008-07-10 10:40:18 
Re: Redirecting standard output to a file
Gregory Weston <uce@[E  2008-07-10 08:56:10 
Re: Redirecting standard output to a file
Michael Hopkins <micha  2008-07-10 16:19:48 
Re: Redirecting standard output to a file
Sherman Pendley <spamt  2008-07-10 11:41:41 
Re: Redirecting standard output to a file
Gregory Weston <uce@[E  2008-07-10 12:39:03 
Re: Redirecting standard output to a file
Michael Hopkins <micha  2008-07-10 20:03:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 6:51:23 CST 2008.