NSLog goes to system console after running NSTask
NSLog goes to system console after running NSTask
- Subject: NSLog goes to system console after running NSTask
- From: Jerry Krinock <email@hidden>
- Date: Fri, 06 May 2011 18:22:15 -0700
In my Cocoa app, I create a shell script as a string, write it to a temporary file, and execute it with an NSTask, without waiting. (Why? See [1] below.)
After the shell script is done executing, any further stderr (e.g. NSLog) from my app no longer appears in the Xcode 3 debugger but instead shows in the system Console. Why does this happen and how can I fix it?
One of the commands in my script has its stderr redirected to /dev/null, but the problem is the same if I eliminate that.
Thank you,
Jerry Krinock
Here's the code:
NSString* cmdPath = [[NSFileManager defaultManager] tempFilePath] ;
// Note: -tempFilePath is from my own category.
// It is based on NSTemporaryDirectory()
NSNumber* octal755 = [NSNumber numberWithUnsignedLong:0755] ;
NSDictionary* attrs = [NSDictionary dictionaryWithObjectsAndKeys:
octal755, NSFilePosixPermissions,
nil] ;
NSString* cmd = [NSString stringWithFormat:
/* Omitting the gory details of the format string here,
it results in a multi-line string like this example:
#!/bin/sh
PLIST_PATH="/Users/jk/Library/LaunchAgents/MyLabel.plist"
/bin/launchctl unload -wF $PLIST_PATH
TEXT=`cat $PLIST_PATH`
rm $PLIST_PATH
sleep 1
echo $TEXT > $PLIST_PATH
NLINES=`launchctl list MyLabel 2>/dev/null | wc -l`
if [ "$NLINES" -eq 0 ] ; then
/bin/launchctl load -wF $PLIST_PATH
fi
rm "/path/to/this/script"
*/
NSData* data = [cmd dataUsingEncoding:NSUTF8StringEncoding] ;
ok = [[NSFileManager defaultManager] createFileAtPath:cmdPath
contents:data
attributes:attrs] ;
task = [[NSTask alloc] init] ;
[task setLaunchPath:cmdPath] ;
[task launch] ;
[task release] ;
[1] Why am I doing this? I have some code in a private framework that sometimes executes in my Cocoa app, and sometimes executes in my Cocoa Foundation Background Helper Tool, which is launched by launchd. I've found that, quite often, the launchd agent, which is triggered by a WatchPath, will "just stop working". I think there's a bug in launchd. But, anyhow, to work around the problem, when I think that it may have stopped working, I execute that script which unloads the agent, deletes its file, waits 1 second, re-creates the file and then reloads it. The reason why I don't do this in Cocoa is because when launchctl is asked to unload an agent, it waits until any processes launched by the agent have exitted. So, if my process was in fact launched by that agent, this results in deadlock. Actually, the deadlock is broken after a timeout (default 25 seconds), when launchctl *kills* my process. By spinning this off into a separate task just before my tool is about to exit, deadlock is avoided.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden