From 2c9381840fe2d6d1c3179350493fe5fd3dcf90b5 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 12 Apr 2022 13:25:29 -0400 Subject: [PATCH] Remove not-very-useful early checks of __pg_log_level in logging.h. Enforce __pg_log_level message filtering centrally in logging.c, instead of relying on the calling macros to do it. This is more reliable (e.g. it works correctly for direct calls to pg_log_generic) and it saves a percent or so of total code size because we get rid of so many duplicate checks of __pg_log_level. This does mean that argument expressions in a logging macro will be evaluated even if we end up not printing anything. That seems of little concern for INFO and higher levels as those messages are printed by default, and most of our frontend programs don't even offer a way to turn them off. I left the unlikely() checks in place for DEBUG messages, though. Discussion: https://postgr.es/m/3993549.1649449609@sss.pgh.pa.us --- src/bin/pg_dump/pg_backup_utils.h | 3 +- src/common/logging.c | 4 +++ src/include/common/logging.h | 57 +++++++++++-------------------- 3 files changed, 24 insertions(+), 40 deletions(-) diff --git a/src/bin/pg_dump/pg_backup_utils.h b/src/bin/pg_dump/pg_backup_utils.h index 5b1c51554d..8173bb93cf 100644 --- a/src/bin/pg_dump/pg_backup_utils.h +++ b/src/bin/pg_dump/pg_backup_utils.h @@ -34,8 +34,7 @@ extern void exit_nicely(int code) pg_attribute_noreturn(); /* In pg_dump, we modify pg_fatal to call exit_nicely instead of exit */ #undef pg_fatal #define pg_fatal(...) do { \ - if (likely(__pg_log_level <= PG_LOG_ERROR)) \ - pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ exit_nicely(1); \ } while(0) diff --git a/src/common/logging.c b/src/common/logging.c index 2933cab85c..0b5bcb1a17 100644 --- a/src/common/logging.c +++ b/src/common/logging.c @@ -228,6 +228,10 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, Assert(fmt); Assert(fmt[strlen(fmt) - 1] != '\n'); + /* Do nothing if log level is too low. */ + if (level < __pg_log_level) + return; + /* * Flush stdout before output to stderr, to ensure sync even when stdout * is buffered. diff --git a/src/include/common/logging.h b/src/include/common/logging.h index e213bb70d0..9f426c32d6 100644 --- a/src/include/common/logging.h +++ b/src/include/common/logging.h @@ -103,50 +103,32 @@ void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, * Preferred style is to use these macros to perform logging; don't call * pg_log_generic[_v] directly, except perhaps in error interface code. */ -#define pg_log_error(...) do { \ - if (likely(__pg_log_level <= PG_LOG_ERROR)) \ - pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ - } while(0) +#define pg_log_error(...) \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__) -#define pg_log_error_detail(...) do { \ - if (likely(__pg_log_level <= PG_LOG_ERROR)) \ - pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__); \ - } while(0) +#define pg_log_error_detail(...) \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__) -#define pg_log_error_hint(...) do { \ - if (likely(__pg_log_level <= PG_LOG_ERROR)) \ - pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__); \ - } while(0) +#define pg_log_error_hint(...) \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__) -#define pg_log_warning(...) do { \ - if (likely(__pg_log_level <= PG_LOG_WARNING)) \ - pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__); \ - } while(0) +#define pg_log_warning(...) \ + pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__) -#define pg_log_warning_detail(...) do { \ - if (likely(__pg_log_level <= PG_LOG_WARNING)) \ - pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__); \ - } while(0) +#define pg_log_warning_detail(...) \ + pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__) -#define pg_log_warning_hint(...) do { \ - if (likely(__pg_log_level <= PG_LOG_WARNING)) \ - pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__); \ - } while(0) +#define pg_log_warning_hint(...) \ + pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__) -#define pg_log_info(...) do { \ - if (likely(__pg_log_level <= PG_LOG_INFO)) \ - pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__); \ - } while(0) +#define pg_log_info(...) \ + pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__) -#define pg_log_info_detail(...) do { \ - if (likely(__pg_log_level <= PG_LOG_INFO)) \ - pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__); \ - } while(0) +#define pg_log_info_detail(...) \ + pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__) -#define pg_log_info_hint(...) do { \ - if (likely(__pg_log_level <= PG_LOG_INFO)) \ - pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__); \ - } while(0) +#define pg_log_info_hint(...) \ + pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__) #define pg_log_debug(...) do { \ if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \ @@ -167,8 +149,7 @@ void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, * A common shortcut: pg_log_error() and immediately exit(1). */ #define pg_fatal(...) do { \ - if (likely(__pg_log_level <= PG_LOG_ERROR)) \ - pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ + pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \ exit(1); \ } while(0)