PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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_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 char * progname
 
static int log_flags
 
static void(* log_pre_callback )(void)
 
static void(* log_locus_callback )(const char **, uint64 *)
 
static const char * sgr_error = NULL
 
static const char * sgr_warning = NULL
 
static const char * sgr_note = NULL
 
static const char * sgr_locus = NULL
 

Macro Definition Documentation

◆ ANSI_ESCAPE_FMT

#define ANSI_ESCAPE_FMT   "\x1b[%sm"

Definition at line 39 of file logging.c.

◆ ANSI_ESCAPE_RESET

#define ANSI_ESCAPE_RESET   "\x1b[0m"

Definition at line 40 of file logging.c.

◆ SGR_ERROR_DEFAULT

#define SGR_ERROR_DEFAULT   "01;31"

Definition at line 34 of file logging.c.

◆ SGR_LOCUS_DEFAULT

#define SGR_LOCUS_DEFAULT   "01"

Definition at line 37 of file logging.c.

◆ SGR_NOTE_DEFAULT

#define SGR_NOTE_DEFAULT   "01;36"

Definition at line 36 of file logging.c.

◆ SGR_WARNING_DEFAULT

#define SGR_WARNING_DEFAULT   "01;35"

Definition at line 35 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 208 of file logging.c.

210 {
211  va_list ap;
212 
213  va_start(ap, fmt);
214  pg_log_generic_v(level, part, fmt, ap);
215  va_end(ap);
216 }
static void const char * fmt
va_end(args)
va_start(args, fmt)
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:219

References fmt, pg_log_generic_v(), va_end(), and va_start().

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 219 of file logging.c.

221 {
222  int save_errno = errno;
223  const char *filename = NULL;
224  uint64 lineno = 0;
225  va_list ap2;
226  size_t required_len;
227  char *buf;
228 
229  Assert(progname);
230  Assert(level);
231  Assert(fmt);
232  Assert(fmt[strlen(fmt) - 1] != '\n');
233 
234  /* Do nothing if log level is too low. */
235  if (level < __pg_log_level)
236  return;
237 
238  /*
239  * Flush stdout before output to stderr, to ensure sync even when stdout
240  * is buffered.
241  */
242  fflush(stdout);
243 
244  if (log_pre_callback)
246 
247  if (log_locus_callback)
248  log_locus_callback(&filename, &lineno);
249 
250  fmt = _(fmt);
251 
253  {
254  if (sgr_locus)
256  if (!(log_flags & PG_LOG_FLAG_TERSE))
257  fprintf(stderr, "%s:", progname);
258  if (filename)
259  {
260  fprintf(stderr, "%s:", filename);
261  if (lineno > 0)
262  fprintf(stderr, UINT64_FORMAT ":", lineno);
263  }
264  fprintf(stderr, " ");
265  if (sgr_locus)
266  fprintf(stderr, ANSI_ESCAPE_RESET);
267  }
268 
269  if (!(log_flags & PG_LOG_FLAG_TERSE))
270  {
271  switch (part)
272  {
273  case PG_LOG_PRIMARY:
274  switch (level)
275  {
276  case PG_LOG_ERROR:
277  if (sgr_error)
279  fprintf(stderr, _("error: "));
280  if (sgr_error)
281  fprintf(stderr, ANSI_ESCAPE_RESET);
282  break;
283  case PG_LOG_WARNING:
284  if (sgr_warning)
286  fprintf(stderr, _("warning: "));
287  if (sgr_warning)
288  fprintf(stderr, ANSI_ESCAPE_RESET);
289  break;
290  default:
291  break;
292  }
293  break;
294  case PG_LOG_DETAIL:
295  if (sgr_note)
296  fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note);
297  fprintf(stderr, _("detail: "));
298  if (sgr_note)
299  fprintf(stderr, ANSI_ESCAPE_RESET);
300  break;
301  case PG_LOG_HINT:
302  if (sgr_note)
303  fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note);
304  fprintf(stderr, _("hint: "));
305  if (sgr_note)
306  fprintf(stderr, ANSI_ESCAPE_RESET);
307  break;
308  }
309  }
310 
311  errno = save_errno;
312 
313  va_copy(ap2, ap);
314  required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
315  va_end(ap2);
316 
317  buf = pg_malloc_extended(required_len, MCXT_ALLOC_NO_OOM);
318 
319  errno = save_errno; /* malloc might change errno */
320 
321  if (!buf)
322  {
323  /* memory trouble, just print what we can and get out of here */
324  vfprintf(stderr, fmt, ap);
325  return;
326  }
327 
328  vsnprintf(buf, required_len, fmt, ap);
329 
330  /* strip one newline, for PQerrorMessage() */
331  if (required_len >= 2 && buf[required_len - 2] == '\n')
332  buf[required_len - 2] = '\0';
333 
334  fprintf(stderr, "%s\n", buf);
335 
336  free(buf);
337 }
#define Assert(condition)
Definition: c.h:863
#define UINT64_FORMAT
Definition: c.h:552
#define _(x)
Definition: elog.c:90
void * pg_malloc_extended(size_t size, int flags)
Definition: fe_memutils.c:59
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:29
#define free(a)
Definition: header.h:65
static void const char fflush(stdout)
vfprintf(stderr, fmt, args)
static int log_flags
Definition: logging.c:24
static const char * sgr_note
Definition: logging.c:31
static const char * sgr_error
Definition: logging.c:29
static const char * sgr_warning
Definition: logging.c:30
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:32
static const char * progname
Definition: logging.c:23
enum pg_log_level __pg_log_level
Definition: logging.c:21
#define ANSI_ESCAPE_RESET
Definition: logging.c:40
#define ANSI_ESCAPE_FMT
Definition: logging.c:39
#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:119
static char * buf
Definition: pg_test_fsync.c:72
#define vsnprintf
Definition: port.h:237
#define fprintf
Definition: port.h:242

