aboutsummaryrefslogtreecommitdiff
path: root/src/common/controldata_utils.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/common/controldata_utils.c
parentb4cc19ab01ffe6a72a915b21aa41536de80923f5 (diff)
downloadpostgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.tar.gz
postgresql-cc8d41511721d25d557fc02a46c053c0a602fed0.zip
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/common/controldata_utils.c')
-rw-r--r--src/common/controldata_utils.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/common/controldata_utils.c b/src/common/controldata_utils.c
index 567281349e0..efca14ba544 100644
--- a/src/common/controldata_utils.c
+++ b/src/common/controldata_utils.c
@@ -28,6 +28,9 @@
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "common/file_perm.h"
+#ifdef FRONTEND
+#include "fe_utils/logging.h"
+#endif
#include "port/pg_crc32c.h"
#ifndef FRONTEND
@@ -45,7 +48,7 @@
* file data is correct.
*/
ControlFileData *
-get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
+get_controlfile(const char *DataDir, bool *crc_ok_p)
{
ControlFileData *ControlFile;
int fd;
@@ -67,8 +70,8 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
#else
if ((fd = open(ControlFilePath, O_RDONLY | PG_BINARY, 0)) == -1)
{
- fprintf(stderr, _("%s: could not open file \"%s\" for reading: %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not open file \"%s\" for reading: %m",
+ ControlFilePath);
exit(EXIT_FAILURE);
}
#endif
@@ -83,8 +86,7 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
errmsg("could not read file \"%s\": %m", ControlFilePath)));
#else
{
- fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not read file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
}
#endif
@@ -96,8 +98,8 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
ControlFilePath, r, sizeof(ControlFileData))));
#else
{
- fprintf(stderr, _("%s: could not read file \"%s\": read %d of %zu\n"),
- progname, ControlFilePath, r, sizeof(ControlFileData));
+ pg_log_fatal("could not read file \"%s\": read %d of %zu",
+ ControlFilePath, r, sizeof(ControlFileData));
exit(EXIT_FAILURE);
}
#endif
@@ -112,8 +114,7 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
#else
if (close(fd))
{
- fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not close file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
}
#endif
@@ -133,10 +134,10 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
#ifndef FRONTEND
elog(ERROR, _("byte ordering mismatch"));
#else
- printf(_("WARNING: possible byte ordering mismatch\n"
- "The byte ordering used to store the pg_control file might not match the one\n"
- "used by this program. In that case the results below would be incorrect, and\n"
- "the PostgreSQL installation would be incompatible with this data directory.\n"));
+ pg_log_warning("possible byte ordering mismatch\n"
+ "The byte ordering used to store the pg_control file might not match the one\n"
+ "used by this program. In that case the results below would be incorrect, and\n"
+ "the PostgreSQL installation would be incompatible with this data directory.");
#endif
return ControlFile;
@@ -152,7 +153,7 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
* routine in the backend.
*/
void
-update_controlfile(const char *DataDir, const char *progname,
+update_controlfile(const char *DataDir,
ControlFileData *ControlFile, bool do_sync)
{
int fd;
@@ -199,8 +200,7 @@ update_controlfile(const char *DataDir, const char *progname,
if ((fd = open(ControlFilePath, O_WRONLY | PG_BINARY,
pg_file_create_mode)) == -1)
{
- fprintf(stderr, _("%s: could not open file \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not open file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
}
#endif
@@ -221,8 +221,7 @@ update_controlfile(const char *DataDir, const char *progname,
errmsg("could not write file \"%s\": %m",
ControlFilePath)));
#else
- fprintf(stderr, _("%s: could not write \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not write file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
#endif
}
@@ -243,8 +242,7 @@ update_controlfile(const char *DataDir, const char *progname,
#else
if (fsync(fd) != 0)
{
- fprintf(stderr, _("%s: could not fsync file \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not fsync file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
}
#endif
@@ -258,8 +256,7 @@ update_controlfile(const char *DataDir, const char *progname,
errmsg("could not close file \"%s\": %m",
ControlFilePath)));
#else
- fprintf(stderr, _("%s: could not close file \"%s\": %s\n"),
- progname, ControlFilePath, strerror(errno));
+ pg_log_fatal("could not close file \"%s\": %m", ControlFilePath);
exit(EXIT_FAILURE);
#endif
}