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

Referenced by pg_log_generic_v().

◆ ANSI_ESCAPE_RESET

#define ANSI_ESCAPE_RESET   "\x1b[0m"

Definition at line 33 of file logging.c.

Referenced by pg_log_generic_v().

◆ SGR_ERROR_DEFAULT

#define SGR_ERROR_DEFAULT   "01;31"

Definition at line 28 of file logging.c.

Referenced by pg_logging_init().

◆ SGR_LOCUS_DEFAULT

#define SGR_LOCUS_DEFAULT   "01"

Definition at line 30 of file logging.c.

Referenced by pg_logging_init().

◆ SGR_WARNING_DEFAULT

#define SGR_WARNING_DEFAULT   "01;35"

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

References pg_log_generic_v().

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

127 {
128  va_list ap;
129 
130  va_start(ap, fmt);
131  pg_log_generic_v(level, fmt, ap);
132  va_end(ap);
133 }
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:136

◆ 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 136 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(), and warn_or_exit_horribly().

137 {
138  int save_errno = errno;
139  const char *filename = NULL;
140  uint64 lineno = 0;
141  va_list ap2;
142  size_t required_len;
143  char *buf;
144 
145  Assert(progname);
146  Assert(level);
147  Assert(fmt);
148  Assert(fmt[strlen(fmt) - 1] != '\n');
149 
150  /*
151  * Flush stdout before output to stderr, to ensure sync even when stdout
152  * is buffered.
153  */
154  fflush(stdout);
155 
156  if (log_pre_callback)
158 
159  if (log_locus_callback)
160  log_locus_callback(&filename, &lineno);
161 
162  fmt = _(fmt);
163 
164  if (!(log_flags & PG_LOG_FLAG_TERSE) || filename)
165  {
166  if (sgr_locus)
168  if (!(log_flags & PG_LOG_FLAG_TERSE))
169  fprintf(stderr, "%s:", progname);
170  if (filename)
171  {
172  fprintf(stderr, "%s:", filename);
173  if (lineno > 0)
174  fprintf(stderr, UINT64_FORMAT ":", lineno);
175  }
176  fprintf(stderr, " ");
177  if (sgr_locus)
178  fprintf(stderr, ANSI_ESCAPE_RESET);
179  }
180 
181  if (!(log_flags & PG_LOG_FLAG_TERSE))
182  {
183  switch (level)
184  {
185  case PG_LOG_FATAL:
186  if (sgr_error)
188  fprintf(stderr, _("fatal: "));
189  if (sgr_error)
190  fprintf(stderr, ANSI_ESCAPE_RESET);
191  break;
192  case PG_LOG_ERROR:
193  if (sgr_error)
195  fprintf(stderr, _("error: "));
196  if (sgr_error)
197  fprintf(stderr, ANSI_ESCAPE_RESET);
198  break;
199  case PG_LOG_WARNING:
200  if (sgr_warning)
202  fprintf(stderr, _("warning: "));
203  if (sgr_warning)
204  fprintf(stderr, ANSI_ESCAPE_RESET);
205  break;
206  default:
207  break;
208  }
209  }
210 
211  errno = save_errno;
212 
213  va_copy(ap2, ap);
214  required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
215  va_end(ap2);
216 
217  buf = pg_malloc_extended(required_len, MCXT_ALLOC_NO_OOM);
218 
219  errno = save_errno; /* malloc might change errno */
220 
221  if (!buf)
222  {
223  /* memory trouble, just print what we can and get out of here */
224  vfprintf(stderr, fmt, ap);
225  return;
226  }
227 
228  vsnprintf(buf, required_len, fmt, ap);
229 
230  /* strip one newline, for PQerrorMessage() */
231  if (required_len >= 2 && buf[required_len - 2] == '\n')
232  buf[required_len - 2] = '\0';
233 
234  fprintf(stderr, "%s\n", buf);
235 
236  free(buf);
237 }
static void(* log_locus_callback)(const char **, uint64 *)
Definition: logging.c:22
#define ANSI_ESCAPE_RESET
Definition: logging.c:33
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
#define fprintf
Definition: port.h:196
#define ANSI_ESCAPE_FMT
Definition: logging.c:32
static void(* log_pre_callback)(void)
Definition: logging.c:21
#define vsnprintf
Definition: port.h:191
static const char * sgr_error
Definition: logging.c:24
static char * buf
Definition: pg_test_fsync.c:67
void * pg_malloc_extended(size_t size, int flags)
Definition: fe_memutils.c:59
static const char * sgr_warning
Definition: logging.c:25
static int log_flags
Definition: logging.c:19
static const char * progname
Definition: logging.c:18
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:739
static char * filename
Definition: pg_dumpall.c:90
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define vfprintf
Definition: port.h:195
static const char * sgr_locus
Definition: logging.c:26
#define _(x)
Definition: elog.c:87
#define UINT64_FORMAT
Definition: c.h:402

