Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fc8cb94

Browse files
committed
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
1 parent 7de19fb commit fc8cb94

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

contrib/oid2name/oid2name.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "catalog/pg_class_d.h"
1313

14+
#include "common/logging.h"
1415
#include "fe_utils/connect.h"
1516
#include "libpq-fe.h"
1617
#include "pg_getopt.h"
@@ -85,6 +86,7 @@ get_opts(int argc, char **argv, struct options *my_opts)
8586
const char *progname;
8687
int optindex;
8788

89+
pg_logging_init(argv[0]);
8890
progname = get_progname(argv[0]);
8991

9092
/* set the defaults */
@@ -328,8 +330,8 @@ sql_conn(struct options *my_opts)
328330

329331
if (!conn)
330332
{
331-
fprintf(stderr, "%s: could not connect to database %s\n",
332-
"oid2name", my_opts->dbname);
333+
pg_log_error("could not connect to database %s",
334+
my_opts->dbname);
333335
exit(1);
334336
}
335337

@@ -347,17 +349,17 @@ sql_conn(struct options *my_opts)
347349
/* check to see that the backend connection was successfully made */
348350
if (PQstatus(conn) == CONNECTION_BAD)
349351
{
350-
fprintf(stderr, "%s: could not connect to database %s: %s",
351-
"oid2name", my_opts->dbname, PQerrorMessage(conn));
352+
pg_log_error("could not connect to database %s: %s",
353+
my_opts->dbname, PQerrorMessage(conn));
352354
PQfinish(conn);
353355
exit(1);
354356
}
355357

