diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2019-04-01 14:24:37 +0200 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2019-04-01 20:01:35 +0200 |
commit | cc8d41511721d25d557fc02a46c053c0a602fed0 (patch) | |
tree | d2f92acac085be1b9cc4756260c7a4f83d1b0041 /src/bin/pg_basebackup/pg_basebackup.c | |
parent | b4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff) |
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error
printing) systems used throughout the command-line programs.
Features:
- Program name is automatically prefixed.
- Message string does not end with newline. This removes a common
source of inconsistencies and omissions.
- Additionally, a final newline is automatically stripped, simplifying
use of PQerrorMessage() etc., another common source of mistakes.
- I converted error message strings to use %m where possible.
- As a result of the above several points, more translatable message
strings can be shared between different components and between
frontends and backend, without gratuitous punctuation or whitespace
differences.
- There is support for setting a "log level". This is not meant to be
user-facing, but can be used internally to implement debug or
verbose modes.
- Lazy argument evaluation, so no significant overhead if logging at
some level is disabled.
- Some color in the messages, similar to gcc and clang. Set
PG_COLOR=auto to try it out. Some colors are predefined, but can be
customized by setting PG_COLORS.
- Common files (common/, fe_utils/, etc.) can handle logging much more
simply by just using one API without worrying too much about the
context of the calling program, requiring callbacks, or having to
pass "progname" around everywhere.
- Some programs called setvbuf() to make sure that stderr is
unbuffered, even on Windows. But not all programs did that. This
is now done centrally.
Soft goals:
- Reduces vertical space use and visual complexity of error reporting
in the source code.
- Encourages more deliberate classification of messages. For example,
in some cases it wasn't clear without analyzing the surrounding code
whether a message was meant as an error or just an info.
- Concepts and terms are vaguely aligned with popular logging
frameworks such as log4j and Python logging.
This is all just about printing stuff out. Nothing affects program
flow (e.g., fatal exits). The uses are just too varied to do that.
Some existing code had wrappers that do some kind of print-and-exit,
and I adapted those.
I tried to keep the output mostly the same, but there is a lot of
historical baggage to unwind and special cases to consider, and I
might not always have succeeded. One significant change is that
pg_rewind used to write all error messages to stdout. That is now
changed to stderr.
Reviewed-by: Donald Dong <xdong@csumb.edu>
Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru>
Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
Diffstat (limited to 'src/bin/pg_basebackup/pg_basebackup.c')
-rw-r--r-- | src/bin/pg_basebackup/pg_basebackup.c | 412 |
1 files changed, 153 insertions, 259 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 916371232bf..1a735b80467 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -30,6 +30,7 @@ #include "common/file_perm.h" #include "common/file_utils.h" #include "common/string.h" +#include "fe_utils/logging.h" #include "fe_utils/string_utils.h" #include "getopt_long.h" #include "libpq-fe.h" @@ -167,57 +168,41 @@ cleanup_directories_atexit(void) { if (made_new_pgdata) { - fprintf(stderr, _("%s: removing data directory \"%s\"\n"), - progname, basedir); + pg_log_info("removing data directory \"%s\"", basedir); if (!rmtree(basedir, true)) - fprintf(stderr, _("%s: failed to remove data directory\n"), - progname); + pg_log_error("failed to remove data directory"); } else if (found_existing_pgdata) { - fprintf(stderr, - _("%s: removing contents of data directory \"%s\"\n"), - progname, basedir); + pg_log_info("removing contents of data directory \"%s\"", basedir); if (!rmtree(basedir, false)) - fprintf(stderr, _("%s: failed to remove contents of data directory\n"), - progname); + pg_log_error("failed to remove contents of data directory"); } if (made_new_xlogdir) { - fprintf(stderr, _("%s: removing WAL directory \"%s\"\n"), - progname, xlog_dir); + pg_log_info("removing WAL directory \"%s\"", xlog_dir); if (!rmtree(xlog_dir, true)) - fprintf(stderr, _("%s: failed to remove WAL directory\n"), - progname); + pg_log_error("failed to remove WAL directory"); } else if (found_existing_xlogdir) { - fprintf(stderr, - _("%s: removing contents of WAL directory \"%s\"\n"), - progname, xlog_dir); + pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir); if (!rmtree(xlog_dir, false)) - fprintf(stderr, _("%s: failed to remove contents of WAL directory\n"), - progname); + pg_log_error("failed to remove contents of WAL directory"); } } else { if ((made_new_pgdata || found_existing_pgdata) && !checksum_failure) - fprintf(stderr, - _("%s: data directory \"%s\" not removed at user's request\n"), - progname, basedir); + pg_log_info("data directory \"%s\" not removed at user's request", basedir); if (made_new_xlogdir || found_existing_xlogdir) - fprintf(stderr, - _("%s: WAL directory \"%s\" not removed at user's request\n"), - progname, xlog_dir); + pg_log_info("WAL directory \"%s\" not removed at user's request", xlog_dir); } if ((made_tablespace_dirs || found_tablespace_dirs) && !checksum_failure) - fprintf(stderr, - _("%s: changes to tablespace directories will not be undone\n"), - progname); + pg_log_info("changes to tablespace directories will not be undone"); } static void @@ -258,7 +243,7 @@ tablespace_list_append(const char *arg) { if (dst_ptr - dst >= MAXPGPATH) { - fprintf(stderr, _("%s: directory name too long\n"), progname); + pg_log_error("directory name too long"); exit(1); } @@ -268,7 +253,7 @@ tablespace_list_append(const char *arg) { if (*cell->new_dir) { - fprintf(stderr, _("%s: multiple \"=\" signs in tablespace mapping\n"), progname); + pg_log_error("multiple \"=\" signs in tablespace mapping"); exit(1); } else @@ -280,9 +265,7 @@ tablespace_list_append(const char *arg) if (!*cell->old_dir || !*cell->new_dir) { - fprintf(stderr, - _("%s: invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"\n"), - progname, arg); + pg_log_error("invalid tablespace mapping format \"%s\", must be \"OLDDIR=NEWDIR\"", arg); exit(1); } @@ -294,15 +277,15 @@ tablespace_list_append(const char *arg) */ if (!is_absolute_path(cell->old_dir)) { - fprintf(stderr, _("%s: old directory is not an absolute path in tablespace mapping: %s\n"), - progname, cell->old_dir); + pg_log_error("old directory is not an absolute path in tablespace mapping: %s", + cell->old_dir); exit(1); } if (!is_absolute_path(cell->new_dir)) { - fprintf(stderr, _("%s: new directory is not an absolute path in tablespace mapping: %s\n"), - progname, cell->new_dir); + pg_log_error("new directory is not an absolute path in tablespace mapping: %s", + cell->new_dir); exit(1); } @@ -425,16 +408,14 @@ reached_end_position(XLogRecPtr segendpos, uint32 timeline, r = read(bgpipe[0], xlogend, sizeof(xlogend) - 1); if (r < 0) { - fprintf(stderr, _("%s: could not read from ready pipe: %s\n"), - progname, strerror(errno)); + pg_log_error("could not read from ready pipe: %m"); exit(1); } if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2) { - fprintf(stderr, - _("%s: could not parse write-ahead log location \"%s\"\n"), - progname, xlogend); + pg_log_error("could not parse write-ahead log location \"%s\"", + xlogend); exit(1); } xlogendptr = ((uint64) hi) << 32 | lo; @@ -526,9 +507,7 @@ LogStreamerMain(logstreamer_param *param) if (!stream.walmethod->finish()) { - fprintf(stderr, - _("%s: could not finish writing WAL files: %s\n"), - progname, strerror(errno)); + pg_log_error("could not finish writing WAL files: %m"); return 1; } @@ -563,9 +542,8 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) /* Convert the starting position */ if (sscanf(startpos, "%X/%X", &hi, &lo) != 2) { - fprintf(stderr, - _("%s: could not parse write-ahead log location \"%s\"\n"), - progname, startpos); + pg_log_error("could not parse write-ahead log location \"%s\"", + startpos); exit(1); } param->startptr = ((uint64) hi) << 32 | lo; @@ -576,9 +554,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) /* Create our background pipe */ if (pipe(bgpipe) < 0) { - fprintf(stderr, - _("%s: could not create pipe for background process: %s\n"), - progname, strerror(errno)); + pg_log_error("could not create pipe for background process: %m"); exit(1); } #endif @@ -613,11 +589,11 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) if (verbose) { if (temp_replication_slot) - fprintf(stderr, _("%s: created temporary replication slot \"%s\"\n"), - progname, replication_slot); + pg_log_info("created temporary replication slot \"%s\"", + replication_slot); else - fprintf(stderr, _("%s: created replication slot \"%s\"\n"), - progname, replication_slot); + pg_log_info("created replication slot \"%s\"", + replication_slot); } } @@ -636,9 +612,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) if (pg_mkdir_p(statusdir, pg_dir_create_mode) != 0 && errno != EEXIST) { - fprintf(stderr, - _("%s: could not create directory \"%s\": %s\n"), - progname, statusdir, strerror(errno)); + pg_log_error("could not create directory \"%s\": %m", statusdir); exit(1); } } @@ -656,8 +630,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) } else if (bgchild < 0) { - fprintf(stderr, _("%s: could not create background process: %s\n"), - progname, strerror(errno)); + pg_log_error("could not create background process: %m"); exit(1); } @@ -669,8 +642,7 @@ StartLogStreamer(char *startpos, uint32 timeline, char *sysidentifier) bgchild = _beginthreadex(NULL, 0, (void *) LogStreamerMain, param, 0, NULL); if (bgchild == 0) { - fprintf(stderr, _("%s: could not create background thread: %s\n"), - progname, strerror(errno)); + pg_log_error("could not create background thread: %m"); exit(1); } #endif @@ -693,9 +665,7 @@ verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found) */ if (pg_mkdir_p(dirname, pg_dir_create_mode) == -1) { - fprintf(stderr, - _("%s: could not create directory \"%s\": %s\n"), - progname, dirname, strerror(errno)); + pg_log_error("could not create directory \"%s\": %m", dirname); exit(1); } if (created) @@ -716,17 +686,14 @@ verify_dir_is_empty_or_create(char *dirname, bool *created, bool *found) /* * Exists, not empty */ - fprintf(stderr, - _("%s: directory \"%s\" exists but is not empty\n"), - progname, dirname); + pg_log_error("directory \"%s\" exists but is not empty", dirname); exit(1); case -1: /* * Access problem */ - fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"), - progname, dirname, strerror(errno)); + pg_log_error("could not access directory \"%s\": %m", dirname); exit(1); } } @@ -839,16 +806,12 @@ parse_max_rate(char *src) result = strtod(src, &after_num); if (src == after_num) { - fprintf(stderr, - _("%s: transfer rate \"%s\" is not a valid value\n"), - progname, src); + pg_log_error("transfer rate \"%s\" is not a valid value", src); exit(1); } if (errno != 0) { - fprintf(stderr, - _("%s: invalid transfer rate \"%s\": %s\n"), - progname, src, strerror(errno)); + pg_log_error("invalid transfer rate \"%s\": %m", src); exit(1); } @@ -857,8 +820,7 @@ parse_max_rate(char *src) /* * Reject obviously wrong values here. */ - fprintf(stderr, _("%s: transfer rate must be greater than zero\n"), - progname); + pg_log_error("transfer rate must be greater than zero"); exit(1); } @@ -890,18 +852,14 @@ parse_max_rate(char *src) if (*after_num != '\0') { - fprintf(stderr, - _("%s: invalid --max-rate unit: \"%s\"\n"), - progname, suffix); + pg_log_error("invalid --max-rate unit: \"%s\"", suffix); exit(1); } /* Valid integer? */ if ((uint64) result != (uint64) ((uint32) result)) { - fprintf(stderr, - _("%s: transfer rate \"%s\" exceeds integer range\n"), - progname, src); + pg_log_error("transfer rate \"%s\" exceeds integer range", src); exit(1); } @@ -911,9 +869,7 @@ parse_max_rate(char *src) */ if (result < MAX_RATE_LOWER || result > MAX_RATE_UPPER) { - fprintf(stderr, - _("%s: transfer rate \"%s\" is out of range\n"), - progname, src); + pg_log_error("transfer rate \"%s\" is out of range", src); exit(1); } @@ -935,9 +891,8 @@ writeTarData( { if (gzwrite(ztarfile, buf, r) != r) { - fprintf(stderr, - _("%s: could not write to compressed file \"%s\": %s\n"), - progname, current_file, get_gz_error(ztarfile)); + pg_log_error("could not write to compressed file \"%s\": %s", + current_file, get_gz_error(ztarfile)); exit(1); } } @@ -946,8 +901,7 @@ writeTarData( { if (fwrite(buf, r, 1, tarfile) != 1) { - fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), - progname, current_file, strerror(errno)); + pg_log_error("could not write to file \"%s\": %m", current_file); exit(1); } } @@ -1012,9 +966,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { - fprintf(stderr, - _("%s: could not set compression level %d: %s\n"), - progname, compresslevel, get_gz_error(ztarfile)); + pg_log_error("could not set compression level %d: %s", + compresslevel, get_gz_error(ztarfile)); exit(1); } } @@ -1033,9 +986,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { - fprintf(stderr, - _("%s: could not set compression level %d: %s\n"), - progname, compresslevel, get_gz_error(ztarfile)); + pg_log_error("could not set compression level %d: %s", + compresslevel, get_gz_error(ztarfile)); exit(1); } } @@ -1061,9 +1013,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) if (gzsetparams(ztarfile, compresslevel, Z_DEFAULT_STRATEGY) != Z_OK) { - fprintf(stderr, - _("%s: could not set compression level %d: %s\n"), - progname, compresslevel, get_gz_error(ztarfile)); + pg_log_error("could not set compression level %d: %s", + compresslevel, get_gz_error(ztarfile)); exit(1); } } @@ -1082,9 +1033,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) if (!ztarfile) { /* Compression is in use */ - fprintf(stderr, - _("%s: could not create compressed file \"%s\": %s\n"), - progname, filename, get_gz_error(ztarfile)); + pg_log_error("could not create compressed file \"%s\": %s", + filename, get_gz_error(ztarfile)); exit(1); } } @@ -1094,8 +1044,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) /* Either no zlib support, or zlib support but compresslevel = 0 */ if (!tarfile) { - fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not create file \"%s\": %m", filename); exit(1); } } @@ -1106,8 +1055,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_COPY_OUT) { - fprintf(stderr, _("%s: could not get COPY data stream: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not get COPY data stream: %s", + PQerrorMessage(conn)); exit(1); } @@ -1191,9 +1140,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) { if (gzclose(ztarfile) != 0) { - fprintf(stderr, - _("%s: could not close compressed file \"%s\": %s\n"), - progname, filename, get_gz_error(ztarfile)); + pg_log_error("could not close compressed file \"%s\": %s", + filename, get_gz_error(ztarfile)); exit(1); } } @@ -1204,9 +1152,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) { if (fclose(tarfile) != 0) { - fprintf(stderr, - _("%s: could not close file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not close file \"%s\": %m", + filename); exit(1); } } @@ -1216,8 +1163,8 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) } else if (r == -2) { - fprintf(stderr, _("%s: could not read COPY data: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not read COPY data: %s", + PQerrorMessage(conn)); exit(1); } @@ -1401,7 +1348,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum) /* sync the resulting tar file, errors are not considered fatal */ if (do_sync && strcmp(basedir, "-") != 0) - (void) fsync_fname(filename, false, progname); + (void) fsync_fname(filename, false); } @@ -1462,8 +1409,8 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_COPY_OUT) { - fprintf(stderr, _("%s: could not get COPY data stream: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not get COPY data stream: %s", + PQerrorMessage(conn)); exit(1); } @@ -1491,8 +1438,8 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) } else if (r == -2) { - fprintf(stderr, _("%s: could not read COPY data: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not read COPY data: %s", + PQerrorMessage(conn)); exit(1); } @@ -1505,8 +1452,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) */ if (r != 512) { - fprintf(stderr, _("%s: invalid tar block header size: %d\n"), - progname, r); + pg_log_error("invalid tar block header size: %d", r); exit(1); } totaldone += 512; @@ -1554,17 +1500,15 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) pg_str_endswith(filename, "/archive_status")) && errno == EEXIST)) { - fprintf(stderr, - _("%s: could not create directory \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not create directory \"%s\": %m", + filename); exit(1); } } #ifndef WIN32 if (chmod(filename, (mode_t) filemode)) - fprintf(stderr, - _("%s: could not set permissions on directory \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not set permissions on directory \"%s\": %m", + filename); #endif } else if (copybuf[156] == '2') @@ -1586,18 +1530,15 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) mapped_tblspc_path = get_tablespace_mapping(©buf[157]); if (symlink(mapped_tblspc_path, filename) != 0) { - fprintf(stderr, - _("%s: could not create symbolic link from \"%s\" to \"%s\": %s\n"), - progname, filename, mapped_tblspc_path, - strerror(errno)); + pg_log_error("could not create symbolic link from \"%s\" to \"%s\": %m", + filename, mapped_tblspc_path); exit(1); } } else { - fprintf(stderr, - _("%s: unrecognized link indicator \"%c\"\n"), - progname, copybuf[156]); + pg_log_error("unrecognized link indicator \"%c\"", + copybuf[156]); exit(1); } continue; /* directory or link handled */ @@ -1609,15 +1550,14 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) file = fopen(filename, "wb"); if (!file) { - fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not create file \"%s\": %m", filename); exit(1); } #ifndef WIN32 if (chmod(filename, (mode_t) filemode)) - fprintf(stderr, _("%s: could not set permissions on file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not set permissions on file \"%s\": %m", + filename); #endif if (current_len_left == 0) @@ -1649,8 +1589,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) if (fwrite(copybuf, r, 1, file) != 1) { - fprintf(stderr, _("%s: could not write to file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not write to file \"%s\": %m", filename); exit(1); } totaldone += r; @@ -1674,9 +1613,7 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum) if (file != NULL) { - fprintf(stderr, - _("%s: COPY stream ended before last file was finished\n"), - progname); + pg_log_error("COPY stream ended before last file was finished"); exit(1); } @@ -1703,7 +1640,7 @@ escape_quotes(const char *src) if (!result) { - fprintf(stderr, _("%s: out of memory\n"), progname); + pg_log_error("out of memory"); exit(1); } return result; @@ -1723,7 +1660,7 @@ GenerateRecoveryConf(PGconn *conn) recoveryconfcontents = createPQExpBuffer(); if (!recoveryconfcontents) { - fprintf(stderr, _("%s: out of memory\n"), progname); + pg_log_error("out of memory"); exit(1); } @@ -1737,7 +1674,7 @@ GenerateRecoveryConf(PGconn *conn) connOptions = PQconninfo(conn); if (connOptions == NULL) { - fprintf(stderr, _("%s: out of memory\n"), progname); + pg_log_error("out of memory"); exit(1); } @@ -1788,7 +1725,7 @@ GenerateRecoveryConf(PGconn *conn) if (PQExpBufferBroken(recoveryconfcontents) || PQExpBufferDataBroken(conninfo_buf)) { - fprintf(stderr, _("%s: out of memory\n"), progname); + pg_log_error("out of memory"); exit(1); } @@ -1821,16 +1758,13 @@ WriteRecoveryConf(void) cf = fopen(filename, is_recovery_guc_supported ? "a" : "w"); if (cf == NULL) { - fprintf(stderr, _("%s: could not open file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not open file \"%s\": %m", filename); exit(1); } if (fwrite(recoveryconfcontents->data, recoveryconfcontents->len, 1, cf) != 1) { - fprintf(stderr, - _("%s: could not write to file \"%s\": %s\n"), - progname, filename, strerror(errno)); + pg_log_error("could not write to file \"%s\": %m", filename); exit(1); } @@ -1842,7 +1776,7 @@ WriteRecoveryConf(void) cf = fopen(filename, "w"); if (cf == NULL) { - fprintf(stderr, _("%s: could not create file \"%s\": %s\n"), progname, filename, strerror(errno)); + pg_log_error("could not create file \"%s\": %m", filename); exit(1); } @@ -1883,8 +1817,8 @@ BaseBackup(void) { const char *serverver = PQparameterStatus(conn, "server_version"); - fprintf(stderr, _("%s: incompatible server version %s\n"), - progname, serverver ? serverver : "'unknown'"); + pg_log_error("incompatible server version %s", + serverver ? serverver : "'unknown'"); exit(1); } @@ -1898,7 +1832,7 @@ BaseBackup(void) * Error message already written in CheckServerVersionForStreaming(), * but add a hint about using -X none. */ - fprintf(stderr, _("HINT: use -X none or -X fetch to disable log streaming\n")); + pg_log_info("HINT: use -X none or -X fetch to disable log streaming"); exit(1); } @@ -1923,9 +1857,7 @@ BaseBackup(void) maxrate_clause = psprintf("MAX_RATE %u", maxrate); if (verbose) - fprintf(stderr, - _("%s: initiating base backup, waiting for checkpoint to complete\n"), - progname); + pg_log_info("initiating base backup, waiting for checkpoint to complete"); if (showprogress && !verbose) { @@ -1949,8 +1881,8 @@ BaseBackup(void) if (PQsendQuery(conn, basebkp) == 0) { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, "BASE_BACKUP", PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + "BASE_BACKUP", PQerrorMessage(conn)); exit(1); } @@ -1960,22 +1892,21 @@ BaseBackup(void) res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not initiate base backup: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not initiate base backup: %s", + PQerrorMessage(conn)); exit(1); } if (PQntuples(res) != 1) { - fprintf(stderr, - _("%s: server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields\n"), - progname, PQntuples(res), PQnfields(res), 1, 2); + pg_log_error("server returned unexpected response to BASE_BACKUP command; got %d rows and %d fields, expected %d rows and %d fields", + PQntuples(res), PQnfields(res), 1, 2); exit(1); } strlcpy(xlogstart, PQgetvalue(res, 0, 0), sizeof(xlogstart)); if (verbose) - fprintf(stderr, _("%s: checkpoint completed\n"), progname); + pg_log_info("checkpoint completed"); /* * 9.3 and later sends the TLI of the starting point. With older servers, @@ -1990,8 +1921,8 @@ BaseBackup(void) MemSet(xlogend, 0, sizeof(xlogend)); if (verbose && includewal != NO_WAL) - fprintf(stderr, _("%s: write-ahead log start point: %s on timeline %u\n"), - progname, xlogstart, starttli); + pg_log_info("write-ahead log start point: %s on timeline %u", + xlogstart, starttli); /* * Get the header @@ -1999,13 +1930,13 @@ BaseBackup(void) res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not get backup header: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not get backup header: %s", + PQerrorMessage(conn)); exit(1); } if (PQntuples(res) < 1) { - fprintf(stderr, _("%s: no data returned from server\n"), progname); + pg_log_error("no data returned from server"); exit(1); } @@ -2036,9 +1967,8 @@ BaseBackup(void) */ if (format == 't' && strcmp(basedir, "-") == 0 && PQntuples(res) > 1) { - fprintf(stderr, - _("%s: can only write single tablespace to stdout, database has %d\n"), - progname, PQntuples(res)); + pg_log_error("can only write single tablespace to stdout, database has %d", + PQntuples(res)); exit(1); } @@ -2049,8 +1979,7 @@ BaseBackup(void) if (includewal == STREAM_WAL) { if (verbose) - fprintf(stderr, _("%s: starting background WAL receiver\n"), - progname); + pg_log_info("starting background WAL receiver"); StartLogStreamer(xlogstart, starttli, sysidentifier); } @@ -2080,21 +2009,18 @@ BaseBackup(void) res = PQgetResult(conn); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, - _("%s: could not get write-ahead log end position from server: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("could not get write-ahead log end position from server: %s", + PQerrorMessage(conn)); exit(1); } if (PQntuples(res) != 1) { - fprintf(stderr, - _("%s: no write-ahead log end position returned from server\n"), - progname); + pg_log_error("no write-ahead log end position returned from server"); exit(1); } strlcpy(xlogend, PQgetvalue(res, 0, 0), sizeof(xlogend)); if (verbose && includewal != NO_WAL) - fprintf(stderr, _("%s: write-ahead log end point: %s\n"), progname, xlogend); + pg_log_info("write-ahead log end point: %s", xlogend); PQclear(res); res = PQgetResult(conn); @@ -2105,14 +2031,13 @@ BaseBackup(void) if (sqlstate && strcmp(sqlstate, ERRCODE_DATA_CORRUPTED) == 0) { - fprintf(stderr, _("%s: checksum error occurred\n"), - progname); + pg_log_error("checksum error occurred"); checksum_failure = true; } else { - fprintf(stderr, _("%s: final receive failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("final receive failed: %s", + PQerrorMessage(conn)); } exit(1); } @@ -2135,15 +2060,12 @@ BaseBackup(void) #endif if (verbose) - fprintf(stderr, - _("%s: waiting for background process to finish streaming ...\n"), progname); + pg_log_info("waiting for background process to finish streaming ..."); #ifndef WIN32 if (write(bgpipe[1], xlogend, strlen(xlogend)) != strlen(xlogend)) { - fprintf(stderr, - _("%s: could not send command to background pipe: %s\n"), - progname, strerror(errno)); + pg_log_info("could not send command to background pipe: %m"); exit(1); } @@ -2151,20 +2073,17 @@ BaseBackup(void) r = waitpid(bgchild, &status, 0); if (r == (pid_t) -1) { - fprintf(stderr, _("%s: could not wait for child process: %s\n"), - progname, strerror(errno)); + pg_log_error("could not wait for child process: %m"); exit(1); } if (r != bgchild) { - fprintf(stderr, _("%s: child %d died, expected %d\n"), - progname, (int) r, (int) bgchild); + pg_log_error("child %d died, expected %d", (int) r, (int) bgchild); exit(1); } if (status != 0) { - fprintf(stderr, "%s: %s\n", - progname, wait_result_to_str(status)); + pg_log_error("%s", wait_result_to_str(status)); exit(1); } /* Exited normally, we're happy! */ @@ -2177,9 +2096,8 @@ BaseBackup(void) */ if (sscanf(xlogend, "%X/%X", &hi, &lo) != 2) { - fprintf(stderr, - _("%s: could not parse write-ahead log location \"%s\"\n"), - progname, xlogend); + pg_log_error("could not parse write-ahead log location \"%s\"", + xlogend); exit(1); } xlogendptr = ((uint64) hi) << 32 | lo; @@ -2190,21 +2108,19 @@ BaseBackup(void) WAIT_OBJECT_0) { _dosmaperr(GetLastError()); - fprintf(stderr, _("%s: could not wait for child thread: %s\n"), - progname, strerror(errno)); + pg_log_error("could not wait for child thread: %m"); exit(1); } if (GetExitCodeThread((HANDLE) bgchild_handle, &status) == 0) { _dosmaperr(GetLastError()); - fprintf(stderr, _("%s: could not get child thread exit status: %s\n"), - progname, strerror(errno)); + pg_log_error("could not get child thread exit status: %m"); exit(1); } if (status != 0) { - fprintf(stderr, _("%s: child thread exited with error %u\n"), - progname, (unsigned int) status); + pg_log_error("child thread exited with error %u", + (unsigned int) status); exit(1); } /* Exited normally, we're happy */ @@ -2231,21 +2147,20 @@ BaseBackup(void) if (do_sync) { if (verbose) - fprintf(stderr, - _("%s: syncing data to disk ...\n"), progname); + pg_log_info("syncing data to disk ..."); if (format == 't') { if (strcmp(basedir, "-") != 0) - (void) fsync_fname(basedir, true, progname); + (void) fsync_fname(basedir, true); } else { - (void) fsync_pgdata(basedir, progname, serverVersion); + (void) fsync_pgdata(basedir, serverVersion); } } if (verbose) - fprintf(stderr, _("%s: base backup completed\n"), progname); + pg_log_info("base backup completed"); } @@ -2287,6 +2202,7 @@ main(int argc, char **argv) int option_index; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup")); @@ -2325,9 +2241,8 @@ main(int argc, char **argv) format = 't'; else { - fprintf(stderr, - _("%s: invalid output format \"%s\", must be \"plain\" or \"tar\"\n"), - progname, optarg); + pg_log_error("invalid output format \"%s\", must be \"plain\" or \"tar\"\n", + optarg); exit(1); } break; @@ -2370,9 +2285,8 @@ main(int argc, char **argv) } else { - fprintf(stderr, - _("%s: invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"\n"), - progname, optarg); + pg_log_error("invalid wal-method option \"%s\", must be \"fetch\", \"stream\", or \"none\"", + optarg); exit(1); } break; @@ -2399,8 +2313,7 @@ main(int argc, char **argv) compresslevel = atoi(optarg); if (compresslevel < 0 || compresslevel > 9) { - fprintf(stderr, _("%s: invalid compression level \"%s\"\n"), - progname, optarg); + pg_log_error("invalid compression level \"%s\"\n", optarg); exit(1); } break; @@ -2411,8 +2324,8 @@ main(int argc, char **argv) fastcheckpoint = false; else { - fprintf(stderr, _("%s: invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"\n"), - progname, optarg); + pg_log_error("invalid checkpoint argument \"%s\", must be \"fast\" or \"spread\"", + optarg); exit(1); } break; @@ -2438,8 +2351,7 @@ main(int argc, char **argv) standby_message_timeout = atoi(optarg) * 1000; if (standby_message_timeout < 0) { - fprintf(stderr, _("%s: invalid status interval \"%s\"\n"), - progname, optarg); + pg_log_error("invalid status interval \"%s\"", optarg); exit(1); } break; @@ -2468,9 +2380,8 @@ main(int argc, char **argv) */ if (optind < argc) { - fprintf(stderr, - _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2481,7 +2392,7 @@ main(int argc, char **argv) */ if (basedir == NULL) { - fprintf(stderr, _("%s: no target directory specified\n"), progname); + pg_log_error("no target directory specified"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2492,9 +2403,7 @@ main(int argc, char **argv) */ if (format == 'p' && compresslevel != 0) { - fprintf(stderr, - _("%s: only tar mode backups can be compressed\n"), - progname); + pg_log_error("only tar mode backups can be compressed"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2502,9 +2411,7 @@ main(int argc, char **argv) if (format == 't' && includewal == STREAM_WAL && strcmp(basedir, "-") == 0) { - fprintf(stderr, - _("%s: cannot stream write-ahead logs in tar mode to stdout\n"), - progname); + pg_log_error("cannot stream write-ahead logs in tar mode to stdout"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2512,9 +2419,7 @@ main(int argc, char **argv) if (replication_slot && includewal != STREAM_WAL) { - fprintf(stderr, - _("%s: replication slots can only be used with WAL streaming\n"), - progname); + pg_log_error("replication slots can only be used with WAL streaming"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2524,9 +2429,7 @@ main(int argc, char **argv) { if (replication_slot) { - fprintf(stderr, - _("%s: --no-slot cannot be used with slot name\n"), - progname); + pg_log_error("--no-slot cannot be used with slot name"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2538,9 +2441,8 @@ main(int argc, char **argv) { if (!replication_slot) { - fprintf(stderr, - _("%s: %s needs a slot to be specified using --slot\n"), - progname, "--create-slot"); + pg_log_error("%s needs a slot to be specified using --slot", + "--create-slot"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2548,9 +2450,7 @@ main(int argc, char **argv) if (no_slot) { - fprintf(stderr, - _("%s: --create-slot and --no-slot are incompatible options\n"), - progname); + pg_log_error("--create-slot and --no-slot are incompatible options"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2561,9 +2461,7 @@ main(int argc, char **argv) { if (format != 'p') { - fprintf(stderr, - _("%s: WAL directory location can only be specified in plain mode\n"), - progname); + pg_log_error("WAL directory location can only be specified in plain mode"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2573,8 +2471,7 @@ main(int argc, char **argv) canonicalize_path(xlog_dir); if (!is_absolute_path(xlog_dir)) { - fprintf(stderr, _("%s: WAL directory location must be " - "an absolute path\n"), progname); + pg_log_error("WAL directory location must be an absolute path"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); @@ -2584,9 +2481,7 @@ main(int argc, char **argv) #ifndef HAVE_LIBZ if (compresslevel != 0) { - fprintf(stderr, - _("%s: this build does not support compression\n"), - progname); + pg_log_error("this build does not support compression"); exit(1); } #endif @@ -2640,12 +2535,11 @@ main(int argc, char **argv) #ifdef HAVE_SYMLINK if (symlink(xlog_dir, linkloc) != 0) { - fprintf(stderr, _("%s: could not create symbolic link \"%s\": %s\n"), - progname, linkloc, strerror(errno)); + pg_log_error("could not create symbolic link \"%s\": %m", linkloc); exit(1); } #else - fprintf(stderr, _("%s: symlinks are not supported on this platform\n"), progname); + pg_log_error("symlinks are not supported on this platform"); exit(1); #endif free(linkloc); |