Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6dd9584

Browse files
committed
Improve pg_upgrade's status display
Pg_upgrade displays file names during copy and database names during dump/restore. Andrew Dunstan identified three bugs: * long file names were being truncated to 60 _leading_ characters, which often do not change for long file names * file names were truncated to 60 characters in log files * carriage returns were being output to log files This commit fixes these --- it prints 60 _trailing_ characters to the status display, and full path names without carriage returns to log files. It also suppresses status output to the log file unless verbose mode is used.
1 parent ef754fb commit 6dd9584

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

contrib/pg_upgrade/dump.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ generate_old_dump(void)
3636
char file_name[MAXPGPATH];
3737
DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
3838

39-
pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_db->db_name);
39+
pg_log(PG_STATUS, "%s", old_db->db_name);
4040
snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
4141

4242
exec_prog(RESTORE_LOG_FILE, NULL, true,

contrib/pg_upgrade/pg_upgrade.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ create_new_objects(void)
310310
char file_name[MAXPGPATH];
311311
DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
312312

313-
pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_db->db_name);
313+
pg_log(PG_STATUS, "%s", old_db->db_name);
314314
snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
315315

316316
/*

contrib/pg_upgrade/pg_upgrade.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525
#define MIGRATOR_API_VERSION 1
2626

27-
#define MESSAGE_WIDTH "60"
27+
#define MESSAGE_WIDTH 60
2828

29-
#define OVERWRITE_MESSAGE " %-" MESSAGE_WIDTH "." MESSAGE_WIDTH "s\r"
3029
#define GET_MAJOR_VERSION(v) ((v) / 100)
3130

3231
/* contains both global db information and CREATE DATABASE commands */
@@ -208,6 +207,7 @@ typedef enum
208207
typedef enum
209208
{
210209
PG_VERBOSE,
210+
PG_STATUS,
211211
PG_REPORT,
212212
PG_WARNING,
213213
PG_FATAL

contrib/pg_upgrade/relfilenode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
213213
unlink(new_file);
214214

215215
/* Copying files might take some time, so give feedback. */
216-
pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_file);
216+
pg_log(PG_STATUS, "%s", old_file);
217217

218218
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
219219
pg_log(PG_FATAL, "This upgrade requires page-by-page conversion, "

contrib/pg_upgrade/util.c

+25-15
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ prep_status(const char *fmt,...)
7575
if (strlen(message) > 0 && message[strlen(message) - 1] == '\n')
7676
pg_log(PG_REPORT, "%s", message);
7777
else
78-
pg_log(PG_REPORT, "%-" MESSAGE_WIDTH "s", message);
78+
/* trim strings that don't end in a newline */
79+
pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
7980
}
8081

8182

@@ -89,22 +90,16 @@ pg_log(eLogType type, char *fmt,...)
8990
vsnprintf(message, sizeof(message), fmt, args);
9091
va_end(args);
9192

92-
/* PG_VERBOSE is only output in verbose mode */
93+
/* PG_VERBOSE and PG_STATUS are only output in verbose mode */
9394
/* fopen() on log_opts.internal might have failed, so check it */
94-
if ((type != PG_VERBOSE || log_opts.verbose) && log_opts.internal != NULL)
95+
if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
96+
log_opts.internal != NULL)
9597
{
96-
/*
97-
* There's nothing much we can do about it if fwrite fails, but some
98-
* platforms declare fwrite with warn_unused_result. Do a little
99-
* dance with casting to void to shut up the compiler in such cases.
100-
*/
101-
size_t rc;
102-
103-
rc = fwrite(message, strlen(message), 1, log_opts.internal);
104-
/* if we are using OVERWRITE_MESSAGE, add newline to log file */
105-
if (strchr(message, '\r') != NULL)
106-
rc = fwrite("\n", 1, 1, log_opts.internal);
107-
(void) rc;
98+
if (type == PG_STATUS)
99+
/* status messages need two leading spaces and a newline */
100+
fprintf(log_opts.internal, " %s\n", message);
101+
else
102+
fprintf(log_opts.internal, "%s", message);
108103
fflush(log_opts.internal);
109104
}
110105

@@ -115,6 +110,21 @@ pg_log(eLogType type, char *fmt,...)
115110
printf("%s", _(message));
116111
break;
117112

113+
case PG_STATUS:
114+
/* for output to a display, do leading truncation and append \r */
115+
if (isatty(fileno(stdout)))
116+
/* -2 because we use a 2-space indent */
117+
printf(" %s%-*.*s\r",
118+
/* prefix with "..." if we do leading truncation */
119+
strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
120+
MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
121+
/* optional leading truncation */
122+
strlen(message) <= MESSAGE_WIDTH - 2 ? message :
123+
message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
124+
else
125+
printf(" %s\n", _(message));
126+
break;
127+
118128
case PG_REPORT:
119129
case PG_WARNING:
120130
printf("%s", _(message));

0 commit comments

Comments
 (0)