PostgreSQL Source Code git master
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * Logging framework for frontend programs
3 *
4 * Copyright (c) 2018-2026, PostgreSQL Global Development Group
5 *
6 * src/include/common/logging.h
7 *
8 *-------------------------------------------------------------------------
9 */
10#ifndef COMMON_LOGGING_H
11#define COMMON_LOGGING_H
12
13/*
14 * Log levels are informational only. They do not affect program flow.
15 */
17{
18 /*
19 * Not initialized yet (not to be used as an actual message log level).
20 */
22
23 /*
24 * Low level messages that are normally off by default.
25 */
27
28 /*
29 * Any program messages that go to stderr, shown by default. (The
30 * program's normal output should go to stdout and not use the logging
31 * system.)
32 */
34
35 /*
36 * Warnings and "almost" errors, depends on the program
37 */
39
40 /*
41 * Errors
42 */
44
45 /*
46 * Turn all logging off (not to be used as an actual message log level).
47 */
49};
50
51/*
52 * __pg_log_level is the minimum log level that will actually be shown.
53 */
55
56/*
57 * A log message can have several parts. The primary message is required,
58 * others are optional. When emitting multiple parts, do so in the order of
59 * this enum, for consistency.
60 */
62{
63 /*
64 * The primary message. Try to keep it to one line; follow the backend's
65 * style guideline for primary messages.
66 */
68
69 /*
70 * Additional detail. Follow the backend's style guideline for detail
71 * messages.
72 */
74
75 /*
76 * Hint (not guaranteed correct) about how to fix the problem. Follow the
77 * backend's style guideline for hint messages.
78 */
80};
81
82/*
83 * Kind of a hack to be able to produce the psql output exactly as required by
84 * the regression tests.
85 */
86#define PG_LOG_FLAG_TERSE 1
87
88void pg_logging_init(const char *argv0);
92void pg_logging_set_pre_callback(void (*cb) (void));
93void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno));
96
98 const char *pg_restrict fmt,...)
103
104/*
105 * Preferred style is to use these macros to perform logging; don't call
106 * pg_log_generic[_v] directly, except perhaps in error interface code.
107 */
108#define pg_log_error(...) \
109 pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__)
110
111#define pg_log_error_detail(...) \
112 pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__)
113
114#define pg_log_error_hint(...) \
115 pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__)
116
117#define pg_log_warning(...) \
118 pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__)
119
120#define pg_log_warning_detail(...) \
121 pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__)
122
123#define pg_log_warning_hint(...) \
124 pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__)
125
126#define pg_log_info(...) \
127 pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__)
128
129#define pg_log_info_detail(...) \
130 pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__)
131
132#define pg_log_info_hint(...) \
133 pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__)
134
135#define pg_log_debug(...) do { \
136 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
137 pg_log_generic(PG_LOG_DEBUG, PG_LOG_PRIMARY, __VA_ARGS__); \
138 } while(0)
139
140#define pg_log_debug_detail(...) do { \
141 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
142 pg_log_generic(PG_LOG_DEBUG, PG_LOG_DETAIL, __VA_ARGS__); \
143 } while(0)
144
145#define pg_log_debug_hint(...) do { \
146 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
147 pg_log_generic(PG_LOG_DEBUG, PG_LOG_HINT, __VA_ARGS__); \
148 } while(0)
149
150/*
151 * A common shortcut: pg_log_error() and immediately exit(1).
152 */
153#define pg_fatal(...) do { \
154 pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
155 exit(1); \
156 } while(0)
157
158/*
159 * Use these variants for "can't happen" cases, if it seems translating their
160 * messages would be a waste of effort.
161 */
162#define pg_log_error_internal(...) pg_log_error(__VA_ARGS__)
163#define pg_fatal_internal(...) pg_fatal(__VA_ARGS__)
164
165#endif /* COMMON_LOGGING_H */
#define pg_attribute_printf(f, a)
Definition c.h:268
uint64_t uint64
Definition c.h:625
void pg_logging_unset_logfile(void)
Definition logging.c:216
void pg_logging_increase_verbosity(void)
Definition logging.c:187
void pg_logging_set_logfile(FILE *logfile)
Definition logging.c:210
void pg_logging_init(const char *argv0)
Definition logging.c:85
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition logging.c:204
void pg_logging_config(int new_flags)
Definition logging.c:168
void pg_logging_set_level(enum pg_log_level new_level)
Definition logging.c:178
void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...) pg_attribute_printf(3
pg_log_part
Definition logging.h:62
@ PG_LOG_PRIMARY
Definition logging.h:67
@ PG_LOG_HINT
Definition logging.h:79
@ PG_LOG_DETAIL
Definition logging.h:73
pg_log_level
Definition logging.h:17
@ PG_LOG_INFO
Definition logging.h:33
@ PG_LOG_DEBUG
Definition logging.h:26
@ PG_LOG_NOTSET
Definition logging.h:21
@ PG_LOG_WARNING
Definition logging.h:38
@ PG_LOG_ERROR
Definition logging.h:43
@ PG_LOG_OFF
Definition logging.h:48
void pg_logging_set_pre_callback(void(*cb)(void))
Definition logging.c:198
enum pg_log_level __pg_log_level
Definition logging.c:21
void void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap) pg_attribute_printf(3
static char * argv0
Definition pg_ctl.c:94
static char * filename
Definition pg_dumpall.c:133
static FILE * logfile
Definition pg_regress.c:128
static int fb(int x)