Skip to content

Commit 2ee0d55

Browse files
committed
Add -L option to psql to log sessions.
Lorne Sunley
1 parent e31cd67 commit 2ee0d55

File tree

11 files changed

+89
-41
lines changed

11 files changed

+89
-41
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.144 2005/06/13 06:36:22 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.145 2005/06/14 02:57:38 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -222,6 +222,17 @@ PostgreSQL documentation
222222
</listitem>
223223
</varlistentry>
224224

225+
<varlistentry>
226+
<term><option>-L <replaceable class="parameter">filename</replaceable></></term>
227+
<term><option>--log <replaceable class="parameter">filename</replaceable></></term>
228+
<listitem>
229+
<para>
230+
Log all query output into file <replaceable
231+
class="parameter">filename</replaceable> in addition to the regular output source.
232+
</para>
233+
</listitem>
234+
</varlistentry>
235+
225236
<varlistentry>
226237
<term><option>-o <replaceable class="parameter">filename</replaceable></></term>
227238
<term><option>--output <replaceable class="parameter">filename</replaceable></></term>

src/bin/psql/common.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.101 2005/06/13 06:36:22 neilc Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.102 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -723,6 +723,13 @@ PSQLexec(const char *query, bool start_xact)
723723
"%s\n"
724724
"**************************\n\n", query);
725725
fflush(stdout);
726+
if (pset.logfile)
727+
{
728+
fprintf(pset.logfile, "********* QUERY **********\n"
729+
"%s\n"
730+
"**************************\n\n", query);
731+
fflush(pset.logfile);
732+
}
726733

727734
if (echo_hidden == 1) /* noexec? */
728735
return NULL;
@@ -803,7 +810,7 @@ PrintQueryTuples(const PGresult *results)
803810
return false;
804811
}
805812

806-
printQuery(results, &my_popt, pset.queryFout);
813+
printQuery(results, &my_popt, pset.queryFout, pset.logfile);
807814

808815
/* close file/pipe, restore old setting */
809816
setQFout(NULL);
@@ -815,7 +822,7 @@ PrintQueryTuples(const PGresult *results)
815822
pset.gfname = NULL;
816823
}
817824
else
818-
printQuery(results, &my_popt, pset.queryFout);
825+
printQuery(results, &my_popt, pset.queryFout, pset.logfile);
819826

820827
return true;
821828
}
@@ -905,6 +912,8 @@ PrintQueryResults(PGresult *results)
905912
else
906913
fprintf(pset.queryFout, "%s\n", PQcmdStatus(results));
907914
}
915+
if (pset.logfile)
916+
fprintf(pset.logfile, "%s\n", PQcmdStatus(results));
908917
SetVariable(pset.vars, "LASTOID", buf);
909918
break;
910919
}
@@ -976,6 +985,14 @@ SendQuery(const char *query)
976985
fflush(stdout);
977986
}
978987

988+
if (pset.logfile)
989+
{
990+
fprintf(pset.logfile, "********* QUERY **********\n"
991+
"%s\n"
992+
"**************************\n\n", query);
993+
fflush(pset.logfile);
994+
}
995+
979996
SetCancelConn();
980997

981998
transaction_status = PQtransactionStatus(pset.db);

src/bin/psql/describe.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.115 2005/04/06 05:23:32 neilc Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.116 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -94,7 +94,7 @@ describeAggregates(const char *pattern, bool verbose)
9494
myopt.nullPrint = NULL;
9595
myopt.title = _("List of aggregate functions");
9696

97-
printQuery(res, &myopt, pset.queryFout);
97+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
9898

9999
PQclear(res);
100100
return true;
@@ -147,7 +147,7 @@ describeTablespaces(const char *pattern, bool verbose)
147147
myopt.nullPrint = NULL;
148148
myopt.title = _("List of tablespaces");
149149

150-
printQuery(res, &myopt, pset.queryFout);
150+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
151151

152152
PQclear(res);
153153
return true;
@@ -219,7 +219,7 @@ describeFunctions(const char *pattern, bool verbose)
219219
myopt.nullPrint = NULL;
220220
myopt.title = _("List of functions");
221221

222-
printQuery(res, &myopt, pset.queryFout);
222+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
223223