356358
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
357359
if (PQresultStatus(res) != PGRES_TUPLES_OK)
358360
{
359-
fprintf(stderr, "oid2name: could not clear search_path: %s\n",
360-
PQerrorMessage(conn));
361+
pg_log_error("could not clear search_path: %s",
362+
PQerrorMessage(conn));
361363
PQclear(res);
362364
PQfinish(conn);
363365
exit(-1);
@@ -390,8 +392,8 @@ sql_exec(PGconn *conn, const char *todo, bool quiet)
390392
/* check and deal with errors */
391393
if (!res || PQresultStatus(res) > 2)
392394
{
393-
fprintf(stderr, "oid2name: query failed: %s\n", PQerrorMessage(conn));
394-
fprintf(stderr, "oid2name: query was: %s\n", todo);
395+
pg_log_error("query failed: %s", PQerrorMessage(conn));
396+
pg_log_error("query was: %s", todo);
395397

396398
PQclear(res);
397399
PQfinish(conn);

contrib/vacuumlo/vacuumlo.c

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "catalog/pg_class_d.h"
2525

26+
#include "common/logging.h"
2627
#include "fe_utils/connect.h"
2728
#include "libpq-fe.h"
2829
#include "pg_getopt.h"
@@ -109,8 +110,7 @@ vacuumlo(const char *database, const struct _param *param)
109110
conn = PQconnectdbParams(keywords, values, true);
110111
if (!conn)
111112
{
112-
fprintf(stderr, "Connection to database \"%s\" failed\n",
113-
database);
113+
pg_log_error("connection to database \"%s\" failed", database);
114114
return -1;
115115
}
116116

@@ -129,8 +129,8 @@ vacuumlo(const char *database, const struct _param *param)
129129
/* check to see that the backend connection was successfully made */
130130
if (PQstatus(conn) == CONNECTION_BAD)
131131
{
132-
fprintf(stderr, "Connection to database \"%s\" failed:\n%s",
133-
database, PQerrorMessage(conn));
132+
pg_log_error("connection to database \"%s\" failed: %s",
133+
database, PQerrorMessage(conn));
134134
PQfinish(conn);
135135
return -1;
136136
}
@@ -145,8 +145,7 @@ vacuumlo(const char *database, const struct _param *param)
145145
res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
146146
if (PQresultStatus(res) != PGRES_TUPLES_OK)
147147
{
148-
fprintf(stderr, "Failed to set search_path:\n");
149-
fprintf(stderr, "%s", PQerrorMessage(conn));
148+
pg_log_error("failed to set search_path: %s", PQerrorMessage(conn));
150149
PQclear(res);
151150
PQfinish(conn);
152151
return -1;
@@ -165,8 +164,7 @@ vacuumlo(const char *database, const struct _param *param)
165164
res = PQexec(conn, buf);
166165
if (PQresultStatus(res) != PGRES_COMMAND_OK)
167166
{
168-
fprintf(stderr, "Failed to create temp table:\n");
169-
fprintf(stderr, "%s", PQerrorMessage(conn));
167+
pg_log_error("failed to create temp table: %s", PQerrorMessage(conn));
170168
PQclear(res);
171169
PQfinish(conn);
172170
return -1;
@@ -182,8 +180,7 @@ vacuumlo(const char *database, const struct _param *param)
182180
res = PQexec(conn, buf);
183181
if (PQresultStatus(res) != PGRES_COMMAND_OK)
184182
{
185-
fprintf(stderr, "Failed to vacuum temp table:\n");
186-
fprintf(stderr, "%s", PQerrorMessage(conn));
183+
pg_log_error("failed to vacuum temp table: %s", PQerrorMessage(conn));
187184
PQclear(res);
188185
PQfinish(conn);
189186
return -1;
@@ -212,8 +209,7 @@ vacuumlo(const char *database, const struct _param *param)
212209
res = PQexec(conn, buf);
213210
if (PQresultStatus(res) != PGRES_TUPLES_OK)
214211
{
215-
fprintf(stderr, "Failed to find OID columns:\n");
216-
fprintf(stderr, "%s", PQerrorMessage(conn));
212+
pg_log_error("failed to find OID columns: %s", PQerrorMessage(conn));
217213
PQclear(res);
218214
PQfinish(conn);
219215
return -1;
@@ -238,7 +234,7 @@ vacuumlo(const char *database, const struct _param *param)
238234

239235
if (!schema || !table || !field)
240236
{
241-
fprintf(stderr, "%s", PQerrorMessage(conn));
237+
pg_log_error("%s", PQerrorMessage(conn));
242238
PQclear(res);
243239
PQfinish(conn);
244240
if (schema != NULL)
@@ -257,9 +253,8 @@ vacuumlo(const char *database, const struct _param *param)
257253
res2 = PQexec(conn, buf);
258254
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
259255
{
260-
fprintf(stderr, "Failed to check %s in table %s.%s:\n",
261-
field, schema, table);
262-
fprintf(stderr, "%s", PQerrorMessage(conn));
256+
pg_log_error("failed to check %s in table %s.%s: %s",
257+
field, schema, table, PQerrorMessage(conn));
263258
PQclear(res2);
264259
PQclear(res);
265260
PQfinish(conn);
@@ -288,8 +283,7 @@ vacuumlo(const char *database, const struct _param *param)
288283
res = PQexec(conn, "begin");
289284
if (PQresultStatus(res) != PGRES_COMMAND_OK)
290285
{
291-
fprintf(stderr, "Failed to start transaction:\n");
292-
fprintf(stderr, "%s", PQerrorMessage(conn));
286+
pg_log_error("failed to start transaction: %s", PQerrorMessage(conn));
293287
PQclear(res);
294288
PQfinish(conn);
295289
return -1;
@@ -302,7 +296,7 @@ vacuumlo(const char *database, const struct _param *param)
302296
res = PQexec(conn, buf);
303297
if (PQresultStatus(res) != PGRES_COMMAND_OK)
304298
{
305-
fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
299+
pg_log_error("DECLARE CURSOR failed: %s", PQerrorMessage(conn));
306300
PQclear(res);
307301
PQfinish(conn);
308302
return -1;
@@ -319,7 +313,7 @@ vacuumlo(const char *database, const struct _param *param)
319313
res = PQexec(conn, buf);
320314
if (PQresultStatus(res) != PGRES_TUPLES_OK)
321315
{
322-
fprintf(stderr, "FETCH FORWARD failed: %s", PQerrorMessage(conn));
316+
pg_log_error("FETCH FORWARD failed: %s", PQerrorMessage(conn));
323317
PQclear(res);
324318
PQfinish(conn);
325319
return -1;
@@ -347,8 +341,8 @@ vacuumlo(const char *database, const struct _param *param)
347341
{
348342
if (lo_unlink(conn, lo) < 0)
349343
{
350-
fprintf(stderr, "\nFailed to remove lo %u: ", lo);
351-
fprintf(stderr, "%s", PQerrorMessage(conn));
344+
pg_log_error("failed to remove lo %u: %s", lo,
345+
PQerrorMessage(conn));
352346
if (PQtransactionStatus(conn) == PQTRANS_INERROR)
353347
{
354348
success = false;
@@ -367,8 +361,8 @@ vacuumlo(const char *database, const struct _param *param)
367361
res2 = PQexec(conn, "commit");
368362
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
369363
{
370-
fprintf(stderr, "Failed to commit transaction:\n");
371-
fprintf(stderr, "%s", PQerrorMessage(conn));
364+
pg_log_error("failed to commit transaction: %s",
365+
PQerrorMessage(conn));
372366
PQclear(res2);
373367
PQclear(res);
374368
PQfinish(conn);
@@ -378,8 +372,8 @@ vacuumlo(const char *database, const struct _param *param)
378372
res2 = PQexec(conn, "begin");
379373
if (PQresultStatus(res2) != PGRES_COMMAND_OK)
380374
{
381-
fprintf(stderr, "Failed to start transaction:\n");
382-
fprintf(stderr, "%s", PQerrorMessage(conn));
375+
pg_log_error("failed to start transaction: %s",
376+
PQerrorMessage(conn));
383377
PQclear(res2);
384378
PQclear(res);
385379
PQfinish(conn);
@@ -398,8 +392,8 @@ vacuumlo(const char *database, const struct _param *param)
398392
res = PQexec(conn, "commit");
399393
if (PQresultStatus(res) != PGRES_COMMAND_OK)
400394
{
401-
fprintf(stderr, "Failed to commit transaction:\n");
402-
fprintf(stderr, "%s", PQerrorMessage(conn));
395+
pg_log_error("failed to commit transaction: %s",
396+
PQerrorMessage(conn));
403397
PQclear(res);
404398
PQfinish(conn);
405399
return -1;
@@ -471,6 +465,7 @@ main(int argc, char **argv)
471465
const char *progname;
472466
int optindex;
473467

468+
pg_logging_init(argv[0]);
474469
progname = get_progname(argv[0]);
475470

476471
/* Set default parameter values */
@@ -512,9 +507,7 @@ main(int argc, char **argv)
512507
param.transaction_limit = strtol(optarg, NULL, 10);
513508
if (param.transaction_limit < 0)
514509
{
515-
fprintf(stderr,
516-
"%s: transaction limit must not be negative (0 disables)\n",
517-
progname);
510+
pg_log_error("transaction limit must not be negative (0 disables)");
518511
exit(1);
519512
}
520513
break;
@@ -526,7 +519,7 @@ main(int argc, char **argv)
526519
port = strtol(optarg, NULL, 10);
527520
if ((port < 1) || (port > 65535))
528521
{
529-
fprintf(stderr, "%s: invalid port number: %s\n", progname, optarg);
522+
pg_log_error("invalid port number: %s", optarg);
530523
exit(1);
531524
}
532525
param.pg_port = pg_strdup(optarg);
@@ -552,7 +545,7 @@ main(int argc, char **argv)
552545
/* No database given? Show usage */
553546
if (optind >= argc)
554547
{
555-
fprintf(stderr, "vacuumlo: missing required argument: database name\n");
548+
pg_log_error("missing required argument: database name");
556549
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
557550
exit(1);
558551
}

0 commit comments

Comments
 (0)