iPhone:将 NSLog 重定向到文件后,如何将其恢复到控制台?
我正在使用:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是取自
http://www.atomicbird.com/blog/2007/07 /code-quickie-redirect-nslog
This is taken from
http://www.atomicbird.com/blog/2007/07/code-quickie-redirect-nslog
这并没有明确回答您的问题,但如果您需要重定向的是 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.)