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)
109110conn = PQconnectdbParams (keywords ,values , true);
110111if (!conn )
111112{
112- fprintf (stderr ,"Connection to database \"%s\" failed\n" ,
113- database );
113+ pg_log_error ("connection to database \"%s\" failed" ,database );
114114return -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 */
130130if (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 ));
134134PQfinish (conn );
135135return -1 ;
136136}
@@ -145,8 +145,7 @@ vacuumlo(const char *database, const struct _param *param)
145145res = PQexec (conn ,ALWAYS_SECURE_SEARCH_PATH_SQL );
146146if (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 ));
150149PQclear (res );
151150PQfinish (conn );
152151return -1 ;
@@ -165,8 +164,7 @@ vacuumlo(const char *database, const struct _param *param)
165164res = PQexec (conn ,buf );
166165if (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 ));
170168PQclear (res );
171169PQfinish (conn );
172170return -1 ;
@@ -182,8 +180,7 @@ vacuumlo(const char *database, const struct _param *param)
182180res = PQexec (conn ,buf );
183181if (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 ));
187184PQclear (res );
188185PQfinish (conn );
189186return -1 ;
@@ -212,8 +209,7 @@ vacuumlo(const char *database, const struct _param *param)
212209res = PQexec (conn ,buf );
213210if (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 ));
217213PQclear (res );
218214PQfinish (conn );
219215return -1 ;
@@ -238,7 +234,7 @@ vacuumlo(const char *database, const struct _param *param)
238234
239235if (!schema || !table || !field )
240236{
241- fprintf ( stderr , "%s" ,PQerrorMessage (conn ));
237+ pg_log_error ( "%s" ,PQerrorMessage (conn ));
242238PQclear (res );
243239PQfinish (conn );
244240if (schema != NULL )
@@ -257,9 +253,8 @@ vacuumlo(const char *database, const struct _param *param)
257253res2 = PQexec (conn ,buf );
258254if (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 ));
263258PQclear (res2 );
264259PQclear (res );
265260PQfinish (conn );
@@ -288,8 +283,7 @@ vacuumlo(const char *database, const struct _param *param)
288283res = PQexec (conn ,"begin" );
289284if (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 ));
293287PQclear (res );
294288PQfinish (conn );
295289return -1 ;
@@ -302,7 +296,7 @@ vacuumlo(const char *database, const struct _param *param)
302296res = PQexec (conn ,buf );
303297if (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 ));
306300PQclear (res );
307301PQfinish (conn );
308302return -1 ;
@@ -319,7 +313,7 @@ vacuumlo(const char *database, const struct _param *param)
319313res = PQexec (conn ,buf );
320314if (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 ));
323317PQclear (res );
324318PQfinish (conn );
325319return -1 ;
@@ -347,8 +341,8 @@ vacuumlo(const char *database, const struct _param *param)
347341{
348342if (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 ));
352346if (PQtransactionStatus (conn )== PQTRANS_INERROR )
353347{
354348success = false;
@@ -367,8 +361,8 @@ vacuumlo(const char *database, const struct _param *param)
367361res2 = PQexec (conn ,"commit" );
368362if (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 ));
372366PQclear (res2 );
373367PQclear (res );
374368PQfinish (conn );
@@ -378,8 +372,8 @@ vacuumlo(const char *database, const struct _param *param)
378372res2 = PQexec (conn ,"begin" );
379373if (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 ));
383377PQclear (res2 );
384378PQclear (res );
385379PQfinish (conn );
@@ -398,8 +392,8 @@ vacuumlo(const char *database, const struct _param *param)
398392res = PQexec (conn ,"commit" );
399393if (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 ));
403397PQclear (res );
404398PQfinish (conn );
405399return -1 ;
@@ -471,6 +465,7 @@ main(int argc, char **argv)
471465const char * progname ;
472466int optindex ;
473467
468+ pg_logging_init (argv [0 ]);
474469progname = get_progname (argv [0 ]);
475470
476471/* Set default parameter values */
@@ -512,9 +507,7 @@ main(int argc, char **argv)
512507param .transaction_limit = strtol (optarg ,NULL ,10 );
513508if (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)" );
518511exit (1 );
519512}
520513break ;
@@ -526,7 +519,7 @@ main(int argc, char **argv)
526519port = strtol (optarg ,NULL ,10 );
527520if ((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 );
530523exit (1 );
531524}
532525param .pg_port = pg_strdup (optarg );
@@ -552,7 +545,7 @@ main(int argc, char **argv)
552545/* No database given? Show usage */
553546if (optind >=argc )
554547{
555- fprintf ( stderr , "vacuumlo: missing required argument: database name\n " );
548+ pg_log_error ( " missing required argument: database name" );
556549fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),progname );
557550exit (1 );
558551}