Make use of generic logging in vacuumlo and oid2name
Doing the switch reduces the footprint of "progname" in both utilities for the messages produced. This also cleans up a couple of inconsistencies in the message formats. Author: Michael Paquier Reviewed-by: Álvaro Herrera, Peter Eisentraut Discussion: https://postgr.es/m/20190820012819.GA8326@paquier.xyz
This commit is contained in:
parent
7de19fbc0b
commit
fc8cb94bf4
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "catalog/pg_class_d.h"
|
#include "catalog/pg_class_d.h"
|
||||||
|
|
||||||
|
#include "common/logging.h"
|
||||||
#include "fe_utils/connect.h"
|
#include "fe_utils/connect.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
#include "pg_getopt.h"
|
#include "pg_getopt.h"
|
||||||
@ -85,6 +86,7 @@ get_opts(int argc, char **argv, struct options *my_opts)
|
|||||||
const char *progname;
|
const char *progname;
|
||||||
int optindex;
|
int optindex;
|
||||||
|
|
||||||
|
pg_logging_init(argv[0]);
|
||||||
progname = get_progname(argv[0]);
|
progname = get_progname(argv[0]);
|
||||||
|
|
||||||
/* set the defaults */
|
/* set the defaults */
|
||||||
@ -328,8 +330,8 @@ sql_conn(struct options *my_opts)
|
|||||||
|
|
||||||
if (!conn)
|
if (!conn)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: could not connect to database %s\n",
|
pg_log_error("could not connect to database %s",
|
||||||
"oid2name", my_opts->dbname);
|
my_opts->dbname);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,8 +349,8 @@ sql_conn(struct options *my_opts)
|
|||||||
/* check to see that the backend connection was successfully made */
|
/* check to see that the backend connection was successfully made */
|
||||||
if (PQstatus(conn) == CONNECTION_BAD)
|
if (PQstatus(conn) == CONNECTION_BAD)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: could not connect to database %s: %s",
|
pg_log_error("could not connect to database %s: %s",
|
||||||
"oid2name", my_opts->dbname, PQerrorMessage(conn));
|
my_opts->dbname, PQerrorMessage(conn));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -356,7 +358,7 @@ sql_conn(struct options *my_opts)
|
|||||||
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "oid2name: could not clear search_path: %s\n",
|
pg_log_error("could not clear search_path: %s",
|
||||||
PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
@ -390,8 +392,8 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
|
|||||||
/* check and deal with errors */
|
/* check and deal with errors */
|
||||||
if (!res || PQresultStatus(res) > 2)
|
if (!res || PQresultStatus(res) > 2)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "oid2name: query failed: %s\n", PQerrorMessage(conn));
|
pg_log_error("query failed: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "oid2name: query was: %s\n", todo);
|
pg_log_error("query was: %s", todo);
|
||||||
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
|
@ -23,6 +23,7 @@
|
|||||||
|
|
||||||
#include "catalog/pg_class_d.h"
|
#include "catalog/pg_class_d.h"
|
||||||
|
|
||||||
|
#include "common/logging.h"
|
||||||
#include "fe_utils/connect.h"
|
#include "fe_utils/connect.h"
|
||||||
#include "libpq-fe.h"
|
#include "libpq-fe.h"
|
||||||
#include "pg_getopt.h"
|
#include "pg_getopt.h"
|
||||||
@ -109,8 +110,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
conn = PQconnectdbParams(keywords, values, true);
|
conn = PQconnectdbParams(keywords, values, true);
|
||||||
if (!conn)
|
if (!conn)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Connection to database \"%s\" failed\n",
|
pg_log_error("connection to database \"%s\" failed", database);
|
||||||
database);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
/* check to see that the backend connection was successfully made */
|
/* check to see that the backend connection was successfully made */
|
||||||
if (PQstatus(conn) == CONNECTION_BAD)
|
if (PQstatus(conn) == CONNECTION_BAD)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
|
pg_log_error("connection to database \"%s\" failed: %s",
|
||||||
database, PQerrorMessage(conn));
|
database, PQerrorMessage(conn));
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -145,8 +145,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to set search_path:\n");
|
pg_log_error("failed to set search_path: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -165,8 +164,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, buf);
|
res = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to create temp table:\n");
|
pg_log_error("failed to create temp table: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -182,8 +180,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, buf);
|
res = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to vacuum temp table:\n");
|
pg_log_error("failed to vacuum temp table: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -212,8 +209,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, buf);
|
res = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to find OID columns:\n");
|
pg_log_error("failed to find OID columns: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -238,7 +234,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
|
|
||||||
if (!schema || !table || !field)
|
if (!schema || !table || !field)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
pg_log_error("%s", PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
if (schema != NULL)
|
if (schema != NULL)
|
||||||
@ -257,9 +253,8 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res2 = PQexec(conn, buf);
|
res2 = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to check %s in table %s.%s:\n",
|
pg_log_error("failed to check %s in table %s.%s: %s",
|
||||||
field, schema, table);
|
field, schema, table, PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res2);
|
PQclear(res2);
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
@ -288,8 +283,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, "begin");
|
res = PQexec(conn, "begin");
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to start transaction:\n");
|
pg_log_error("failed to start transaction: %s", PQerrorMessage(conn));
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -302,7 +296,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, buf);
|
res = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
|
pg_log_error("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -319,7 +313,7 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, buf);
|
res = PQexec(conn, buf);
|
||||||
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
if (PQresultStatus(res) != PGRES_TUPLES_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "FETCH FORWARD failed: %s", PQerrorMessage(conn));
|
pg_log_error("FETCH FORWARD failed: %s", PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -347,8 +341,8 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
{
|
{
|
||||||
if (lo_unlink(conn, lo) < 0)
|
if (lo_unlink(conn, lo) < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "\nFailed to remove lo %u: ", lo);
|
pg_log_error("failed to remove lo %u: %s", lo,
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
if (PQtransactionStatus(conn) == PQTRANS_INERROR)
|
if (PQtransactionStatus(conn) == PQTRANS_INERROR)
|
||||||
{
|
{
|
||||||
success = false;
|
success = false;
|
||||||
@ -367,8 +361,8 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res2 = PQexec(conn, "commit");
|
res2 = PQexec(conn, "commit");
|
||||||
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to commit transaction:\n");
|
pg_log_error("failed to commit transaction: %s",
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
PQclear(res2);
|
PQclear(res2);
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
@ -378,8 +372,8 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res2 = PQexec(conn, "begin");
|
res2 = PQexec(conn, "begin");
|
||||||
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to start transaction:\n");
|
pg_log_error("failed to start transaction: %s",
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
PQclear(res2);
|
PQclear(res2);
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
@ -398,8 +392,8 @@ vacuumlo(const char *database, const struct _param *param)
|
|||||||
res = PQexec(conn, "commit");
|
res = PQexec(conn, "commit");
|
||||||
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Failed to commit transaction:\n");
|
pg_log_error("failed to commit transaction: %s",
|
||||||
fprintf(stderr, "%s", PQerrorMessage(conn));
|
PQerrorMessage(conn));
|
||||||
PQclear(res);
|
PQclear(res);
|
||||||
PQfinish(conn);
|
PQfinish(conn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -471,6 +465,7 @@ main(int argc, char **argv)
|
|||||||
const char *progname;
|
const char *progname;
|
||||||
int optindex;
|
int optindex;
|
||||||
|
|
||||||
|
pg_logging_init(argv[0]);
|
||||||
progname = get_progname(argv[0]);
|
progname = get_progname(argv[0]);
|
||||||
|
|
||||||
/* Set default parameter values */
|
/* Set default parameter values */
|
||||||
@ -512,9 +507,7 @@ main(int argc, char **argv)
|
|||||||
param.transaction_limit = strtol(optarg, NULL, 10);
|
param.transaction_limit = strtol(optarg, NULL, 10);
|
||||||
if (param.transaction_limit < 0)
|
if (param.transaction_limit < 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr,
|
pg_log_error("transaction limit must not be negative (0 disables)");
|
||||||
"%s: transaction limit must not be negative (0 disables)\n",
|
|
||||||
progname);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -526,7 +519,7 @@ main(int argc, char **argv)
|
|||||||
port = strtol(optarg, NULL, 10);
|
port = strtol(optarg, NULL, 10);
|
||||||
if ((port < 1) || (port > 65535))
|
if ((port < 1) || (port > 65535))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "%s: invalid port number: %s\n", progname, optarg);
|
pg_log_error("invalid port number: %s", optarg);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
param.pg_port = pg_strdup(optarg);
|
param.pg_port = pg_strdup(optarg);
|
||||||
@ -552,7 +545,7 @@ main(int argc, char **argv)
|
|||||||
/* No database given? Show usage */
|
/* No database given? Show usage */
|
||||||
if (optind >= argc)
|
if (optind >= argc)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "vacuumlo: missing required argument: database name\n");
|
pg_log_error("missing required argument: database name");
|
||||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user