mirror of
https://github.com/postgres/postgres.git
synced 2025-10-19 15:49:24 +03:00
Improve frontend error logging style.
Get rid of the separate "FATAL" log level, as it was applied so inconsistently as to be meaningless. This mostly involves s/pg_log_fatal/pg_log_error/g. Create a macro pg_fatal() to handle the common use-case of pg_log_error() immediately followed by exit(1). Various modules had already invented either this or equivalent macros; standardize on pg_fatal() and apply it where possible. Invent the ability to add "detail" and "hint" messages to a frontend message, much as we have long had in the backend. Except where rewording was needed to convert existing coding to detail/hint style, I have (mostly) resisted the temptation to change existing message wording. Patch by me. Design and patch reviewed at various stages by Robert Haas, Kyotaro Horiguchi, Peter Eisentraut and Daniel Gustafsson. Discussion: https://postgr.es/m/1363732.1636496441@sss.pgh.pa.us
This commit is contained in:
@@ -151,6 +151,9 @@ pg_logging_init(const char *argv0)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Change the logging flags.
|
||||
*/
|
||||
void
|
||||
pg_logging_config(int new_flags)
|
||||
{
|
||||
@@ -194,17 +197,19 @@ pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno)
|
||||
}
|
||||
|
||||
void
|
||||
pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...)
|
||||
pg_log_generic(enum pg_log_level level, enum pg_log_part part,
|
||||
const char *pg_restrict fmt,...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
pg_log_generic_v(level, fmt, ap);
|
||||
pg_log_generic_v(level, part, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
|
||||
pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
|
||||
const char *pg_restrict fmt, va_list ap)
|
||||
{
|
||||
int save_errno = errno;
|
||||
const char *filename = NULL;
|
||||
@@ -232,7 +237,8 @@ pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list a
|
||||
|
||||
fmt = _(fmt);
|
||||
|
||||
if (!(log_flags & PG_LOG_FLAG_TERSE) || filename)
|
||||
if (part == PG_LOG_PRIMARY &&
|
||||
(!(log_flags & PG_LOG_FLAG_TERSE) || filename))
|
||||
{
|
||||
if (sgr_locus)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_locus);
|
||||
@@ -251,30 +257,34 @@ pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list a
|
||||
|
||||
if (!(log_flags & PG_LOG_FLAG_TERSE))
|
||||
{
|
||||
switch (level)
|
||||
switch (part)
|
||||
{
|
||||
case PG_LOG_FATAL:
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_error);
|
||||
fprintf(stderr, _("fatal: "));
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_RESET);
|
||||
case PG_LOG_PRIMARY:
|
||||
switch (level)
|
||||
{
|
||||
case PG_LOG_ERROR:
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_error);
|
||||
fprintf(stderr, _("error: "));
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_RESET);
|
||||
break;
|
||||
case PG_LOG_WARNING:
|
||||
if (sgr_warning)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_warning);
|
||||
fprintf(stderr, _("warning: "));
|
||||
if (sgr_warning)
|
||||
fprintf(stderr, ANSI_ESCAPE_RESET);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case PG_LOG_ERROR:
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_error);
|
||||
fprintf(stderr, _("error: "));
|
||||
if (sgr_error)
|
||||
fprintf(stderr, ANSI_ESCAPE_RESET);
|
||||
case PG_LOG_DETAIL:
|
||||
fprintf(stderr, _("detail: "));
|
||||
break;
|
||||
case PG_LOG_WARNING:
|
||||
if (sgr_warning)
|
||||
fprintf(stderr, ANSI_ESCAPE_FMT, sgr_warning);
|
||||
fprintf(stderr, _("warning: "));
|
||||
if (sgr_warning)
|
||||
fprintf(stderr, ANSI_ESCAPE_RESET);
|
||||
break;
|
||||
default:
|
||||
case PG_LOG_HINT:
|
||||
fprintf(stderr, _("hint: "));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user