diff options
-rw-r--r-- | macosx/HBOutputRedirect.h | 31 | ||||
-rw-r--r-- | macosx/HBOutputRedirect.m | 47 |
2 files changed, 33 insertions, 45 deletions
diff --git a/macosx/HBOutputRedirect.h b/macosx/HBOutputRedirect.h index 116463e60..f689b8795 100644 --- a/macosx/HBOutputRedirect.h +++ b/macosx/HBOutputRedirect.h @@ -5,7 +5,7 @@ * Interface of class HBOutputRedirect. */ -#import <Cocoa/Cocoa.h> +#import <Foundation/Foundation.h> @protocol HBOutputRedirectListening <NSObject> @@ -27,19 +27,6 @@ * level. */ @interface HBOutputRedirect : NSObject -{ - /// Set that contains all registered listeners for this output. - NSMutableSet *listeners; - - /// Selector that is called on listeners to forward the output. - SEL forwardingSelector; - - /// Output stream (@c stdout or @c stderr) redirected by this object. - FILE *stream; - - /// Pointer to old write function for the stream. - int (*oldWriteFunc)(void *, const char *, int); -} + (id)stdoutRedirect; + (id)stderrRedirect; @@ -48,19 +35,3 @@ - (void)removeListener:(id <HBOutputRedirectListening>)aListener; @end - -/* Here is another technique to redirect stderr, but it is done at lower level - which also redirects NSLog() and other writes that are done directly to the - file descriptor. This method is not used by HBOutputRedirect, but should - be easy to implement if needed. Code is untested, but this is shows basic - idea for future reference. - - // Create a pipe - NSPipe *pipe = [[NSPipe alloc] init]; - - // Connect stderr to the writing end of the pipe - dup2([[pipe fileHandleForWriting] fileDescriptor], STDERR_FILENO); - - // Get reading end of the pipe, we can use this to read stderr - NSFileHandle *fh = [pipe fileHandleForReading]; -*/ diff --git a/macosx/HBOutputRedirect.m b/macosx/HBOutputRedirect.m index aa34b27d5..9856e840a 100644 --- a/macosx/HBOutputRedirect.m +++ b/macosx/HBOutputRedirect.m @@ -16,6 +16,24 @@ static HBOutputRedirect *g_stderrRedirect = nil; static int stdoutwrite(void *inFD, const char *buffer, int size); static int stderrwrite(void *inFD, const char *buffer, int size); +@interface HBOutputRedirect () +{ + /// Set that contains all registered listeners for this output. + NSMutableSet *listeners; + + /// Selector that is called on listeners to forward the output. + SEL forwardingSelector; + + /// Output stream (@c stdout or @c stderr) redirected by this object. + FILE *stream; + + /// Pointer to old write function for the stream. + int (*oldWriteFunc)(void *, const char *, int); +} + +@end + + @interface HBOutputRedirect (Private) - (id)initWithStream:(FILE *)aStream selector:(SEL)aSelector; - (void)startRedirect; @@ -28,25 +46,24 @@ static int stderrwrite(void *inFD, const char *buffer, int size); */ int stdoutwrite(void *inFD, const char *buffer, int size) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSData *data = [[NSData alloc] initWithBytes:buffer length:size]; - [g_stdoutRedirect performSelectorOnMainThread:@selector(forwardOutput:) withObject:data waitUntilDone:NO]; - [data release]; - [pool release]; - return size; + @autoreleasepool + { + NSData *data = [[NSData alloc] initWithBytes:buffer length:size]; + [g_stdoutRedirect performSelectorOnMainThread:@selector(forwardOutput:) withObject:data waitUntilDone:NO]; + [data release]; + } + return size; } -/** - * Function that replaces stderr->_write and forwards stderr to g_stderrRedirect. - */ int stderrwrite(void *inFD, const char *buffer, int size) { - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSData *data = [[NSData alloc] initWithBytes:buffer length:size]; - [g_stderrRedirect performSelectorOnMainThread:@selector(forwardOutput:) withObject:data waitUntilDone:NO]; - [data release]; - [pool release]; - return size; + @autoreleasepool + { + NSData *data = [[NSData alloc] initWithBytes:buffer length:size]; + [g_stderrRedirect performSelectorOnMainThread:@selector(forwardOutput:) withObject:data waitUntilDone:NO]; + [data release]; + } + return size; } @implementation HBOutputRedirect |