PostgreSQL Source Code git master
Loading...
Searching...
No Matches
logging.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "common/logging.h"
Include dependency graph for logging.c:

Go to the source code of this file.

Macros

#define SGR_ERROR_DEFAULT   "01;31"
 
#define SGR_WARNING_DEFAULT   "01;35"
 
#define SGR_NOTE_DEFAULT   "01;36"
 
#define SGR_LOCUS_DEFAULT   "01"
 
#define ANSI_ESCAPE_FMT   "\x1b[%sm"
 
#define ANSI_ESCAPE_RESET   "\x1b[0m"
 

Functions

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_increase_verbosity (void)
 
void pg_logging_set_pre_callback (void(*cb)(void))
 
void pg_logging_set_locus_callback (void(*cb)(const char **filename, uint64 *lineno))
 
void pg_logging_set_logfile (FILE *logfile)
 
void pg_logging_unset_logfile (void)
 
void pg_log_generic (enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...)
 
void pg_log_generic_v (enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
 

Variables

enum pg_log_level __pg_log_level
 
static const charprogname
 
static int log_flags
 
static void(* log_pre_callback )(void)
 
static void(* log_locus_callback )(const char **, uint64 *)
 
static FILElog_logfile
 
static const charsgr_error = NULL
 
static const charsgr_warning = NULL
 
static const charsgr_note = NULL
 
static const charsgr_locus = NULL
 

Macro Definition Documentation

◆ ANSI_ESCAPE_FMT

#define ANSI_ESCAPE_FMT   "\x1b[%sm"

Definition at line 41 of file logging.c.

◆ ANSI_ESCAPE_RESET

#define ANSI_ESCAPE_RESET   "\x1b[0m"

Definition at line 42 of file logging.c.

◆ SGR_ERROR_DEFAULT

#define SGR_ERROR_DEFAULT   "01;31"

Definition at line 36 of file logging.c.

◆ SGR_LOCUS_DEFAULT

#define SGR_LOCUS_DEFAULT   "01"

Definition at line 39 of file logging.c.

◆ SGR_NOTE_DEFAULT

#define SGR_NOTE_DEFAULT   "01;36"

Definition at line 38 of file logging.c.

◆ SGR_WARNING_DEFAULT

#define SGR_WARNING_DEFAULT   "01;35"

Definition at line 37 of file logging.c.

Function Documentation

◆ pg_log_generic()

void pg_log_generic ( enum pg_log_level  level,
enum pg_log_part  part,
const char *pg_restrict  fmt,
  ... 
)

Definition at line 222 of file logging.c.

224{
225 va_list ap;
226
227 va_start(ap, fmt);
228 pg_log_generic_v(level, part, fmt, ap);
229 va_end(ap);
230}
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
Definition logging.c:233
static int fb(int x)

References fb(), and pg_log_generic_v().

Referenced by get_dirent_type().

◆ pg_log_generic_v()

void pg_log_generic_v ( enum pg_log_level  level,
enum pg_log_part  part,
const char *pg_restrict  fmt,
va_list  ap 
)

Definition at line 233 of file logging.c.

235{
236 int save_errno = errno;
237 const char *filename = NULL;
238 uint64 lineno = 0;
239 va_list ap2;
240 size_t required_len;
241 char *buf;
242
244 Assert(level);
245 Assert(fmt);
246 Assert(fmt[strlen(fmt) - 1] != '\n');
247
248 /* Do nothing if log level is too low. */
249 if (level < __pg_log_level)
250 return;
251
252 /*
253 * Flush stdout before output to stderr, to ensure sync even when stdout
254 * is buffered.
255 */
256 fflush(stdout);
257
260
262 log_locus_callback(&filename, &lineno);
263
264 fmt = _(fmt);
265
267 {
268 if (sgr_locus)
271 fprintf(stderr, "%s:", progname);
272 if (filename)
273 {
274 fprintf(stderr, "%s:", filename);
275 if (lineno > 0)
276 fprintf(stderr, UINT64_FORMAT ":", lineno);
277 }
278 fprintf(stderr, " ");
279 if (sgr_locus)
281 }
282
284 {
285 switch (part)
286 {
287 case PG_LOG_PRIMARY:
288 switch (level)
289 {
290 case PG_LOG_ERROR:
291 if (sgr_error)
293 fprintf(stderr, _("error: "));
294 if (log_logfile)
295 fprintf(log_logfile, _("error: "));
296 if (sgr_error)
298 break;
299 case PG_LOG_WARNING:
300 if (sgr_warning)
302 fprintf(stderr, _("warning: "));
303 if (log_logfile)
304 fprintf(log_logfile, _("warning: "));
305 if (sgr_warning)
307 break;
308 default:
309 break;
310 }
311 break;
312 case PG_LOG_DETAIL:
313 if (sgr_note)
315 fprintf(stderr, _("detail: "));
316 if (log_logfile)
317 fprintf(log_logfile, _("detail: "));
318 if (sgr_note)
320 break;
321 case PG_LOG_HINT:
322 if (sgr_note)
324 fprintf(stderr, _("hint: "));
325 if (log_logfile)
326 fprintf(log_logfile, _("hint: "));
327 if (sgr_note)
329 break;
330 }
331 }
332
334
335 va_copy(ap2, ap);
336 required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
337 va_end(ap2);
338
340
341 errno = save_errno; /* malloc might change errno */
342
343 if (!buf)
344 {
345 /* memory trouble, just print what we can and get out of here */
347 return;
348 }
349
351
352 /* strip one newline, for PQerrorMessage() */
353 if (required_len >= 2 && buf[required_len - 2] == '\n')
354 buf[required_len - 2] = '\0';
355
356 fprintf(stderr, "%s\n", buf);
357 if (log_logfile)
358 {
359 fprintf(log_logfile, "%s\n", buf);
361 }
362
363 free(buf);
364}
#define Assert(condition)
Definition c.h:943
#define UINT64_FORMAT
Definition c.h:635
uint64_t uint64
Definition c.h:625
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
#define _(x)
Definition elog.c:96
void * pg_malloc_extended(size_t size, int flags)
Definition fe_memutils.c:59
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
static int log_flags
Definition logging.c:24
static const char * sgr_note
Definition logging.c:33
static const char * sgr_error
Definition logging.c:31
static const char * sgr_warning
Definition logging.c:32
static void(* log_pre_callback)(void)
Definition logging.c:26
static void(* log_locus_callback)(const char **, uint64 *)
Definition logging.c:27
static const char * sgr_locus
Definition logging.c:34
static const char * progname
Definition logging.c:23
enum pg_log_level __pg_log_level
Definition logging.c:21
static FILE * log_logfile
Definition logging.c:29
#define ANSI_ESCAPE_RESET
Definition logging.c:42
#define ANSI_ESCAPE_FMT
Definition logging.c:41
#define PG_LOG_FLAG_TERSE
Definition logging.h:86
@ PG_LOG_PRIMARY
Definition logging.h:67
@ PG_LOG_HINT
Definition logging.h:79
@ PG_LOG_DETAIL
Definition logging.h:73
@ PG_LOG_WARNING
Definition logging.h:38
@ PG_LOG_ERROR
Definition logging.h:43
static char * filename
Definition pg_dumpall.c:133
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define vsnprintf
Definition port.h:259
#define vfprintf
Definition port.h:263
#define free(a)

References _, __pg_log_level, ANSI_ESCAPE_FMT, ANSI_ESCAPE_RESET, Assert, buf, fb(), filename, fprintf, free, log_flags, log_locus_callback, log_logfile, log_pre_callback, MCXT_ALLOC_NO_OOM, PG_LOG_DETAIL, PG_LOG_ERROR, PG_LOG_FLAG_TERSE, PG_LOG_HINT, PG_LOG_PRIMARY, PG_LOG_WARNING, pg_malloc_extended(), progname, sgr_error, sgr_locus, sgr_note, sgr_warning, UINT64_FORMAT, vfprintf, and vsnprintf.

Referenced by pg_log_generic(), report_backup_error(), report_fatal_error(), report_manifest_error(), report_manifest_error(), walsummary_error_callback(), and warn_or_exit_horribly().

◆ pg_logging_config()

void pg_logging_config ( int  new_flags)

Definition at line 168 of file logging.c.

169{
171}

References fb(), and log_flags.

Referenced by main(), and process_file().

◆ pg_logging_increase_verbosity()

void pg_logging_increase_verbosity ( void  )

Definition at line 187 of file logging.c.

188{
189 /*
190 * The enum values are chosen such that we have to decrease __pg_log_level
191 * in order to become more verbose.
192 */
195}
@ PG_LOG_NOTSET
Definition logging.h:21

References __pg_log_level, and PG_LOG_NOTSET.

Referenced by main(), and main().

◆ pg_logging_init()

void pg_logging_init ( const char argv0)

Definition at line 85 of file logging.c.

86{
87 const char *pg_color_env = getenv("PG_COLOR");
88 bool log_color = false;
89 bool color_terminal = isatty(fileno(stderr));
90
91#ifdef WIN32
92
93 /*
94 * On Windows, check if environment is VT100-compatible if using a
95 * terminal.
96 */
99#endif
100
101 /* usually the default, but not on Windows */
103
106
107 if (pg_color_env)
108 {
109 if (strcmp(pg_color_env, "always") == 0 ||
110 (strcmp(pg_color_env, "auto") == 0 && color_terminal))
111 log_color = true;
112 }
113
114 if (log_color)
115 {
116 const char *pg_colors_env = getenv("PG_COLORS");
117
118 if (pg_colors_env)
119 {
120 char *colors = strdup(pg_colors_env);
121
122 if (colors)
123 {
124 char *token;
125 char *cp = colors;
126
127 while ((token = strsep(&cp, ":")))
128 {
129 char *e = strchr(token, '=');
130
131 if (e)
132 {
133 char *name;
134 char *value;
135
136 *e = '\0';
137 name = token;
138 value = e + 1;
139
140 if (strcmp(name, "error") == 0)
142 if (strcmp(name, "warning") == 0)
144 if (strcmp(name, "note") == 0)
146 if (strcmp(name, "locus") == 0)
148 }
149 }
150
151 free(colors);
152 }
153 }
154 else
155 {
160 }
161 }
162}
#define token
static struct @177 value
#define SGR_WARNING_DEFAULT
Definition logging.c:37
#define SGR_NOTE_DEFAULT
Definition logging.c:38
#define SGR_LOCUS_DEFAULT
Definition logging.c:39
#define SGR_ERROR_DEFAULT
Definition logging.c:36
@ PG_LOG_INFO
Definition logging.h:33
static char * argv0
Definition pg_ctl.c:94
char * strsep(char **stringp, const char *delim)
Definition strsep.c:49
const char * get_progname(const char *argv0)
Definition path.c:652
e
const char * name

References __pg_log_level, argv0, fb(), free, get_progname(), name, PG_LOG_INFO, progname, sgr_error, SGR_ERROR_DEFAULT, sgr_locus, SGR_LOCUS_DEFAULT, sgr_note, SGR_NOTE_DEFAULT, sgr_warning, SGR_WARNING_DEFAULT, strsep(), token, and value.

Referenced by get_opts(), main(), main(), and regression_main().

◆ pg_logging_set_level()

void pg_logging_set_level ( enum pg_log_level  new_level)

Definition at line 178 of file logging.c.

179{
181}

References __pg_log_level, and fb().

Referenced by main(), and main().

◆ pg_logging_set_locus_callback()

void pg_logging_set_locus_callback ( void(*)(const char **filename, uint64 *lineno)  cb)

Definition at line 204 of file logging.c.

205{
207}

References log_locus_callback.

Referenced by main().

◆ pg_logging_set_logfile()

void pg_logging_set_logfile ( FILE logfile)

Definition at line 210 of file logging.c.

211{
213}
static FILE * logfile
Definition pg_regress.c:128

References log_logfile, and logfile.

Referenced by main().

◆ pg_logging_set_pre_callback()

void pg_logging_set_pre_callback ( void(*)(void cb)

Definition at line 198 of file logging.c.

199{
200 log_pre_callback = cb;
201}

References log_pre_callback.

Referenced by main().

◆ pg_logging_unset_logfile()

void pg_logging_unset_logfile ( void  )

Definition at line 216 of file logging.c.

217{
219}

References fb(), and log_logfile.

Variable Documentation

◆ __pg_log_level

◆ log_flags

int log_flags
static

Definition at line 24 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_config().

◆ log_locus_callback

void(* log_locus_callback) (const char **, uint64 *) ( const char **  ,
uint64  
)
static

Definition at line 27 of file logging.c.

Referenced by main(), pg_log_generic_v(), and pg_logging_set_locus_callback().

◆ log_logfile

FILE* log_logfile
static

Definition at line 29 of file logging.c.

Referenced by pg_log_generic_v(), pg_logging_set_logfile(), and pg_logging_unset_logfile().

◆ log_pre_callback

void(* log_pre_callback) (void) ( void  )
static

Definition at line 26 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_set_pre_callback().

◆ progname

const char* progname
static

Definition at line 23 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().

◆ sgr_error

const char* sgr_error = NULL
static

Definition at line 31 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().

◆ sgr_locus

const char* sgr_locus = NULL
static

Definition at line 34 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().

◆ sgr_note

const char* sgr_note = NULL
static

Definition at line 33 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().

◆ sgr_warning

const char* sgr_warning = NULL
static

Definition at line 32 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().