pretty-print: introduce WITH_BUFFERED_STDERR macro to enable buffering

Then, the buffer will be flushed and buffering will be disabled on
exiting from the scope.
This commit is contained in:
Yu Watanabe
2024-09-03 09:34:50 +09:00
parent b9e4b2c569
commit 4edbea7de2
3 changed files with 32 additions and 20 deletions
+2 -16
View File
@@ -531,28 +531,14 @@ void clear_progress_bar_impl(const char *prefix) {
}
void draw_progress_bar(const char *prefix, double percentage) {
/* We are going output a bunch of small strings that shall appear as a single line to STDERR which is
* unbuffered by default. Let's temporarily turn on full buffering, so that this is passed to the tty
* as a single buffer, to make things more efficient. */
char buffer[LONG_LINE_MAX];
setvbuf(stderr, buffer, _IOFBF, sizeof(buffer));
WITH_BUFFERED_STDERR;
draw_progress_bar_impl(prefix, percentage);
fflush(stderr);
/* Disable buffering again */
setvbuf(stderr, NULL, _IONBF, 0);
}
void clear_progress_bar(const char *prefix) {
char buffer[LONG_LINE_MAX];
setvbuf(stderr, buffer, _IOFBF, sizeof(buffer));
WITH_BUFFERED_STDERR;
clear_progress_bar_impl(prefix);
fflush(stderr);
setvbuf(stderr, NULL, _IONBF, 0);
}
+29
View File
@@ -57,3 +57,32 @@ void draw_progress_bar(const char *prefix, double percentage);
void clear_progress_bar(const char *prefix);
void draw_progress_bar_impl(const char *prefix, double percentage);
void clear_progress_bar_impl(const char *prefix);
static inline FILE* enable_buffering(FILE *f, char *buffer, size_t size) {
assert(f);
assert(buffer);
assert(size > 0);
if (setvbuf(f, buffer, _IOFBF, size) != 0)
return NULL;
return f;
}
static inline void fflush_and_disable_bufferingp(FILE **p) {
assert(p);
if (*p) {
fflush(*p);
setvbuf(*p, NULL, _IONBF, 0); /* Disable buffering again. */
}
}
/* Even though the macro below is slightly generic, but it may not work most streams except for stderr,
* as stdout is buffered and fopen() enables buffering by default. */
#define _WITH_BUFFERED_STREAM(f, size, p) \
_unused_ _cleanup_(fflush_and_disable_bufferingp) FILE *p = \
enable_buffering(f, (char[size]) {}, size)
#define WITH_BUFFERED_STDERR \
_WITH_BUFFERED_STREAM(stderr, LONG_LINE_MAX, UNIQ_T(p, UNIQ))
+1 -4
View File
@@ -798,8 +798,7 @@ static int update_render_progress(sd_event_source *source, void *userdata) {
/* We're outputting lots of small strings to STDERR, which is unbuffered by default. So let's turn
* on full buffering, so we pass this all to the TTY in one go, to make things more efficient */
char buffer[LONG_LINE_MAX];
setvbuf(stderr, buffer, _IOFBF, sizeof(buffer));
WITH_BUFFERED_STDERR;
if (!terminal_is_dumb()) {
for (size_t i = 0; i <= n; i++)
@@ -855,8 +854,6 @@ static int update_render_progress(sd_event_source *source, void *userdata) {
} else if (!exiting)
fputs("------\n", stderr);
fflush(stderr);
setvbuf(stderr, NULL, _IONBF, 0); /* Disable buffering again */
return 0;
}