224224
PQclear(res);
225225
return true;
@@ -287,7 +287,7 @@ describeTypes(const char *pattern, bool verbose)
287287
myopt.nullPrint = NULL;
288288
myopt.title = _("List of data types");
289289

290-
printQuery(res, &myopt, pset.queryFout);
290+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
291291

292292
PQclear(res);
293293
return true;
@@ -334,7 +334,7 @@ describeOperators(const char *pattern)
334334
myopt.nullPrint = NULL;
335335
myopt.title = _("List of operators");
336336

337-
printQuery(res, &myopt, pset.queryFout);
337+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
338338

339339
PQclear(res);
340340
return true;
@@ -379,7 +379,7 @@ listAllDbs(bool verbose)
379379
myopt.nullPrint = NULL;
380380
myopt.title = _("List of databases");
381381

382-
printQuery(res, &myopt, pset.queryFout);
382+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
383383

384384
PQclear(res);
385385
return true;
@@ -436,7 +436,7 @@ permissionsList(const char *pattern)
436436
printfPQExpBuffer(&buf, _("Access privileges for database \"%s\""), PQdb(pset.db));
437437
myopt.title = buf.data;
438438

439-
printQuery(res, &myopt, pset.queryFout);
439+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
440440

441441
termPQExpBuffer(&buf);
442442
PQclear(res);
@@ -592,7 +592,7 @@ objectDescription(const char *pattern)
592592
myopt.nullPrint = NULL;
593593
myopt.title = _("Object descriptions");
594594

595-
printQuery(res, &myopt, pset.queryFout);
595+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
596596

597597
PQclear(res);
598598
return true;
@@ -1279,7 +1279,7 @@ describeOneTableDetails(const char *schemaname,
12791279

12801280
printTable(title.data, headers,
12811281
(const char **) cells, (const char **) footers,
1282-
"llll", &myopt, pset.queryFout);
1282+
"llll", &myopt, pset.queryFout, pset.logfile);
12831283

12841284
retval = true;
12851285

@@ -1391,7 +1391,7 @@ describeUsers(const char *pattern)
13911391
myopt.nullPrint = NULL;
13921392
myopt.title = _("List of users");
13931393

1394-
printQuery(res, &myopt, pset.queryFout);
1394+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
13951395

13961396
PQclear(res);
13971397
return true;
@@ -1431,7 +1431,7 @@ describeGroups(const char *pattern)
14311431
myopt.nullPrint = NULL;
14321432
myopt.title = _("List of groups");
14331433

1434-
printQuery(res, &myopt, pset.queryFout);
1434+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
14351435

14361436
PQclear(res);
14371437
return true;
@@ -1549,7 +1549,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose)
15491549
myopt.nullPrint = NULL;
15501550
myopt.title = _("List of relations");
15511551

1552-
printQuery(res, &myopt, pset.queryFout);
1552+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
15531553
}
15541554

15551555
PQclear(res);
@@ -1605,7 +1605,7 @@ listDomains(const char *pattern)
16051605
myopt.nullPrint = NULL;
16061606
myopt.title = _("List of domains");
16071607

1608-
printQuery(res, &myopt, pset.queryFout);
1608+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
16091609

16101610
PQclear(res);
16111611
return true;
@@ -1656,7 +1656,7 @@ listConversions(const char *pattern)
16561656
myopt.nullPrint = NULL;
16571657
myopt.title = _("List of conversions");
16581658

1659-
printQuery(res, &myopt, pset.queryFout);
1659+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
16601660

16611661
PQclear(res);
16621662
return true;
@@ -1706,7 +1706,7 @@ listCasts(const char *pattern)
17061706
myopt.nullPrint = NULL;
17071707
myopt.title = _("List of casts");
17081708

1709-
printQuery(res, &myopt, pset.queryFout);
1709+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
17101710

17111711
PQclear(res);
17121712
return true;
@@ -1756,7 +1756,7 @@ listSchemas(const char *pattern, bool verbose)
17561756
myopt.nullPrint = NULL;
17571757
myopt.title = _("List of schemas");
17581758

1759-
printQuery(res, &myopt, pset.queryFout);
1759+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
17601760

