How to redirect STDOUT to a NSTextView?

后端 未结 3 805
旧巷少年郎
旧巷少年郎 2021-02-15 14:13

Could anybody show me how to redirect the Stdout to a NSTextView?

and whether the info print by NSLog belong to the std?

Thanks

3条回答
  •  太阳男子
    2021-02-15 14:20

    The code below uses dup2 to plug stdout onto the write-end of an NSPipe object. The read-end is observed with a GCD dispatch source, that reads data from the pipe and appends it to a textview.

    NSPipe* pipe = [NSPipe pipe];
    NSFileHandle* pipeReadHandle = [pipe fileHandleForReading];
    dup2([[pipe fileHandleForWriting] fileDescriptor], fileno(stdout));
    dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, [pipeReadHandle fileDescriptor], 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
    dispatch_source_set_event_handler(source, ^{
        void* data = malloc(4096);
        ssize_t readResult = 0;
        do
        {
            errno = 0;
            readResult = read([pipeReadHandle fileDescriptor], data, 4096);
        } while (readResult == -1 && errno == EINTR);
        if (readResult > 0)
        {
            //AppKit UI should only be updated from the main thread
            dispatch_async(dispatch_get_main_queue(),^{
                NSString* stdOutString = [[NSString alloc] initWithBytesNoCopy:data length:readResult encoding:NSUTF8StringEncoding freeWhenDone:YES];
                NSAttributedString* stdOutAttributedString = [[NSAttributedString alloc] initWithString:stdOutString];
                [self.logView.textStorage appendAttributedString:stdOutAttributedString];
            });
        }
        else{free(data);}
    });
    dispatch_resume(source);
    

    NSLog(@"...") does not output to stdout though - It prints to stderr. If you want to redirect that into your textview, change

    dup2([[pipe fileHandleForWriting] fileDescriptor], fileno(stdout));
    

    to

    dup2([[pipe fileHandleForWriting] fileDescriptor], fileno(stderr));
    

提交回复
热议问题