PostgreSQL Source Code  git master
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_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, const char *pg_restrict fmt,...)
 
void pg_log_generic_v (enum pg_log_level level, 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_locus = NULL
 

Macro Definition Documentation

◆ ANSI_ESCAPE_FMT

#define ANSI_ESCAPE_FMT   "\x1b[%sm"

Definition at line 37 of file logging.c.

Referenced by pg_log_generic_v().

◆ ANSI_ESCAPE_RESET

#define ANSI_ESCAPE_RESET   "\x1b[0m"

Definition at line 38 of file logging.c.

Referenced by pg_log_generic_v().

◆ SGR_ERROR_DEFAULT

#define SGR_ERROR_DEFAULT   "01;31"

Definition at line 33 of file logging.c.

Referenced by pg_logging_init().

◆ SGR_LOCUS_DEFAULT

#define SGR_LOCUS_DEFAULT   "01"

Definition at line 35 of file logging.c.

Referenced by pg_logging_init().

◆ SGR_WARNING_DEFAULT

#define SGR_WARNING_DEFAULT   "01;35"

Definition at line 34 of file logging.c.

Referenced by pg_logging_init().

Function Documentation

◆ pg_log_generic()

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

Definition at line 197 of file logging.c.

References pg_log_generic_v().

Referenced by get_dirent_type(), notice_processor(), repairDependencyLoop(), and warn_or_exit_horribly().

198 {
199  va_list ap;
200 
201  va_start(ap, fmt);
202  pg_log_generic_v(level, fmt, ap);
203  va_end(ap);
204 }
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:207

◆ pg_log_generic_v()

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

Definition at line 207 of file logging.c.

References _, ANSI_ESCAPE_FMT, ANSI_ESCAPE_RESET, Assert, buf, filename, fprintf, free, log_flags, log_locus_callback, log_pre_callback, MCXT_ALLOC_NO_OOM, PG_LOG_ERROR, PG_LOG_FATAL, PG_LOG_FLAG_TERSE, PG_LOG_WARNING, pg_malloc_extended(), progname, sgr_error, sgr_locus, sgr_warning, generate_unaccent_rules::stdout, UINT64_FORMAT, vfprintf, and vsnprintf.

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

208 {
209  int save_errno = errno;
210  const char *filename = NULL;
211  uint64 lineno = 0;
212  va_list ap2;
213  size_t required_len;
214  char *buf;
215 
216  Assert(progname);
217  Assert(level);
218  Assert(fmt);
219  Assert(fmt[strlen(fmt) - 1] != '\n');
220 
221  /*
222  * Flush stdout before output to stderr, to ensure sync even when stdout
223  * is buffered.
224  */
225  fflush(stdout);
226 
227  if (log_pre_callback)
229 
230  if (log_locus_callback)
231  log_locus_callback(&filename, &lineno);
232 
233  fmt = _(fmt);
234 
235  if (!(log_flags & PG_LOG_FLAG_TERSE) || filename)
236  {
237  if (sgr_locus)
239  if (!(log_flags & PG_LOG_FLAG_TERSE))
240  fprintf(stderr, "%s:", progname);
241  if (filename)
242  {
243  fprintf(stderr, "%s:", filename);
244  if (lineno > 0)
245  fprintf(stderr, UINT64_FORMAT ":", lineno);
246  }
247  fprintf(stderr, " ");
248  if (sgr_locus)
249  fprintf(stderr, ANSI_ESCAPE_RESET);
250  }
251 
252  if (!(log_flags & PG_LOG_FLAG_TERSE))
253  {
254  switch (level)
255  {
256  case PG_LOG_FATAL:
257  if (sgr_error)
259  fprintf(stderr, _("fatal: "));
260  if (sgr_error)
261  fprintf(stderr, ANSI_ESCAPE_RESET);
262  break;
263  case PG_LOG_ERROR:
264  if (sgr_error)
266  fprintf(stderr, _("error: "));
267  if (sgr_error)
268  fprintf(stderr, ANSI_ESCAPE_RESET);
269  break;
270  case PG_LOG_WARNING:
271  if (sgr_warning)
273  fprintf(stderr, _("warning: "));
274  if (sgr_warning)
275  fprintf(stderr, ANSI_ESCAPE_RESET);
276  break;
277  default:
278  break;
279  }
280  }
281 
282  errno = save_errno;
283 
284  va_copy(ap2, ap);
285  required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
286  va_end(ap2);
287 
288  buf = pg_malloc_extended(required_len, MCXT_ALLOC_NO_OOM);
289 
290  errno = save_errno; /* malloc might change errno */
291 
292  if (!buf)
293  {
294  /* memory trouble, just print what we can and get out of here */
295  vfprintf(stderr, fmt, ap);
296  return;
297  }
298 
299  vsnprintf(buf, required_len, fmt, ap);
300 
301  /* strip one newline, for PQerrorMessage() */
302  if (required_len >= 2 && buf[required_len - 2] == '\n')
303  buf[required_len - 2] = '\0';
304 
305  fprintf(stderr, "%s\n", buf);
306 
307  free(buf);
308 }
static void(* log_locus_callback)(const char **, uint64 *)
Definition: logging.c:27
#define ANSI_ESCAPE_RESET
Definition: logging.c:38
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
#define fprintf
Definition: port.h:219
#define ANSI_ESCAPE_FMT
Definition: logging.c:37
static void(* log_pre_callback)(void)
Definition: logging.c:26
#define vsnprintf
Definition: port.h:214
static const char * sgr_error
Definition: logging.c:29
static char * buf
Definition: pg_test_fsync.c:68
void * pg_malloc_extended(size_t size, int flags)
Definition: fe_memutils.c:59
static const char * sgr_warning
Definition: logging.c:30
static int log_flags
Definition: logging.c:24
static const char * progname
Definition: logging.c:23
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:800
static char * filename
Definition: pg_dumpall.c:91
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define vfprintf
Definition: port.h:218
static const char * sgr_locus
Definition: logging.c:31
#define _(x)
Definition: elog.c:88
#define UINT64_FORMAT
Definition: c.h:472

◆ pg_logging_config()

void pg_logging_config ( int  new_flags)

Definition at line 155 of file logging.c.

References log_flags.

Referenced by main(), and process_file().

156 {
157  log_flags = new_flags;
158 }
static int log_flags
Definition: logging.c:24

◆ pg_logging_increase_verbosity()

void pg_logging_increase_verbosity ( void  )

Definition at line 174 of file logging.c.

References __pg_log_level, and PG_LOG_NOTSET.

Referenced by main().

175 {
176  /*
177  * The enum values are chosen such that we have to decrease __pg_log_level
178  * in order to become more verbose.
179  */
180  if (__pg_log_level > PG_LOG_NOTSET + 1)
181  __pg_log_level--;
182 }
enum pg_log_level __pg_log_level
Definition: logging.c:21

◆ pg_logging_init()

void pg_logging_init ( const char *  argv0)

Definition at line 81 of file logging.c.

References __pg_log_level, free, get_progname(), name, PG_LOG_INFO, progname, sgr_error, SGR_ERROR_DEFAULT, sgr_locus, SGR_LOCUS_DEFAULT, sgr_warning, SGR_WARNING_DEFAULT, and value.

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

82 {
83  const char *pg_color_env = getenv("PG_COLOR");
84  bool log_color = false;
85  bool color_terminal = isatty(fileno(stderr));
86 
87 #ifdef WIN32
88 
89  /*
90  * On Windows, check if environment is VT100-compatible if using a
91  * terminal.
92  */
93  if (color_terminal)
94  color_terminal = enable_vt_processing();
95 #endif
96 
97  /* usually the default, but not on Windows */
98  setvbuf(stderr, NULL, _IONBF, 0);
99 
102 
103  if (pg_color_env)
104  {
105  if (strcmp(pg_color_env, "always") == 0 ||
106  (strcmp(pg_color_env, "auto") == 0 && color_terminal))
107  log_color = true;
108  }
109 
110  if (log_color)
111  {
112  const char *pg_colors_env = getenv("PG_COLORS");
113 
114  if (pg_colors_env)
115  {
116  char *colors = strdup(pg_colors_env);
117 
118  if (colors)
119  {
120  for (char *token = strtok(colors, ":"); token; token = strtok(NULL, ":"))
121  {
122  char *e = strchr(token, '=');
123 
124  if (e)
125  {
126  char *name;
127  char *value;
128 
129  *e = '\0';
130  name = token;
131  value = e + 1;
132 
133  if (strcmp(name, "error") == 0)
134  sgr_error = strdup(value);
135  if (strcmp(name, "warning") == 0)
136  sgr_warning = strdup(value);
137  if (strcmp(name, "locus") == 0)
138  sgr_locus = strdup(value);
139  }
140  }
141 
142  free(colors);
143  }
144  }
145  else
146  {
150  }
151  }
152 }
static char * argv0
Definition: pg_ctl.c:97
const char * get_progname(const char *argv0)
Definition: path.c:453
#define SGR_LOCUS_DEFAULT
Definition: logging.c:35
static const char * sgr_error
Definition: logging.c:29
static const char * sgr_warning
Definition: logging.c:30
enum pg_log_level __pg_log_level
Definition: logging.c:21
static const char * progname
Definition: logging.c:23
static struct @143 value
#define free(a)
Definition: header.h:65
#define SGR_ERROR_DEFAULT
Definition: logging.c:33
const char * name
Definition: encode.c:561
e
Definition: preproc-init.c:82
static const char * sgr_locus
Definition: logging.c:31
#define SGR_WARNING_DEFAULT
Definition: logging.c:34

◆ pg_logging_set_level()

void pg_logging_set_level ( enum pg_log_level  new_level)

Definition at line 165 of file logging.c.

References __pg_log_level.

Referenced by main().

166 {
167  __pg_log_level = new_level;
168 }
enum pg_log_level __pg_log_level
Definition: logging.c:21

◆ pg_logging_set_locus_callback()

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

Definition at line 191 of file logging.c.

References log_locus_callback.

Referenced by main().

192 {
193  log_locus_callback = cb;
194 }
static void(* log_locus_callback)(const char **, uint64 *)
Definition: logging.c:27

◆ pg_logging_set_pre_callback()

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

Definition at line 185 of file logging.c.

References log_pre_callback.

Referenced by main().

186 {
187  log_pre_callback = cb;
188 }
static void(* log_pre_callback)(void)
Definition: logging.c:26

Variable Documentation

◆ __pg_log_level

enum pg_log_level __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 *)
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)
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 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().