diff options
author | Peter Eisentraut | 2019-04-01 12:24:37 +0000 |
---|---|---|
committer | Peter Eisentraut | 2019-04-01 18:01:35 +0000 |
commit | cc8d41511721d25d557fc02a46c053c0a602fed0 (patch) | |
tree | d2f92acac085be1b9cc4756260c7a4f83d1b0041 /src/bin/scripts | |
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 <[email protected]>
Reviewed-by: Arthur Zakirov <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/[email protected]
Diffstat (limited to 'src/bin/scripts')
-rw-r--r-- | src/bin/scripts/clusterdb.c | 20 | ||||
-rw-r--r-- | src/bin/scripts/common.c | 30 | ||||
-rw-r--r-- | src/bin/scripts/createdb.c | 22 | ||||
-rw-r--r-- | src/bin/scripts/createuser.c | 13 | ||||
-rw-r--r-- | src/bin/scripts/dropdb.c | 11 | ||||
-rw-r--r-- | src/bin/scripts/dropuser.c | 12 | ||||
-rw-r--r-- | src/bin/scripts/nls.mk | 6 | ||||
-rw-r--r-- | src/bin/scripts/pg_isready.c | 10 | ||||
-rw-r--r-- | src/bin/scripts/reindexdb.c | 46 | ||||
-rw-r--r-- | src/bin/scripts/vacuumdb.c | 67 |
10 files changed, 118 insertions, 119 deletions
diff --git a/src/bin/scripts/clusterdb.c b/src/bin/scripts/clusterdb.c index cc6efce5960..dd0ba68864d 100644 --- a/src/bin/scripts/clusterdb.c +++ b/src/bin/scripts/clusterdb.c @@ -11,6 +11,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -62,6 +63,7 @@ main(int argc, char *argv[]) bool verbose = false; SimpleStringList tables = {NULL, NULL}; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -125,8 +127,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); } @@ -137,15 +139,13 @@ main(int argc, char *argv[]) { if (dbname) { - fprintf(stderr, _("%s: cannot cluster all databases and a specific one at the same time\n"), - progname); + pg_log_error("cannot cluster all databases and a specific one at the same time"); exit(1); } if (tables.head != NULL) { - fprintf(stderr, _("%s: cannot cluster specific table(s) in all databases\n"), - progname); + pg_log_error("cannot cluster specific table(s) in all databases"); exit(1); } @@ -213,11 +213,11 @@ cluster_one_database(const char *dbname, bool verbose, const char *table, if (!executeMaintenanceCommand(conn, sql.data, echo)) { if (table) - fprintf(stderr, _("%s: clustering of table \"%s\" in database \"%s\" failed: %s"), - progname, table, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("clustering of table \"%s\" in database \"%s\" failed: %s", + table, PQdb(conn), PQerrorMessage(conn)); else - fprintf(stderr, _("%s: clustering of database \"%s\" failed: %s"), - progname, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("clustering of database \"%s\" failed: %s", + PQdb(conn), PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/common.c b/src/bin/scripts/common.c index 7139b7c6672..a661556ba91 100644 --- a/src/bin/scripts/common.c +++ b/src/bin/scripts/common.c @@ -19,6 +19,7 @@ #include "common.h" #include "fe_utils/connect.h" +#include "fe_utils/logging.h" #include "fe_utils/string_utils.h" @@ -113,8 +114,8 @@ connectDatabase(const char *dbname, const char *pghost, if (!conn) { - fprintf(stderr, _("%s: could not connect to database %s: out of memory\n"), - progname, dbname); + pg_log_error("could not connect to database %s: out of memory", + dbname); exit(1); } @@ -140,8 +141,8 @@ connectDatabase(const char *dbname, const char *pghost, PQfinish(conn); return NULL; } - fprintf(stderr, _("%s: could not connect to database %s: %s"), - progname, dbname, PQerrorMessage(conn)); + pg_log_error("could not connect to database %s: %s", + dbname, PQerrorMessage(conn)); exit(1); } @@ -193,10 +194,8 @@ executeQuery(PGconn *conn, const char *query, const char *progname, bool echo) if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { - fprintf(stderr, _("%s: query failed: %s"), - progname, PQerrorMessage(conn)); - fprintf(stderr, _("%s: query was: %s\n"), - progname, query); + pg_log_error("query failed: %s", PQerrorMessage(conn)); + pg_log_info("query was: %s", query); PQfinish(conn); exit(1); } @@ -221,10 +220,8 @@ executeCommand(PGconn *conn, const char *query, if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: query failed: %s"), - progname, PQerrorMessage(conn)); - fprintf(stderr, _("%s: query was: %s\n"), - progname, query); + pg_log_error("query failed: %s", PQerrorMessage(conn)); + pg_log_info("query was: %s", query); PQfinish(conn); exit(1); } @@ -347,11 +344,10 @@ appendQualifiedRelation(PQExpBuffer buf, const char *spec, ntups = PQntuples(res); if (ntups != 1) { - fprintf(stderr, - ngettext("%s: query returned %d row instead of one: %s\n", - "%s: query returned %d rows instead of one: %s\n", - ntups), - progname, ntups, sql.data); + pg_log_error(ngettext("query returned %d row instead of one: %s", + "query returned %d rows instead of one: %s", + ntups), + ntups, sql.data); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/createdb.c b/src/bin/scripts/createdb.c index b40eea4365e..e4b497859b5 100644 --- a/src/bin/scripts/createdb.c +++ b/src/bin/scripts/createdb.c @@ -12,6 +12,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/string_utils.h" @@ -64,6 +65,7 @@ main(int argc, char *argv[]) PGconn *conn; PGresult *result; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -133,8 +135,8 @@ main(int argc, char *argv[]) comment = argv[optind + 1]; break; default: - fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind + 2]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind + 2]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -143,14 +145,12 @@ main(int argc, char *argv[]) { if (lc_ctype) { - fprintf(stderr, _("%s: only one of --locale and --lc-ctype can be specified\n"), - progname); + pg_log_error("only one of --locale and --lc-ctype can be specified"); exit(1); } if (lc_collate) { - fprintf(stderr, _("%s: only one of --locale and --lc-collate can be specified\n"), - progname); + pg_log_error("only one of --locale and --lc-collate can be specified"); exit(1); } lc_ctype = locale; @@ -161,8 +161,7 @@ main(int argc, char *argv[]) { if (pg_char_to_encoding(encoding) < 0) { - fprintf(stderr, _("%s: \"%s\" is not a valid encoding name\n"), - progname, encoding); + pg_log_error("\"%s\" is not a valid encoding name", encoding); exit(1); } } @@ -210,8 +209,7 @@ main(int argc, char *argv[]) if (PQresultStatus(result) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: database creation failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("database creation failed: %s", PQerrorMessage(conn)); PQfinish(conn); exit(1); } @@ -230,8 +228,8 @@ main(int argc, char *argv[]) if (PQresultStatus(result) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: comment creation failed (database was created): %s"), - progname, PQerrorMessage(conn)); + pg_log_error("comment creation failed (database was created): %s", + PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/createuser.c b/src/bin/scripts/createuser.c index ae546c70fbd..6db0dbf2a37 100644 --- a/src/bin/scripts/createuser.c +++ b/src/bin/scripts/createuser.c @@ -12,6 +12,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -81,6 +82,7 @@ main(int argc, char *argv[]) PGconn *conn; PGresult *result; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -176,8 +178,8 @@ main(int argc, char *argv[]) newuser = argv[optind]; break; default: - fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind + 1]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind + 1]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -269,8 +271,8 @@ main(int argc, char *argv[]) NULL); if (!encrypted_password) { - fprintf(stderr, _("%s: password encryption failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("password encryption failed: %s", + PQerrorMessage(conn)); exit(1); } appendStringLiteralConn(&sql, encrypted_password, conn); @@ -324,8 +326,7 @@ main(int argc, char *argv[]) if (PQresultStatus(result) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: creation of new role failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("creation of new role failed: %s", PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/dropdb.c b/src/bin/scripts/dropdb.c index df7823df991..42a9bd46860 100644 --- a/src/bin/scripts/dropdb.c +++ b/src/bin/scripts/dropdb.c @@ -12,6 +12,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/string_utils.h" @@ -54,6 +55,7 @@ main(int argc, char *argv[]) PGconn *conn; PGresult *result; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -99,15 +101,15 @@ main(int argc, char *argv[]) switch (argc - optind) { case 0: - fprintf(stderr, _("%s: missing required argument database name\n"), progname); + pg_log_error("missing required argument database name"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); case 1: dbname = argv[optind]; break; default: - fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind + 1]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind + 1]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -137,8 +139,7 @@ main(int argc, char *argv[]) result = PQexec(conn, sql.data); if (PQresultStatus(result) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: database removal failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("database removal failed: %s", PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/dropuser.c b/src/bin/scripts/dropuser.c index 62971131c54..831243815a3 100644 --- a/src/bin/scripts/dropuser.c +++ b/src/bin/scripts/dropuser.c @@ -12,6 +12,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/string_utils.h" @@ -53,6 +54,7 @@ main(int argc, char *argv[]) PGconn *conn; PGresult *result; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -100,8 +102,8 @@ main(int argc, char *argv[]) dropuser = argv[optind]; break; default: - fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), - progname, argv[optind + 1]); + pg_log_error("too many command-line arguments (first is \"%s\")", + argv[optind + 1]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -116,7 +118,7 @@ main(int argc, char *argv[]) } else { - fprintf(stderr, _("%s: missing required argument role name\n"), progname); + pg_log_error("missing required argument role name"); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } @@ -142,8 +144,8 @@ main(int argc, char *argv[]) if (PQresultStatus(result) != PGRES_COMMAND_OK) { - fprintf(stderr, _("%s: removal of role \"%s\" failed: %s"), - progname, dropuser, PQerrorMessage(conn)); + pg_log_error("removal of role \"%s\" failed: %s", + dropuser, PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/nls.mk b/src/bin/scripts/nls.mk index 4038cdb3b69..2c99a6a4618 100644 --- a/src/bin/scripts/nls.mk +++ b/src/bin/scripts/nls.mk @@ -1,11 +1,13 @@ # src/bin/scripts/nls.mk CATALOG_NAME = pgscripts AVAIL_LANGUAGES = cs de es fr he it ja ko pl pt_BR ru sv tr zh_CN -GETTEXT_FILES = createdb.c createuser.c \ +GETTEXT_FILES = $(FRONTEND_COMMON_GETTEXT_FILES) \ + createdb.c createuser.c \ dropdb.c dropuser.c \ clusterdb.c vacuumdb.c reindexdb.c \ pg_isready.c \ common.c \ ../../fe_utils/print.c \ ../../common/fe_memutils.c ../../common/username.c -GETTEXT_TRIGGERS = simple_prompt yesno_prompt +GETTEXT_TRIGGERS = $(FRONTEND_COMMON_GETTEXT_TRIGGERS) simple_prompt yesno_prompt +GETTEXT_FLAGS = $(FRONTEND_COMMON_GETTEXT_FLAGS) diff --git a/src/bin/scripts/pg_isready.c b/src/bin/scripts/pg_isready.c index 26b21dcbb07..fe15507caf7 100644 --- a/src/bin/scripts/pg_isready.c +++ b/src/bin/scripts/pg_isready.c @@ -11,6 +11,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #define DEFAULT_CONNECT_TIMEOUT "3" @@ -63,6 +64,7 @@ main(int argc, char **argv) {NULL, 0, NULL, 0} }; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); handle_help_version_opts(argc, argv, progname, help); @@ -102,8 +104,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); /* @@ -139,7 +141,7 @@ main(int argc, char **argv) opts = PQconninfoParse(pgdbname, &errmsg); if (opts == NULL) { - fprintf(stderr, _("%s: %s"), progname, errmsg); + pg_log_error("%s", errmsg); exit(PQPING_NO_ATTEMPT); } } @@ -147,7 +149,7 @@ main(int argc, char **argv) defs = PQconndefaults(); if (defs == NULL) { - fprintf(stderr, _("%s: could not fetch default options\n"), progname); + pg_log_error("could not fetch default options"); exit(PQPING_NO_ATTEMPT); } diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c index 438500cb08a..d6f3efd313d 100644 --- a/src/bin/scripts/reindexdb.c +++ b/src/bin/scripts/reindexdb.c @@ -11,6 +11,7 @@ #include "postgres_fe.h" #include "common.h" +#include "fe_utils/logging.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -75,6 +76,7 @@ main(int argc, char *argv[]) SimpleStringList tables = {NULL, NULL}; SimpleStringList schemas = {NULL, NULL}; + pg_logging_init(argv[0]); progname = get_progname(argv[0]); set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); @@ -151,8 +153,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); } @@ -163,27 +165,27 @@ main(int argc, char *argv[]) { if (dbname) { - fprintf(stderr, _("%s: cannot reindex all databases and a specific one at the same time\n"), progname); + pg_log_error("cannot reindex all databases and a specific one at the same time"); exit(1); } if (syscatalog) { - fprintf(stderr, _("%s: cannot reindex all databases and system catalogs at the same time\n"), progname); + pg_log_error("cannot reindex all databases and system catalogs at the same time"); exit(1); } if (schemas.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific schema(s) in all databases\n"), progname); + pg_log_error("cannot reindex specific schema(s) in all databases"); exit(1); } if (tables.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific table(s) in all databases\n"), progname); + pg_log_error("cannot reindex specific table(s) in all databases"); exit(1); } if (indexes.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific index(es) in all databases\n"), progname); + pg_log_error("cannot reindex specific index(es) in all databases"); exit(1); } @@ -194,17 +196,17 @@ main(int argc, char *argv[]) { if (schemas.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific schema(s) and system catalogs at the same time\n"), progname); + pg_log_error("cannot reindex specific schema(s) and system catalogs at the same time"); exit(1); } if (tables.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific table(s) and system catalogs at the same time\n"), progname); + pg_log_error("cannot reindex specific table(s) and system catalogs at the same time"); exit(1); } if (indexes.head != NULL) { - fprintf(stderr, _("%s: cannot reindex specific index(es) and system catalogs at the same time\n"), progname); + pg_log_error("cannot reindex specific index(es) and system catalogs at the same time"); exit(1); } @@ -293,8 +295,8 @@ reindex_one_database(const char *name, const char *dbname, const char *type, if (concurrently && PQserverVersion(conn) < 120000) { PQfinish(conn); - fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL %s\n"), - progname, "concurrently", "12"); + pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL %s", + "concurrently", "12"); exit(1); } @@ -321,17 +323,17 @@ reindex_one_database(const char *name, const char *dbname, const char *type, if (!executeMaintenanceCommand(conn, sql.data, echo)) { if (strcmp(type, "TABLE") == 0) - fprintf(stderr, _("%s: reindexing of table \"%s\" in database \"%s\" failed: %s"), - progname, name, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("reindexing of table \"%s\" in database \"%s\" failed: %s", + name, PQdb(conn), PQerrorMessage(conn)); if (strcmp(type, "INDEX") == 0) - fprintf(stderr, _("%s: reindexing of index \"%s\" in database \"%s\" failed: %s"), - progname, name, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("reindexing of index \"%s\" in database \"%s\" failed: %s", + name, PQdb(conn), PQerrorMessage(conn)); if (strcmp(type, "SCHEMA") == 0) - fprintf(stderr, _("%s: reindexing of schema \"%s\" in database \"%s\" failed: %s"), - progname, name, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("reindexing of schema \"%s\" in database \"%s\" failed: %s", + name, PQdb(conn), PQerrorMessage(conn)); else - fprintf(stderr, _("%s: reindexing of database \"%s\" failed: %s"), - progname, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("reindexing of database \"%s\" failed: %s", + PQdb(conn), PQerrorMessage(conn)); PQfinish(conn); exit(1); } @@ -407,8 +409,8 @@ reindex_system_catalogs(const char *dbname, const char *host, const char *port, if (!executeMaintenanceCommand(conn, sql.data, echo)) { - fprintf(stderr, _("%s: reindexing of system catalogs failed: %s"), - progname, PQerrorMessage(conn)); + pg_log_error("reindexing of system catalogs failed: %s", + PQerrorMessage(conn)); PQfinish(conn); exit(1); } diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index 5ac41ea757b..25ff19e0a1d 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -20,6 +20,7 @@ #include "common.h" #include "fe_utils/connect.h" +#include "fe_utils/logging.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -141,8 +142,8 @@ main(int argc, char *argv[]) /* initialize options to all false */ memset(&vacopts, 0, sizeof(vacopts)); + pg_logging_init(argv[0]); progname = get_progname(argv[0]); - set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts")); handle_help_version_opts(argc, argv, "vacuumdb", help); @@ -203,14 +204,13 @@ main(int argc, char *argv[]) concurrentCons = atoi(optarg); if (concurrentCons <= 0) { - fprintf(stderr, _("%s: number of parallel jobs must be at least 1\n"), - progname); + pg_log_error("number of parallel jobs must be at least 1"); exit(1); } if (concurrentCons > FD_SETSIZE - 1) { - fprintf(stderr, _("%s: too many parallel jobs requested (maximum: %d)\n"), - progname, FD_SETSIZE - 1); + pg_log_error("too many parallel jobs requested (maximum: %d)", + FD_SETSIZE - 1); exit(1); } break; @@ -230,8 +230,7 @@ main(int argc, char *argv[]) vacopts.min_xid_age = atoi(optarg); if (vacopts.min_xid_age <= 0) { - fprintf(stderr, _("%s: minimum transaction ID age must be at least 1\n"), - progname); + pg_log_error("minimum transaction ID age must be at least 1"); exit(1); } break; @@ -239,8 +238,7 @@ main(int argc, char *argv[]) vacopts.min_mxid_age = atoi(optarg); if (vacopts.min_mxid_age <= 0) { - fprintf(stderr, _("%s: minimum multixact ID age must be at least 1\n"), - progname); + pg_log_error("minimum multixact ID age must be at least 1"); exit(1); } break; @@ -262,8 +260,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); } @@ -272,20 +270,20 @@ main(int argc, char *argv[]) { if (vacopts.full) { - fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"), - progname, "full"); + pg_log_error("cannot use the \"%s\" option when performing only analyze", + "full"); exit(1); } if (vacopts.freeze) { - fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"), - progname, "freeze"); + pg_log_error("cannot use the \"%s\" option when performing only analyze", + "freeze"); exit(1); } if (vacopts.disable_page_skipping) { - fprintf(stderr, _("%s: cannot use the \"%s\" option when performing only analyze\n"), - progname, "disable-page-skipping"); + pg_log_error("cannot use the \"%s\" option when performing only analyze", + "disable-page-skipping"); exit(1); } /* allow 'and_analyze' with 'analyze_only' */ @@ -301,14 +299,12 @@ main(int argc, char *argv[]) { if (dbname) { - fprintf(stderr, _("%s: cannot vacuum all databases and a specific one at the same time\n"), - progname); + pg_log_error("cannot vacuum all databases and a specific one at the same time"); exit(1); } if (tables.head != NULL) { - fprintf(stderr, _("%s: cannot vacuum specific table(s) in all databases\n"), - progname); + pg_log_error("cannot vacuum specific table(s) in all databases"); exit(1); } @@ -413,30 +409,30 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, if (vacopts->disable_page_skipping && PQserverVersion(conn) < 90600) { PQfinish(conn); - fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL 9.6\n"), - progname, "disable-page-skipping"); + pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL 9.6", + "disable-page-skipping"); exit(1); } if (vacopts->skip_locked && PQserverVersion(conn) < 120000) { PQfinish(conn); - fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL 12\n"), - progname, "skip-locked"); + pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL 12", + "skip-locked"); exit(1); } if (vacopts->min_xid_age != 0 && PQserverVersion(conn) < 90600) { - fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL 9.6\n"), - progname, "--min-xid-age"); + pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL 9.6", + "--min-xid-age"); exit(1); } if (vacopts->min_mxid_age != 0 && PQserverVersion(conn) < 90600) { - fprintf(stderr, _("%s: cannot use the \"%s\" option on server versions older than PostgreSQL 9.6\n"), - progname, "--min-mxid-age"); + pg_log_error("cannot use the \"%s\" option on server versions older than PostgreSQL 9.6", + "--min-mxid-age"); exit(1); } @@ -940,12 +936,11 @@ run_vacuum_command(PGconn *conn, const char *sql, bool echo, if (!status) { if (table) - fprintf(stderr, - _("%s: vacuuming of table \"%s\" in database \"%s\" failed: %s"), - progname, table, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("vacuuming of table \"%s\" in database \"%s\" failed: %s", + table, PQdb(conn), PQerrorMessage(conn)); else - fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"), - progname, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("vacuuming of database \"%s\" failed: %s", + PQdb(conn), PQerrorMessage(conn)); if (!async) { @@ -1079,8 +1074,8 @@ ProcessQueryResult(PGconn *conn, PGresult *result, const char *progname) { char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE); - fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"), - progname, PQdb(conn), PQerrorMessage(conn)); + pg_log_error("vacuuming of database \"%s\" failed: %s", + PQdb(conn), PQerrorMessage(conn)); if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0) { |