NSTask Tail -f Using objective C
Posted
by
Bach
on Stack Overflow
See other posts from Stack Overflow
or by Bach
Published on 2011-01-12T01:26:28Z
Indexed on
2011/01/14
13:53 UTC
Read the original article
Hit count: 245
I need to read the last added line to a log file, in realtime, and capture that line being added.
Something similar to Tail -f.
So my first attempt was to use Tail -f using NSTask.
I can't see any output using the code below:
NSTask *server = [[NSTask alloc] init];
[server setLaunchPath:@"/usr/bin/tail"];
[server setArguments:[NSArray arrayWithObjects:@"-f", @"/path/to/my/LogFile.txt",nil]];
NSPipe *outputPipe = [NSPipe pipe];
[server setStandardInput:[NSPipe pipe]];
[server setStandardOutput:outputPipe];
[server launch];
[server waitUntilExit];
[server release];
NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
NSLog (@"Output \n%@", outputString);
I can see the output as expected when using:
[server setLaunchPath:@"/bin/ls"];
How can i capture the output of that tail NSTask?
Is there any alternative to this method, where I can open a stream to file and each time a line is added, output it on screen? (basic logging functionality)
© Stack Overflow or respective owner