mirror of
https://github.com/postgres/postgres.git
synced 2025-12-16 16:42:29 +03:00
Fix printing last progress report line in client programs.
A number of client programs have a "--progress" option that when printing
to a TTY, updates the current line by printing a '\r' and overwriting it.
After the last line, '\n' needs to be printed to move the cursor to the
next line. pg_basebackup and pgbench got this right, but pg_rewind and
pg_checksums were slightly wrong. pg_rewind printed the newline to stdout
instead of stderr, and pg_checksums printed the newline even when not
printing to a TTY. Fix them, and also add a 'finished' argument to
pg_basebackup's progress_report() function, to keep it consistent with
the other programs.
Backpatch to v12. pg_rewind's newline was broken with the logging changes
in commit cc8d415117 in v12, and pg_checksums was introduced in v12.
Discussion: https://www.postgresql.org/message-id/82b539e5-ae33-34b0-1aee-22b3379fd3eb@iki.fi
This commit is contained in:
@@ -422,7 +422,6 @@ main(int argc, char **argv)
|
||||
executeFileMap();
|
||||
|
||||
progress_report(true);
|
||||
printf("\n");
|
||||
|
||||
if (showprogress)
|
||||
pg_log_info("creating backup label and updating control file");
|
||||
@@ -519,11 +518,14 @@ sanityChecks(void)
|
||||
/*
|
||||
* Print a progress report based on the fetch_size and fetch_done variables.
|
||||
*
|
||||
* Progress report is written at maximum once per second, unless the
|
||||
* force parameter is set to true.
|
||||
* Progress report is written at maximum once per second, except that the
|
||||
* last progress report is always printed.
|
||||
*
|
||||
* If finished is set to true, this is the last progress report. The cursor
|
||||
* is moved to the next line.
|
||||
*/
|
||||
void
|
||||
progress_report(bool force)
|
||||
progress_report(bool finished)
|
||||
{
|
||||
static pg_time_t last_progress_report = 0;
|
||||
int percent;
|
||||
@@ -535,7 +537,7 @@ progress_report(bool force)
|
||||
return;
|
||||
|
||||
now = time(NULL);
|
||||
if (now == last_progress_report && !force)
|
||||
if (now == last_progress_report && !finished)
|
||||
return; /* Max once per second */
|
||||
|
||||
last_progress_report = now;
|
||||
@@ -565,10 +567,12 @@ progress_report(bool force)
|
||||
fprintf(stderr, _("%*s/%s kB (%d%%) copied"),
|
||||
(int) strlen(fetch_size_str), fetch_done_str, fetch_size_str,
|
||||
percent);
|
||||
if (isatty(fileno(stderr)))
|
||||
fprintf(stderr, "\r");
|
||||
else
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
/*
|
||||
* Stay on the same line if reporting to a terminal and we're not done
|
||||
* yet.
|
||||
*/
|
||||
fprintf(stderr, (!finished && isatty(fileno(stderr))) ? "\r" : "\n");
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user