mirror of
https://github.com/postgres/postgres.git
synced 2025-11-04 20:11:56 +03:00
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
This commit is contained in:
@@ -74,9 +74,6 @@ struct CompressorState
|
||||
#endif
|
||||
};
|
||||
|
||||
/* translator: this is a module name */
|
||||
static const char *modulename = gettext_noop("compress_io");
|
||||
|
||||
static void ParseCompressionOption(int compression, CompressionAlgorithm *alg,
|
||||
int *level);
|
||||
|
||||
@@ -111,8 +108,7 @@ ParseCompressionOption(int compression, CompressionAlgorithm *alg, int *level)
|
||||
*alg = COMPR_ALG_NONE;
|
||||
else
|
||||
{
|
||||
exit_horribly(modulename, "invalid compression code: %d\n",
|
||||
compression);
|
||||
fatal("invalid compression code: %d", compression);
|
||||
*alg = COMPR_ALG_NONE; /* keep compiler quiet */
|
||||
}
|
||||
|
||||
@@ -135,7 +131,7 @@ AllocateCompressor(int compression, WriteFunc writeF)
|
||||
|
||||
#ifndef HAVE_LIBZ
|
||||
if (alg == COMPR_ALG_LIBZ)
|
||||
exit_horribly(modulename, "not built with zlib support\n");
|
||||
fatal("not built with zlib support");
|
||||
#endif
|
||||
|
||||
cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
|
||||
@@ -171,7 +167,7 @@ ReadDataFromArchive(ArchiveHandle *AH, int compression, ReadFunc readF)
|
||||
#ifdef HAVE_LIBZ
|
||||
ReadDataFromArchiveZlib(AH, readF);
|
||||
#else
|
||||
exit_horribly(modulename, "not built with zlib support\n");
|
||||
fatal("not built with zlib support");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -189,7 +185,7 @@ WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
|
||||
#ifdef HAVE_LIBZ
|
||||
WriteDataToArchiveZlib(AH, cs, data, dLen);
|
||||
#else
|
||||
exit_horribly(modulename, "not built with zlib support\n");
|
||||
fatal("not built with zlib support");
|
||||
#endif
|
||||
break;
|
||||
case COMPR_ALG_NONE:
|
||||
@@ -238,8 +234,7 @@ InitCompressorZlib(CompressorState *cs, int level)
|
||||
cs->zlibOutSize = ZLIB_OUT_SIZE;
|
||||
|
||||
if (deflateInit(zp, level) != Z_OK)
|
||||
exit_horribly(modulename,
|
||||
"could not initialize compression library: %s\n",
|
||||
fatal("could not initialize compression library: %s",
|
||||
zp->msg);
|
||||
|
||||
/* Just be paranoid - maybe End is called after Start, with no Write */
|
||||
@@ -259,8 +254,7 @@ EndCompressorZlib(ArchiveHandle *AH, CompressorState *cs)
|
||||
DeflateCompressorZlib(AH, cs, true);
|
||||
|
||||
if (deflateEnd(zp) != Z_OK)
|
||||
exit_horribly(modulename,
|
||||
"could not close compression stream: %s\n", zp->msg);
|
||||
fatal("could not close compression stream: %s", zp->msg);
|
||||
|
||||
free(cs->zlibOut);
|
||||
free(cs->zp);
|
||||
@@ -277,8 +271,7 @@ DeflateCompressorZlib(ArchiveHandle *AH, CompressorState *cs, bool flush)
|
||||
{
|
||||
res = deflate(zp, flush ? Z_FINISH : Z_NO_FLUSH);
|
||||
if (res == Z_STREAM_ERROR)
|
||||
exit_horribly(modulename,
|
||||
"could not compress data: %s\n", zp->msg);
|
||||
fatal("could not compress data: %s", zp->msg);
|
||||
if ((flush && (zp->avail_out < cs->zlibOutSize))
|
||||
|| (zp->avail_out == 0)
|
||||
|| (zp->avail_in != 0)
|
||||
@@ -340,8 +333,7 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
|
||||
out = pg_malloc(ZLIB_OUT_SIZE + 1);
|
||||
|
||||
if (inflateInit(zp) != Z_OK)
|
||||
exit_horribly(modulename,
|
||||
"could not initialize compression library: %s\n",
|
||||
fatal("could not initialize compression library: %s",
|
||||
zp->msg);
|
||||
|
||||
/* no minimal chunk size for zlib */
|
||||
@@ -357,8 +349,7 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
|
||||
|
||||
res = inflate(zp, 0);
|
||||
if (res != Z_OK && res != Z_STREAM_END)
|
||||
exit_horribly(modulename,
|
||||
"could not uncompress data: %s\n", zp->msg);
|
||||
fatal("could not uncompress data: %s", zp->msg);
|
||||
|
||||
out[ZLIB_OUT_SIZE - zp->avail_out] = '\0';
|
||||
ahwrite(out, 1, ZLIB_OUT_SIZE - zp->avail_out, AH);
|
||||
@@ -373,16 +364,14 @@ ReadDataFromArchiveZlib(ArchiveHandle *AH, ReadFunc readF)
|
||||
zp->avail_out = ZLIB_OUT_SIZE;
|
||||
res = inflate(zp, 0);
|
||||
if (res != Z_OK && res != Z_STREAM_END)
|
||||
exit_horribly(modulename,
|
||||
"could not uncompress data: %s\n", zp->msg);
|
||||
fatal("could not uncompress data: %s", zp->msg);
|
||||
|
||||
out[ZLIB_OUT_SIZE - zp->avail_out] = '\0';
|
||||
ahwrite(out, 1, ZLIB_OUT_SIZE - zp->avail_out, AH);
|
||||
}
|
||||
|
||||
if (inflateEnd(zp) != Z_OK)
|
||||
exit_horribly(modulename,
|
||||
"could not close compression library: %s\n", zp->msg);
|
||||
fatal("could not close compression library: %s", zp->msg);
|
||||
|
||||
free(buf);
|
||||
free(out);
|
||||
@@ -516,7 +505,7 @@ cfopen_write(const char *path, const char *mode, int compression)
|
||||
fp = cfopen(fname, mode, compression);
|
||||
free_keep_errno(fname);
|
||||
#else
|
||||
exit_horribly(modulename, "not built with zlib support\n");
|
||||
fatal("not built with zlib support");
|
||||
fp = NULL; /* keep compiler quiet */
|
||||
#endif
|
||||
}
|
||||
@@ -559,7 +548,7 @@ cfopen(const char *path, const char *mode, int compression)
|
||||
fp = NULL;
|
||||
}
|
||||
#else
|
||||
exit_horribly(modulename, "not built with zlib support\n");
|
||||
fatal("not built with zlib support");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
@@ -596,8 +585,7 @@ cfread(void *ptr, int size, cfp *fp)
|
||||
int errnum;
|
||||
const char *errmsg = gzerror(fp->compressedfp, &errnum);
|
||||
|
||||
exit_horribly(modulename,
|
||||
"could not read from input file: %s\n",
|
||||
fatal("could not read from input file: %s",
|
||||
errnum == Z_ERRNO ? strerror(errno) : errmsg);
|
||||
}
|
||||
}
|
||||
@@ -634,11 +622,9 @@ cfgetc(cfp *fp)
|
||||
if (ret == EOF)
|
||||
{
|
||||
if (!gzeof(fp->compressedfp))
|
||||
exit_horribly(modulename,
|
||||
"could not read from input file: %s\n", strerror(errno));
|
||||
fatal("could not read from input file: %s", strerror(errno));
|
||||
else
|
||||
exit_horribly(modulename,
|
||||
"could not read from input file: end of file\n");
|
||||
fatal("could not read from input file: end of file");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user