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

References pg_log_generic_v().

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

175 {
176  va_list ap;
177 
178  va_start(ap, fmt);
179  pg_log_generic_v(level, fmt, ap);
180  va_end(ap);
181 }
void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap)
Definition: logging.c:184

◆ 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 184 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().

185 {
186  int save_errno = errno;
187  const char *filename = NULL;
188  uint64 lineno = 0;
189  va_list ap2;
190  size_t required_len;
191  char *buf;
192 
193  Assert(progname);
194  Assert(level);
195  Assert(fmt);
196  Assert(fmt[strlen(fmt) - 1] != '\n');
197 
198  /*
199  * Flush stdout before output to stderr, to ensure sync even when stdout
200  * is buffered.
201  */
202  fflush(stdout);
203 
204  if (log_pre_callback)
206 
207  if (log_locus_callback)
208  log_locus_callback(&filename, &lineno);
209 
210  fmt = _(fmt);
211 
212  if (!(log_flags & PG_LOG_FLAG_TERSE) || filename)
213  {
214  if (sgr_locus)
216  if (!(log_flags & PG_LOG_FLAG_TERSE))
217  fprintf(stderr, "%s:", progname);
218  if (filename)
219  {
220  fprintf(stderr, "%s:", filename);
221  if (lineno > 0)
222  fprintf(stderr, UINT64_FORMAT ":", lineno);
223  }
224  fprintf(stderr, " ");
225  if (sgr_locus)
226  fprintf(stderr, ANSI_ESCAPE_RESET);
227  }
228 
229  if (!(log_flags & PG_LOG_FLAG_TERSE))
230  {
231  switch (level)
232  {
233  case PG_LOG_FATAL:
234  if (sgr_error)
236  fprintf(stderr, _("fatal: "));
237  if (sgr_error)
238  fprintf(stderr, ANSI_ESCAPE_RESET);
239  break;
240  case PG_LOG_ERROR:
241  if (sgr_error)
243  fprintf(stderr, _("error: "));
244  if (sgr_error)
245  fprintf(stderr, ANSI_ESCAPE_RESET);
246  break;
247  case PG_LOG_WARNING:
248  if (sgr_warning)
250  fprintf(stderr, _("warning: "));
251  if (sgr_warning)
252  fprintf(stderr, ANSI_ESCAPE_RESET);
253  break;
254  default:
255  break;
256  }
257  }
258 
259  errno = save_errno;
260 
261  va_copy(ap2, ap);
262  required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
263  va_end(ap2);
264 
265  buf = pg_malloc_extended(required_len, MCXT_ALLOC_NO_OOM);
266 
267  errno = save_errno; /* malloc might change errno */
268 
269  if (!buf)
270  {
271  /* memory trouble, just print what we can and get out of here */
272  vfprintf(stderr, fmt, ap);
273  return;
274  }
275 
276  vsnprintf(buf, required_len, fmt, ap);
277 
278  /* strip one newline, for PQerrorMessage() */
279  if (required_len >= 2 && buf[required_len - 2] == '\n')
280  buf[required_len - 2] = '\0';
281 
282  fprintf(stderr, "%s\n", buf);
283 
284  free(buf);
285 }
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:197
#define ANSI_ESCAPE_FMT
Definition: logging.c:32
static void(* log_pre_callback)(void)
Definition: logging.c:21
#define vsnprintf
Definition: port.h:192
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:738
static char * filename
Definition: pg_dumpall.c:90
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
#define vfprintf
Definition: port.h:196
static const char * sgr_locus
Definition: logging.c:26
#define _(x)
Definition: elog.c:88
#define UINT64_FORMAT
Definition: c.h:410

◆ pg_logging_config()

void pg_logging_config ( int  new_flags)

Definition at line 150 of file logging.c.

References log_flags.

Referenced by main(), and process_file().

151 {
152  log_flags = new_flags;
153 }
static int log_flags
Definition: logging.c:19

◆ pg_logging_init()

void pg_logging_init ( const char *  argv0)

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

77 {
78  const char *pg_color_env = getenv("PG_COLOR");
79  bool log_color = false;
80  bool color_terminal = isatty(fileno(stderr));
81 
82 #ifdef WIN32
83 
84  /*
85  * On Windows, check if environment is VT100-compatible if using a
86  * terminal.
87  */
88  if (color_terminal)
89  color_terminal = enable_vt_processing();
90 #endif
91 
92  /* usually the default, but not on Windows */
93  setvbuf(stderr, NULL, _IONBF, 0);
94 
97 
98  if (pg_color_env)
99  {
100  if (strcmp(pg_color_env, "always") == 0 ||
101  (strcmp(pg_color_env, "auto") == 0 && color_terminal))
102  log_color = true;
103  }
104 
105  if (log_color)
106  {
107  const char *pg_colors_env = getenv("PG_COLORS");
108 
109  if (pg_colors_env)
110  {
111  char *colors = strdup(pg_colors_env);
112 
113  if (colors)
114  {
115  for (char *token = strtok(colors, ":"); token; token = strtok(NULL, ":"))
116  {
117  char *e = strchr(token, '=');
118 
119  if (e)
120  {
121  char *name;
122  char *value;
123 
124  *e = '\0';
125  name = token;
126  value = e + 1;
127 
128  if (strcmp(name, "error") == 0)
129  sgr_error = strdup(value);
130  if (strcmp(name, "warning") == 0)
131  sgr_warning = strdup(value);
132  if (strcmp(name, "locus") == 0)
133  sgr_locus = strdup(value);
134  }
135  }
136 
137  free(colors);
138  }
139  }
140  else
141  {
145  }
146  }
147 }
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: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
static struct @143 value
#define free(a)
Definition: header.h:65
#define SGR_ERROR_DEFAULT
Definition: logging.c:28
const char * name
Definition: encode.c:555
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 156 of file logging.c.

References __pg_log_level.

Referenced by main().

157 {
158  __pg_log_level = new_level;
159 }
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 168 of file logging.c.

References log_locus_callback.

Referenced by main().

169 {
170  log_locus_callback = cb;
171 }
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 162 of file logging.c.

References log_pre_callback.

Referenced by main().

163 {
164  log_pre_callback = cb;
165 }
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 executeMetaCommand(), 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().