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/streamutil.c | |
parent | b4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff) | |
download | postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.tar.gz postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.zip |
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/streamutil.c')
-rw-r--r-- | src/bin/pg_basebackup/streamutil.c | 97 |
1 files changed, 42 insertions, 55 deletions
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 7e86830ad88..ab2e55d6950 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -26,6 +26,7 @@ #include "common/file_perm.h" #include "datatype/timestamp.h" #include "fe_utils/connect.h" +#include "fe_utils/logging.h" #include "port/pg_bswap.h" #include "pqexpbuffer.h" @@ -90,7 +91,7 @@ GetConnection(void) conn_opts = PQconninfoParse(connection_string, &err_msg); if (conn_opts == NULL) { - fprintf(stderr, "%s: %s", progname, err_msg); + pg_log_error("%s", err_msg); exit(1); } @@ -183,8 +184,7 @@ GetConnection(void) */ if (!tmpconn) { - fprintf(stderr, _("%s: could not connect to server\n"), - progname); + pg_log_error("could not connect to server"); exit(1); } @@ -201,8 +201,8 @@ GetConnection(void) if (PQstatus(tmpconn) != CONNECTION_OK) { - fprintf(stderr, _("%s: could not connect to server: %s"), - progname, PQerrorMessage(tmpconn)); + pg_log_error("could not connect to server: %s", + PQerrorMessage(tmpconn)); PQfinish(tmpconn); free(values); free(keywords); @@ -230,8 +230,8 @@ GetConnection(void) res = PQexec(tmpconn, ALWAYS_SECURE_SEARCH_PATH_SQL); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not clear search_path: %s"), - progname, PQerrorMessage(tmpconn)); + pg_log_error("could not clear search_path: %s", + PQerrorMessage(tmpconn)); PQclear(res); PQfinish(tmpconn); exit(1); @@ -246,18 +246,14 @@ GetConnection(void) tmpparam = PQparameterStatus(tmpconn, "integer_datetimes"); if (!tmpparam) { - fprintf(stderr, - _("%s: could not determine server setting for integer_datetimes\n"), - progname); + pg_log_error("could not determine server setting for integer_datetimes"); PQfinish(tmpconn); exit(1); } if (strcmp(tmpparam, "on") != 0) { - fprintf(stderr, - _("%s: integer_datetimes compile flag does not match server\n"), - progname); + pg_log_error("integer_datetimes compile flag does not match server"); PQfinish(tmpconn); exit(1); } @@ -300,17 +296,16 @@ RetrieveWalSegSize(PGconn *conn) res = PQexec(conn, "SHOW wal_segment_size"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, "SHOW wal_segment_size", PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + "SHOW wal_segment_size", PQerrorMessage(conn)); PQclear(res); return false; } if (PQntuples(res) != 1 || PQnfields(res) < 1) { - fprintf(stderr, - _("%s: could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields\n"), - progname, PQntuples(res), PQnfields(res), 1, 1); + pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields", + PQntuples(res), PQnfields(res), 1, 1); PQclear(res); return false; @@ -319,8 +314,7 @@ RetrieveWalSegSize(PGconn *conn) /* fetch xlog value and unit from the result */ if (sscanf(PQgetvalue(res, 0, 0), "%d%s", &xlog_val, xlog_unit) != 2) { - fprintf(stderr, _("%s: WAL segment size could not be parsed\n"), - progname); + pg_log_error("WAL segment size could not be parsed"); return false; } @@ -335,11 +329,10 @@ RetrieveWalSegSize(PGconn *conn) if (!IsValidWalSegSize(WalSegSz)) { - fprintf(stderr, - ngettext("%s: WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d byte\n", - "%s: WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d bytes\n", - WalSegSz), - progname, WalSegSz); + pg_log_error(ngettext("WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d byte", + "WAL segment size must be a power of two between 1 MB and 1 GB, but the remote server reported a value of %d bytes", + WalSegSz), + WalSegSz); return false; } @@ -374,17 +367,16 @@ RetrieveDataDirCreatePerm(PGconn *conn) res = PQexec(conn, "SHOW data_directory_mode"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, "SHOW data_directory_mode", PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + "SHOW data_directory_mode", PQerrorMessage(conn)); PQclear(res); return false; } if (PQntuples(res) != 1 || PQnfields(res) < 1) { - fprintf(stderr, - _("%s: could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields\n"), - progname, PQntuples(res), PQnfields(res), 1, 1); + pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields", + PQntuples(res), PQnfields(res), 1, 1); PQclear(res); return false; @@ -392,8 +384,8 @@ RetrieveDataDirCreatePerm(PGconn *conn) if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1) { - fprintf(stderr, _("%s: group access flag could not be parsed: %s\n"), - progname, PQgetvalue(res, 0, 0)); + pg_log_error("group access flag could not be parsed: %s", + PQgetvalue(res, 0, 0)); PQclear(res); return false; @@ -427,17 +419,16 @@ RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, res = PQexec(conn, "IDENTIFY_SYSTEM"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, "IDENTIFY_SYSTEM", PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + "IDENTIFY_SYSTEM", PQerrorMessage(conn)); PQclear(res); return false; } if (PQntuples(res) != 1 || PQnfields(res) < 3) { - fprintf(stderr, - _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"), - progname, PQntuples(res), PQnfields(res), 1, 3); + pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields", + PQntuples(res), PQnfields(res), 1, 3); PQclear(res); return false; @@ -456,9 +447,8 @@ RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, { if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2) { - fprintf(stderr, - _("%s: could not parse write-ahead log location \"%s\"\n"), - progname, PQgetvalue(res, 0, 2)); + pg_log_error("could not parse write-ahead log location \"%s\"", + PQgetvalue(res, 0, 2)); PQclear(res); return false; @@ -474,9 +464,8 @@ RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli, { if (PQnfields(res) < 4) { - fprintf(stderr, - _("%s: could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields\n"), - progname, PQntuples(res), PQnfields(res), 1, 4); + pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields", + PQntuples(res), PQnfields(res), 1, 4); PQclear(res); return false; @@ -541,8 +530,8 @@ CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin, } else { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, query->data, PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + query->data, PQerrorMessage(conn)); destroyPQExpBuffer(query); PQclear(res); @@ -552,10 +541,9 @@ CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin, if (PQntuples(res) != 1 || PQnfields(res) != 4) { - fprintf(stderr, - _("%s: could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"), - progname, slot_name, - PQntuples(res), PQnfields(res), 1, 4); + pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields", + slot_name, + PQntuples(res), PQnfields(res), 1, 4); destroyPQExpBuffer(query); PQclear(res); @@ -587,8 +575,8 @@ DropReplicationSlot(PGconn *conn, const char *slot_name) res = PQexec(conn, query->data); if (PQresultStatus(res) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: could not send replication command \"%s\": %s"), - progname, query->data, PQerrorMessage(conn)); + pg_log_error("could not send replication command \"%s\": %s", + query->data, PQerrorMessage(conn)); destroyPQExpBuffer(query); PQclear(res); @@ -597,10 +585,9 @@ DropReplicationSlot(PGconn *conn, const char *slot_name) if (PQntuples(res) != 0 || PQnfields(res) != 0) { - fprintf(stderr, - _("%s: could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields\n"), - progname, slot_name, - PQntuples(res), PQnfields(res), 0, 0); + pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields", + slot_name, + PQntuples(res), PQnfields(res), 0, 0); destroyPQExpBuffer(query); PQclear(res); |