Hi all,

While refactoring some code, I have bumped into $subject, which causes
both tools to have incorrect error message formats and progname being
used all through the code, sometimes incorrectly or just missing.

At the same time, we are missing a call to set_pglocale_pgservice()
for both binaries.

Any objections to the cleanup attached?
--
Michael
diff --git a/contrib/oid2name/oid2name.c b/contrib/oid2name/oid2name.c
index d3a4a50005..b33e4d3d82 100644
--- a/contrib/oid2name/oid2name.c
+++ b/contrib/oid2name/oid2name.c
@@ -11,6 +11,7 @@
 
 #include "catalog/pg_class_d.h"
 
+#include "common/logging.h"
 #include "fe_utils/connect.h"
 #include "libpq-fe.h"
 #include "pg_getopt.h"
@@ -85,6 +86,8 @@ get_opts(int argc, char **argv, struct options *my_opts)
 	const char *progname;
 	int			optindex;
 
+	pg_logging_init(argv[0]);
+	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("oid2name"));
 	progname = get_progname(argv[0]);
 
 	/* set the defaults */
@@ -320,8 +323,8 @@ sql_conn(struct options *my_opts)
 
 		if (!conn)
 		{
-			fprintf(stderr, "%s: could not connect to database %s\n",
-					"oid2name", my_opts->dbname);
+			pg_log_error("could not connect to database %s",
+						 my_opts->dbname);
 			exit(1);
 		}
 
@@ -339,8 +342,8 @@ sql_conn(struct options *my_opts)
 	/* check to see that the backend connection was successfully made */
 	if (PQstatus(conn) == CONNECTION_BAD)
 	{
-		fprintf(stderr, "%s: could not connect to database %s: %s",
-				"oid2name", my_opts->dbname, PQerrorMessage(conn));
+		pg_log_error("could not connect to database %s: %s",
+					 my_opts->dbname, PQerrorMessage(conn));
 		PQfinish(conn);
 		exit(1);
 	}
