Re: Standard Input/Output
Re: Standard Input/Output
- Subject: Re: Standard Input/Output
- From: Frode <email@hidden>
- Date: Wed, 21 Sep 2005 18:59:03 +0200
Hi!
2005-09-19 kl. 05.17 skrev Lachlan Cotter:
Thanks Tommy. I thought there might be some kind of wrapper for this
like NSPipe or NSStream or something like that. I couldn't find
anything in the documentation. Am I missing something?
I'm not aware of any utility classes for (or remiscenting of) FILE
pointers in Cocoa. Other classes that provide the same functionality
don't rely on POSIX or Standard C streams.
Anyway, here is a home-brewed method of redirecting NSLog() to a custom
output instead of "stderr" if that is what you want.
// NSFLog.m - redirection of format printing
// gcc NSFLog.m -o NSFLog.exe -framework Cocoa -DTEST
#import <Foundation/NSObjCRuntime.h> // NSLog
#import <Foundation/NSString.h> // NSString
#import <Foundation/NSAutoreleasePool.h> // NSAutoreleasePool
#include <stdarg.h> // va_list, va_begin, va_end
void NSFLog(FILE *fp, NSString *format, ...);
void NSFLogv(FILE *fp, NSString *format, va_list argList);
#ifdef TEST
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFLog(stdout, @"Test this application with:");
NSFLog(stdout, @"\"%s\"", argv[0]);
NSFLog(stdout, @"\"%s > myout.log\"", argv[0]);
NSFLog(stdout, @"\"%s &> myout.log\"", argv[0]);
NSFLog(stdout, @"stdout> Hello world");
NSFLog(stdout, @"stdout> argc = %d", argc);
NSFLog(stderr, @"stderr> Hello world");
NSFLog(stderr, @"stderr> argc = %d", argc);
[pool release];
return 0;
}
#endif
void NSFLog(FILE *fp, NSString *format, ...) {
va_list argList;
va_start(argList, format);
NSFLogv(fp, format, argList);
va_end(argList);
}
void NSFLogv(FILE *fp, NSString *format, va_list argList) {
int old_fd = dup(fileno(stderr));
int new_fd = dup2(fileno(fp), fileno(stderr));
NSLogv(format, argList);
(void)dup2(old_fd, fileno(stderr));
}
Hope this helps!
Regards
Roger
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden