Re: logfile for psql patch update - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: logfile for psql patch update
Date
Msg-id 200506140304.j5E34SH17896@candle.pha.pa.us
Whole thread Raw
In response to logfile for psql patch update  (lsunley@mb.sympatico.ca)
List pgsql-patches
lsunley@mb.sympatico.ca wrote:
> This update replaces the previous patch for the psql logfile option
>
> The print.c routine is also built into src/bin/scripts
>
> The ifdef USE_LOGFILE wrapper is to keep out compile errors when the
> routine is linked into programs other than psql

I have applied the psql -L logging patch, thanks, but I made a few changes.

Instead of using a Makefile flag, I properly isolated print.c by passing
in the log file descriptor.  I had to modify  printQuery() and
printTable() to pass the log file descriptor.

I see no need for the \set LOGFILE value --- it didn't allow you to
specify the log file name or turn on logging.  I think the -L flag is
sufficient.

I modified the OS2 test so the WIN32 code is run.  Please let me know if
that works.

The **** you used at the top and bottom of the query was similar to the
existing single-step printing style we already have, so it seemed OK.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/ref/psql-ref.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v
retrieving revision 1.144
diff -c -c -r1.144 psql-ref.sgml
*** doc/src/sgml/ref/psql-ref.sgml    13 Jun 2005 06:36:22 -0000    1.144
--- doc/src/sgml/ref/psql-ref.sgml    14 Jun 2005 02:53:30 -0000
***************
*** 223,228 ****
--- 223,239 ----
      </varlistentry>

      <varlistentry>
+       <term><option>-L <replaceable class="parameter">filename</replaceable></></term>
+       <term><option>--log <replaceable class="parameter">filename</replaceable></></term>
+       <listitem>
+       <para>
+       Log all query output into file <replaceable
+       class="parameter">filename</replaceable> in addition to the regular output source.
+       </para>
+       </listitem>
+     </varlistentry>
+
+     <varlistentry>
        <term><option>-o <replaceable class="parameter">filename</replaceable></></term>
        <term><option>--output <replaceable class="parameter">filename</replaceable></></term>
        <listitem>
Index: src/bin/psql/common.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/common.c,v
retrieving revision 1.101
diff -c -c -r1.101 common.c
*** src/bin/psql/common.c    13 Jun 2005 06:36:22 -0000    1.101
--- src/bin/psql/common.c    14 Jun 2005 02:53:33 -0000
***************
*** 723,728 ****
--- 723,735 ----
                 "%s\n"
                 "**************************\n\n", query);
          fflush(stdout);
+         if (pset.logfile)
+         {
+             fprintf(pset.logfile, "********* QUERY **********\n"
+                "%s\n"
+                "**************************\n\n", query);
+             fflush(pset.logfile);
+         }

          if (echo_hidden == 1)    /* noexec? */
              return NULL;
***************
*** 803,809 ****
              return false;
          }

!         printQuery(results, &my_popt, pset.queryFout);

          /* close file/pipe, restore old setting */
          setQFout(NULL);
--- 810,816 ----
              return false;
          }

!         printQuery(results, &my_popt, pset.queryFout, pset.logfile);

          /* close file/pipe, restore old setting */
          setQFout(NULL);
***************
*** 815,821 ****
          pset.gfname = NULL;
      }
      else
!         printQuery(results, &my_popt, pset.queryFout);

      return true;
  }
--- 822,828 ----
          pset.gfname = NULL;
      }
      else
!         printQuery(results, &my_popt, pset.queryFout, pset.logfile);

      return true;
  }
***************
*** 905,910 ****
--- 912,919 ----
                      else
                          fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
                  }
+                 if (pset.logfile)
+                     fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
                  SetVariable(pset.vars, "LASTOID", buf);
                  break;
              }
***************
*** 976,981 ****
--- 985,999 ----
          fflush(stdout);
      }

+     /* log query */
+     if (pset.logfile)
+     {
+         fprintf(pset.logfile, "********* QUERY **********\n"
+            "%s\n"
+            "**************************\n\n", query);
+         fflush(pset.logfile);
+     }
+
      SetCancelConn();

      transaction_status = PQtransactionStatus(pset.db);
Index: src/bin/psql/describe.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/describe.c,v
retrieving revision 1.115
diff -c -c -r1.115 describe.c
*** src/bin/psql/describe.c    6 Apr 2005 05:23:32 -0000    1.115
--- src/bin/psql/describe.c    14 Jun 2005 02:53:35 -0000
***************
*** 94,100 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of aggregate functions");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 94,100 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of aggregate functions");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 147,153 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of tablespaces");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 147,153 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of tablespaces");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 219,225 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of functions");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 219,225 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of functions");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 287,293 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of data types");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 287,293 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of data types");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 334,340 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of operators");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 334,340 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of operators");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 379,385 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of databases");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 379,385 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of databases");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 436,442 ****
      printfPQExpBuffer(&buf, _("Access privileges for database \"%s\""), PQdb(pset.db));
      myopt.title = buf.data;

!     printQuery(res, &myopt, pset.queryFout);

      termPQExpBuffer(&buf);
      PQclear(res);
--- 436,442 ----
      printfPQExpBuffer(&buf, _("Access privileges for database \"%s\""), PQdb(pset.db));
      myopt.title = buf.data;

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      termPQExpBuffer(&buf);
      PQclear(res);
***************
*** 592,598 ****
      myopt.nullPrint = NULL;
      myopt.title = _("Object descriptions");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 592,598 ----
      myopt.nullPrint = NULL;
      myopt.title = _("Object descriptions");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1279,1285 ****

      printTable(title.data, headers,
                 (const char **) cells, (const char **) footers,
!                "llll", &myopt, pset.queryFout);

      retval = true;

--- 1279,1285 ----

      printTable(title.data, headers,
                 (const char **) cells, (const char **) footers,
!                "llll", &myopt, pset.queryFout, pset.logfile);

      retval = true;

***************
*** 1391,1397 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of users");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1391,1397 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of users");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1431,1437 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of groups");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1431,1437 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of groups");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1549,1555 ****
          myopt.nullPrint = NULL;
          myopt.title = _("List of relations");

!         printQuery(res, &myopt, pset.queryFout);
      }

      PQclear(res);
--- 1549,1555 ----
          myopt.nullPrint = NULL;
          myopt.title = _("List of relations");

!         printQuery(res, &myopt, pset.queryFout, pset.logfile);
      }

      PQclear(res);
***************
*** 1605,1611 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of domains");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1605,1611 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of domains");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1656,1662 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of conversions");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1656,1662 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of conversions");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1706,1712 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of casts");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1706,1712 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of casts");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
***************
*** 1756,1762 ****
      myopt.nullPrint = NULL;
      myopt.title = _("List of schemas");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 1756,1762 ----
      myopt.nullPrint = NULL;
      myopt.title = _("List of schemas");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
