From d37ddb745be07502814635585cbf935363c8a33d Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 4 Dec 2019 11:33:14 +0900 Subject: [PATCH] Use carriage returns for data insertion logs in pgbench on terminal This is similar to what pg_basebackup and pg_rewind do when reporting cumulative data, and that's more user-friendly. Carriage returns are now used when stderr points to a terminal, and newlines are used in other cases, like a redirection to a log file. Author: Amit Langote Reviewed-by: Fabien Coelho Discussion: https://postgr.es/m/CA+HiwqFNwEjPeVaQsp2L7DyCPv1Eg1guwhrVhzMYqUJUk8ULKg@mail.gmail.com --- src/bin/pgbench/pgbench.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 5129aea516..6ab79eef99 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -3835,6 +3835,9 @@ initGenerateDataClientSide(PGconn *con) remaining_sec; int log_interval = 1; + /* Stay on the same line if reporting to a terminal */ + char eol = isatty(fileno(stderr)) ? '\r' : '\n'; + fprintf(stderr, "generating data (client-side)...\n"); /* @@ -3910,10 +3913,10 @@ initGenerateDataClientSide(PGconn *con) elapsed_sec = INSTR_TIME_GET_DOUBLE(diff); remaining_sec = ((double) scale * naccounts - j) * elapsed_sec / j; - fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)\n", + fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)%c", j, (int64) naccounts * scale, (int) (((int64) j * 100) / (naccounts * (int64) scale)), - elapsed_sec, remaining_sec); + elapsed_sec, remaining_sec, eol); } /* let's not call the timing for each row, but only each 100 rows */ else if (use_quiet && (j % 100 == 0)) @@ -3927,16 +3930,19 @@ initGenerateDataClientSide(PGconn *con) /* have we reached the next interval (or end)? */ if ((j == scale * naccounts) || (elapsed_sec >= log_interval * LOG_STEP_SECONDS)) { - fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)\n", + fprintf(stderr, INT64_FORMAT " of " INT64_FORMAT " tuples (%d%%) done (elapsed %.2f s, remaining %.2f s)%c", j, (int64) naccounts * scale, - (int) (((int64) j * 100) / (naccounts * (int64) scale)), elapsed_sec, remaining_sec); + (int) (((int64) j * 100) / (naccounts * (int64) scale)), elapsed_sec, remaining_sec, eol); /* skip to the next interval */ log_interval = (int) ceil(elapsed_sec / LOG_STEP_SECONDS); } } - } + + if (eol != '\n') + fputc('\n', stderr); /* Need to move to next line */ + if (PQputline(con, "\\.\n")) { fprintf(stderr, "very last PQputline failed\n");