mirror of
https://github.com/postgres/postgres.git
synced 2025-04-24 10:47:04 +03:00
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
80 lines
1.9 KiB
C
80 lines
1.9 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* logging.c
|
|
* logging functions
|
|
*
|
|
* Copyright (c) 2010-2019, PostgreSQL Global Development Group
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres_fe.h"
|
|
|
|
#include <unistd.h>
|
|
#include <time.h>
|
|
|
|
#include "pg_rewind.h"
|
|
#include "logging.h"
|
|
|
|
#include "pgtime.h"
|
|
|
|
/* Progress counters */
|
|
uint64 fetch_size;
|
|
uint64 fetch_done;
|
|
|
|
static pg_time_t last_progress_report = 0;
|
|
|
|
|
|
/*
|
|
* Print a progress report based on the global variables.
|
|
*
|
|
* Progress report is written at maximum once per second, unless the
|
|
* force parameter is set to true.
|
|
*/
|
|
void
|
|
progress_report(bool force)
|
|
{
|
|
int percent;
|
|
char fetch_done_str[32];
|
|
char fetch_size_str[32];
|
|
pg_time_t now;
|
|
|
|
if (!showprogress)
|
|
return;
|
|
|
|
now = time(NULL);
|
|
if (now == last_progress_report && !force)
|
|
return; /* Max once per second */
|
|
|
|
last_progress_report = now;
|
|
percent = fetch_size ? (int) ((fetch_done) * 100 / fetch_size) : 0;
|
|
|
|
/*
|
|
* Avoid overflowing past 100% or the full size. This may make the total
|
|
* size number change as we approach the end of the backup (the estimate
|
|
* will always be wrong if WAL is included), but that's better than having
|
|
* the done column be bigger than the total.
|
|
*/
|
|
if (percent > 100)
|
|
percent = 100;
|
|
if (fetch_done > fetch_size)
|
|
fetch_size = fetch_done;
|
|
|
|
/*
|
|
* Separate step to keep platform-dependent format code out of
|
|
* translatable strings. And we only test for INT64_FORMAT availability
|
|
* in snprintf, not fprintf.
|
|
*/
|
|
snprintf(fetch_done_str, sizeof(fetch_done_str), INT64_FORMAT,
|
|
fetch_done / 1024);
|
|
snprintf(fetch_size_str, sizeof(fetch_size_str), INT64_FORMAT,
|
|
fetch_size / 1024);
|
|
|
|
fprintf(stderr, _("%*s/%s kB (%d%%) copied"),
|
|
(int) strlen(fetch_size_str), fetch_done_str, fetch_size_str,
|
|
percent);
|
|
if (isatty(fileno(stderr)))
|
|
fprintf(stderr, "\r");
|
|
else
|
|
fprintf(stderr, "\n");
|
|
}
|