NSTask +在任务中调用 NSLog 会导致将消息重复打印到控制台

发布于 2024-11-07 11:12:17 字数 891 浏览 3 评论 0原文

我有一个调用 NSTask 的应用程序(我已经编写了 NSTask 和应用程序代码),并且 NSTask 在我想要将一行写入控制台的地方调用 NSLog。

问题是我看到来自 NSTask 的控制台消息,然后我看到调用进程输出的相同消息,带有双标头......

5/16/11 5:50:01 PM  theNSTask[7934] BLAH BLAH BLAH 
[0x0-0x256256].com.someid[7505] 2011-05-16 17:50:01.708 theNSTask[7934:903] BLAH BLAH BLAH

读取所需的输出非常令人困惑(BLAH BLAH BLAH)。有没有一个神奇的设置可以解决这个问题?

谢谢,

--汤姆

NSTask* task = [[NSTask alloc] init];
NSString* path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"theNSTask"];
[task setLaunchPath:path];
NSMutableArray* arguments = [NSMutableArray array];
// get the dict as base64 string (start with binary plist):
NSString* base64Dict = [[self class] base64FromDictionary:message];
    [arguments addObject:base64Dict];
    [task setArguments:arguments];
[task launch];

[self.runningTasks addObject:task];
[task release];

I have a app that calls an NSTask, (I have written the NSTask and App code) and the NSTask calls NSLog at places where I want a line written out to the console.

Problem is that I see the console message from the NSTask, then I see the same message output by the calling process, with a double header...

5/16/11 5:50:01 PM  theNSTask[7934] BLAH BLAH BLAH 
[0x0-0x256256].com.someid[7505] 2011-05-16 17:50:01.708 theNSTask[7934:903] BLAH BLAH BLAH

Super confusing just to read the desired output (BLAH BLAH BLAH). Is there a magic setting that fixes this problem?

Thanks,

--Tom

NSTask* task = [[NSTask alloc] init];
NSString* path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"theNSTask"];
[task setLaunchPath:path];
NSMutableArray* arguments = [NSMutableArray array];
// get the dict as base64 string (start with binary plist):
NSString* base64Dict = [[self class] base64FromDictionary:message];
    [arguments addObject:base64Dict];
    [task setArguments:arguments];
[task launch];

[self.runningTasks addObject:task];
[task release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2024-11-14 11:12:17

我的猜测是,该任务将消息直接记录到控制台及其标准输出,默认情况下,它与父进程的标准输出相同的流,您的应用程序将其发送到控制台。

如果这是真的,那么您应该能够通过将任务的标准输出设置为 /dev/null 来修复它,如下所示:

NSFileHandle *nullFileHandle = [NSFileHandle fileHandleWithNullDevice];
[task setStandardOutput:nullFileHandle];
[task setStandardError:nullFileHandle];

My guess is that the task is logging the messages directly to the console and also to its standard output, which by default is the same stream as the parent process's standard output, which your app is sending to the console.

If that's true, then you should be able to fix it by setting the task's standard output to /dev/null like:

NSFileHandle *nullFileHandle = [NSFileHandle fileHandleWithNullDevice];
[task setStandardOutput:nullFileHandle];
[task setStandardError:nullFileHandle];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文