将 iPhone 上的 stderr 写入文件和控制台
我按照此处答案中的建议进行重定向iOS 设备上的 NSLog 输出到文件,效果很好。问题是它不再显示在设备的控制台中。我真正想要的是一种将 stderr 流连接到控制台和文件的方法。有谁知道如何做到这一点?
I'm following the suggestion in the answer here for redirecting NSLog output on an iOS device to a file, which works great. The problem is that it no longer shows up in the console on the device. What I'd really like is a way to tee the stderr stream to both the console and the file. Does anyone have an idea how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在另一个线程上找到了一个可接受的答案(NSLog() 到控制台和文件)。
那里提供的解决方案是仅在未检测到调试器的情况下重定向到文件,如下所示:
感谢 Sailesh 的回答。
I found an acceptable answer on another thread (NSLog() to both console and file).
The solution provided there is to only redirect to a file if a debugger is not detected, like this:
Thanks to Sailesh for that answer.
一旦
freopen()
文件描述符,您就可以从中读取数据并按照您的意愿处理数据。 本文中的一些想法将对您有用。您可以将其写回到标准输出,或者尝试直接写入
/dev/console
。我从未尝试过在 iPhone 上打开/dev/console
,但我猜尽管在沙箱之外,但还是有可能的。我不确定应用程序审核流程将如何处理它。Once you
freopen()
the file descriptor, you can read from it and do as you please with the data. Some ideas from this will be useful to you.You could either write it back out to stdout, or try to write directly to
/dev/console
. I've never tried to open/dev/console
on an iPhone, but I'm guessing it's possible despite being outside of the sandbox. I'm not sure how the app review process will treat it.或者您可以重定向到 TCP 套接字并在远程 telnet 客户端上查看。这样就不需要 XCode 了!
基本上:
创建一个调用 Obj-C 静态方法的标准 C 函数:
静态 Obj-C 方法:
然后在您的 .pch 文件中,添加以下行来覆盖 NSLog()
当然,需要更多详细信息来处理 TCP 套接字。工作源代码可在此处获取:
https://github.com/driedler/iOS-TCP-Logger/wiki/About
Or you can redirect to a TCP socket and view on a remote telnet client. No need for XCode this way!
Basically:
Create a standard C function which calls an Obj-C static method:
The static Obj-C method:
Then in your .pch file, add the following lines to override NSLog()
Of course more details are required to handle the TCP Socket. Working source code is available here:
https://github.com/driedler/iOS-TCP-Logger/wiki/About