References _, __pg_log_level, ANSI_ESCAPE_FMT, ANSI_ESCAPE_RESET, Assert, buf, fflush(), filename, fmt, fprintf, free, log_flags, log_locus_callback, 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, generate_unaccent_rules::stdout, UINT64_FORMAT, va_end(), vfprintf(), and vsnprintf.

Referenced by pg_log_generic(), report_backup_error(), report_fatal_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 166 of file logging.c.

167 {
168  log_flags = new_flags;
169 }

References log_flags.

Referenced by main(), and process_file().

◆ pg_logging_increase_verbosity()

void pg_logging_increase_verbosity ( void  )

Definition at line 185 of file logging.c.

186 {
187  /*
188  * The enum values are chosen such that we have to decrease __pg_log_level
189  * in order to become more verbose.
190  */
191  if (__pg_log_level > PG_LOG_NOTSET + 1)
192  __pg_log_level--;
193 }
@ PG_LOG_NOTSET
Definition: logging.h:21

References __pg_log_level, and PG_LOG_NOTSET.

Referenced by main().

◆ pg_logging_init()

void pg_logging_init ( const char *  argv0)

Definition at line 83 of file logging.c.

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

References __pg_log_level, argv0, 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(), and regression_main().

◆ pg_logging_set_level()

void pg_logging_set_level ( enum pg_log_level  new_level)

Definition at line 176 of file logging.c.

177 {
178  __pg_log_level = new_level;
179 }

References __pg_log_level.

Referenced by main().

◆ pg_logging_set_locus_callback()

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

Definition at line 202 of file logging.c.

203 {
204  log_locus_callback = cb;
205 }

References log_locus_callback.

Referenced by main().

◆ pg_logging_set_pre_callback()

void pg_logging_set_pre_callback ( void(*)(void)  cb)

Definition at line 196 of file logging.c.

197 {
198  log_pre_callback = cb;
199 }

References log_pre_callback.

Referenced by main().

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 pg_log_generic_v(), and pg_logging_set_locus_callback().

◆ 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 29 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 32 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 31 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 30 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().