Index: src/bin/psql/help.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/help.c,v
retrieving revision 1.101
diff -c -c -r1.101 help.c
*** src/bin/psql/help.c    22 Feb 2005 04:40:55 -0000    1.101
--- src/bin/psql/help.c    14 Jun 2005 02:53:35 -0000
***************
*** 60,66 ****
      user = getenv("PGUSER");
      if (!user)
      {
! #ifndef WIN32
          pw = getpwuid(geteuid());
          if (pw)
              user = pw->pw_name;
--- 60,66 ----
      user = getenv("PGUSER");
      if (!user)
      {
! #if !defined(WIN32) && !defined(__OS2__)
          pw = getpwuid(geteuid());
          if (pw)
              user = pw->pw_name;
***************
*** 107,112 ****
--- 107,113 ----
      puts(_("  -n              disable enhanced command line editing (readline)"));
      puts(_("  -s              single-step mode (confirm each query)"));
      puts(_("  -S              single-line mode (end of line terminates SQL command)"));
+     puts(_("  -L FILENAME     send session log to file"));

      puts(_("\nOutput format options:"));
      puts(_("  -A              unaligned table output mode (-P format=unaligned)"));
Index: src/bin/psql/large_obj.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/large_obj.c,v
retrieving revision 1.36
diff -c -c -r1.36 large_obj.c
*** src/bin/psql/large_obj.c    22 Feb 2005 04:40:55 -0000    1.36
--- src/bin/psql/large_obj.c    14 Jun 2005 02:53:35 -0000
***************
*** 263,269 ****
      myopt.nullPrint = NULL;
      myopt.title = _("Large objects");

!     printQuery(res, &myopt, pset.queryFout);

      PQclear(res);
      return true;
--- 263,269 ----
      myopt.nullPrint = NULL;
      myopt.title = _("Large objects");

!     printQuery(res, &myopt, pset.queryFout, pset.logfile);

      PQclear(res);
      return true;
Index: src/bin/psql/print.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/print.c,v
retrieving revision 1.58
diff -c -c -r1.58 print.c
*** src/bin/psql/print.c    13 Jun 2005 06:36:22 -0000    1.58
--- src/bin/psql/print.c    14 Jun 2005 02:53:36 -0000
***************
*** 1255,1261 ****
             const char *const * cells,
             const char *const * footers,
             const char *align,
!            const printTableOpt *opt, FILE *fout)
  {
      const char *default_footer[] = {NULL};
      unsigned short int border = opt->border;
--- 1255,1261 ----
             const char *const * cells,
             const char *const * footers,
             const char *align,
!            const printTableOpt *opt, FILE *fout, FILE *flog)
  {
      const char *default_footer[] = {NULL};
      unsigned short int border = opt->border;
***************
*** 1312,1317 ****
--- 1312,1320 ----

      /* print the stuff */

+     if (flog)
+         print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->encoding, flog);
+
      switch (opt->format)
      {
          case PRINT_UNALIGNED:
***************
*** 1380,1386 ****


  void
! printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
  {
      int            nfields;
      int            ncells;
--- 1383,1389 ----


  void
! printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *flog)
  {
      int            nfields;
      int            ncells;
***************
*** 1476,1482 ****
      /* call table printer */
      printTable(opt->title, headers, cells,
                 (const char *const *) footers,
!                align, &opt->topt, fout);

      free(headers);
      free(cells);
--- 1479,1485 ----
      /* call table printer */
      printTable(opt->title, headers, cells,
                 (const char *const *) footers,
!                align, &opt->topt, fout, flog);

      free(headers);
      free(cells);
Index: src/bin/psql/print.h
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/print.h,v
retrieving revision 1.24
diff -c -c -r1.24 print.h
*** src/bin/psql/print.h    13 Jun 2005 06:36:22 -0000    1.24
--- src/bin/psql/print.h    14 Jun 2005 02:53:36 -0000
***************
*** 62,68 ****
  void printTable(const char *title, const char *const * headers,
             const char *const * cells, const char *const * footers,
             const char *align,
!            const printTableOpt *opt, FILE *fout);



--- 62,68 ----
  void printTable(const char *title, const char *const * headers,
             const char *const * cells, const char *const * footers,
             const char *align,
!            const printTableOpt *opt, FILE *fout, FILE *flog);



***************
*** 82,88 ****
   *
   * It calls the printTable above with all the things set straight.
   */
! void        printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout);

  #ifndef __CYGWIN__
  #define DEFAULT_PAGER "more"
--- 82,89 ----
   *
   * It calls the printTable above with all the things set straight.
   */
! void        printQuery(const PGresult *result, const printQueryOpt *opt,
!                        FILE *fout, FILE *flog);

  #ifndef __CYGWIN__
  #define DEFAULT_PAGER "more"
Index: src/bin/psql/settings.h
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/settings.h,v
retrieving revision 1.24
diff -c -c -r1.24 settings.h
*** src/bin/psql/settings.h    9 Jun 2005 23:28:10 -0000    1.24
--- src/bin/psql/settings.h    14 Jun 2005 02:53:37 -0000
***************
*** 56,61 ****
--- 56,62 ----
      bool        timing;            /* enable timing of all queries */

      PGVerbosity verbosity;        /* current error verbosity level */
+     FILE        *logfile;    /* session log file handle */
  } PsqlSettings;

  extern PsqlSettings pset;
Index: src/bin/psql/startup.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/psql/startup.c,v
retrieving revision 1.116
diff -c -c -r1.116 startup.c
*** src/bin/psql/startup.c    13 Jun 2005 06:36:22 -0000    1.116
--- src/bin/psql/startup.c    14 Jun 2005 02:53:37 -0000
***************
*** 71,76 ****
--- 71,77 ----
      char       *host;
      char       *port;
      char       *username;
+     char       *logfilename;
      enum _actions action;
      char       *action_string;
      bool        no_readline;
***************
*** 109,116 ****

      set_pglocale_pgservice(argv[0], "psql");

-     pset.progname = get_progname(argv[0]);
-
      if (argc > 1)
      {
          if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
--- 110,115 ----
***************
*** 125,130 ****
--- 124,131 ----
          }
      }