@@ -348,8 +351,8 @@ sql_conn(struct options *my_opts)
 	res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		fprintf(stderr, "oid2name: could not clear search_path: %s\n",
-				PQerrorMessage(conn));
+		pg_log_error("could not clear search_path: %s",
+					 PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		exit(-1);
@@ -382,8 +385,8 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
 	/* check and deal with errors */
 	if (!res || PQresultStatus(res) > 2)
 	{
-		fprintf(stderr, "oid2name: query failed: %s\n", PQerrorMessage(conn));
-		fprintf(stderr, "oid2name: query was: %s\n", todo);
+		pg_log_error("query failed: %s", PQerrorMessage(conn));
+		pg_log_error("query was: %s", todo);
 
 		PQclear(res);
 		PQfinish(conn);
diff --git a/contrib/vacuumlo/vacuumlo.c b/contrib/vacuumlo/vacuumlo.c
index 73c06a043e..7fbcaee846 100644
--- a/contrib/vacuumlo/vacuumlo.c
+++ b/contrib/vacuumlo/vacuumlo.c
@@ -23,6 +23,7 @@
 
 #include "catalog/pg_class_d.h"
 
+#include "common/logging.h"
 #include "fe_utils/connect.h"
 #include "libpq-fe.h"
 #include "pg_getopt.h"
@@ -109,8 +110,7 @@ vacuumlo(const char *database, const struct _param *param)
 		conn = PQconnectdbParams(keywords, values, true);
 		if (!conn)
 		{
-			fprintf(stderr, "Connection to database \"%s\" failed\n",
-					database);
+			pg_log_error("connection to database \"%s\" failed", database);
 			return -1;
 		}
 
@@ -129,8 +129,8 @@ vacuumlo(const char *database, const struct _param *param)
 	/* check to see that the backend connection was successfully made */
 	if (PQstatus(conn) == CONNECTION_BAD)
 	{
-		fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
-				database, PQerrorMessage(conn));
+		pg_log_error("connection to database \"%s\" failed: %s",
+					 database, PQerrorMessage(conn));
 		PQfinish(conn);
 		return -1;
 	}
@@ -145,8 +145,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		fprintf(stderr, "Failed to set search_path:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to set search_path: %s", PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -165,8 +164,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, buf);
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
 	{
-		fprintf(stderr, "Failed to create temp table:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to create temp table: %s", PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -182,8 +180,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, buf);
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
 	{
-		fprintf(stderr, "Failed to vacuum temp table:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to vacuum temp table: %s", PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -212,8 +209,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, buf);
 	if (PQresultStatus(res) != PGRES_TUPLES_OK)
 	{
-		fprintf(stderr, "Failed to find OID columns:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to find OID columns: %s", PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -238,7 +234,7 @@ vacuumlo(const char *database, const struct _param *param)
 
 		if (!schema || !table || !field)
 		{
-			fprintf(stderr, "%s", PQerrorMessage(conn));
+			pg_log_error("%s", PQerrorMessage(conn));
 			PQclear(res);
 			PQfinish(conn);
 			if (schema != NULL)
@@ -257,9 +253,8 @@ vacuumlo(const char *database, const struct _param *param)
 		res2 = PQexec(conn, buf);
 		if (PQresultStatus(res2) != PGRES_COMMAND_OK)
 		{
-			fprintf(stderr, "Failed to check %s in table %s.%s:\n",
-					field, schema, table);
-			fprintf(stderr, "%s", PQerrorMessage(conn));
+			pg_log_error("failed to check %s in table %s.%s: %s",
+						 field, schema, table, PQerrorMessage(conn));
 			PQclear(res2);
 			PQclear(res);
 			PQfinish(conn);
@@ -288,8 +283,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, "begin");
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
 	{
-		fprintf(stderr, "Failed to start transaction:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to start transaction: %s", PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -302,7 +296,7 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, buf);
 	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);
 		PQfinish(conn);
 		return -1;
@@ -319,7 +313,7 @@ vacuumlo(const char *database, const struct _param *param)
 		res = PQexec(conn, buf);
 		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);
 			PQfinish(conn);
 			return -1;
@@ -347,8 +341,8 @@ vacuumlo(const char *database, const struct _param *param)
 			{
 				if (lo_unlink(conn, lo) < 0)
 				{
-					fprintf(stderr, "\nFailed to remove lo %u: ", lo);
-					fprintf(stderr, "%s", PQerrorMessage(conn));
+					pg_log_error("\nfailed to remove lo %u: %s", lo,
+								 PQerrorMessage(conn));
 					if (PQtransactionStatus(conn) == PQTRANS_INERROR)
 					{
 						success = false;
@@ -367,8 +361,8 @@ vacuumlo(const char *database, const struct _param *param)
 				res2 = PQexec(conn, "commit");
 				if (PQresultStatus(res2) != PGRES_COMMAND_OK)
 				{
-					fprintf(stderr, "Failed to commit transaction:\n");
-					fprintf(stderr, "%s", PQerrorMessage(conn));
+					pg_log_error("failed to commit transaction: %s",
+								 PQerrorMessage(conn));
 					PQclear(res2);
 					PQclear(res);
 					PQfinish(conn);
@@ -378,8 +372,8 @@ vacuumlo(const char *database, const struct _param *param)
 				res2 = PQexec(conn, "begin");
 				if (PQresultStatus(res2) != PGRES_COMMAND_OK)
 				{
-					fprintf(stderr, "Failed to start transaction:\n");
-					fprintf(stderr, "%s", PQerrorMessage(conn));
+					pg_log_error("failed to start transaction: %s",
+								 PQerrorMessage(conn));
 					PQclear(res2);
 					PQclear(res);
 					PQfinish(conn);
@@ -398,8 +392,8 @@ vacuumlo(const char *database, const struct _param *param)
 	res = PQexec(conn, "commit");
 	if (PQresultStatus(res) != PGRES_COMMAND_OK)
 	{
-		fprintf(stderr, "Failed to commit transaction:\n");
-		fprintf(stderr, "%s", PQerrorMessage(conn));
+		pg_log_error("failed to commit transaction: %s",
+					 PQerrorMessage(conn));
 		PQclear(res);
 		PQfinish(conn);
 		return -1;
@@ -471,6 +465,8 @@ main(int argc, char **argv)
 	const char *progname;
 	int			optindex;
 
+	pg_logging_init(argv[0]);
+	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("vacuumlo"));
 	progname = get_progname(argv[0]);
 
 	/* Set default parameter values */
@@ -512,9 +508,7 @@ main(int argc, char **argv)
 				param.transaction_limit = strtol(optarg, NULL, 10);
 				if (param.transaction_limit < 0)
 				{
-					fprintf(stderr,
-							"%s: transaction limit must not be negative (0 disables)\n",
-							progname);
+					pg_log_error("transaction limit must not be negative (0 disables)");
 					exit(1);
 				}
 				break;
@@ -526,7 +520,7 @@ main(int argc, char **argv)
 				port = strtol(optarg, NULL, 10);
 				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);
 				}
 				param.pg_port = pg_strdup(optarg);
@@ -552,7 +546,7 @@ main(int argc, char **argv)
 	/* No database given? Show usage */
 	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);
 		exit(1);
 	}

Attachment: signature.asc
Description: PGP signature

Reply via email to