开发者

Giving freopen() and stderr a buffer (restricting size of a log file for iOS app)

开发者 https://www.devze.com 2023-03-02 20:00 出处:网络
I\'m currently redirecting NSLog() output to a file using a call to freopen() from the App Delegate. I would like to rest开发者_如何学Crict the log file size, but doing this-

I'm currently redirecting NSLog() output to a file using a call to freopen() from the App Delegate. I would like to rest开发者_如何学Crict the log file size, but doing this-

  unsigned long long fs = 3000;
  while ([fileAttributes fileSize] < fs) {

    freopen([FILEPATH cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

  }

causes the app to be stuck with a black screen in an infinite loop. Is there any way I can set a buffer size for stderr, and then have a loop wherein I only continue to write to the file if the filesize + buffer size does not exceed the file size?


Looks like you are stuck in that infinite for loop. Can you not do that, but just check the fileSize and dump strings once the file size is greater than fs?


Your code says:

As long as [fielAttributes fileSize] is less than fs, keep calling freopen(...).

Is that really what you want? I'd guess that you might prefer something like:

if ([fileAttributes fileSize] < fs) {
    file = freopen([FILEPATH cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
fwrite(file, "something to log");
fclose(file);

Of course, that's not great either, because it writes log information for fs characters and then stops. Instead, it seems like you probably want to preserve the LAST fs characters. If that's the case, then a pragmatic approach would be to simply copy the last fs characters to a new file and delete the old one from time to time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号