summaryrefslogtreecommitdiff
path: root/src/bin/psql/crosstabview.c
diff options
context:
space:
mode:
authorPeter Eisentraut2019-04-01 12:24:37 +0000
committerPeter Eisentraut2019-04-01 18:01:35 +0000
commitcc8d41511721d25d557fc02a46c053c0a602fed0 (patch)
treed2f92acac085be1b9cc4756260c7a4f83d1b0041 /src/bin/psql/crosstabview.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 <[email protected]> Reviewed-by: Arthur Zakirov <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
Diffstat (limited to 'src/bin/psql/crosstabview.c')
-rw-r--r--src/bin/psql/crosstabview.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index fb77ce70334..6afd3e0939c 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -13,6 +13,7 @@
#include "psqlscanslash.h"
#include "settings.h"
+#include "fe_utils/logging.h"
/*
* Value/position from the resultset that goes into the horizontal or vertical
@@ -120,13 +121,13 @@ PrintResultsInCrosstab(const PGresult *res)
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
- psql_error("\\crosstabview: statement did not return a result set\n");
+ pg_log_error("\\crosstabview: statement did not return a result set");
goto error_return;
}
if (PQnfields(res) < 3)
{
- psql_error("\\crosstabview: query must return at least three columns\n");
+ pg_log_error("\\crosstabview: query must return at least three columns");
goto error_return;
}
@@ -153,7 +154,7 @@ PrintResultsInCrosstab(const PGresult *res)
/* Insist that header columns be distinct */
if (field_for_columns == field_for_rows)
{
- psql_error("\\crosstabview: vertical and horizontal headers must be different columns\n");
+ pg_log_error("\\crosstabview: vertical and horizontal headers must be different columns");
goto error_return;
}
@@ -169,7 +170,7 @@ PrintResultsInCrosstab(const PGresult *res)
*/
if (PQnfields(res) != 3)
{
- psql_error("\\crosstabview: data column must be specified when query returns more than three columns\n");
+ pg_log_error("\\crosstabview: data column must be specified when query returns more than three columns");
goto error_return;
}
@@ -225,7 +226,7 @@ PrintResultsInCrosstab(const PGresult *res)
if (piv_columns.count > CROSSTABVIEW_MAX_COLUMNS)
{
- psql_error("\\crosstabview: maximum number of columns (%d) exceeded\n",
+ pg_log_error("\\crosstabview: maximum number of columns (%d) exceeded",
CROSSTABVIEW_MAX_COLUMNS);
goto error_return;
}
@@ -394,7 +395,7 @@ printCrosstab(const PGresult *results,
*/
if (cont.cells[idx] != NULL)
{
- psql_error("\\crosstabview: query result contains multiple data values for row \"%s\", column \"%s\"\n",
+ pg_log_error("\\crosstabview: query result contains multiple data values for row \"%s\", column \"%s\"",
rp->name ? rp->name :
(popt.nullPrint ? popt.nullPrint : "(null)"),
cp->name ? cp->name :
@@ -642,7 +643,7 @@ indexOfColumn(char *arg, const PGresult *res)
idx = atoi(arg) - 1;
if (idx < 0 || idx >= PQnfields(res))
{
- psql_error("\\crosstabview: column number %d is out of range 1..%d\n",
+ pg_log_error("\\crosstabview: column number %d is out of range 1..%d",
idx + 1, PQnfields(res));
return -1;
}
@@ -667,7 +668,7 @@ indexOfColumn(char *arg, const PGresult *res)
if (idx >= 0)
{
/* another idx was already found for the same name */
- psql_error("\\crosstabview: ambiguous column name: \"%s\"\n", arg);
+ pg_log_error("\\crosstabview: ambiguous column name: \"%s\"", arg);
return -1;
}
idx = i;
@@ -675,7 +676,7 @@ indexOfColumn(char *arg, const PGresult *res)
}
if (idx == -1)
{
- psql_error("\\crosstabview: column name not found: \"%s\"\n", arg);
+ pg_log_error("\\crosstabview: column name not found: \"%s\"", arg);
return -1;
}
}