17611761
PQclear(res);
17621762
return true;

src/bin/psql/help.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.101 2005/02/22 04:40:55 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.102 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -60,7 +60,7 @@ usage(void)
6060
user = getenv("PGUSER");
6161
if (!user)
6262
{
63-
#ifndef WIN32
63+
#if !defined(WIN32) && !defined(__OS2__)
6464
pw = getpwuid(geteuid());
6565
if (pw)
6666
user = pw->pw_name;
@@ -107,6 +107,7 @@ usage(void)
107107
puts(_(" -n disable enhanced command line editing (readline)"));
108108
puts(_(" -s single-step mode (confirm each query)"));
109109
puts(_(" -S single-line mode (end of line terminates SQL command)"));
110+
puts(_(" -L FILENAME send session log to file"));
110111

111112
puts(_("\nOutput format options:"));
112113
puts(_(" -A unaligned table output mode (-P format=unaligned)"));

src/bin/psql/large_obj.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.36 2005/02/22 04:40:55 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.37 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "large_obj.h"
@@ -263,7 +263,7 @@ do_lo_list(void)
263263
myopt.nullPrint = NULL;
264264
myopt.title = _("Large objects");
265265

266-
printQuery(res, &myopt, pset.queryFout);
266+
printQuery(res, &myopt, pset.queryFout, pset.logfile);
267267

268268
PQclear(res);
269269
return true;

src/bin/psql/print.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.58 2005/06/13 06:36:22 neilc Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.59 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -1255,7 +1255,7 @@ printTable(const char *title,
12551255
const char *const * cells,
12561256
const char *const * footers,
12571257
const char *align,
1258-
const printTableOpt *opt, FILE *fout)
1258+
const printTableOpt *opt, FILE *fout, FILE *flog)
12591259
{
12601260
const char *default_footer[] = {NULL};
12611261
unsigned short int border = opt->border;
@@ -1312,6 +1312,9 @@ printTable(const char *title,
13121312

13131313
/* print the stuff */
13141314

1315+
if (flog)
1316+
print_aligned_text(title, headers, cells, footers, align, opt->tuples_only, border, opt->encoding, flog);
1317+
13151318
switch (opt->format)
13161319
{
13171320
case PRINT_UNALIGNED:
@@ -1380,7 +1383,7 @@ printTable(const char *title,
13801383

13811384

13821385
void
1383-
printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
1386+
printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, FILE *flog)
13841387
{
13851388
int nfields;
13861389
int ncells;
@@ -1476,7 +1479,7 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout)
14761479
/* call table printer */
14771480
printTable(opt->title, headers, cells,
14781481
(const char *const *) footers,
1479-
align, &opt->topt, fout);
1482+
align, &opt->topt, fout, flog);
14801483

14811484
free(headers);
14821485
free(cells);

src/bin/psql/print.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.24 2005/06/13 06:36:22 neilc Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/print.h,v 1.25 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#ifndef PRINT_H
99
#define PRINT_H
@@ -62,7 +62,7 @@ typedef struct _printTableOpt
6262
void printTable(const char *title, const char *const * headers,
6363
const char *const * cells, const char *const * footers,
6464
const char *align,
65-
const printTableOpt *opt, FILE *fout);
65+
const printTableOpt *opt, FILE *fout, FILE *flog);
6666

6767

6868

@@ -82,7 +82,8 @@ typedef struct _printQueryOpt
8282
*
8383
* It calls the printTable above with all the things set straight.
8484
*/
85-
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout);
85+
void printQuery(const PGresult *result, const printQueryOpt *opt,
86+
FILE *fout, FILE *flog);
8687

8788
#ifndef __CYGWIN__
8889
#define DEFAULT_PAGER "more"

src/bin/psql/settings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/settings.h,v 1.24 2005/06/09 23:28:10 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/settings.h,v 1.25 2005/06/14 02:57:41 momjian Exp $
77
*/
88
#ifndef SETTINGS_H
99
#define SETTINGS_H
@@ -56,6 +56,7 @@ typedef struct _psqlSettings
5656
bool timing; /* enable timing of all queries */
5757

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

6162
extern PsqlSettings pset;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy