iPhone:将 NSLog 重定向到文件后,如何将其恢复到控制台?

发布于 2024-08-18 17:08:18 字数 549 浏览 9 评论 0原文

我正在使用:

#if TARGET_IPHONE_SIMULATOR == 0
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif

.. 将 NSLog 重定向到一个文件,顺便说一下,它效果很好。

我想将日志记录到我的应用程序的用户可以打开和关闭的文件中。那么有人知道我如何将 NSLog/stderr 返回 重定向到控制台吗?

谢谢!

I'm using:

#if TARGET_IPHONE_SIMULATOR == 0
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif

.. to redirect NSLog to a file, which works great incidentally.

I want to make logging to file something the user of my app can turn on and off.. so does anyone know how I go about redirecting NSLog/stderr back to the console?

Thanks!

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

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

发布评论

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

评论(2

错爱 2024-08-25 17:08:18

这是取自
http://www.atomicbird.com/blog/2007/07 /code-quickie-redirect-nslog

// Save stderr so it can be restored.
int stderrSave = dup(STDERR_FILENO);

// Send stderr to our file
FILE *newStderr = freopen("/tmp/redirect.log", "a", stderr);

NSLog(@"This goes to the file");

// Flush before restoring stderr
fflush(stderr);

// Now restore stderr, so new output goes to console.
dup2(stderrSave, STDERR_FILENO);
close(stderrSave);

// This NSLog will go to the console.
NSLog(@"This goes to the console");

This is taken from
http://www.atomicbird.com/blog/2007/07/code-quickie-redirect-nslog

// Save stderr so it can be restored.
int stderrSave = dup(STDERR_FILENO);

// Send stderr to our file
FILE *newStderr = freopen("/tmp/redirect.log", "a", stderr);

NSLog(@"This goes to the file");

// Flush before restoring stderr
fflush(stderr);

// Now restore stderr, so new output goes to console.
dup2(stderrSave, STDERR_FILENO);
close(stderrSave);

// This NSLog will go to the console.
NSLog(@"This goes to the console");
烦人精 2024-08-25 17:08:18

这并没有明确回答您的问题,但如果您需要重定向的是 NSLog 的输出,那么我会考虑使用 NSLog() 周围的包装器,并根据以下标志决定是发送到文件还是发送到常规 NSLog/stderr您保留用户可以控制的内容。

(对我来说,从根本上重定向 stderr 流来完成此操作是一个令人惊讶的设计 - 并且使用 NSLog 级别之上的包装器,如果您愿意,您可以轻松选择将输出发送到这两个地方。)

This doesn't explicitly answer your question, but if all you need to redirect is NSLog's output, then I would consider using a wrapper around NSLog() and deciding there whether to send to file or to regular NSLog/stderr based on a flag that you keep around that the user can control.

(It feels like a surprising design to me to fundamentally redirect the stderr stream to accomplish this-- and with a wrapper above the NSLog level, you could just as easily choose to send the output to both places if you ever wanted.)

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