Use carriage returns for data insertion logs in pgbench on terminal
authorMichael Paquier <michael@paquier.xyz>
Wed, 4 Dec 2019 02:33:14 +0000 (11:33 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 4 Dec 2019 02:33:14 +0000 (11:33 +0900)
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

index 5129aea51603fe241288abe624084cb487c8d275..6ab79eef99fba6db4791a791e1d68302d186cfaf 100644 (file)
@@ -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");