diff --git a/cmdline/check.c b/cmdline/check.c index f1d4ac72..81a720c6 100644 --- a/cmdline/check.c +++ b/cmdline/check.c @@ -1809,7 +1809,7 @@ bail: else ftag("summary:exit:error\n"); } - fflush(stdlog); + fflush_log(); free(failed); free(failed_map); diff --git a/cmdline/dup.c b/cmdline/dup.c index 9ee4bfa7..2945c0c9 100644 --- a/cmdline/dup.c +++ b/cmdline/dup.c @@ -151,6 +151,6 @@ void state_dup(struct snapraid_state* state) } else { ftag("summary:exit:dup\n"); } - fflush(stdlog); + fflush_log(); } diff --git a/cmdline/list.c b/cmdline/list.c index 6b9e2d3e..c9cd07cb 100644 --- a/cmdline/list.c +++ b/cmdline/list.c @@ -117,6 +117,6 @@ void state_list(struct snapraid_state* state) ftag("summary:file_size:%" PRIu64 "\n", file_size); ftag("summary:link_count:%u\n", link_count); ftag("summary:exit:ok\n"); - fflush(stdlog); + fflush_log(); } diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 1a4827b9..d82e36a8 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -173,6 +173,11 @@ void os_init(int opt) SetThreadExecutionState(WIN32_ES_CONTINUOUS | WIN32_ES_SYSTEM_REQUIRED); } + /* set stdout and stderr as "line buffered" */ + /* this ensures that messages are mixed correctly */ + setvbuf(stdout, 0, _IOLBF, BUFSIZ); + setvbuf(stderr, 0, _IOLBF, BUFSIZ); + last_error = 0; } @@ -1907,7 +1912,6 @@ static void* thread_spinup(void* arg) pthread_mutex_lock(&io_lock); fprintf(stdout, "Spunup device '%" PRIx64 "' for disk '%s' in %" PRIu64 " ms.\n", (uint64_t)device, devinfo->name, tick_ms() - start); - fflush(stdout); pthread_mutex_unlock(&io_lock); return 0; diff --git a/cmdline/parity.c b/cmdline/parity.c index 838161bb..8009f77c 100644 --- a/cmdline/parity.c +++ b/cmdline/parity.c @@ -103,7 +103,7 @@ void parity_overflow(struct snapraid_state* state, data_off_t size) if (file->blockmax > 0) { if (file->blockvec[file->blockmax - 1].parity_pos >= blockalloc) { ftag("outofparity:%s:%s\n", disk->name, file->sub); - fflush(stdlog); + fflush_log(); if (first) { first = 0; printf("\nYour data requires more parity than the available space.\n"); diff --git a/cmdline/pool.c b/cmdline/pool.c index ae78ab5e..dcd43fb7 100644 --- a/cmdline/pool.c +++ b/cmdline/pool.c @@ -259,6 +259,6 @@ void state_pool(struct snapraid_state* state) ftag("summary:link_count::%u\n", count); ftag("summary:exit:ok\n"); - fflush(stdlog); + fflush_log(); } diff --git a/cmdline/scan.c b/cmdline/scan.c index ca1c84ce..44ac7ff8 100644 --- a/cmdline/scan.c +++ b/cmdline/scan.c @@ -1750,7 +1750,7 @@ void state_scan(struct snapraid_state* state, int is_diff) } else { ftag("summary:exit:diff\n"); } - fflush(stdlog); + fflush_log(); } tommy_list_foreach(&scanlist, (tommy_foreach_func*)free); diff --git a/cmdline/scrub.c b/cmdline/scrub.c index 2844a331..643592dd 100644 --- a/cmdline/scrub.c +++ b/cmdline/scrub.c @@ -502,7 +502,7 @@ static int state_scrub_process(struct snapraid_state* state, struct snapraid_par ftag("summary:exit:ok\n"); else ftag("summary:exit:error\n"); - fflush(stdlog); + fflush_log(); bail: for (j = 0; j < diskmax; ++j) { diff --git a/cmdline/selftest.c b/cmdline/selftest.c index 81b3b8d6..42485ec2 100644 --- a/cmdline/selftest.c +++ b/cmdline/selftest.c @@ -381,7 +381,7 @@ bail: void selftest(void) { ftag("selftest:\n"); - fflush(stdlog); + fflush_log(); printf("Self test...\n"); diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index 49452865..1f333c52 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -924,7 +924,7 @@ int main(int argc, char* argv[]) ftag("command:%s\n", command); for (i = 0; i < argc; ++i) ftag("argv:%u:%s\n", i, argv[i]); - fflush(stdlog); + fflush_log(); if (!opt.skip_self) selftest(); diff --git a/cmdline/state.c b/cmdline/state.c index 1e1d2df6..a006bc89 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -930,7 +930,7 @@ void state_config(struct snapraid_state* state, const char* path, const char* co if (state->filter_hidden) ftag("filter:nohidden:\n"); - fflush(stdlog); + fflush_log(); } /** @@ -4284,7 +4284,7 @@ void state_read(struct snapraid_state* state) if (!state->no_conf) { ftag("content:%s\n", path); - fflush(stdlog); + fflush_log(); } printf("Loading state from %s...\n", path); @@ -4593,7 +4593,7 @@ int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, b if (state->opt.gui) { ftag("run:begin:%u:%u:%u\n", blockstart, blockmax, countmax); - fflush(stdlog); + fflush_log(); } now = time(0); @@ -4611,7 +4611,7 @@ int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, b printf("Not starting for interruption\n"); } ftag("sigint:0: SIGINT received\n"); - fflush(stdlog); + fflush_log(); return 0; /* LCOV_EXCL_STOP */ } @@ -4623,7 +4623,7 @@ void state_progress_end(struct snapraid_state* state, block_off_t countpos, bloc { if (state->opt.gui) { ftag("run:end\n"); - fflush(stdlog); + fflush_log(); } else { time_t now; time_t elapsed; @@ -4758,7 +4758,7 @@ int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off if (state->opt.gui) { ftag("run:pos:%u:%u:%" PRIu64 ":%u:%u:%u:%u:%" PRIu64 "\n", blockpos, countpos, countsize, out_perc, out_eta, out_speed, out_cpu, (uint64_t)elapsed); - fflush(stdlog); + fflush_log(); } else { printf("%u%%, %u MiB", out_perc, (unsigned)(countsize / (1024 * 1024))); if (out_speed) @@ -4795,7 +4795,7 @@ int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off printf("Stopping for interruption at block %u\n", blockpos); } ftag("sigint:%u: SIGINT received\n", blockpos); - fflush(stdlog); + fflush_log(); return 1; /* LCOV_EXCL_STOP */ } diff --git a/cmdline/status.c b/cmdline/status.c index 0e8e2cac..8d280ef7 100644 --- a/cmdline/status.c +++ b/cmdline/status.c @@ -242,7 +242,7 @@ int state_status(struct snapraid_state* state) ftag("summary:hash:%s\n", hash_config_name(state->hash)); ftag("summary:prev_hash:%s\n", hash_config_name(state->prevhash)); ftag("summary:best_hash:%s\n", hash_config_name(state->besthash)); - fflush(stdlog); + fflush_log(); /* copy the info a temp vector, and count bad/rehash/unsynced blocks */ infomap = malloc_nofail(blockmax * sizeof(snapraid_info)); @@ -303,7 +303,7 @@ int state_status(struct snapraid_state* state) ftag("summary:has_unsynced:%u\n", unsynced_blocks); ftag("summary:has_rehash:%u\n", rehash); ftag("summary:has_bad:%u:%u:%u\n", bad, bad_first, bad_last); - fflush(stdlog); + fflush_log(); if (!count) { ferr("The array is empty.\n"); diff --git a/cmdline/support.c b/cmdline/support.c index 1ebd95fb..a2420108 100644 --- a/cmdline/support.c +++ b/cmdline/support.c @@ -63,6 +63,12 @@ void flog(const char* format, ...) } } +void fflush_log(void) +{ + if (stdlog) + fflush(stdlog); +} + void ftag(const char* format, ...) { va_list ap; diff --git a/cmdline/support.h b/cmdline/support.h index c79bfbdf..0c7f6085 100644 --- a/cmdline/support.h +++ b/cmdline/support.h @@ -48,6 +48,11 @@ void ferr(const char* format, ...) __attribute__((format(printf, 1, 2))); */ void flog(const char* format, ...) __attribute__((format(printf, 1, 2))); +/** + * Flush the log output. + */ +void fflush_log(void); + /** * Tag message. * diff --git a/cmdline/sync.c b/cmdline/sync.c index 60f468ca..2283ab35 100644 --- a/cmdline/sync.c +++ b/cmdline/sync.c @@ -1151,7 +1151,7 @@ end: ftag("summary:exit:ok\n"); else ftag("summary:exit:error\n"); - fflush(stdlog); + fflush_log(); bail: for (j = 0; j < diskmax; ++j) { diff --git a/cmdline/unix.c b/cmdline/unix.c index 0d995a7b..47f94f9a 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -957,6 +957,11 @@ int devquery(tommy_list* high, tommy_list* low, int operation) void os_init(int opt) { (void)opt; + + /* set stdout and stderr as "line buffered" */ + /* this ensures that messages are mixed correctly */ + setvbuf(stdout, 0, _IOLBF, BUFSIZ); + setvbuf(stderr, 0, _IOLBF, BUFSIZ); } void os_done(void)