1
0
mirror of https://github.com/postgres/postgres.git synced 2025-09-02 04:21:28 +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:
Peter Eisentraut
2019-04-01 14:24:37 +02:00
parent b4cc19ab01
commit cc8d415117
132 changed files with 2555 additions and 2686 deletions

View File

@@ -12,10 +12,8 @@
#include "catalog/pg_control.h"
extern ControlFileData *get_controlfile(const char *DataDir,
const char *progname,
bool *crc_ok_p);
extern void update_controlfile(const char *DataDir, const char *progname,
extern ControlFileData *get_controlfile(const char *DataDir, bool *crc_ok_p);
extern void update_controlfile(const char *DataDir,
ControlFileData *ControlFile, bool do_sync);
#endif /* COMMON_CONTROLDATA_UTILS_H */

View File

@@ -15,13 +15,10 @@
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
extern int fsync_fname(const char *fname, bool isdir,
const char *progname);
extern void fsync_pgdata(const char *pg_data, const char *progname,
int serverVersion);
extern void fsync_dir_recurse(const char *dir, const char *progname);
extern int durable_rename(const char *oldfile, const char *newfile,
const char *progname);
extern int fsync_parent_path(const char *fname, const char *progname);
extern int fsync_fname(const char *fname, bool isdir);
extern void fsync_pgdata(const char *pg_data, int serverVersion);
extern void fsync_dir_recurse(const char *dir);
extern int durable_rename(const char *oldfile, const char *newfile);
extern int fsync_parent_path(const char *fname);
#endif /* FILE_UTILS_H */

View File

@@ -14,11 +14,11 @@
* On Windows make sure that we are running with a restricted token,
* On other platforms do nothing.
*/
void get_restricted_token(const char *progname);
void get_restricted_token(void);
#ifdef WIN32
/* Create a restricted token and execute the specified process with it. */
HANDLE CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, const char *progname);
HANDLE CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo);
#endif
#endif /* COMMON_RESTRICTED_TOKEN_H */

View File

@@ -0,0 +1,95 @@
/*-------------------------------------------------------------------------
* Logging framework for frontend programs
*
* Copyright (c) 2018, PostgreSQL Global Development Group
*
* src/include/fe_utils/logging.h
*
*-------------------------------------------------------------------------
*/
#ifndef FE_UTILS_LOGGING_H
#define FE_UTILS_LOGGING_H
/*
* Log levels are informational only. They do not affect program flow.
*/
enum pg_log_level
{
/*
* Not initialized yet
*/
PG_LOG_NOTSET = 0,
/*
* Low level messages that are normally off by default.
*/
PG_LOG_DEBUG,
/*
* Any program messages that go to stderr, shown by default. (The
* program's normal output should go to stdout and not use the logging
* system.)
*/
PG_LOG_INFO,
/*
* Warnings and "almost" errors, depends on the program
*/
PG_LOG_WARNING,
/*
* Errors
*/
PG_LOG_ERROR,
/*
* Severe errors that cause program termination. (One-shot programs may
* chose to label even fatal errors as merely "errors". The distinction
* is up to the program.)
*/
PG_LOG_FATAL,
/*
* Turn all logging off.
*/
PG_LOG_OFF,
};
extern enum pg_log_level __pg_log_level;
/*
* Kind of a hack to be able to produce the psql output exactly as required by
* the regression tests.
*/
#define PG_LOG_FLAG_TERSE 1
void pg_logging_init(const char *argv0);
void pg_logging_config(int new_flags);
void pg_logging_set_level(enum pg_log_level new_level);
void pg_logging_set_pre_callback(void (*cb)(void));
void 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_attribute_printf(2, 3);
void pg_log_generic_v(enum pg_log_level level, const char * pg_restrict fmt, va_list ap) pg_attribute_printf(2, 0);
#define pg_log_fatal(...) do { \
if (likely(__pg_log_level <= PG_LOG_FATAL)) pg_log_generic(PG_LOG_FATAL, __VA_ARGS__); \
} while(0)
#define pg_log_error(...) do { \
if (likely(__pg_log_level <= PG_LOG_ERROR)) pg_log_generic(PG_LOG_ERROR, __VA_ARGS__); \
} while(0)
#define pg_log_warning(...) do { \
if (likely(__pg_log_level <= PG_LOG_WARNING)) pg_log_generic(PG_LOG_WARNING, __VA_ARGS__); \
} while(0)
#define pg_log_info(...) do { \
if (likely(__pg_log_level <= PG_LOG_INFO)) pg_log_generic(PG_LOG_INFO, __VA_ARGS__); \
} while(0)
#define pg_log_debug(...) do { \
if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) pg_log_generic(PG_LOG_DEBUG, __VA_ARGS__); \
} while(0)
#endif /* FE_UTILS_LOGGING_H */

View File

@@ -64,13 +64,6 @@ typedef struct PsqlScanCallbacks
/* This pointer can be NULL if no variable substitution is wanted */
char *(*get_variable) (const char *varname, PsqlScanQuoteType quote,
void *passthrough);
/* Print an error message someplace appropriate */
/* (very old gcc versions don't support attributes on function pointers) */
#if defined(__GNUC__) && __GNUC__ < 4
void (*write_error) (const char *fmt,...);
#else
void (*write_error) (const char *fmt,...) pg_attribute_printf(1, 2);
#endif
} PsqlScanCallbacks;