could show me how redirect stdout nstextview?
and whether info print nslog belong std?
thanks
the code below uses dup2
plug stdout onto write-end of nspipe
object. read-end observed gcd dispatch source, reads data pipe , appends textview.
nspipe* pipe = [nspipe pipe]; nsfilehandle* pipereadhandle = [pipe filehandleforreading]; dup2([[pipe filehandleforwriting] filedescriptor], fileno(stdout)); 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; { errno = 0; readresult = read([pipereadhandle filedescriptor], data, 4096); } while (readresult == -1 && errno == eintr); if (readresult > 0) { //appkit ui should updated 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(@"...")
not output stdout
though - prints stderr
. if want redirect textview, change
dup2([[pipe filehandleforwriting] filedescriptor], fileno(stdout));
to
dup2([[pipe filehandleforwriting] filedescriptor], fileno(stderr));
Comments
Post a Comment