Make use of generic logging in vacuumlo and oid2name
authorMichael Paquier <michael@paquier.xyz>
Fri, 6 Sep 2019 05:00:13 +0000 (14:00 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 6 Sep 2019 05:00:13 +0000 (14:00 +0900)
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

contrib/oid2name/oid2name.c
contrib/vacuumlo/vacuumlo.c

index df4f9c062ffc4eb886dfd8191b68dd254270c05d..fa1e7959e7722b5ea4929151b575936da28c8feb 100644 (file)
@@ -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,7 @@ get_opts(int argc, char **argv, struct options *my_opts)
    const char *progname;
    int         optindex;
 
+   pg_logging_init(argv[0]);
    progname = get_progname(argv[0]);
 
    /* set the defaults */
@@ -328,8 +330,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);
        }
 
@@ -347,8 +349,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);
    }
@@ -356,8 +358,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);
@@ -390,8 +392,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);
index 73c06a043ed24e2e98956dcf00b43af52c55ca96..533e2ce33c58909f42d54d9cfa8d57003f2160f5 100644 (file)
@@ -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("failed 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,7 @@ main(int argc, char **argv)
    const char *progname;
    int         optindex;
 
+   pg_logging_init(argv[0]);
    progname = get_progname(argv[0]);
 
    /* Set default parameter values */
@@ -512,9 +507,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 +519,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 +545,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);
    }