Swift stderr and stout into single file

2019-09-06 06:46发布

问题:

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

The above correctly returns a UnsafeMutablePointer<FILE> which I keep track of in order to close the file when necessary.

However, I would need the content of both stdout and stderr in one file, with the correct time sequence line by line.

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.

Any idea how to accomplish this?

回答1:

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.