Swift stderr and stout into single file
Asked Answered
R

1

0

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?

Recording answered 29/1, 2017 at 17:43 Comment(0)
S
2

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.

Steep answered 29/1, 2017 at 17:57 Comment(4)
No, it seems it will only capture the one you used last.Recording
Correction. It seems to be working. Wow. But is it safe? What if the two streams try to access the file at the same time?Recording
Yeah, it should be "safe", i.e. It shouldn't crash, but you may get output interleaved because of buffering rules. I think you can modify how buffering happens on those file descriptors, but I'm not sure how off the top of my head.Steep
@Mundi: setlinebuf() can be used to set stdout/stderr to line buffering mode (example). By default, both are unbuffered when writing to a file.Opossum

© 2022 - 2024 — McMap. All rights reserved.