Skip to content

Commit 2c93818

Browse files
committed
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
1 parent d4f109e commit 2c93818

File tree

3 files changed

+24
-40
lines changed

3 files changed

+24
-40
lines changed

src/bin/pg_dump/pg_backup_utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ extern void exit_nicely(int code) pg_attribute_noreturn();
3434
/* In pg_dump, we modify pg_fatal to call exit_nicely instead of exit */
3535
#undef pg_fatal
3636
#define pg_fatal(...) do { \
37-
if (likely(__pg_log_level <= PG_LOG_ERROR)) \
38-
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
37+
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
3938
exit_nicely(1); \
4039
} while(0)
4140

src/common/logging.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
228228
Assert(fmt);
229229
Assert(fmt[strlen(fmt) - 1] != '\n');
230230

231+
/* Do nothing if log level is too low. */
232+
if (level < __pg_log_level)
233+
return;
234+
231235
/*
232236
* Flush stdout before output to stderr, to ensure sync even when stdout
233237
* is buffered.

src/include/common/logging.h

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -103,50 +103,32 @@ void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
103103
* Preferred style is to use these macros to perform logging; don't call
104104
* pg_log_generic[_v] directly, except perhaps in error interface code.
105105
*/
106-
#define pg_log_error(...) do { \
107-
if (likely(__pg_log_level <= PG_LOG_ERROR)) \
108-
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
109-
} while(0)
106+
#define pg_log_error(...) \
107+
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__)
110108

111-
#define pg_log_error_detail(...) do { \
112-
if (likely(__pg_log_level <= PG_LOG_ERROR)) \
113-
pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__); \
114-
} while(0)
109+
#define pg_log_error_detail(...) \
110+
pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__)
115111

116-
#define pg_log_error_hint(...) do { \
117-
if (likely(__pg_log_level <= PG_LOG_ERROR)) \
118-
pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__); \
119-
} while(0)
112+
#define pg_log_error_hint(...) \
113+
pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__)
120114

121-
#define pg_log_warning(...) do { \
122-
if (likely(__pg_log_level <= PG_LOG_WARNING)) \
123-
pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__); \
124-
} while(0)
115+
#define pg_log_warning(...) \
116+
pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__)
125117

126-
#define pg_log_warning_detail(...) do { \
127-
if (likely(__pg_log_level <= PG_LOG_WARNING)) \
128-
pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__); \
129-
} while(0)
118+
#define pg_log_warning_detail(...) \
119+
pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__)
130120

131-
#define pg_log_warning_hint(...) do { \
132-
if (likely(__pg_log_level <= PG_LOG_WARNING)) \
133-
pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__); \
134-
} while(0)
121+
#define pg_log_warning_hint(...) \
122+
pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__)
135123

136-
#define pg_log_info(...) do { \
137-
if (likely(__pg_log_level <= PG_LOG_INFO)) \
138-
pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__); \
139-
} while(0)
124+
#define pg_log_info(...) \
125+
pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__)
140126

141-
#define pg_log_info_detail(...) do { \
142-
if (likely(__pg_log_level <= PG_LOG_INFO)) \
143-
pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__); \
144-
} while(0)
127+
#define pg_log_info_detail(...) \
128+
pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__)
145129

146-
#define pg_log_info_hint(...) do { \
147-
if (likely(__pg_log_level <= PG_LOG_INFO)) \
148-
pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__); \
149-
} while(0)
130+
#define pg_log_info_hint(...) \
131+
pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__)
150132

151133
#define pg_log_debug(...) do { \
152134
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,
167149
* A common shortcut: pg_log_error() and immediately exit(1).
168150
*/
169151
#define pg_fatal(...) do { \
170-
if (likely(__pg_log_level <= PG_LOG_ERROR)) \
171-
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
152+
pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
172153
exit(1); \
173154
} while(0)
174155

0 commit comments

Comments
 (0)