Re: NSLog broken after call of NSTask and setStandardError
Re: NSLog broken after call of NSTask and setStandardError
- Subject: Re: NSLog broken after call of NSTask and setStandardError
- From: "Hamish Allan" <email@hidden>
- Date: Wed, 16 Jan 2008 19:03:40 +0000
Oliver,
Not sure why there's a disparity between your script and /bin/ls, but
they should both be writing to stdout, whereas your NSPipe is
attempting to read from stderr. Presumably the "Hello" and the output
of /bin/ls are being written to console without NSLog-style
timestamps?
Hamish
On Jan 15, 2008 9:38 PM, Oliver Siemoneit <email@hidden> wrote:
> Hello,
> I'm new to cocoa and obj c programming. Thanks to such great mailling lists
> like this one here, I have learned a lof during the last weeks. However here
> is something I cannot cope with:
>
> Code:
> --------
>
> I use this cocoa method to execute shell commands and shell scripts form my
> cocoa app:
> - (void)executeShellScript:(NSString*)path withArgs:(NSArray*)args {
> NSLog(@"Testing NSLog: Entry");
> NSString *fullpath = [path stringByExpandingTildeInPath];
> NSFileManager *fm = [NSFileManager defaultManager];
> if ([fm fileExistsAtPath:fullpath]) {
> NSTask *shellProcess = [[NSTask alloc] init];
> NSPipe *errorPipe = [[NSPipe alloc] init];
> [shellProcess setLaunchPath:fullpath];
> [shellProcess setArguments:args];
> [shellProcess setStandardError:errorPipe];
> [shellProcess launch];
> [shellProcess waitUntilExit];
> NSData *result = [[errorPipe fileHandleForReading]
> readDataToEndOfFile];
> if ([result length]>1) {
> NSString *error = [[NSString alloc] initWithData:result
> encoding:NSASCIIStringEncoding];
> NSLog(error); //Normally I would show an error dialog with a
> NSTextView here
> [error release];
> }
> [shellProcess release];
> }
> else {
> NSString *error = [fullpath stringByAppendingString:@": File does
> not exist"];
> NSLog(error); //Normally I would show an error dialog with a
> NSTextView here
> }
> NSLog(@"Testing NSLog: Exit");
> }
>
>
> Problem:
> -------
>
> At the first glance: no problems! I can call shell commands, shell scripts
> and retrieve stderr from the shell and display this stuff in cocoa.
> The probelm is:
> - When I call above method e.g. with [self
> executeShellScript:@"/Users/siemoneit/Documents/LocationManager/heim/activate.sh"
> withArgs:args];, .i.e. I try to execute a shell script, NSLog gets broken,
> only outputs in the console "Testing NSLog: Entry" + "Hello" (=some ouput of
> the shell script via echo) and nothing more. Further NSLog ouput is lost
> totally. NSLog remains broken. You have to restart the app.
> This is the shell script I call:
> #!/bin/bash
> echo Hello
> exit 0
>
> - When I call above method e.g. with [self executeShellScript:@"/bin/ls"
> withArgs:args]; i.e. with a shell command, everything works fine! NSLog
> doesn't get broken. Everything goes smoothly and nicely. The output is the
> console is as expected: "Testing NSLog:Entry" + the command output +
> "Testing NSLog: Exit". NSLog doesn't get broken.
> What am I missing? What am I doing wrong? This drives me nuts...
> Therefore: Any hint is highly appreciated!
> Many thanks,
> Oliver
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden