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: "Oliver Siemoneit" <email@hidden>
- Date: Thu, 17 Jan 2008 01:11:00 +0100
Hello Hamish,
thank you very much for your comment! Yes you are right: The output of my
script (or a shell command like ls) should be written to stdout which also
appears in the console window.
But that's strange: after calling a shell script with my code, I can get no
ouput in the console window anymore: it's broken. No logs form NSLog, no
stdout from shell scripts. Console output is simply dead.
But this also means: not only NSLog is broken but something else, too.
So I did some more testing: I took out all the code with NSPipe and
setStandardError and reran the code again. The same result! That means: the
console window gets broken by simply calling a shell script (myscript.sh)
instead of a shell command (e.g. ls). It has noting to do with the piping
stuff. So the correct title of my question to this mailing list would be
therefore: "Console output gets broken after call of a shell script (not a
shell command!) via NSTask".
Is there an explanation for this? Or am I doing something wrong?
Cheers,
Oliver
----- Original Message -----
From: "Hamish Allan" <email@hidden>
To: "Oliver Siemoneit" <email@hidden>
Cc: <email@hidden>
Sent: Wednesday, January 16, 2008 8:03 PM
Subject: Re: NSLog broken after call of NSTask and setStandardError
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