开发者

stderr to file; but without buffering

开发者 https://www.devze.com 2023-01-01 00:44 出处:网络
I am trying to isolate开发者_如何学C a nasty bug, which brings down my linux kernel. I am printing messages to stderr and stderr is redirected to a log file. Is there a way to disable buffering on the

I am trying to isolate开发者_如何学C a nasty bug, which brings down my linux kernel. I am printing messages to stderr and stderr is redirected to a log file. Is there a way to disable buffering on the file access? When kernel hangs, I am losing the messages in the buffer.


Actually, stderr is unbuffered by default but I think that's only in terms of the C runtime. We've solved this before with:

fflush (stderr); fsync (fileno (stderr));

(although we actually did it to stdout but the same rules apply - the actual fflush may not be necessary for stderr but it does no harm).

The fflush flushes the C runtime buffers to the OS, the fsync forces write to disk.

Keep in mind this may severely affect your performance.


You can force flushing the buffer, using fflush(stderr);


You can try to use setvbuf when starting your app

setvbuf(stderr, NULL, _IONBF, 0);

However, you will get read of the stdio buffer, but still have the "in kernel" buffer problem, that won't disappear unless you fsync. However may be tracking a kernel bug from userspace isn't the best way to go at it.

Can you use a serial console and get the output on another machine ? This way you could get both the oops and the stderr messages

0

精彩评论

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