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
______________________________________________


|