summaryrefslogtreecommitdiff
path: root/src/bin/psql/startup.c
diff options
context:
space:
mode:
authorPeter Eisentraut <peter@eisentraut.org>2019-04-01 14:24:37 +0200
committerPeter Eisentraut <peter@eisentraut.org>2019-04-01 20:01:35 +0200
commitcc8d41511721d25d557fc02a46c053c0a602fed0 (patch)
treed2f92acac085be1b9cc4756260c7a4f83d1b0041 /src/bin/psql/startup.c
parentb4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff)
Unified logging system for command-line programs
This unifies the various ad hoc logging (message printing, error printing) systems used throughout the command-line programs. Features: - Program name is automatically prefixed. - Message string does not end with newline. This removes a common source of inconsistencies and omissions. - Additionally, a final newline is automatically stripped, simplifying use of PQerrorMessage() etc., another common source of mistakes. - I converted error message strings to use %m where possible. - As a result of the above several points, more translatable message strings can be shared between different components and between frontends and backend, without gratuitous punctuation or whitespace differences. - There is support for setting a "log level". This is not meant to be user-facing, but can be used internally to implement debug or verbose modes. - Lazy argument evaluation, so no significant overhead if logging at some level is disabled. - Some color in the messages, similar to gcc and clang. Set PG_COLOR=auto to try it out. Some colors are predefined, but can be customized by setting PG_COLORS. - Common files (common/, fe_utils/, etc.) can handle logging much more simply by just using one API without worrying too much about the context of the calling program, requiring callbacks, or having to pass "progname" around everywhere. - Some programs called setvbuf() to make sure that stderr is unbuffered, even on Windows. But not all programs did that. This is now done centrally. Soft goals: - Reduces vertical space use and visual complexity of error reporting in the source code. - Encourages more deliberate classification of messages. For example, in some cases it wasn't clear without analyzing the surrounding code whether a message was meant as an error or just an info. - Concepts and terms are vaguely aligned with popular logging frameworks such as log4j and Python logging. This is all just about printing stuff out. Nothing affects program flow (e.g., fatal exits). The uses are just too varied to do that. Some existing code had wrappers that do some kind of print-and-exit, and I adapted those. I tried to keep the output mostly the same, but there is a lot of historical baggage to unwind and special cases to consider, and I might not always have succeeded. One significant change is that pg_rewind used to write all error messages to stdout. That is now changed to stderr. Reviewed-by: Donald Dong <xdong@csumb.edu> Reviewed-by: Arthur Zakirov <a.zakirov@postgrespro.ru> Discussion: https://www.postgresql.org/message-id/flat/6a609b43-4f57-7348-6480-bd022f924310@2ndquadrant.com
Diffstat (limited to 'src/bin/psql/startup.c')
-rw-r--r--src/bin/psql/startup.c50
1 files changed, 38 insertions, 12 deletions
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index e1c0754a554..ac82087445e 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -22,6 +22,7 @@
#include "help.h"
#include "input.h"
#include "mainloop.h"
+#include "fe_utils/logging.h"
#include "fe_utils/print.h"
#include "settings.h"
@@ -89,6 +90,28 @@ static void EstablishVariableSpace(void);
#define NOPAGER 0
+static void
+log_pre_callback(void)
+{
+ if (pset.queryFout && pset.queryFout != stdout)
+ fflush(pset.queryFout);
+}
+
+static void
+log_locus_callback(const char **filename, uint64 *lineno)
+{
+ if (pset.inputfile)
+ {
+ *filename = pset.inputfile;
+ *lineno = pset.lineno;
+ }
+ else
+ {
+ *filename = NULL;
+ *lineno = 0;
+ }
+}
+
/*
*
* main
@@ -103,6 +126,9 @@ main(int argc, char *argv[])
char password[100];
bool new_pass;
+ pg_logging_init(argv[0]);
+ pg_logging_set_pre_callback(log_pre_callback);
+ pg_logging_set_locus_callback(log_locus_callback);
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
if (argc > 1)
@@ -119,10 +145,6 @@ main(int argc, char *argv[])
}
}
-#ifdef WIN32
- setvbuf(stderr, NULL, _IONBF, 0);
-#endif
-
pset.progname = get_progname(argv[0]);
pset.db = NULL;
@@ -190,7 +212,7 @@ main(int argc, char *argv[])
/* Bail out if -1 was specified but will be ignored. */
if (options.single_txn && options.actions.head == NULL)
{
- fprintf(stderr, _("%s: -1 can only be used in non-interactive mode\n"), pset.progname);
+ pg_log_fatal("-1 can only be used in non-interactive mode");
exit(EXIT_FAILURE);
}
@@ -277,7 +299,7 @@ main(int argc, char *argv[])
if (PQstatus(pset.db) == CONNECTION_BAD)
{
- fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
+ pg_log_error("could not connect to server: %s", PQerrorMessage(pset.db));
PQfinish(pset.db);
exit(EXIT_BADCONN);
}
@@ -305,8 +327,8 @@ main(int argc, char *argv[])
pset.logfile = fopen(options.logfilename, "a");
if (!pset.logfile)
{
- fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
- pset.progname, options.logfilename, strerror(errno));
+ pg_log_fatal("could not open log file \"%s\": %m",
+ options.logfilename);
exit(EXIT_FAILURE);
}
}
@@ -343,6 +365,8 @@ main(int argc, char *argv[])
{
if (cell->action == ACT_SINGLE_QUERY)
{
+ pg_logging_config(PG_LOG_FLAG_TERSE);
+
if (pset.echo == PSQL_ECHO_ALL)
puts(cell->val);
@@ -354,6 +378,8 @@ main(int argc, char *argv[])
PsqlScanState scan_state;
ConditionalStack cond_stack;
+ pg_logging_config(PG_LOG_FLAG_TERSE);
+
if (pset.echo == PSQL_ECHO_ALL)
puts(cell->val);
@@ -562,7 +588,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
if (!result)
{
- fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
+ pg_log_fatal("could not set printing parameter \"%s\"", value);
exit(EXIT_FAILURE);
}
@@ -684,8 +710,8 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
else if (!options->username)
options->username = argv[optind];
else if (!pset.quiet)
- fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
- pset.progname, argv[optind]);
+ pg_log_warning("extra command-line argument \"%s\" ignored",
+ argv[optind]);
optind++;
}
@@ -733,7 +759,7 @@ process_psqlrc(char *argv0)
if (find_my_exec(argv0, my_exec_path) < 0)
{
- fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
+ pg_log_fatal("could not find own program executable");
exit(EXIT_FAILURE);
}