问题描述
对于iPhone应用程序,我需要将调试数据收集到一个文件中,然后发送到在线服务进行分析.通过将stderr
输出转移到文件句柄并在适当的时候发送文件,可以很好地工作. (NSLog
输出也以stderr
结尾,因此可以全面使用.)相同的方法对于stdout
也可以正常工作.
For an iPhone app, I need to collect debugging data into a file to be sent to an online service for analysis. This works fine by diverting the stderr
output to a file handle and sending the file when appropriate. (NSLog
output also ends up in stderr
, so this works across the board.) The same method also works fine with stdout
.
这是我保存到文件的方法:
Here is how I do the saving to a file:
freopen(cStringFilePath, "a+", stderr) // or stdout
上面的代码正确地返回了UnsafeMutablePointer<FILE>
,为了在必要时关闭文件,我会对其进行跟踪.
The above correctly returns a UnsafeMutablePointer<FILE>
which I keep track of in order to close the file when necessary.
但是,我需要一个文件中的stdout
和stderr
的内容,并逐行正确地排列时间顺序.
However, I would need the content of both stdout
and stderr
in one file, with the correct time sequence line by line.
这样做的原因是我正在尝试调试第三方框架,该框架会生成诊断输出,但会将其写入stdout
而不是stderr
.
The reason for this is that I am trying to debug a third party framework which produces diagnostic output but writes it to stdout
rather to stderr
.
有什么想法要实现吗?
推荐答案
您是否尝试过两次调用freopen
?一次使用stderr
,一次使用stdout
?我认为当我尝试时,此方法可以很好地达到我的目的.
Have you tried just calling freopen
twice? Once with stderr
and once with stdout
? I think that worked fine for my purposes when I tried it.
这篇关于Swift stderr和stoutr合并为单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!