From 951378ff8a3c88162fb2df908b78b11f6b87fbe9 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Wed, 28 Nov 2018 11:24:53 +0900
Subject: [PATCH 3/3] Add psql -g option to control debug print

Adds -g option and related help messages. The code acutually uses the
option is still defaultly inactivated.
---
 src/bin/psql/help.c     |  1 +
 src/bin/psql/settings.h |  1 +
 src/bin/psql/startup.c  | 12 +++++++++++-
 3 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 2e9fe760eb..d5d4c85a9c 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -123,6 +123,7 @@ usage(unsigned short int pager)
 					  "                           set field separator for unaligned output to zero byte\n"));
 	fprintf(output, _("  -0, --record-separator-zero\n"
 					  "                           set record separator for unaligned output to zero byte\n"));
+	fprintf(output, _("  -g, --debug              turn on debug print to session log file\n"));
 
 	fprintf(output, _("\nConnection options:\n"));
 	/* Display default host */
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
index 176c85afd0..c0bc96bf20 100644
--- a/src/bin/psql/settings.h
+++ b/src/bin/psql/settings.h
@@ -140,6 +140,7 @@ typedef struct _psqlSettings
 	const char *prompt3;
 	PGVerbosity verbosity;		/* current error verbosity level */
 	PGContextVisibility show_context;	/* current context display level */
+	bool		debug;
 } PsqlSettings;
 
 extern PsqlSettings pset;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index e7536a8a06..25f78bcc2c 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -310,6 +310,12 @@ main(int argc, char *argv[])
 			exit(EXIT_FAILURE);
 		}
 	}
+	else if (pset.debug)
+	{
+		fprintf(stderr, _("%s: no session log file, turn off debug print\n"),
+				pset.progname);
+		pset.debug = false;
+	}
 
 	if (!options.no_psqlrc)
 		process_psqlrc(argv[0]);
@@ -440,6 +446,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 		{"no-align", no_argument, NULL, 'A'},
 		{"command", required_argument, NULL, 'c'},
 		{"dbname", required_argument, NULL, 'd'},
+		{"debug", no_argument, NULL, 'g'},
 		{"echo-queries", no_argument, NULL, 'e'},
 		{"echo-errors", no_argument, NULL, 'b'},
 		{"echo-hidden", no_argument, NULL, 'E'},
@@ -480,7 +487,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 
 	memset(options, 0, sizeof *options);
 
-	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
+	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:gh:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
 							long_options, &optindex)) != -1)
 	{
 		switch (c)
@@ -518,6 +525,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
 										  ACT_FILE,
 										  optarg);
 				break;
+			case 'g':
+				pset.debug = true;
+				break;
 			case 'F':
 				pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
 				pset.popt.topt.fieldSep.separator_zero = false;
-- 
2.16.3

