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_rewind/libpq_fetch.c | |
parent | b4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff) | |
download | postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.tar.gz |
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_rewind/libpq_fetch.c')
-rw-r--r-- | src/bin/pg_rewind/libpq_fetch.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/src/bin/pg_rewind/libpq_fetch.c b/src/bin/pg_rewind/libpq_fetch.c index d06e277432..11ec045b8e 100644 --- a/src/bin/pg_rewind/libpq_fetch.c +++ b/src/bin/pg_rewind/libpq_fetch.c @@ -24,6 +24,7 @@ #include "libpq-fe.h" #include "catalog/pg_type_d.h" #include "fe_utils/connect.h" +#include "fe_utils/logging.h" #include "port/pg_bswap.h" static PGconn *conn = NULL; @@ -52,7 +53,8 @@ libpqConnect(const char *connstr) pg_fatal("could not connect to server: %s", PQerrorMessage(conn)); - pg_log(PG_PROGRESS, "connected to server\n"); + if (showprogress) + pg_log_info("connected to server"); res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL); if (PQresultStatus(res) != PGRES_TUPLES_OK) @@ -68,7 +70,7 @@ libpqConnect(const char *connstr) */ str = run_simple_query("SELECT pg_is_in_recovery()"); if (strcmp(str, "f") != 0) - pg_fatal("source server must not be in recovery mode\n"); + pg_fatal("source server must not be in recovery mode"); pg_free(str); /* @@ -78,7 +80,7 @@ libpqConnect(const char *connstr) */ str = run_simple_query("SHOW full_page_writes"); if (strcmp(str, "on") != 0) - pg_fatal("full_page_writes must be enabled in the source server\n"); + pg_fatal("full_page_writes must be enabled in the source server"); pg_free(str); /* @@ -113,7 +115,7 @@ run_simple_query(const char *sql) /* sanity check the result set */ if (PQnfields(res) != 1 || PQntuples(res) != 1 || PQgetisnull(res, 0, 0)) - pg_fatal("unexpected result set from query\n"); + pg_fatal("unexpected result set from query"); result = pg_strdup(PQgetvalue(res, 0, 0)); @@ -136,7 +138,7 @@ libpqGetCurrentXlogInsertLocation(void) val = run_simple_query("SELECT pg_current_wal_insert_lsn()"); if (sscanf(val, "%X/%X", &hi, &lo) != 2) - pg_fatal("unrecognized result \"%s\" for current WAL insert location\n", val); + pg_fatal("unrecognized result \"%s\" for current WAL insert location", val); result = ((uint64) hi) << 32 | lo; @@ -191,7 +193,7 @@ libpqProcessFileList(void) /* sanity check the result set */ if (PQnfields(res) != 4) - pg_fatal("unexpected result set while fetching file list\n"); + pg_fatal("unexpected result set while fetching file list"); /* Read result to local variables */ for (i = 0; i < PQntuples(res); i++) @@ -241,10 +243,10 @@ receiveFileChunks(const char *sql) if (PQsendQueryParams(conn, sql, 0, NULL, NULL, NULL, NULL, 1) != 1) pg_fatal("could not send query: %s", PQerrorMessage(conn)); - pg_log(PG_DEBUG, "getting file chunks\n"); + pg_log_debug("getting file chunks"); if (PQsetSingleRowMode(conn) != 1) - pg_fatal("could not set libpq connection to single row mode\n"); + pg_fatal("could not set libpq connection to single row mode"); while ((res = PQgetResult(conn)) != NULL) { @@ -271,13 +273,13 @@ receiveFileChunks(const char *sql) /* sanity check the result set */ if (PQnfields(res) != 3 || PQntuples(res) != 1) - pg_fatal("unexpected result set size while fetching remote files\n"); + pg_fatal("unexpected result set size while fetching remote files"); if (PQftype(res, 0) != TEXTOID || PQftype(res, 1) != INT8OID || PQftype(res, 2) != BYTEAOID) { - pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u\n", + pg_fatal("unexpected data types in result set while fetching remote files: %u %u %u", PQftype(res, 0), PQftype(res, 1), PQftype(res, 2)); } @@ -285,17 +287,17 @@ receiveFileChunks(const char *sql) PQfformat(res, 1) != 1 && PQfformat(res, 2) != 1) { - pg_fatal("unexpected result format while fetching remote files\n"); + pg_fatal("unexpected result format while fetching remote files"); } if (PQgetisnull(res, 0, 0) || PQgetisnull(res, 0, 1)) { - pg_fatal("unexpected null values in result while fetching remote files\n"); + pg_fatal("unexpected null values in result while fetching remote files"); } if (PQgetlength(res, 0, 1) != sizeof(int64)) - pg_fatal("unexpected result length while fetching remote files\n"); + pg_fatal("unexpected result length while fetching remote files"); /* Read result set to local variables */ memcpy(&chunkoff, PQgetvalue(res, 0, 1), sizeof(int64)); @@ -319,8 +321,7 @@ receiveFileChunks(const char *sql) */ if (PQgetisnull(res, 0, 2)) { - pg_log(PG_DEBUG, - "received null value for chunk for file \"%s\", file has been deleted\n", + pg_log_debug("received null value for chunk for file \"%s\", file has been deleted", filename); remove_target_file(filename, true); pg_free(filename); @@ -333,7 +334,7 @@ receiveFileChunks(const char *sql) * translatable strings. */ snprintf(chunkoff_str, sizeof(chunkoff_str), INT64_FORMAT, chunkoff); - pg_log(PG_DEBUG, "received chunk for file \"%s\", offset %s, size %d\n", + pg_log_debug("received chunk for file \"%s\", offset %s, size %d", filename, chunkoff_str, chunksize); open_target_file(filename, false); @@ -367,7 +368,7 @@ libpqGetFile(const char *filename, size_t *filesize) /* sanity check the result set */ if (PQntuples(res) != 1 || PQgetisnull(res, 0, 0)) - pg_fatal("unexpected result set while fetching remote file \"%s\"\n", + pg_fatal("unexpected result set while fetching remote file \"%s\"", filename); /* Read result to local variables */ @@ -378,7 +379,7 @@ libpqGetFile(const char *filename, size_t *filesize) PQclear(res); - pg_log(PG_DEBUG, "fetched file \"%s\", length %d\n", filename, len); + pg_log_debug("fetched file \"%s\", length %d", filename, len); if (filesize) *filesize = len; |