aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_rewind/file_ops.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/pg_rewind/file_ops.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/bin/pg_rewind/file_ops.c')
-rw-r--r--src/bin/pg_rewind/file_ops.c60
1 files changed, 30 insertions, 30 deletions
diff --git a/src/bin/pg_rewind/file_ops.c b/src/bin/pg_rewind/file_ops.c
index bbdcbe44ac9..e442f935555 100644
--- a/src/bin/pg_rewind/file_ops.c
+++ b/src/bin/pg_rewind/file_ops.c
@@ -60,8 +60,8 @@ open_target_file(const char *path, bool trunc)
mode |= O_TRUNC;
dstfd = open(dstpath, mode, pg_file_create_mode);
if (dstfd < 0)
- pg_fatal("could not open target file \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not open target file \"%s\": %m",
+ dstpath);
}
/*
@@ -74,8 +74,8 @@ close_target_file(void)
return;
if (close(dstfd) != 0)
- pg_fatal("could not close target file \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not close target file \"%s\": %m",
+ dstpath);
dstfd = -1;
}
@@ -94,8 +94,8 @@ write_target_range(char *buf, off_t begin, size_t size)
return;
if (lseek(dstfd, begin, SEEK_SET) == -1)
- pg_fatal("could not seek in target file \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not seek in target file \"%s\": %m",
+ dstpath);
writeleft = size;
p = buf;
@@ -110,8 +110,8 @@ write_target_range(char *buf, off_t begin, size_t size)
/* if write didn't set errno, assume problem is no disk space */
if (errno == 0)
errno = ENOSPC;
- pg_fatal("could not write file \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not write file \"%s\": %m",
+ dstpath);
}
p += writelen;
@@ -160,7 +160,7 @@ create_target(file_entry_t *entry)
case FILE_TYPE_REGULAR:
/* can't happen. Regular files are created with open_target_file. */
- pg_fatal("invalid action (CREATE) for regular file\n");
+ pg_fatal("invalid action (CREATE) for regular file");
break;
}
}
@@ -183,8 +183,8 @@ remove_target_file(const char *path, bool missing_ok)
if (errno == ENOENT && missing_ok)
return;
- pg_fatal("could not remove file \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not remove file \"%s\": %m",
+ dstpath);
}
}
@@ -201,12 +201,12 @@ truncate_target_file(const char *path, off_t newsize)
fd = open(dstpath, O_WRONLY, pg_file_create_mode);
if (fd < 0)
- pg_fatal("could not open file \"%s\" for truncation: %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not open file \"%s\" for truncation: %m",
+ dstpath);
if (ftruncate(fd, newsize) != 0)
- pg_fatal("could not truncate file \"%s\" to %u: %s\n",
- dstpath, (unsigned int) newsize, strerror(errno));
+ pg_fatal("could not truncate file \"%s\" to %u: %m",
+ dstpath, (unsigned int) newsize);
close(fd);
}
@@ -221,8 +221,8 @@ create_target_dir(const char *path)
snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
if (mkdir(dstpath, pg_dir_create_mode) != 0)
- pg_fatal("could not create directory \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not create directory \"%s\": %m",
+ dstpath);
}
static void
@@ -235,8 +235,8 @@ remove_target_dir(const char *path)
snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
if (rmdir(dstpath) != 0)
- pg_fatal("could not remove directory \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not remove directory \"%s\": %m",
+ dstpath);
}
static void
@@ -249,8 +249,8 @@ create_target_symlink(const char *path, const char *link)
snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
if (symlink(link, dstpath) != 0)
- pg_fatal("could not create symbolic link at \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not create symbolic link at \"%s\": %m",
+ dstpath);
}
static void
@@ -263,8 +263,8 @@ remove_target_symlink(const char *path)
snprintf(dstpath, sizeof(dstpath), "%s/%s", datadir_target, path);
if (unlink(dstpath) != 0)
- pg_fatal("could not remove symbolic link \"%s\": %s\n",
- dstpath, strerror(errno));
+ pg_fatal("could not remove symbolic link \"%s\": %m",
+ dstpath);
}
@@ -294,12 +294,12 @@ slurpFile(const char *datadir, const char *path, size_t *filesize)
snprintf(fullpath, sizeof(fullpath), "%s/%s", datadir, path);
if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) == -1)
- pg_fatal("could not open file \"%s\" for reading: %s\n",
- fullpath, strerror(errno));
+ pg_fatal("could not open file \"%s\" for reading: %m",
+ fullpath);
if (fstat(fd, &statbuf) < 0)
- pg_fatal("could not open file \"%s\" for reading: %s\n",
- fullpath, strerror(errno));
+ pg_fatal("could not open file \"%s\" for reading: %m",
+ fullpath);
len = statbuf.st_size;
@@ -309,10 +309,10 @@ slurpFile(const char *datadir, const char *path, size_t *filesize)
if (r != len)
{
if (r < 0)
- pg_fatal("could not read file \"%s\": %s\n",
- fullpath, strerror(errno));
+ pg_fatal("could not read file \"%s\": %m",
+ fullpath);
else
- pg_fatal("could not read file \"%s\": read %d of %zu\n",
+ pg_fatal("could not read file \"%s\": read %d of %zu",
fullpath, r, (Size) len);
}
close(fd);