PostgreSQL Source Code: src/include/common/logging.h Source File

PostgreSQL Source Code git master
logging.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * Logging framework for frontend programs
3 *
4 * Copyright (c) 2018-2025, 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 */
16 enum pg_log_level
17{
18 /*
19 * Not initialized yet (not to be used as an actual message log level).
20 */
21 PG_LOG_NOTSET = 0,
22
23 /*
24 * Low level messages that are normally off by default.
25 */
26 PG_LOG_DEBUG,
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 */
33 PG_LOG_INFO,
34
35 /*
36 * Warnings and "almost" errors, depends on the program
37 */
38 PG_LOG_WARNING,
39
40 /*
41 * Errors
42 */
43 PG_LOG_ERROR,
44
45 /*
46 * Turn all logging off (not to be used as an actual message log level).
47 */
48 PG_LOG_OFF,
49};
50
51/*
52 * __pg_log_level is the minimum log level that will actually be shown.
53 */
54extern enum pg_log_level __pg_log_level;
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 */
61 enum pg_log_part
62{
63 /*
64 * The primary message. Try to keep it to one line; follow the backend's
65 * style guideline for primary messages.
66 */
67 PG_LOG_PRIMARY,
68
69 /*
70 * Additional detail. Follow the backend's style guideline for detail
71 * messages.
72 */
73 PG_LOG_DETAIL,
74
75 /*
76 * Hint (not guaranteed correct) about how to fix the problem. Follow the
77 * backend's style guideline for hint messages.
78 */
79 PG_LOG_HINT,
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);
89void pg_logging_config(int new_flags);
90void pg_logging_set_level(enum pg_log_level new_level);
91void pg_logging_increase_verbosity(void);
92void pg_logging_set_pre_callback(void (*cb) (void));
93void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno));
94
95 void pg_log_generic(enum pg_log_level level, enum pg_log_part part,
96 const char *pg_restrict fmt,...)
97 pg_attribute_printf(3, 4);
98 void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
99 const char *pg_restrict fmt, va_list ap)
100 pg_attribute_printf(3, 0);
101
102/*
103 * Preferred style is to use these macros to perform logging; don't call
104 * pg_log_generic[_v] directly, except perhaps in error interface code.
105 */
106 #define pg_log_error(...) \
107 pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__)
108
109 #define pg_log_error_detail(...) \
110 pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__)
111
112 #define pg_log_error_hint(...) \
113 pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__)
114
115 #define pg_log_warning(...) \
116 pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__)
117
118 #define pg_log_warning_detail(...) \
119 pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__)
120
121 #define pg_log_warning_hint(...) \
122 pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__)
123
124 #define pg_log_info(...) \
125 pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__)
126
127 #define pg_log_info_detail(...) \
128 pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__)
129
130 #define pg_log_info_hint(...) \
131 pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__)
132
133 #define pg_log_debug(...) do { \
134 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
135 pg_log_generic(PG_LOG_DEBUG, PG_LOG_PRIMARY, __VA_ARGS__); \
136 } while(0)
137
138 #define pg_log_debug_detail(...) do { \
139 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
140 pg_log_generic(PG_LOG_DEBUG, PG_LOG_DETAIL, __VA_ARGS__); \
141 } while(0)
142
143 #define pg_log_debug_hint(...) do { \
144 if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
145 pg_log_generic(PG_LOG_DEBUG, PG_LOG_HINT, __VA_ARGS__); \
146 } while(0)
147
148/*
149 * A common shortcut: pg_log_error() and immediately exit(1).
150 */
151 #define pg_fatal(...) do { \
152 pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
153 exit(1); \
154 } while(0)
155
156/*
157 * Use these variants for "can't happen" cases, if it seems translating their
158 * messages would be a waste of effort.
159 */
160 #define pg_log_error_internal(...) pg_log_error(__VA_ARGS__)
161 #define pg_fatal_internal(...) pg_fatal(__VA_ARGS__)
162
163#endif /* COMMON_LOGGING_H */
#define pg_attribute_printf(f, a)
Definition: c.h:232
uint64_t uint64
Definition: c.h:539
void pg_logging_increase_verbosity(void)
Definition: logging.c:185
void pg_logging_init(const char *argv0)
Definition: logging.c:83
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition: logging.c:202
void pg_logging_config(int new_flags)
Definition: logging.c:166
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:176
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:196
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:93
static char * filename
Definition: pg_dumpall.c:120

AltStyle によって変換されたページ (->オリジナル) /