◆ pg_logging_config()

void pg_logging_config ( int  new_flags)

Definition at line 102 of file logging.c.

References log_flags.

Referenced by main(), and process_file().

103 {
104  log_flags = new_flags;
105 }
static int log_flags
Definition: logging.c:19

◆ pg_logging_init()

void pg_logging_init ( const char *  argv0)

Definition at line 39 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().

40 {
41  const char *pg_color_env = getenv("PG_COLOR");
42  bool log_color = false;
43 
44  /* usually the default, but not on Windows */
45  setvbuf(stderr, NULL, _IONBF, 0);
46 
49 
50  if (pg_color_env)
51  {
52  if (strcmp(pg_color_env, "always") == 0 ||
53  (strcmp(pg_color_env, "auto") == 0 && isatty(fileno(stderr))))
54  log_color = true;
55  }
56 
57  if (log_color)
58  {
59  const char *pg_colors_env = getenv("PG_COLORS");
60 
61  if (pg_colors_env)
62  {
63  char *colors = strdup(pg_colors_env);
64 
65  if (colors)
66  {
67  for (char *token = strtok(colors, ":"); token; token = strtok(NULL, ":"))
68  {
69  char *e = strchr(token, '=');
70 
71  if (e)
72  {
73  char *name;
74  char *value;
75 
76  *e = '\0';
77  name = token;
78  value = e + 1;
79 
80  if (strcmp(name, "error") == 0)
81  sgr_error = strdup(value);
82  if (strcmp(name, "warning") == 0)
83  sgr_warning = strdup(value);
84  if (strcmp(name, "locus") == 0)
85  sgr_locus = strdup(value);
86  }
87  }
88 
89  free(colors);
90  }
91  }
92  else
93  {
97  }
98  }
99 }
static char * argv0
Definition: pg_ctl.c:97
const char * get_progname(const char *argv0)
Definition: path.c:453
static struct @145 value
#define SGR_LOCUS_DEFAULT
Definition: logging.c:30
static const char * sgr_error
Definition: logging.c:24
static const char * sgr_warning
Definition: logging.c:25
enum pg_log_level __pg_log_level
Definition: logging.c:16
static const char * progname
Definition: logging.c:18
#define free(a)
Definition: header.h:65
#define SGR_ERROR_DEFAULT
Definition: logging.c:28
const char * name
Definition: encode.c:521
e
Definition: preproc-init.c:82
static const char * sgr_locus
Definition: logging.c:26
#define SGR_WARNING_DEFAULT
Definition: logging.c:29

◆ pg_logging_set_level()

void pg_logging_set_level ( enum pg_log_level  new_level)

Definition at line 108 of file logging.c.

References __pg_log_level.

Referenced by main().

109 {
110  __pg_log_level = new_level;
111 }
enum pg_log_level __pg_log_level
Definition: logging.c:16

◆ pg_logging_set_locus_callback()

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

Definition at line 120 of file logging.c.

References log_locus_callback.

Referenced by main().

121 {
122  log_locus_callback = cb;
123 }
static void(* log_locus_callback)(const char **, uint64 *)
Definition: logging.c:22

◆ pg_logging_set_pre_callback()

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

Definition at line 114 of file logging.c.

References log_pre_callback.

Referenced by main().

115 {
116  log_pre_callback = cb;
117 }
static void(* log_pre_callback)(void)
Definition: logging.c:21

Variable Documentation

◆ __pg_log_level

enum pg_log_level __pg_log_level

Definition at line 16 of file logging.c.

Referenced by pg_logging_init(), and pg_logging_set_level().

◆ log_flags

int log_flags
static

Definition at line 19 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 22 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 21 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_set_pre_callback().

◆ progname

const char* progname
static

Definition at line 18 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 24 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 26 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 25 of file logging.c.

Referenced by pg_log_generic_v(), and pg_logging_init().