+     pset.progname = get_progname(argv[0]);
+
  #ifdef WIN32
      setvbuf(stderr, NULL, _IONBF, 0);
      setup_win32_locks();
***************
*** 234,239 ****
--- 235,247 ----
          exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
      }

+     if (options.logfilename)
+     {
+         pset.logfile = fopen(options.logfilename, "a");
+         if (!pset.logfile)
+             fprintf(stderr, gettext("logfile open failed for %s\n\n"), options.logfilename);
+     }
+
      /*
       * Now find something to do
       */
***************
*** 316,321 ****
--- 324,331 ----
      }

      /* clean up */
+     if (pset.logfile)
+         fclose(pset.logfile);
      PQfinish(pset.db);
      setQFout(NULL);

***************
*** 344,349 ****
--- 354,360 ----
          {"host", required_argument, NULL, 'h'},
          {"html", no_argument, NULL, 'H'},
          {"list", no_argument, NULL, 'l'},
+         {"log", required_argument, NULL, 'L'},
          {"no-readline", no_argument, NULL, 'n'},
          {"output", required_argument, NULL, 'o'},
          {"port", required_argument, NULL, 'p'},
***************
*** 373,379 ****

      memset(options, 0, sizeof *options);

!     while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?",
                              long_options, &optindex)) != -1)
      {
          switch (c)
--- 384,390 ----

      memset(options, 0, sizeof *options);

!     while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?",
                              long_options, &optindex)) != -1)
      {
          switch (c)
***************
*** 419,424 ****
--- 430,438 ----
              case 'l':
                  options->action = ACT_LIST_DB;
                  break;
+             case 'L':
+                 options->logfilename = optarg;
+                 break;
              case 'n':
                  options->no_readline = true;
                  break;
Index: src/bin/scripts/createlang.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/scripts/createlang.c,v
retrieving revision 1.15
diff -c -c -r1.15 createlang.c
*** src/bin/scripts/createlang.c    31 Dec 2004 22:03:17 -0000    1.15
--- src/bin/scripts/createlang.c    14 Jun 2005 02:53:37 -0000
***************
*** 148,154 ****
          popt.topt.border = 1;
          popt.topt.encoding = PQclientEncoding(conn);
          popt.title = _("Procedural Languages");
!         printQuery(result, &popt, stdout);

          PQfinish(conn);
          exit(0);
--- 148,154 ----
          popt.topt.border = 1;
          popt.topt.encoding = PQclientEncoding(conn);
          popt.title = _("Procedural Languages");
!         printQuery(result, &popt, stdout, NULL);

          PQfinish(conn);
          exit(0);
Index: src/bin/scripts/droplang.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/scripts/droplang.c,v
retrieving revision 1.14
diff -c -c -r1.14 droplang.c
*** src/bin/scripts/droplang.c    31 Dec 2004 22:03:17 -0000    1.14
--- src/bin/scripts/droplang.c    14 Jun 2005 02:53:37 -0000
***************
*** 145,151 ****
          popt.topt.border = 1;
          popt.topt.encoding = PQclientEncoding(conn);
          popt.title = _("Procedural Languages");
!         printQuery(result, &popt, stdout);

          PQfinish(conn);
          exit(0);
--- 145,151 ----
          popt.topt.border = 1;
          popt.topt.encoding = PQclientEncoding(conn);
          popt.title = _("Procedural Languages");
!         printQuery(result, &popt, stdout, NULL);

          PQfinish(conn);
          exit(0);

pgsql-patches by date:

Previous
From: Tom Lane
Date:
Subject: Re: plpgsql raise - parameters can be expressions
Next
From: Neil Conway
Date:
Subject: hash join: probe both inputs first