| 1 | /*------------------------------------------------------------------------- |
| 2 | * |
| 3 | * elog.c |
| 4 | * error logging and reporting |
| 5 | * |
| 6 | * Because of the extremely high rate at which log messages can be generated, |
| 7 | * we need to be mindful of the performance cost of obtaining any information |
| 8 | * that may be logged. Also, it's important to keep in mind that this code may |
| 9 | * get called from within an aborted transaction, in which case operations |
| 10 | * such as syscache lookups are unsafe. |
| 11 | * |
| 12 | * Some notes about recursion and errors during error processing: |
| 13 | * |
| 14 | * We need to be robust about recursive-error scenarios --- for example, |
| 15 | * if we run out of memory, it's important to be able to report that fact. |
| 16 | * There are a number of considerations that go into this. |
| 17 | * |
| 18 | * First, distinguish between re-entrant use and actual recursion. It |
| 19 | * is possible for an error or warning message to be emitted while the |
| 20 | * parameters for an error message are being computed. In this case |
| 21 | * errstart has been called for the outer message, and some field values |
| 22 | * may have already been saved, but we are not actually recursing. We handle |
| 23 | * this by providing a (small) stack of ErrorData records. The inner message |
| 24 | * can be computed and sent without disturbing the state of the outer message. |
| 25 | * (If the inner message is actually an error, this isn't very interesting |
| 26 | * because control won't come back to the outer message generator ... but |
| 27 | * if the inner message is only debug or log data, this is critical.) |
| 28 | * |
| 29 | * Second, actual recursion will occur if an error is reported by one of |
| 30 | * the elog.c routines or something they call. By far the most probable |
| 31 | * scenario of this sort is "out of memory"; and it's also the nastiest |
| 32 | * to handle because we'd likely also run out of memory while trying to |
| 33 | * report this error! Our escape hatch for this case is to reset the |
| 34 | * ErrorContext to empty before trying to process the inner error. Since |
| 35 | * ErrorContext is guaranteed to have at least 8K of space in it (see mcxt.c), |
| 36 | * we should be able to process an "out of memory" message successfully. |
| 37 | * Since we lose the prior error state due to the reset, we won't be able |
| 38 | * to return to processing the original error, but we wouldn't have anyway. |
| 39 | * (NOTE: the escape hatch is not used for recursive situations where the |
| 40 | * inner message is of less than ERROR severity; in that case we just |
| 41 | * try to process it and return normally. Usually this will work, but if |
| 42 | * it ends up in infinite recursion, we will PANIC due to error stack |
| 43 | * overflow.) |
| 44 | * |
| 45 | * |
| 46 | * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group |
| 47 | * Portions Copyright (c) 1994, Regents of the University of California |
| 48 | * |
| 49 | * |
| 50 | * IDENTIFICATION |
| 51 | * src/backend/utils/error/elog.c |
| 52 | * |
| 53 | *------------------------------------------------------------------------- |
| 54 | */ |
| 55 | #include "postgres.h" |
| 56 | |
| 57 | #include <fcntl.h> |
| 58 | #include <time.h> |
| 59 | #include <unistd.h> |
| 60 | #include <signal.h> |
| 61 | #include <ctype.h> |
| 62 | #ifdef HAVE_SYSLOG |
| 63 | #include <syslog.h> |
| 64 | #endif |
| 65 | |
| 66 | #include "access/transam.h" |
| 67 | #include "access/xact.h" |
| 68 | #include "libpq/libpq.h" |
| 69 | #include "libpq/pqformat.h" |
| 70 | #include "mb/pg_wchar.h" |
| 71 | #include "miscadmin.h" |
| 72 | #include "postmaster/postmaster.h" |
| 73 | #include "postmaster/syslogger.h" |
| 74 | #include "storage/ipc.h" |
| 75 | #include "storage/proc.h" |
| 76 | #include "tcop/tcopprot.h" |
| 77 | #include "utils/guc.h" |
| 78 | #include "utils/memutils.h" |
| 79 | #include "utils/ps_status.h" |
| 80 | |
| 81 | |
| 82 | /* In this module, access gettext() via err_gettext() */ |
| 83 | #undef _ |
| 84 | #define _(x) err_gettext(x) |
| 85 | |
| 86 | |
| 87 | /* Global variables */ |
| 88 | ErrorContextCallback *error_context_stack = NULL; |
| 89 | |
| 90 | sigjmp_buf *PG_exception_stack = NULL; |
| 91 | |
| 92 | extern bool redirection_done; |
| 93 | |
| 94 | /* |
| 95 | * Hook for intercepting messages before they are sent to the server log. |
| 96 | * Note that the hook will not get called for messages that are suppressed |
| 97 | * by log_min_messages. Also note that logging hooks implemented in preload |
| 98 | * libraries will miss any log messages that are generated before the |
| 99 | * library is loaded. |
| 100 | */ |
| 101 | emit_log_hook_type emit_log_hook = NULL; |
| 102 | |
| 103 | /* GUC parameters */ |
| 104 | int Log_error_verbosity = PGERROR_VERBOSE; |
| 105 | char *Log_line_prefix = NULL; /* format for extra log line info */ |
| 106 | int Log_destination = LOG_DESTINATION_STDERR; |
| 107 | char *Log_destination_string = NULL; |
| 108 | bool syslog_sequence_numbers = true; |
| 109 | bool syslog_split_messages = true; |
| 110 | |
| 111 | #ifdef HAVE_SYSLOG |
| 112 | |
| 113 | /* |
| 114 | * Max string length to send to syslog(). Note that this doesn't count the |
| 115 | * sequence-number prefix we add, and of course it doesn't count the prefix |
| 116 | * added by syslog itself. Solaris and sysklogd truncate the final message |
| 117 | * at 1024 bytes, so this value leaves 124 bytes for those prefixes. (Most |
| 118 | * other syslog implementations seem to have limits of 2KB or so.) |
| 119 | */ |
| 120 | #ifndef PG_SYSLOG_LIMIT |
| 121 | #define PG_SYSLOG_LIMIT 900 |
| 122 | #endif |
| 123 | |
| 124 | static bool openlog_done = false; |
| 125 | static char *syslog_ident = NULL; |
| 126 | static int syslog_facility = LOG_LOCAL0; |
| 127 | |
| 128 | static void write_syslog(int level, const char *line); |
| 129 | #endif |
| 130 | |
| 131 | #ifdef WIN32 |
| 132 | extern char *event_source; |
| 133 | |
| 134 | static void write_eventlog(int level, const char *line, int len); |
| 135 | #endif |
| 136 | |
| 137 | /* We provide a small stack of ErrorData records for re-entrant cases */ |
| 138 | #define ERRORDATA_STACK_SIZE 5 |
| 139 | |
| 140 | static ErrorData errordata[ERRORDATA_STACK_SIZE]; |
| 141 | |
| 142 | static int errordata_stack_depth = -1; /* index of topmost active frame */ |
| 143 | |
| 144 | static int recursion_depth = 0; /* to detect actual recursion */ |
| 145 | |
| 146 | /* |
| 147 | * Saved timeval and buffers for formatted timestamps that might be used by |
| 148 | * both log_line_prefix and csv logs. |
| 149 | */ |
| 150 | static struct timeval saved_timeval; |
| 151 | static bool saved_timeval_set = false; |
| 152 | |
| 153 | #define FORMATTED_TS_LEN 128 |
| 154 | static char formatted_start_time[FORMATTED_TS_LEN]; |
| 155 | static char formatted_log_time[FORMATTED_TS_LEN]; |
| 156 | |
| 157 | |
| 158 | /* Macro for checking errordata_stack_depth is reasonable */ |
| 159 | #define CHECK_STACK_DEPTH() \ |
| 160 | do { \ |
| 161 | if (errordata_stack_depth < 0) \ |
| 162 | { \ |
| 163 | errordata_stack_depth = -1; \ |
| 164 | ereport(ERROR, (errmsg_internal("errstart was not called"))); \ |
| 165 | } \ |
| 166 | } while (0) |
| 167 | |
| 168 | |
| 169 | static const char *err_gettext(const char *str) pg_attribute_format_arg(1); |
| 170 | static void set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str); |
| 171 | static void write_console(const char *line, int len); |
| 172 | static void setup_formatted_log_time(void); |
| 173 | static void setup_formatted_start_time(void); |
| 174 | static const char *process_log_prefix_padding(const char *p, int *padding); |
| 175 | static void log_line_prefix(StringInfo buf, ErrorData *edata); |
| 176 | static void write_csvlog(ErrorData *edata); |
| 177 | static void send_message_to_server_log(ErrorData *edata); |
| 178 | static void write_pipe_chunks(char *data, int len, int dest); |
| 179 | static void send_message_to_frontend(ErrorData *edata); |
| 180 | static const char *error_severity(int elevel); |
| 181 | static void append_with_tabs(StringInfo buf, const char *str); |
| 182 | static bool is_log_level_output(int elevel, int log_min_level); |
| 183 | |
| 184 | |
| 185 | /* |
| 186 | * in_error_recursion_trouble --- are we at risk of infinite error recursion? |
| 187 | * |
| 188 | * This function exists to provide common control of various fallback steps |
| 189 | * that we take if we think we are facing infinite error recursion. See the |
| 190 | * callers for details. |
| 191 | */ |
| 192 | bool |
| 193 | in_error_recursion_trouble(void) |
| 194 | { |
| 195 | /* Pull the plug if recurse more than once */ |
| 196 | return (recursion_depth > 2); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * One of those fallback steps is to stop trying to localize the error |
| 201 | * message, since there's a significant probability that that's exactly |
| 202 | * what's causing the recursion. |
| 203 | */ |
| 204 | static inline const char * |
| 205 | err_gettext(const char *str) |
| 206 | { |
| 207 | #ifdef ENABLE_NLS |
| 208 | if (in_error_recursion_trouble()) |
| 209 | return str; |
| 210 | else |
| 211 | return gettext(str); |
| 212 | #else |
| 213 | return str; |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /* |
| 219 | * errstart --- begin an error-reporting cycle |
| 220 | * |
| 221 | * Create a stack entry and store the given parameters in it. Subsequently, |
| 222 | * errmsg() and perhaps other routines will be called to further populate |
| 223 | * the stack entry. Finally, errfinish() will be called to actually process |
| 224 | * the error report. |
| 225 | * |
| 226 | * Returns true in normal case. Returns false to short-circuit the error |
| 227 | * report (if it's a warning or lower and not to be reported anywhere). |
| 228 | */ |
| 229 | bool |
| 230 | errstart(int elevel, const char *filename, int lineno, |
| 231 | const char *funcname, const char *domain) |
| 232 | { |
| 233 | ErrorData *edata; |
| 234 | bool output_to_server; |
| 235 | bool output_to_client = false; |
| 236 | int i; |
| 237 | |
| 238 | /* |
| 239 | * Check some cases in which we want to promote an error into a more |
| 240 | * severe error. None of this logic applies for non-error messages. |
| 241 | */ |
| 242 | if (elevel >= ERROR) |
| 243 | { |
| 244 | /* |
| 245 | * If we are inside a critical section, all errors become PANIC |
| 246 | * errors. See miscadmin.h. |
| 247 | */ |
| 248 | if (CritSectionCount > 0) |
| 249 | elevel = PANIC; |
| 250 | |
| 251 | /* |
| 252 | * Check reasons for treating ERROR as FATAL: |
| 253 | * |
| 254 | * 1. we have no handler to pass the error to (implies we are in the |
| 255 | * postmaster or in backend startup). |
| 256 | * |
| 257 | * 2. ExitOnAnyError mode switch is set (initdb uses this). |
| 258 | * |
| 259 | * 3. the error occurred after proc_exit has begun to run. (It's |
| 260 | * proc_exit's responsibility to see that this doesn't turn into |
| 261 | * infinite recursion!) |
| 262 | */ |
| 263 | if (elevel == ERROR) |
| 264 | { |
| 265 | if (PG_exception_stack == NULL || |
| 266 | ExitOnAnyError || |
| 267 | proc_exit_inprogress) |
| 268 | elevel = FATAL; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * If the error level is ERROR or more, errfinish is not going to |
| 273 | * return to caller; therefore, if there is any stacked error already |
| 274 | * in progress it will be lost. This is more or less okay, except we |
| 275 | * do not want to have a FATAL or PANIC error downgraded because the |
| 276 | * reporting process was interrupted by a lower-grade error. So check |
| 277 | * the stack and make sure we panic if panic is warranted. |
| 278 | */ |
| 279 | for (i = 0; i <= errordata_stack_depth; i++) |
| 280 | elevel = Max(elevel, errordata[i].elevel); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Now decide whether we need to process this report at all; if it's |
| 285 | * warning or less and not enabled for logging, just return false without |
| 286 | * starting up any error logging machinery. |
| 287 | */ |
| 288 | |
| 289 | /* Determine whether message is enabled for server log output */ |
| 290 | output_to_server = is_log_level_output(elevel, log_min_messages); |
| 291 | |
| 292 | /* Determine whether message is enabled for client output */ |
| 293 | if (whereToSendOutput == DestRemote && elevel != LOG_SERVER_ONLY) |
| 294 | { |
| 295 | /* |
| 296 | * client_min_messages is honored only after we complete the |
| 297 | * authentication handshake. This is required both for security |
| 298 | * reasons and because many clients can't handle NOTICE messages |
| 299 | * during authentication. |
| 300 | */ |
| 301 | if (ClientAuthInProgress) |
| 302 | output_to_client = (elevel >= ERROR); |
| 303 | else |
| 304 | output_to_client = (elevel >= client_min_messages || |
| 305 | elevel == INFO); |
| 306 | } |
| 307 | |
| 308 | /* Skip processing effort if non-error message will not be output */ |
| 309 | if (elevel < ERROR && !output_to_server && !output_to_client) |
| 310 | return false; |
| 311 | |
| 312 | /* |
| 313 | * We need to do some actual work. Make sure that memory context |
| 314 | * initialization has finished, else we can't do anything useful. |
| 315 | */ |
| 316 | if (ErrorContext == NULL) |
| 317 | { |
| 318 | /* Oops, hard crash time; very little we can do safely here */ |
| 319 | write_stderr("error occurred at %s:%d before error message processing is available\n" , |
| 320 | filename ? filename : "(unknown file)" , lineno); |
| 321 | exit(2); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Okay, crank up a stack entry to store the info in. |
| 326 | */ |
| 327 | |
| 328 | if (recursion_depth++ > 0 && elevel >= ERROR) |
| 329 | { |
| 330 | /* |
| 331 | * Oops, error during error processing. Clear ErrorContext as |
| 332 | * discussed at top of file. We will not return to the original |
| 333 | * error's reporter or handler, so we don't need it. |
| 334 | */ |
| 335 | MemoryContextReset(ErrorContext); |
| 336 | |
| 337 | /* |
| 338 | * Infinite error recursion might be due to something broken in a |
| 339 | * context traceback routine. Abandon them too. We also abandon |
| 340 | * attempting to print the error statement (which, if long, could |
| 341 | * itself be the source of the recursive failure). |
| 342 | */ |
| 343 | if (in_error_recursion_trouble()) |
| 344 | { |
| 345 | error_context_stack = NULL; |
| 346 | debug_query_string = NULL; |
| 347 | } |
| 348 | } |
| 349 | if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE) |
| 350 | { |
| 351 | /* |
| 352 | * Wups, stack not big enough. We treat this as a PANIC condition |
| 353 | * because it suggests an infinite loop of errors during error |
| 354 | * recovery. |
| 355 | */ |
| 356 | errordata_stack_depth = -1; /* make room on stack */ |
| 357 | ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded" ))); |
| 358 | } |
| 359 | |
| 360 | /* Initialize data for this error frame */ |
| 361 | edata = &errordata[errordata_stack_depth]; |
| 362 | MemSet(edata, 0, sizeof(ErrorData)); |
| 363 | edata->elevel = elevel; |
| 364 | edata->output_to_server = output_to_server; |
| 365 | edata->output_to_client = output_to_client; |
| 366 | if (filename) |
| 367 | { |
| 368 | const char *slash; |
| 369 | |
| 370 | /* keep only base name, useful especially for vpath builds */ |
| 371 | slash = strrchr(filename, '/'); |
| 372 | if (slash) |
| 373 | filename = slash + 1; |
| 374 | } |
| 375 | edata->filename = filename; |
| 376 | edata->lineno = lineno; |
| 377 | edata->funcname = funcname; |
| 378 | /* the default text domain is the backend's */ |
| 379 | edata->domain = domain ? domain : PG_TEXTDOMAIN("postgres" ); |
| 380 | /* initialize context_domain the same way (see set_errcontext_domain()) */ |
| 381 | edata->context_domain = edata->domain; |
| 382 | /* Select default errcode based on elevel */ |
| 383 | if (elevel >= ERROR) |
| 384 | edata->sqlerrcode = ERRCODE_INTERNAL_ERROR; |
| 385 | else if (elevel == WARNING) |
| 386 | edata->sqlerrcode = ERRCODE_WARNING; |
| 387 | else |
| 388 | edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION; |
| 389 | /* errno is saved here so that error parameter eval can't change it */ |
| 390 | edata->saved_errno = errno; |
| 391 | |
| 392 | /* |
| 393 | * Any allocations for this error state level should go into ErrorContext |
| 394 | */ |
| 395 | edata->assoc_context = ErrorContext; |
| 396 | |
| 397 | recursion_depth--; |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * errfinish --- end an error-reporting cycle |
| 403 | * |
| 404 | * Produce the appropriate error report(s) and pop the error stack. |
| 405 | * |
| 406 | * If elevel is ERROR or worse, control does not return to the caller. |
| 407 | * See elog.h for the error level definitions. |
| 408 | */ |
| 409 | void |
| 410 | errfinish(int dummy,...) |
| 411 | { |
| 412 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 413 | int elevel; |
| 414 | MemoryContext oldcontext; |
| 415 | ErrorContextCallback *econtext; |
| 416 | |
| 417 | recursion_depth++; |
| 418 | CHECK_STACK_DEPTH(); |
| 419 | elevel = edata->elevel; |
| 420 | |
| 421 | /* |
| 422 | * Do processing in ErrorContext, which we hope has enough reserved space |
| 423 | * to report an error. |
| 424 | */ |
| 425 | oldcontext = MemoryContextSwitchTo(ErrorContext); |
| 426 | |
| 427 | /* |
| 428 | * Call any context callback functions. Errors occurring in callback |
| 429 | * functions will be treated as recursive errors --- this ensures we will |
| 430 | * avoid infinite recursion (see errstart). |
| 431 | */ |
| 432 | for (econtext = error_context_stack; |
| 433 | econtext != NULL; |
| 434 | econtext = econtext->previous) |
| 435 | econtext->callback(econtext->arg); |
| 436 | |
| 437 | /* |
| 438 | * If ERROR (not more nor less) we pass it off to the current handler. |
| 439 | * Printing it and popping the stack is the responsibility of the handler. |
| 440 | */ |
| 441 | if (elevel == ERROR) |
| 442 | { |
| 443 | /* |
| 444 | * We do some minimal cleanup before longjmp'ing so that handlers can |
| 445 | * execute in a reasonably sane state. |
| 446 | * |
| 447 | * Reset InterruptHoldoffCount in case we ereport'd from inside an |
| 448 | * interrupt holdoff section. (We assume here that no handler will |
| 449 | * itself be inside a holdoff section. If necessary, such a handler |
| 450 | * could save and restore InterruptHoldoffCount for itself, but this |
| 451 | * should make life easier for most.) |
| 452 | */ |
| 453 | InterruptHoldoffCount = 0; |
| 454 | QueryCancelHoldoffCount = 0; |
| 455 | |
| 456 | CritSectionCount = 0; /* should be unnecessary, but... */ |
| 457 | |
| 458 | /* |
| 459 | * Note that we leave CurrentMemoryContext set to ErrorContext. The |
| 460 | * handler should reset it to something else soon. |
| 461 | */ |
| 462 | |
| 463 | recursion_depth--; |
| 464 | PG_RE_THROW(); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * If we are doing FATAL or PANIC, abort any old-style COPY OUT in |
| 469 | * progress, so that we can report the message before dying. (Without |
| 470 | * this, pq_putmessage will refuse to send the message at all, which is |
| 471 | * what we want for NOTICE messages, but not for fatal exits.) This hack |
| 472 | * is necessary because of poor design of old-style copy protocol. |
| 473 | */ |
| 474 | if (elevel >= FATAL && whereToSendOutput == DestRemote) |
| 475 | pq_endcopyout(true); |
| 476 | |
| 477 | /* Emit the message to the right places */ |
| 478 | EmitErrorReport(); |
| 479 | |
| 480 | /* Now free up subsidiary data attached to stack entry, and release it */ |
| 481 | if (edata->message) |
| 482 | pfree(edata->message); |
| 483 | if (edata->detail) |
| 484 | pfree(edata->detail); |
| 485 | if (edata->detail_log) |
| 486 | pfree(edata->detail_log); |
| 487 | if (edata->hint) |
| 488 | pfree(edata->hint); |
| 489 | if (edata->context) |
| 490 | pfree(edata->context); |
| 491 | if (edata->schema_name) |
| 492 | pfree(edata->schema_name); |
| 493 | if (edata->table_name) |
| 494 | pfree(edata->table_name); |
| 495 | if (edata->column_name) |
| 496 | pfree(edata->column_name); |
| 497 | if (edata->datatype_name) |
| 498 | pfree(edata->datatype_name); |
| 499 | if (edata->constraint_name) |
| 500 | pfree(edata->constraint_name); |
| 501 | if (edata->internalquery) |
| 502 | pfree(edata->internalquery); |
| 503 | |
| 504 | errordata_stack_depth--; |
| 505 | |
| 506 | /* Exit error-handling context */ |
| 507 | MemoryContextSwitchTo(oldcontext); |
| 508 | recursion_depth--; |
| 509 | |
| 510 | /* |
| 511 | * Perform error recovery action as specified by elevel. |
| 512 | */ |
| 513 | if (elevel == FATAL) |
| 514 | { |
| 515 | /* |
| 516 | * For a FATAL error, we let proc_exit clean up and exit. |
| 517 | * |
| 518 | * If we just reported a startup failure, the client will disconnect |
| 519 | * on receiving it, so don't send any more to the client. |
| 520 | */ |
| 521 | if (PG_exception_stack == NULL && whereToSendOutput == DestRemote) |
| 522 | whereToSendOutput = DestNone; |
| 523 | |
| 524 | /* |
| 525 | * fflush here is just to improve the odds that we get to see the |
| 526 | * error message, in case things are so hosed that proc_exit crashes. |
| 527 | * Any other code you might be tempted to add here should probably be |
| 528 | * in an on_proc_exit or on_shmem_exit callback instead. |
| 529 | */ |
| 530 | fflush(stdout); |
| 531 | fflush(stderr); |
| 532 | |
| 533 | /* |
| 534 | * Do normal process-exit cleanup, then return exit code 1 to indicate |
| 535 | * FATAL termination. The postmaster may or may not consider this |
| 536 | * worthy of panic, depending on which subprocess returns it. |
| 537 | */ |
| 538 | proc_exit(1); |
| 539 | } |
| 540 | |
| 541 | if (elevel >= PANIC) |
| 542 | { |
| 543 | /* |
| 544 | * Serious crash time. Postmaster will observe SIGABRT process exit |
| 545 | * status and kill the other backends too. |
| 546 | * |
| 547 | * XXX: what if we are *in* the postmaster? abort() won't kill our |
| 548 | * children... |
| 549 | */ |
| 550 | fflush(stdout); |
| 551 | fflush(stderr); |
| 552 | abort(); |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Check for cancel/die interrupt first --- this is so that the user can |
| 557 | * stop a query emitting tons of notice or warning messages, even if it's |
| 558 | * in a loop that otherwise fails to check for interrupts. |
| 559 | */ |
| 560 | CHECK_FOR_INTERRUPTS(); |
| 561 | } |
| 562 | |
| 563 | |
| 564 | /* |
| 565 | * errcode --- add SQLSTATE error code to the current error |
| 566 | * |
| 567 | * The code is expected to be represented as per MAKE_SQLSTATE(). |
| 568 | */ |
| 569 | int |
| 570 | errcode(int sqlerrcode) |
| 571 | { |
| 572 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 573 | |
| 574 | /* we don't bother incrementing recursion_depth */ |
| 575 | CHECK_STACK_DEPTH(); |
| 576 | |
| 577 | edata->sqlerrcode = sqlerrcode; |
| 578 | |
| 579 | return 0; /* return value does not matter */ |
| 580 | } |
| 581 | |
| 582 | |
| 583 | /* |
| 584 | * errcode_for_file_access --- add SQLSTATE error code to the current error |
| 585 | * |
| 586 | * The SQLSTATE code is chosen based on the saved errno value. We assume |
| 587 | * that the failing operation was some type of disk file access. |
| 588 | * |
| 589 | * NOTE: the primary error message string should generally include %m |
| 590 | * when this is used. |
| 591 | */ |
| 592 | int |
| 593 | errcode_for_file_access(void) |
| 594 | { |
| 595 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 596 | |
| 597 | /* we don't bother incrementing recursion_depth */ |
| 598 | CHECK_STACK_DEPTH(); |
| 599 | |
| 600 | switch (edata->saved_errno) |
| 601 | { |
| 602 | /* Permission-denied failures */ |
| 603 | case EPERM: /* Not super-user */ |
| 604 | case EACCES: /* Permission denied */ |
| 605 | #ifdef EROFS |
| 606 | case EROFS: /* Read only file system */ |
| 607 | #endif |
| 608 | edata->sqlerrcode = ERRCODE_INSUFFICIENT_PRIVILEGE; |
| 609 | break; |
| 610 | |
| 611 | /* File not found */ |
| 612 | case ENOENT: /* No such file or directory */ |
| 613 | edata->sqlerrcode = ERRCODE_UNDEFINED_FILE; |
| 614 | break; |
| 615 | |
| 616 | /* Duplicate file */ |
| 617 | case EEXIST: /* File exists */ |
| 618 | edata->sqlerrcode = ERRCODE_DUPLICATE_FILE; |
| 619 | break; |
| 620 | |
| 621 | /* Wrong object type or state */ |
| 622 | case ENOTDIR: /* Not a directory */ |
| 623 | case EISDIR: /* Is a directory */ |
| 624 | #if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */ |
| 625 | case ENOTEMPTY: /* Directory not empty */ |
| 626 | #endif |
| 627 | edata->sqlerrcode = ERRCODE_WRONG_OBJECT_TYPE; |
| 628 | break; |
| 629 | |
| 630 | /* Insufficient resources */ |
| 631 | case ENOSPC: /* No space left on device */ |
| 632 | edata->sqlerrcode = ERRCODE_DISK_FULL; |
| 633 | break; |
| 634 | |
| 635 | case ENFILE: /* File table overflow */ |
| 636 | case EMFILE: /* Too many open files */ |
| 637 | edata->sqlerrcode = ERRCODE_INSUFFICIENT_RESOURCES; |
| 638 | break; |
| 639 | |
| 640 | /* Hardware failure */ |
| 641 | case EIO: /* I/O error */ |
| 642 | edata->sqlerrcode = ERRCODE_IO_ERROR; |
| 643 | break; |
| 644 | |
| 645 | /* All else is classified as internal errors */ |
| 646 | default: |
| 647 | edata->sqlerrcode = ERRCODE_INTERNAL_ERROR; |
| 648 | break; |
| 649 | } |
| 650 | |
| 651 | return 0; /* return value does not matter */ |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * errcode_for_socket_access --- add SQLSTATE error code to the current error |
| 656 | * |
| 657 | * The SQLSTATE code is chosen based on the saved errno value. We assume |
| 658 | * that the failing operation was some type of socket access. |
| 659 | * |
| 660 | * NOTE: the primary error message string should generally include %m |
| 661 | * when this is used. |
| 662 | */ |
| 663 | int |
| 664 | errcode_for_socket_access(void) |
| 665 | { |
| 666 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 667 | |
| 668 | /* we don't bother incrementing recursion_depth */ |
| 669 | CHECK_STACK_DEPTH(); |
| 670 | |
| 671 | switch (edata->saved_errno) |
| 672 | { |
| 673 | /* Loss of connection */ |
| 674 | case EPIPE: |
| 675 | #ifdef ECONNRESET |
| 676 | case ECONNRESET: |
| 677 | #endif |
| 678 | edata->sqlerrcode = ERRCODE_CONNECTION_FAILURE; |
| 679 | break; |
| 680 | |
| 681 | /* All else is classified as internal errors */ |
| 682 | default: |
| 683 | edata->sqlerrcode = ERRCODE_INTERNAL_ERROR; |
| 684 | break; |
| 685 | } |
| 686 | |
| 687 | return 0; /* return value does not matter */ |
| 688 | } |
| 689 | |
| 690 | |
| 691 | /* |
| 692 | * This macro handles expansion of a format string and associated parameters; |
| 693 | * it's common code for errmsg(), errdetail(), etc. Must be called inside |
| 694 | * a routine that is declared like "const char *fmt, ..." and has an edata |
| 695 | * pointer set up. The message is assigned to edata->targetfield, or |
| 696 | * appended to it if appendval is true. The message is subject to translation |
| 697 | * if translateit is true. |
| 698 | * |
| 699 | * Note: we pstrdup the buffer rather than just transferring its storage |
| 700 | * to the edata field because the buffer might be considerably larger than |
| 701 | * really necessary. |
| 702 | */ |
| 703 | #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \ |
| 704 | { \ |
| 705 | StringInfoData buf; \ |
| 706 | /* Internationalize the error format string */ \ |
| 707 | if ((translateit) && !in_error_recursion_trouble()) \ |
| 708 | fmt = dgettext((domain), fmt); \ |
| 709 | initStringInfo(&buf); \ |
| 710 | if ((appendval) && edata->targetfield) { \ |
| 711 | appendStringInfoString(&buf, edata->targetfield); \ |
| 712 | appendStringInfoChar(&buf, '\n'); \ |
| 713 | } \ |
| 714 | /* Generate actual output --- have to use appendStringInfoVA */ \ |
| 715 | for (;;) \ |
| 716 | { \ |
| 717 | va_list args; \ |
| 718 | int needed; \ |
| 719 | errno = edata->saved_errno; \ |
| 720 | va_start(args, fmt); \ |
| 721 | needed = appendStringInfoVA(&buf, fmt, args); \ |
| 722 | va_end(args); \ |
| 723 | if (needed == 0) \ |
| 724 | break; \ |
| 725 | enlargeStringInfo(&buf, needed); \ |
| 726 | } \ |
| 727 | /* Save the completed message into the stack item */ \ |
| 728 | if (edata->targetfield) \ |
| 729 | pfree(edata->targetfield); \ |
| 730 | edata->targetfield = pstrdup(buf.data); \ |
| 731 | pfree(buf.data); \ |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Same as above, except for pluralized error messages. The calling routine |
| 736 | * must be declared like "const char *fmt_singular, const char *fmt_plural, |
| 737 | * unsigned long n, ...". Translation is assumed always wanted. |
| 738 | */ |
| 739 | #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \ |
| 740 | { \ |
| 741 | const char *fmt; \ |
| 742 | StringInfoData buf; \ |
| 743 | /* Internationalize the error format string */ \ |
| 744 | if (!in_error_recursion_trouble()) \ |
| 745 | fmt = dngettext((domain), fmt_singular, fmt_plural, n); \ |
| 746 | else \ |
| 747 | fmt = (n == 1 ? fmt_singular : fmt_plural); \ |
| 748 | initStringInfo(&buf); \ |
| 749 | if ((appendval) && edata->targetfield) { \ |
| 750 | appendStringInfoString(&buf, edata->targetfield); \ |
| 751 | appendStringInfoChar(&buf, '\n'); \ |
| 752 | } \ |
| 753 | /* Generate actual output --- have to use appendStringInfoVA */ \ |
| 754 | for (;;) \ |
| 755 | { \ |
| 756 | va_list args; \ |
| 757 | int needed; \ |
| 758 | errno = edata->saved_errno; \ |
| 759 | va_start(args, n); \ |
| 760 | needed = appendStringInfoVA(&buf, fmt, args); \ |
| 761 | va_end(args); \ |
| 762 | if (needed == 0) \ |
| 763 | break; \ |
| 764 | enlargeStringInfo(&buf, needed); \ |
| 765 | } \ |
| 766 | /* Save the completed message into the stack item */ \ |
| 767 | if (edata->targetfield) \ |
| 768 | pfree(edata->targetfield); \ |
| 769 | edata->targetfield = pstrdup(buf.data); \ |
| 770 | pfree(buf.data); \ |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /* |
| 775 | * errmsg --- add a primary error message text to the current error |
| 776 | * |
| 777 | * In addition to the usual %-escapes recognized by printf, "%m" in |
| 778 | * fmt is replaced by the error message for the caller's value of errno. |
| 779 | * |
| 780 | * Note: no newline is needed at the end of the fmt string, since |
| 781 | * ereport will provide one for the output methods that need it. |
| 782 | */ |
| 783 | int |
| 784 | errmsg(const char *fmt,...) |
| 785 | { |
| 786 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 787 | MemoryContext oldcontext; |
| 788 | |
| 789 | recursion_depth++; |
| 790 | CHECK_STACK_DEPTH(); |
| 791 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 792 | |
| 793 | edata->message_id = fmt; |
| 794 | EVALUATE_MESSAGE(edata->domain, message, false, true); |
| 795 | |
| 796 | MemoryContextSwitchTo(oldcontext); |
| 797 | recursion_depth--; |
| 798 | return 0; /* return value does not matter */ |
| 799 | } |
| 800 | |
| 801 | |
| 802 | /* |
| 803 | * errmsg_internal --- add a primary error message text to the current error |
| 804 | * |
| 805 | * This is exactly like errmsg() except that strings passed to errmsg_internal |
| 806 | * are not translated, and are customarily left out of the |
| 807 | * internationalization message dictionary. This should be used for "can't |
| 808 | * happen" cases that are probably not worth spending translation effort on. |
| 809 | * We also use this for certain cases where we *must* not try to translate |
| 810 | * the message because the translation would fail and result in infinite |
| 811 | * error recursion. |
| 812 | */ |
| 813 | int |
| 814 | errmsg_internal(const char *fmt,...) |
| 815 | { |
| 816 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 817 | MemoryContext oldcontext; |
| 818 | |
| 819 | recursion_depth++; |
| 820 | CHECK_STACK_DEPTH(); |
| 821 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 822 | |
| 823 | edata->message_id = fmt; |
| 824 | EVALUATE_MESSAGE(edata->domain, message, false, false); |
| 825 | |
| 826 | MemoryContextSwitchTo(oldcontext); |
| 827 | recursion_depth--; |
| 828 | return 0; /* return value does not matter */ |
| 829 | } |
| 830 | |
| 831 | |
| 832 | /* |
| 833 | * errmsg_plural --- add a primary error message text to the current error, |
| 834 | * with support for pluralization of the message text |
| 835 | */ |
| 836 | int |
| 837 | errmsg_plural(const char *fmt_singular, const char *fmt_plural, |
| 838 | unsigned long n,...) |
| 839 | { |
| 840 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 841 | MemoryContext oldcontext; |
| 842 | |
| 843 | recursion_depth++; |
| 844 | CHECK_STACK_DEPTH(); |
| 845 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 846 | |
| 847 | edata->message_id = fmt_singular; |
| 848 | EVALUATE_MESSAGE_PLURAL(edata->domain, message, false); |
| 849 | |
| 850 | MemoryContextSwitchTo(oldcontext); |
| 851 | recursion_depth--; |
| 852 | return 0; /* return value does not matter */ |
| 853 | } |
| 854 | |
| 855 | |
| 856 | /* |
| 857 | * errdetail --- add a detail error message text to the current error |
| 858 | */ |
| 859 | int |
| 860 | errdetail(const char *fmt,...) |
| 861 | { |
| 862 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 863 | MemoryContext oldcontext; |
| 864 | |
| 865 | recursion_depth++; |
| 866 | CHECK_STACK_DEPTH(); |
| 867 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 868 | |
| 869 | EVALUATE_MESSAGE(edata->domain, detail, false, true); |
| 870 | |
| 871 | MemoryContextSwitchTo(oldcontext); |
| 872 | recursion_depth--; |
| 873 | return 0; /* return value does not matter */ |
| 874 | } |
| 875 | |
| 876 | |
| 877 | /* |
| 878 | * errdetail_internal --- add a detail error message text to the current error |
| 879 | * |
| 880 | * This is exactly like errdetail() except that strings passed to |
| 881 | * errdetail_internal are not translated, and are customarily left out of the |
| 882 | * internationalization message dictionary. This should be used for detail |
| 883 | * messages that seem not worth translating for one reason or another |
| 884 | * (typically, that they don't seem to be useful to average users). |
| 885 | */ |
| 886 | int |
| 887 | errdetail_internal(const char *fmt,...) |
| 888 | { |
| 889 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 890 | MemoryContext oldcontext; |
| 891 | |
| 892 | recursion_depth++; |
| 893 | CHECK_STACK_DEPTH(); |
| 894 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 895 | |
| 896 | EVALUATE_MESSAGE(edata->domain, detail, false, false); |
| 897 | |
| 898 | MemoryContextSwitchTo(oldcontext); |
| 899 | recursion_depth--; |
| 900 | return 0; /* return value does not matter */ |
| 901 | } |
| 902 | |
| 903 | |
| 904 | /* |
| 905 | * errdetail_log --- add a detail_log error message text to the current error |
| 906 | */ |
| 907 | int |
| 908 | errdetail_log(const char *fmt,...) |
| 909 | { |
| 910 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 911 | MemoryContext oldcontext; |
| 912 | |
| 913 | recursion_depth++; |
| 914 | CHECK_STACK_DEPTH(); |
| 915 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 916 | |
| 917 | EVALUATE_MESSAGE(edata->domain, detail_log, false, true); |
| 918 | |
| 919 | MemoryContextSwitchTo(oldcontext); |
| 920 | recursion_depth--; |
| 921 | return 0; /* return value does not matter */ |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * errdetail_log_plural --- add a detail_log error message text to the current error |
| 926 | * with support for pluralization of the message text |
| 927 | */ |
| 928 | int |
| 929 | errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, |
| 930 | unsigned long n,...) |
| 931 | { |
| 932 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 933 | MemoryContext oldcontext; |
| 934 | |
| 935 | recursion_depth++; |
| 936 | CHECK_STACK_DEPTH(); |
| 937 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 938 | |
| 939 | EVALUATE_MESSAGE_PLURAL(edata->domain, detail_log, false); |
| 940 | |
| 941 | MemoryContextSwitchTo(oldcontext); |
| 942 | recursion_depth--; |
| 943 | return 0; /* return value does not matter */ |
| 944 | } |
| 945 | |
| 946 | |
| 947 | /* |
| 948 | * errdetail_plural --- add a detail error message text to the current error, |
| 949 | * with support for pluralization of the message text |
| 950 | */ |
| 951 | int |
| 952 | errdetail_plural(const char *fmt_singular, const char *fmt_plural, |
| 953 | unsigned long n,...) |
| 954 | { |
| 955 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 956 | MemoryContext oldcontext; |
| 957 | |
| 958 | recursion_depth++; |
| 959 | CHECK_STACK_DEPTH(); |
| 960 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 961 | |
| 962 | EVALUATE_MESSAGE_PLURAL(edata->domain, detail, false); |
| 963 | |
| 964 | MemoryContextSwitchTo(oldcontext); |
| 965 | recursion_depth--; |
| 966 | return 0; /* return value does not matter */ |
| 967 | } |
| 968 | |
| 969 | |
| 970 | /* |
| 971 | * errhint --- add a hint error message text to the current error |
| 972 | */ |
| 973 | int |
| 974 | errhint(const char *fmt,...) |
| 975 | { |
| 976 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 977 | MemoryContext oldcontext; |
| 978 | |
| 979 | recursion_depth++; |
| 980 | CHECK_STACK_DEPTH(); |
| 981 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 982 | |
| 983 | EVALUATE_MESSAGE(edata->domain, hint, false, true); |
| 984 | |
| 985 | MemoryContextSwitchTo(oldcontext); |
| 986 | recursion_depth--; |
| 987 | return 0; /* return value does not matter */ |
| 988 | } |
| 989 | |
| 990 | |
| 991 | /* |
| 992 | * errcontext_msg --- add a context error message text to the current error |
| 993 | * |
| 994 | * Unlike other cases, multiple calls are allowed to build up a stack of |
| 995 | * context information. We assume earlier calls represent more-closely-nested |
| 996 | * states. |
| 997 | */ |
| 998 | int |
| 999 | errcontext_msg(const char *fmt,...) |
| 1000 | { |
| 1001 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1002 | MemoryContext oldcontext; |
| 1003 | |
| 1004 | recursion_depth++; |
| 1005 | CHECK_STACK_DEPTH(); |
| 1006 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 1007 | |
| 1008 | EVALUATE_MESSAGE(edata->context_domain, context, true, true); |
| 1009 | |
| 1010 | MemoryContextSwitchTo(oldcontext); |
| 1011 | recursion_depth--; |
| 1012 | return 0; /* return value does not matter */ |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * set_errcontext_domain --- set message domain to be used by errcontext() |
| 1017 | * |
| 1018 | * errcontext_msg() can be called from a different module than the original |
| 1019 | * ereport(), so we cannot use the message domain passed in errstart() to |
| 1020 | * translate it. Instead, each errcontext_msg() call should be preceded by |
| 1021 | * a set_errcontext_domain() call to specify the domain. This is usually |
| 1022 | * done transparently by the errcontext() macro. |
| 1023 | * |
| 1024 | * Although errcontext is primarily meant for use at call sites distant from |
| 1025 | * the original ereport call, there are a few places that invoke errcontext |
| 1026 | * within ereport. The expansion of errcontext as a comma expression calling |
| 1027 | * set_errcontext_domain then errcontext_msg is problematic in this case, |
| 1028 | * because the intended comma expression becomes two arguments to errfinish, |
| 1029 | * which the compiler is at liberty to evaluate in either order. But in |
| 1030 | * such a case, the set_errcontext_domain calls must be selecting the same |
| 1031 | * TEXTDOMAIN value that the errstart call did, so order does not matter |
| 1032 | * so long as errstart initializes context_domain along with domain. |
| 1033 | */ |
| 1034 | int |
| 1035 | set_errcontext_domain(const char *domain) |
| 1036 | { |
| 1037 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1038 | |
| 1039 | /* we don't bother incrementing recursion_depth */ |
| 1040 | CHECK_STACK_DEPTH(); |
| 1041 | |
| 1042 | /* the default text domain is the backend's */ |
| 1043 | edata->context_domain = domain ? domain : PG_TEXTDOMAIN("postgres" ); |
| 1044 | |
| 1045 | return 0; /* return value does not matter */ |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | /* |
| 1050 | * errhidestmt --- optionally suppress STATEMENT: field of log entry |
| 1051 | * |
| 1052 | * This should be called if the message text already includes the statement. |
| 1053 | */ |
| 1054 | int |
| 1055 | errhidestmt(bool hide_stmt) |
| 1056 | { |
| 1057 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1058 | |
| 1059 | /* we don't bother incrementing recursion_depth */ |
| 1060 | CHECK_STACK_DEPTH(); |
| 1061 | |
| 1062 | edata->hide_stmt = hide_stmt; |
| 1063 | |
| 1064 | return 0; /* return value does not matter */ |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * errhidecontext --- optionally suppress CONTEXT: field of log entry |
| 1069 | * |
| 1070 | * This should only be used for verbose debugging messages where the repeated |
| 1071 | * inclusion of context would bloat the log volume too much. |
| 1072 | */ |
| 1073 | int |
| 1074 | errhidecontext(bool hide_ctx) |
| 1075 | { |
| 1076 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1077 | |
| 1078 | /* we don't bother incrementing recursion_depth */ |
| 1079 | CHECK_STACK_DEPTH(); |
| 1080 | |
| 1081 | edata->hide_ctx = hide_ctx; |
| 1082 | |
| 1083 | return 0; /* return value does not matter */ |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | /* |
| 1088 | * errfunction --- add reporting function name to the current error |
| 1089 | * |
| 1090 | * This is used when backwards compatibility demands that the function |
| 1091 | * name appear in messages sent to old-protocol clients. Note that the |
| 1092 | * passed string is expected to be a non-freeable constant string. |
| 1093 | */ |
| 1094 | int |
| 1095 | errfunction(const char *funcname) |
| 1096 | { |
| 1097 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1098 | |
| 1099 | /* we don't bother incrementing recursion_depth */ |
| 1100 | CHECK_STACK_DEPTH(); |
| 1101 | |
| 1102 | edata->funcname = funcname; |
| 1103 | edata->show_funcname = true; |
| 1104 | |
| 1105 | return 0; /* return value does not matter */ |
| 1106 | } |
| 1107 | |
| 1108 | /* |
| 1109 | * errposition --- add cursor position to the current error |
| 1110 | */ |
| 1111 | int |
| 1112 | errposition(int cursorpos) |
| 1113 | { |
| 1114 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1115 | |
| 1116 | /* we don't bother incrementing recursion_depth */ |
| 1117 | CHECK_STACK_DEPTH(); |
| 1118 | |
| 1119 | edata->cursorpos = cursorpos; |
| 1120 | |
| 1121 | return 0; /* return value does not matter */ |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * internalerrposition --- add internal cursor position to the current error |
| 1126 | */ |
| 1127 | int |
| 1128 | internalerrposition(int cursorpos) |
| 1129 | { |
| 1130 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1131 | |
| 1132 | /* we don't bother incrementing recursion_depth */ |
| 1133 | CHECK_STACK_DEPTH(); |
| 1134 | |
| 1135 | edata->internalpos = cursorpos; |
| 1136 | |
| 1137 | return 0; /* return value does not matter */ |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * internalerrquery --- add internal query text to the current error |
| 1142 | * |
| 1143 | * Can also pass NULL to drop the internal query text entry. This case |
| 1144 | * is intended for use in error callback subroutines that are editorializing |
| 1145 | * on the layout of the error report. |
| 1146 | */ |
| 1147 | int |
| 1148 | internalerrquery(const char *query) |
| 1149 | { |
| 1150 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1151 | |
| 1152 | /* we don't bother incrementing recursion_depth */ |
| 1153 | CHECK_STACK_DEPTH(); |
| 1154 | |
| 1155 | if (edata->internalquery) |
| 1156 | { |
| 1157 | pfree(edata->internalquery); |
| 1158 | edata->internalquery = NULL; |
| 1159 | } |
| 1160 | |
| 1161 | if (query) |
| 1162 | edata->internalquery = MemoryContextStrdup(edata->assoc_context, query); |
| 1163 | |
| 1164 | return 0; /* return value does not matter */ |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * err_generic_string -- used to set individual ErrorData string fields |
| 1169 | * identified by PG_DIAG_xxx codes. |
| 1170 | * |
| 1171 | * This intentionally only supports fields that don't use localized strings, |
| 1172 | * so that there are no translation considerations. |
| 1173 | * |
| 1174 | * Most potential callers should not use this directly, but instead prefer |
| 1175 | * higher-level abstractions, such as errtablecol() (see relcache.c). |
| 1176 | */ |
| 1177 | int |
| 1178 | err_generic_string(int field, const char *str) |
| 1179 | { |
| 1180 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1181 | |
| 1182 | /* we don't bother incrementing recursion_depth */ |
| 1183 | CHECK_STACK_DEPTH(); |
| 1184 | |
| 1185 | switch (field) |
| 1186 | { |
| 1187 | case PG_DIAG_SCHEMA_NAME: |
| 1188 | set_errdata_field(edata->assoc_context, &edata->schema_name, str); |
| 1189 | break; |
| 1190 | case PG_DIAG_TABLE_NAME: |
| 1191 | set_errdata_field(edata->assoc_context, &edata->table_name, str); |
| 1192 | break; |
| 1193 | case PG_DIAG_COLUMN_NAME: |
| 1194 | set_errdata_field(edata->assoc_context, &edata->column_name, str); |
| 1195 | break; |
| 1196 | case PG_DIAG_DATATYPE_NAME: |
| 1197 | set_errdata_field(edata->assoc_context, &edata->datatype_name, str); |
| 1198 | break; |
| 1199 | case PG_DIAG_CONSTRAINT_NAME: |
| 1200 | set_errdata_field(edata->assoc_context, &edata->constraint_name, str); |
| 1201 | break; |
| 1202 | default: |
| 1203 | elog(ERROR, "unsupported ErrorData field id: %d" , field); |
| 1204 | break; |
| 1205 | } |
| 1206 | |
| 1207 | return 0; /* return value does not matter */ |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * set_errdata_field --- set an ErrorData string field |
| 1212 | */ |
| 1213 | static void |
| 1214 | set_errdata_field(MemoryContextData *cxt, char **ptr, const char *str) |
| 1215 | { |
| 1216 | Assert(*ptr == NULL); |
| 1217 | *ptr = MemoryContextStrdup(cxt, str); |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * geterrcode --- return the currently set SQLSTATE error code |
| 1222 | * |
| 1223 | * This is only intended for use in error callback subroutines, since there |
| 1224 | * is no other place outside elog.c where the concept is meaningful. |
| 1225 | */ |
| 1226 | int |
| 1227 | geterrcode(void) |
| 1228 | { |
| 1229 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1230 | |
| 1231 | /* we don't bother incrementing recursion_depth */ |
| 1232 | CHECK_STACK_DEPTH(); |
| 1233 | |
| 1234 | return edata->sqlerrcode; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * geterrposition --- return the currently set error position (0 if none) |
| 1239 | * |
| 1240 | * This is only intended for use in error callback subroutines, since there |
| 1241 | * is no other place outside elog.c where the concept is meaningful. |
| 1242 | */ |
| 1243 | int |
| 1244 | geterrposition(void) |
| 1245 | { |
| 1246 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1247 | |
| 1248 | /* we don't bother incrementing recursion_depth */ |
| 1249 | CHECK_STACK_DEPTH(); |
| 1250 | |
| 1251 | return edata->cursorpos; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * getinternalerrposition --- same for internal error position |
| 1256 | * |
| 1257 | * This is only intended for use in error callback subroutines, since there |
| 1258 | * is no other place outside elog.c where the concept is meaningful. |
| 1259 | */ |
| 1260 | int |
| 1261 | getinternalerrposition(void) |
| 1262 | { |
| 1263 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1264 | |
| 1265 | /* we don't bother incrementing recursion_depth */ |
| 1266 | CHECK_STACK_DEPTH(); |
| 1267 | |
| 1268 | return edata->internalpos; |
| 1269 | } |
| 1270 | |
| 1271 | |
| 1272 | /* |
| 1273 | * elog_start --- startup for old-style API |
| 1274 | * |
| 1275 | * All that we do here is stash the hidden filename/lineno/funcname |
| 1276 | * arguments into a stack entry, along with the current value of errno. |
| 1277 | * |
| 1278 | * We need this to be separate from elog_finish because there's no other |
| 1279 | * C89-compliant way to deal with inserting extra arguments into the elog |
| 1280 | * call. (When using C99's __VA_ARGS__, we could possibly merge this with |
| 1281 | * elog_finish, but there doesn't seem to be a good way to save errno before |
| 1282 | * evaluating the format arguments if we do that.) |
| 1283 | */ |
| 1284 | void |
| 1285 | elog_start(const char *filename, int lineno, const char *funcname) |
| 1286 | { |
| 1287 | ErrorData *edata; |
| 1288 | |
| 1289 | /* Make sure that memory context initialization has finished */ |
| 1290 | if (ErrorContext == NULL) |
| 1291 | { |
| 1292 | /* Oops, hard crash time; very little we can do safely here */ |
| 1293 | write_stderr("error occurred at %s:%d before error message processing is available\n" , |
| 1294 | filename ? filename : "(unknown file)" , lineno); |
| 1295 | exit(2); |
| 1296 | } |
| 1297 | |
| 1298 | if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE) |
| 1299 | { |
| 1300 | /* |
| 1301 | * Wups, stack not big enough. We treat this as a PANIC condition |
| 1302 | * because it suggests an infinite loop of errors during error |
| 1303 | * recovery. Note that the message is intentionally not localized, |
| 1304 | * else failure to convert it to client encoding could cause further |
| 1305 | * recursion. |
| 1306 | */ |
| 1307 | errordata_stack_depth = -1; /* make room on stack */ |
| 1308 | ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded" ))); |
| 1309 | } |
| 1310 | |
| 1311 | edata = &errordata[errordata_stack_depth]; |
| 1312 | if (filename) |
| 1313 | { |
| 1314 | const char *slash; |
| 1315 | |
| 1316 | /* keep only base name, useful especially for vpath builds */ |
| 1317 | slash = strrchr(filename, '/'); |
| 1318 | if (slash) |
| 1319 | filename = slash + 1; |
| 1320 | } |
| 1321 | edata->filename = filename; |
| 1322 | edata->lineno = lineno; |
| 1323 | edata->funcname = funcname; |
| 1324 | /* errno is saved now so that error parameter eval can't change it */ |
| 1325 | edata->saved_errno = errno; |
| 1326 | |
| 1327 | /* Use ErrorContext for any allocations done at this level. */ |
| 1328 | edata->assoc_context = ErrorContext; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * elog_finish --- finish up for old-style API |
| 1333 | */ |
| 1334 | void |
| 1335 | elog_finish(int elevel, const char *fmt,...) |
| 1336 | { |
| 1337 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1338 | MemoryContext oldcontext; |
| 1339 | |
| 1340 | CHECK_STACK_DEPTH(); |
| 1341 | |
| 1342 | /* |
| 1343 | * Do errstart() to see if we actually want to report the message. |
| 1344 | */ |
| 1345 | errordata_stack_depth--; |
| 1346 | errno = edata->saved_errno; |
| 1347 | if (!errstart(elevel, edata->filename, edata->lineno, edata->funcname, NULL)) |
| 1348 | return; /* nothing to do */ |
| 1349 | |
| 1350 | /* |
| 1351 | * Format error message just like errmsg_internal(). |
| 1352 | */ |
| 1353 | recursion_depth++; |
| 1354 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 1355 | |
| 1356 | edata->message_id = fmt; |
| 1357 | EVALUATE_MESSAGE(edata->domain, message, false, false); |
| 1358 | |
| 1359 | MemoryContextSwitchTo(oldcontext); |
| 1360 | recursion_depth--; |
| 1361 | |
| 1362 | /* |
| 1363 | * And let errfinish() finish up. |
| 1364 | */ |
| 1365 | errfinish(0); |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | /* |
| 1370 | * Functions to allow construction of error message strings separately from |
| 1371 | * the ereport() call itself. |
| 1372 | * |
| 1373 | * The expected calling convention is |
| 1374 | * |
| 1375 | * pre_format_elog_string(errno, domain), var = format_elog_string(format,...) |
| 1376 | * |
| 1377 | * which can be hidden behind a macro such as GUC_check_errdetail(). We |
| 1378 | * assume that any functions called in the arguments of format_elog_string() |
| 1379 | * cannot result in re-entrant use of these functions --- otherwise the wrong |
| 1380 | * text domain might be used, or the wrong errno substituted for %m. This is |
| 1381 | * okay for the current usage with GUC check hooks, but might need further |
| 1382 | * effort someday. |
| 1383 | * |
| 1384 | * The result of format_elog_string() is stored in ErrorContext, and will |
| 1385 | * therefore survive until FlushErrorState() is called. |
| 1386 | */ |
| 1387 | static int save_format_errnumber; |
| 1388 | static const char *save_format_domain; |
| 1389 | |
| 1390 | void |
| 1391 | pre_format_elog_string(int errnumber, const char *domain) |
| 1392 | { |
| 1393 | /* Save errno before evaluation of argument functions can change it */ |
| 1394 | save_format_errnumber = errnumber; |
| 1395 | /* Save caller's text domain */ |
| 1396 | save_format_domain = domain; |
| 1397 | } |
| 1398 | |
| 1399 | char * |
| 1400 | format_elog_string(const char *fmt,...) |
| 1401 | { |
| 1402 | ErrorData errdata; |
| 1403 | ErrorData *edata; |
| 1404 | MemoryContext oldcontext; |
| 1405 | |
| 1406 | /* Initialize a mostly-dummy error frame */ |
| 1407 | edata = &errdata; |
| 1408 | MemSet(edata, 0, sizeof(ErrorData)); |
| 1409 | /* the default text domain is the backend's */ |
| 1410 | edata->domain = save_format_domain ? save_format_domain : PG_TEXTDOMAIN("postgres" ); |
| 1411 | /* set the errno to be used to interpret %m */ |
| 1412 | edata->saved_errno = save_format_errnumber; |
| 1413 | |
| 1414 | oldcontext = MemoryContextSwitchTo(ErrorContext); |
| 1415 | |
| 1416 | edata->message_id = fmt; |
| 1417 | EVALUATE_MESSAGE(edata->domain, message, false, true); |
| 1418 | |
| 1419 | MemoryContextSwitchTo(oldcontext); |
| 1420 | |
| 1421 | return edata->message; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /* |
| 1426 | * Actual output of the top-of-stack error message |
| 1427 | * |
| 1428 | * In the ereport(ERROR) case this is called from PostgresMain (or not at all, |
| 1429 | * if the error is caught by somebody). For all other severity levels this |
| 1430 | * is called by errfinish. |
| 1431 | */ |
| 1432 | void |
| 1433 | EmitErrorReport(void) |
| 1434 | { |
| 1435 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1436 | MemoryContext oldcontext; |
| 1437 | |
| 1438 | recursion_depth++; |
| 1439 | CHECK_STACK_DEPTH(); |
| 1440 | oldcontext = MemoryContextSwitchTo(edata->assoc_context); |
| 1441 | |
| 1442 | /* |
| 1443 | * Call hook before sending message to log. The hook function is allowed |
| 1444 | * to turn off edata->output_to_server, so we must recheck that afterward. |
| 1445 | * Making any other change in the content of edata is not considered |
| 1446 | * supported. |
| 1447 | * |
| 1448 | * Note: the reason why the hook can only turn off output_to_server, and |
| 1449 | * not turn it on, is that it'd be unreliable: we will never get here at |
| 1450 | * all if errstart() deems the message uninteresting. A hook that could |
| 1451 | * make decisions in that direction would have to hook into errstart(), |
| 1452 | * where it would have much less information available. emit_log_hook is |
| 1453 | * intended for custom log filtering and custom log message transmission |
| 1454 | * mechanisms. |
| 1455 | * |
| 1456 | * The log hook has access to both the translated and original English |
| 1457 | * error message text, which is passed through to allow it to be used as a |
| 1458 | * message identifier. Note that the original text is not available for |
| 1459 | * detail, detail_log, hint and context text elements. |
| 1460 | */ |
| 1461 | if (edata->output_to_server && emit_log_hook) |
| 1462 | (*emit_log_hook) (edata); |
| 1463 | |
| 1464 | /* Send to server log, if enabled */ |
| 1465 | if (edata->output_to_server) |
| 1466 | send_message_to_server_log(edata); |
| 1467 | |
| 1468 | /* Send to client, if enabled */ |
| 1469 | if (edata->output_to_client) |
| 1470 | send_message_to_frontend(edata); |
| 1471 | |
| 1472 | MemoryContextSwitchTo(oldcontext); |
| 1473 | recursion_depth--; |
| 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * CopyErrorData --- obtain a copy of the topmost error stack entry |
| 1478 | * |
| 1479 | * This is only for use in error handler code. The data is copied into the |
| 1480 | * current memory context, so callers should always switch away from |
| 1481 | * ErrorContext first; otherwise it will be lost when FlushErrorState is done. |
| 1482 | */ |
| 1483 | ErrorData * |
| 1484 | CopyErrorData(void) |
| 1485 | { |
| 1486 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1487 | ErrorData *newedata; |
| 1488 | |
| 1489 | /* |
| 1490 | * we don't increment recursion_depth because out-of-memory here does not |
| 1491 | * indicate a problem within the error subsystem. |
| 1492 | */ |
| 1493 | CHECK_STACK_DEPTH(); |
| 1494 | |
| 1495 | Assert(CurrentMemoryContext != ErrorContext); |
| 1496 | |
| 1497 | /* Copy the struct itself */ |
| 1498 | newedata = (ErrorData *) palloc(sizeof(ErrorData)); |
| 1499 | memcpy(newedata, edata, sizeof(ErrorData)); |
| 1500 | |
| 1501 | /* Make copies of separately-allocated fields */ |
| 1502 | if (newedata->message) |
| 1503 | newedata->message = pstrdup(newedata->message); |
| 1504 | if (newedata->detail) |
| 1505 | newedata->detail = pstrdup(newedata->detail); |
| 1506 | if (newedata->detail_log) |
| 1507 | newedata->detail_log = pstrdup(newedata->detail_log); |
| 1508 | if (newedata->hint) |
| 1509 | newedata->hint = pstrdup(newedata->hint); |
| 1510 | if (newedata->context) |
| 1511 | newedata->context = pstrdup(newedata->context); |
| 1512 | if (newedata->schema_name) |
| 1513 | newedata->schema_name = pstrdup(newedata->schema_name); |
| 1514 | if (newedata->table_name) |
| 1515 | newedata->table_name = pstrdup(newedata->table_name); |
| 1516 | if (newedata->column_name) |
| 1517 | newedata->column_name = pstrdup(newedata->column_name); |
| 1518 | if (newedata->datatype_name) |
| 1519 | newedata->datatype_name = pstrdup(newedata->datatype_name); |
| 1520 | if (newedata->constraint_name) |
| 1521 | newedata->constraint_name = pstrdup(newedata->constraint_name); |
| 1522 | if (newedata->internalquery) |
| 1523 | newedata->internalquery = pstrdup(newedata->internalquery); |
| 1524 | |
| 1525 | /* Use the calling context for string allocation */ |
| 1526 | newedata->assoc_context = CurrentMemoryContext; |
| 1527 | |
| 1528 | return newedata; |
| 1529 | } |
| 1530 | |
| 1531 | /* |
| 1532 | * FreeErrorData --- free the structure returned by CopyErrorData. |
| 1533 | * |
| 1534 | * Error handlers should use this in preference to assuming they know all |
| 1535 | * the separately-allocated fields. |
| 1536 | */ |
| 1537 | void |
| 1538 | FreeErrorData(ErrorData *edata) |
| 1539 | { |
| 1540 | if (edata->message) |
| 1541 | pfree(edata->message); |
| 1542 | if (edata->detail) |
| 1543 | pfree(edata->detail); |
| 1544 | if (edata->detail_log) |
| 1545 | pfree(edata->detail_log); |
| 1546 | if (edata->hint) |
| 1547 | pfree(edata->hint); |
| 1548 | if (edata->context) |
| 1549 | pfree(edata->context); |
| 1550 | if (edata->schema_name) |
| 1551 | pfree(edata->schema_name); |
| 1552 | if (edata->table_name) |
| 1553 | pfree(edata->table_name); |
| 1554 | if (edata->column_name) |
| 1555 | pfree(edata->column_name); |
| 1556 | if (edata->datatype_name) |
| 1557 | pfree(edata->datatype_name); |
| 1558 | if (edata->constraint_name) |
| 1559 | pfree(edata->constraint_name); |
| 1560 | if (edata->internalquery) |
| 1561 | pfree(edata->internalquery); |
| 1562 | pfree(edata); |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * FlushErrorState --- flush the error state after error recovery |
| 1567 | * |
| 1568 | * This should be called by an error handler after it's done processing |
| 1569 | * the error; or as soon as it's done CopyErrorData, if it intends to |
| 1570 | * do stuff that is likely to provoke another error. You are not "out" of |
| 1571 | * the error subsystem until you have done this. |
| 1572 | */ |
| 1573 | void |
| 1574 | FlushErrorState(void) |
| 1575 | { |
| 1576 | /* |
| 1577 | * Reset stack to empty. The only case where it would be more than one |
| 1578 | * deep is if we serviced an error that interrupted construction of |
| 1579 | * another message. We assume control escaped out of that message |
| 1580 | * construction and won't ever go back. |
| 1581 | */ |
| 1582 | errordata_stack_depth = -1; |
| 1583 | recursion_depth = 0; |
| 1584 | /* Delete all data in ErrorContext */ |
| 1585 | MemoryContextResetAndDeleteChildren(ErrorContext); |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * ThrowErrorData --- report an error described by an ErrorData structure |
| 1590 | * |
| 1591 | * This is somewhat like ReThrowError, but it allows elevels besides ERROR, |
| 1592 | * and the boolean flags such as output_to_server are computed via the |
| 1593 | * default rules rather than being copied from the given ErrorData. |
| 1594 | * This is primarily used to re-report errors originally reported by |
| 1595 | * background worker processes and then propagated (with or without |
| 1596 | * modification) to the backend responsible for them. |
| 1597 | */ |
| 1598 | void |
| 1599 | ThrowErrorData(ErrorData *edata) |
| 1600 | { |
| 1601 | ErrorData *newedata; |
| 1602 | MemoryContext oldcontext; |
| 1603 | |
| 1604 | if (!errstart(edata->elevel, edata->filename, edata->lineno, |
| 1605 | edata->funcname, NULL)) |
| 1606 | return; /* error is not to be reported at all */ |
| 1607 | |
| 1608 | newedata = &errordata[errordata_stack_depth]; |
| 1609 | recursion_depth++; |
| 1610 | oldcontext = MemoryContextSwitchTo(newedata->assoc_context); |
| 1611 | |
| 1612 | /* Copy the supplied fields to the error stack entry. */ |
| 1613 | if (edata->sqlerrcode != 0) |
| 1614 | newedata->sqlerrcode = edata->sqlerrcode; |
| 1615 | if (edata->message) |
| 1616 | newedata->message = pstrdup(edata->message); |
| 1617 | if (edata->detail) |
| 1618 | newedata->detail = pstrdup(edata->detail); |
| 1619 | if (edata->detail_log) |
| 1620 | newedata->detail_log = pstrdup(edata->detail_log); |
| 1621 | if (edata->hint) |
| 1622 | newedata->hint = pstrdup(edata->hint); |
| 1623 | if (edata->context) |
| 1624 | newedata->context = pstrdup(edata->context); |
| 1625 | /* assume message_id is not available */ |
| 1626 | if (edata->schema_name) |
| 1627 | newedata->schema_name = pstrdup(edata->schema_name); |
| 1628 | if (edata->table_name) |
| 1629 | newedata->table_name = pstrdup(edata->table_name); |
| 1630 | if (edata->column_name) |
| 1631 | newedata->column_name = pstrdup(edata->column_name); |
| 1632 | if (edata->datatype_name) |
| 1633 | newedata->datatype_name = pstrdup(edata->datatype_name); |
| 1634 | if (edata->constraint_name) |
| 1635 | newedata->constraint_name = pstrdup(edata->constraint_name); |
| 1636 | newedata->cursorpos = edata->cursorpos; |
| 1637 | newedata->internalpos = edata->internalpos; |
| 1638 | if (edata->internalquery) |
| 1639 | newedata->internalquery = pstrdup(edata->internalquery); |
| 1640 | |
| 1641 | MemoryContextSwitchTo(oldcontext); |
| 1642 | recursion_depth--; |
| 1643 | |
| 1644 | /* Process the error. */ |
| 1645 | errfinish(0); |
| 1646 | } |
| 1647 | |
| 1648 | /* |
| 1649 | * ReThrowError --- re-throw a previously copied error |
| 1650 | * |
| 1651 | * A handler can do CopyErrorData/FlushErrorState to get out of the error |
| 1652 | * subsystem, then do some processing, and finally ReThrowError to re-throw |
| 1653 | * the original error. This is slower than just PG_RE_THROW() but should |
| 1654 | * be used if the "some processing" is likely to incur another error. |
| 1655 | */ |
| 1656 | void |
| 1657 | ReThrowError(ErrorData *edata) |
| 1658 | { |
| 1659 | ErrorData *newedata; |
| 1660 | |
| 1661 | Assert(edata->elevel == ERROR); |
| 1662 | |
| 1663 | /* Push the data back into the error context */ |
| 1664 | recursion_depth++; |
| 1665 | MemoryContextSwitchTo(ErrorContext); |
| 1666 | |
| 1667 | if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE) |
| 1668 | { |
| 1669 | /* |
| 1670 | * Wups, stack not big enough. We treat this as a PANIC condition |
| 1671 | * because it suggests an infinite loop of errors during error |
| 1672 | * recovery. |
| 1673 | */ |
| 1674 | errordata_stack_depth = -1; /* make room on stack */ |
| 1675 | ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded" ))); |
| 1676 | } |
| 1677 | |
| 1678 | newedata = &errordata[errordata_stack_depth]; |
| 1679 | memcpy(newedata, edata, sizeof(ErrorData)); |
| 1680 | |
| 1681 | /* Make copies of separately-allocated fields */ |
| 1682 | if (newedata->message) |
| 1683 | newedata->message = pstrdup(newedata->message); |
| 1684 | if (newedata->detail) |
| 1685 | newedata->detail = pstrdup(newedata->detail); |
| 1686 | if (newedata->detail_log) |
| 1687 | newedata->detail_log = pstrdup(newedata->detail_log); |
| 1688 | if (newedata->hint) |
| 1689 | newedata->hint = pstrdup(newedata->hint); |
| 1690 | if (newedata->context) |
| 1691 | newedata->context = pstrdup(newedata->context); |
| 1692 | if (newedata->schema_name) |
| 1693 | newedata->schema_name = pstrdup(newedata->schema_name); |
| 1694 | if (newedata->table_name) |
| 1695 | newedata->table_name = pstrdup(newedata->table_name); |
| 1696 | if (newedata->column_name) |
| 1697 | newedata->column_name = pstrdup(newedata->column_name); |
| 1698 | if (newedata->datatype_name) |
| 1699 | newedata->datatype_name = pstrdup(newedata->datatype_name); |
| 1700 | if (newedata->constraint_name) |
| 1701 | newedata->constraint_name = pstrdup(newedata->constraint_name); |
| 1702 | if (newedata->internalquery) |
| 1703 | newedata->internalquery = pstrdup(newedata->internalquery); |
| 1704 | |
| 1705 | /* Reset the assoc_context to be ErrorContext */ |
| 1706 | newedata->assoc_context = ErrorContext; |
| 1707 | |
| 1708 | recursion_depth--; |
| 1709 | PG_RE_THROW(); |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * pg_re_throw --- out-of-line implementation of PG_RE_THROW() macro |
| 1714 | */ |
| 1715 | void |
| 1716 | pg_re_throw(void) |
| 1717 | { |
| 1718 | /* If possible, throw the error to the next outer setjmp handler */ |
| 1719 | if (PG_exception_stack != NULL) |
| 1720 | siglongjmp(*PG_exception_stack, 1); |
| 1721 | else |
| 1722 | { |
| 1723 | /* |
| 1724 | * If we get here, elog(ERROR) was thrown inside a PG_TRY block, which |
| 1725 | * we have now exited only to discover that there is no outer setjmp |
| 1726 | * handler to pass the error to. Had the error been thrown outside |
| 1727 | * the block to begin with, we'd have promoted the error to FATAL, so |
| 1728 | * the correct behavior is to make it FATAL now; that is, emit it and |
| 1729 | * then call proc_exit. |
| 1730 | */ |
| 1731 | ErrorData *edata = &errordata[errordata_stack_depth]; |
| 1732 | |
| 1733 | Assert(errordata_stack_depth >= 0); |
| 1734 | Assert(edata->elevel == ERROR); |
| 1735 | edata->elevel = FATAL; |
| 1736 | |
| 1737 | /* |
| 1738 | * At least in principle, the increase in severity could have changed |
| 1739 | * where-to-output decisions, so recalculate. This should stay in |
| 1740 | * sync with errstart(), which see for comments. |
| 1741 | */ |
| 1742 | if (IsPostmasterEnvironment) |
| 1743 | edata->output_to_server = is_log_level_output(FATAL, |
| 1744 | log_min_messages); |
| 1745 | else |
| 1746 | edata->output_to_server = (FATAL >= log_min_messages); |
| 1747 | if (whereToSendOutput == DestRemote) |
| 1748 | edata->output_to_client = true; |
| 1749 | |
| 1750 | /* |
| 1751 | * We can use errfinish() for the rest, but we don't want it to call |
| 1752 | * any error context routines a second time. Since we know we are |
| 1753 | * about to exit, it should be OK to just clear the context stack. |
| 1754 | */ |
| 1755 | error_context_stack = NULL; |
| 1756 | |
| 1757 | errfinish(0); |
| 1758 | } |
| 1759 | |
| 1760 | /* Doesn't return ... */ |
| 1761 | ExceptionalCondition("pg_re_throw tried to return" , "FailedAssertion" , |
| 1762 | __FILE__, __LINE__); |
| 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | /* |
| 1767 | * GetErrorContextStack - Return the context stack, for display/diags |
| 1768 | * |
| 1769 | * Returns a pstrdup'd string in the caller's context which includes the PG |
| 1770 | * error call stack. It is the caller's responsibility to ensure this string |
| 1771 | * is pfree'd (or its context cleaned up) when done. |
| 1772 | * |
| 1773 | * This information is collected by traversing the error contexts and calling |
| 1774 | * each context's callback function, each of which is expected to call |
| 1775 | * errcontext() to return a string which can be presented to the user. |
| 1776 | */ |
| 1777 | char * |
| 1778 | GetErrorContextStack(void) |
| 1779 | { |
| 1780 | ErrorData *edata; |
| 1781 | ErrorContextCallback *econtext; |
| 1782 | |
| 1783 | /* |
| 1784 | * Okay, crank up a stack entry to store the info in. |
| 1785 | */ |
| 1786 | recursion_depth++; |
| 1787 | |
| 1788 | if (++errordata_stack_depth >= ERRORDATA_STACK_SIZE) |
| 1789 | { |
| 1790 | /* |
| 1791 | * Wups, stack not big enough. We treat this as a PANIC condition |
| 1792 | * because it suggests an infinite loop of errors during error |
| 1793 | * recovery. |
| 1794 | */ |
| 1795 | errordata_stack_depth = -1; /* make room on stack */ |
| 1796 | ereport(PANIC, (errmsg_internal("ERRORDATA_STACK_SIZE exceeded" ))); |
| 1797 | } |
| 1798 | |
| 1799 | /* |
| 1800 | * Things look good so far, so initialize our error frame |
| 1801 | */ |
| 1802 | edata = &errordata[errordata_stack_depth]; |
| 1803 | MemSet(edata, 0, sizeof(ErrorData)); |
| 1804 | |
| 1805 | /* |
| 1806 | * Set up assoc_context to be the caller's context, so any allocations |
| 1807 | * done (which will include edata->context) will use their context. |
| 1808 | */ |
| 1809 | edata->assoc_context = CurrentMemoryContext; |
| 1810 | |
| 1811 | /* |
| 1812 | * Call any context callback functions to collect the context information |
| 1813 | * into edata->context. |
| 1814 | * |
| 1815 | * Errors occurring in callback functions should go through the regular |
| 1816 | * error handling code which should handle any recursive errors, though we |
| 1817 | * double-check above, just in case. |
| 1818 | */ |
| 1819 | for (econtext = error_context_stack; |
| 1820 | econtext != NULL; |
| 1821 | econtext = econtext->previous) |
| 1822 | econtext->callback(econtext->arg); |
| 1823 | |
| 1824 | /* |
| 1825 | * Clean ourselves off the stack, any allocations done should have been |
| 1826 | * using edata->assoc_context, which we set up earlier to be the caller's |
| 1827 | * context, so we're free to just remove our entry off the stack and |
| 1828 | * decrement recursion depth and exit. |
| 1829 | */ |
| 1830 | errordata_stack_depth--; |
| 1831 | recursion_depth--; |
| 1832 | |
| 1833 | /* |
| 1834 | * Return a pointer to the string the caller asked for, which should have |
| 1835 | * been allocated in their context. |
| 1836 | */ |
| 1837 | return edata->context; |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | /* |
| 1842 | * Initialization of error output file |
| 1843 | */ |
| 1844 | void |
| 1845 | DebugFileOpen(void) |
| 1846 | { |
| 1847 | int fd, |
| 1848 | istty; |
| 1849 | |
| 1850 | if (OutputFileName[0]) |
| 1851 | { |
| 1852 | /* |
| 1853 | * A debug-output file name was given. |
| 1854 | * |
| 1855 | * Make sure we can write the file, and find out if it's a tty. |
| 1856 | */ |
| 1857 | if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY, |
| 1858 | 0666)) < 0) |
| 1859 | ereport(FATAL, |
| 1860 | (errcode_for_file_access(), |
| 1861 | errmsg("could not open file \"%s\": %m" , OutputFileName))); |
| 1862 | istty = isatty(fd); |
| 1863 | close(fd); |
| 1864 | |
| 1865 | /* |
| 1866 | * Redirect our stderr to the debug output file. |
| 1867 | */ |
| 1868 | if (!freopen(OutputFileName, "a" , stderr)) |
| 1869 | ereport(FATAL, |
| 1870 | (errcode_for_file_access(), |
| 1871 | errmsg("could not reopen file \"%s\" as stderr: %m" , |
| 1872 | OutputFileName))); |
| 1873 | |
| 1874 | /* |
| 1875 | * If the file is a tty and we're running under the postmaster, try to |
| 1876 | * send stdout there as well (if it isn't a tty then stderr will block |
| 1877 | * out stdout, so we may as well let stdout go wherever it was going |
| 1878 | * before). |
| 1879 | */ |
| 1880 | if (istty && IsUnderPostmaster) |
| 1881 | if (!freopen(OutputFileName, "a" , stdout)) |
| 1882 | ereport(FATAL, |
| 1883 | (errcode_for_file_access(), |
| 1884 | errmsg("could not reopen file \"%s\" as stdout: %m" , |
| 1885 | OutputFileName))); |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | #ifdef HAVE_SYSLOG |
| 1891 | |
| 1892 | /* |
| 1893 | * Set or update the parameters for syslog logging |
| 1894 | */ |
| 1895 | void |
| 1896 | set_syslog_parameters(const char *ident, int facility) |
| 1897 | { |
| 1898 | /* |
| 1899 | * guc.c is likely to call us repeatedly with same parameters, so don't |
| 1900 | * thrash the syslog connection unnecessarily. Also, we do not re-open |
| 1901 | * the connection until needed, since this routine will get called whether |
| 1902 | * or not Log_destination actually mentions syslog. |
| 1903 | * |
| 1904 | * Note that we make our own copy of the ident string rather than relying |
| 1905 | * on guc.c's. This may be overly paranoid, but it ensures that we cannot |
| 1906 | * accidentally free a string that syslog is still using. |
| 1907 | */ |
| 1908 | if (syslog_ident == NULL || strcmp(syslog_ident, ident) != 0 || |
| 1909 | syslog_facility != facility) |
| 1910 | { |
| 1911 | if (openlog_done) |
| 1912 | { |
| 1913 | closelog(); |
| 1914 | openlog_done = false; |
| 1915 | } |
| 1916 | if (syslog_ident) |
| 1917 | free(syslog_ident); |
| 1918 | syslog_ident = strdup(ident); |
| 1919 | /* if the strdup fails, we will cope in write_syslog() */ |
| 1920 | syslog_facility = facility; |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | |
| 1925 | /* |
| 1926 | * Write a message line to syslog |
| 1927 | */ |
| 1928 | static void |
| 1929 | write_syslog(int level, const char *line) |
| 1930 | { |
| 1931 | static unsigned long seq = 0; |
| 1932 | |
| 1933 | int len; |
| 1934 | const char *nlpos; |
| 1935 | |
| 1936 | /* Open syslog connection if not done yet */ |
| 1937 | if (!openlog_done) |
| 1938 | { |
| 1939 | openlog(syslog_ident ? syslog_ident : "postgres" , |
| 1940 | LOG_PID | LOG_NDELAY | LOG_NOWAIT, |
| 1941 | syslog_facility); |
| 1942 | openlog_done = true; |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * We add a sequence number to each log message to suppress "same" |
| 1947 | * messages. |
| 1948 | */ |
| 1949 | seq++; |
| 1950 | |
| 1951 | /* |
| 1952 | * Our problem here is that many syslog implementations don't handle long |
| 1953 | * messages in an acceptable manner. While this function doesn't help that |
| 1954 | * fact, it does work around by splitting up messages into smaller pieces. |
| 1955 | * |
| 1956 | * We divide into multiple syslog() calls if message is too long or if the |
| 1957 | * message contains embedded newline(s). |
| 1958 | */ |
| 1959 | len = strlen(line); |
| 1960 | nlpos = strchr(line, '\n'); |
| 1961 | if (syslog_split_messages && (len > PG_SYSLOG_LIMIT || nlpos != NULL)) |
| 1962 | { |
| 1963 | int chunk_nr = 0; |
| 1964 | |
| 1965 | while (len > 0) |
| 1966 | { |
| 1967 | char buf[PG_SYSLOG_LIMIT + 1]; |
| 1968 | int buflen; |
| 1969 | int i; |
| 1970 | |
| 1971 | /* if we start at a newline, move ahead one char */ |
| 1972 | if (line[0] == '\n') |
| 1973 | { |
| 1974 | line++; |
| 1975 | len--; |
| 1976 | /* we need to recompute the next newline's position, too */ |
| 1977 | nlpos = strchr(line, '\n'); |
| 1978 | continue; |
| 1979 | } |
| 1980 | |
| 1981 | /* copy one line, or as much as will fit, to buf */ |
| 1982 | if (nlpos != NULL) |
| 1983 | buflen = nlpos - line; |
| 1984 | else |
| 1985 | buflen = len; |
| 1986 | buflen = Min(buflen, PG_SYSLOG_LIMIT); |
| 1987 | memcpy(buf, line, buflen); |
| 1988 | buf[buflen] = '\0'; |
| 1989 | |
| 1990 | /* trim to multibyte letter boundary */ |
| 1991 | buflen = pg_mbcliplen(buf, buflen, buflen); |
| 1992 | if (buflen <= 0) |
| 1993 | return; |
| 1994 | buf[buflen] = '\0'; |
| 1995 | |
| 1996 | /* already word boundary? */ |
| 1997 | if (line[buflen] != '\0' && |
| 1998 | !isspace((unsigned char) line[buflen])) |
| 1999 | { |
| 2000 | /* try to divide at word boundary */ |
| 2001 | i = buflen - 1; |
| 2002 | while (i > 0 && !isspace((unsigned char) buf[i])) |
| 2003 | i--; |
| 2004 | |
| 2005 | if (i > 0) /* else couldn't divide word boundary */ |
| 2006 | { |
| 2007 | buflen = i; |
| 2008 | buf[i] = '\0'; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | chunk_nr++; |
| 2013 | |
| 2014 | if (syslog_sequence_numbers) |
| 2015 | syslog(level, "[%lu-%d] %s" , seq, chunk_nr, buf); |
| 2016 | else |
| 2017 | syslog(level, "[%d] %s" , chunk_nr, buf); |
| 2018 | |
| 2019 | line += buflen; |
| 2020 | len -= buflen; |
| 2021 | } |
| 2022 | } |
| 2023 | else |
| 2024 | { |
| 2025 | /* message short enough */ |
| 2026 | if (syslog_sequence_numbers) |
| 2027 | syslog(level, "[%lu] %s" , seq, line); |
| 2028 | else |
| 2029 | syslog(level, "%s" , line); |
| 2030 | } |
| 2031 | } |
| 2032 | #endif /* HAVE_SYSLOG */ |
| 2033 | |
| 2034 | #ifdef WIN32 |
| 2035 | /* |
| 2036 | * Get the PostgreSQL equivalent of the Windows ANSI code page. "ANSI" system |
| 2037 | * interfaces (e.g. CreateFileA()) expect string arguments in this encoding. |
| 2038 | * Every process in a given system will find the same value at all times. |
| 2039 | */ |
| 2040 | static int |
| 2041 | GetACPEncoding(void) |
| 2042 | { |
| 2043 | static int encoding = -2; |
| 2044 | |
| 2045 | if (encoding == -2) |
| 2046 | encoding = pg_codepage_to_encoding(GetACP()); |
| 2047 | |
| 2048 | return encoding; |
| 2049 | } |
| 2050 | |
| 2051 | /* |
| 2052 | * Write a message line to the windows event log |
| 2053 | */ |
| 2054 | static void |
| 2055 | write_eventlog(int level, const char *line, int len) |
| 2056 | { |
| 2057 | WCHAR *utf16; |
| 2058 | int eventlevel = EVENTLOG_ERROR_TYPE; |
| 2059 | static HANDLE evtHandle = INVALID_HANDLE_VALUE; |
| 2060 | |
| 2061 | if (evtHandle == INVALID_HANDLE_VALUE) |
| 2062 | { |
| 2063 | evtHandle = RegisterEventSource(NULL, |
| 2064 | event_source ? event_source : DEFAULT_EVENT_SOURCE); |
| 2065 | if (evtHandle == NULL) |
| 2066 | { |
| 2067 | evtHandle = INVALID_HANDLE_VALUE; |
| 2068 | return; |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | switch (level) |
| 2073 | { |
| 2074 | case DEBUG5: |
| 2075 | case DEBUG4: |
| 2076 | case DEBUG3: |
| 2077 | case DEBUG2: |
| 2078 | case DEBUG1: |
| 2079 | case LOG: |
| 2080 | case LOG_SERVER_ONLY: |
| 2081 | case INFO: |
| 2082 | case NOTICE: |
| 2083 | eventlevel = EVENTLOG_INFORMATION_TYPE; |
| 2084 | break; |
| 2085 | case WARNING: |
| 2086 | eventlevel = EVENTLOG_WARNING_TYPE; |
| 2087 | break; |
| 2088 | case ERROR: |
| 2089 | case FATAL: |
| 2090 | case PANIC: |
| 2091 | default: |
| 2092 | eventlevel = EVENTLOG_ERROR_TYPE; |
| 2093 | break; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * If message character encoding matches the encoding expected by |
| 2098 | * ReportEventA(), call it to avoid the hazards of conversion. Otherwise, |
| 2099 | * try to convert the message to UTF16 and write it with ReportEventW(). |
| 2100 | * Fall back on ReportEventA() if conversion failed. |
| 2101 | * |
| 2102 | * Since we palloc the structure required for conversion, also fall |
| 2103 | * through to writing unconverted if we have not yet set up |
| 2104 | * CurrentMemoryContext. |
| 2105 | * |
| 2106 | * Also verify that we are not on our way into error recursion trouble due |
| 2107 | * to error messages thrown deep inside pgwin32_message_to_UTF16(). |
| 2108 | */ |
| 2109 | if (!in_error_recursion_trouble() && |
| 2110 | CurrentMemoryContext != NULL && |
| 2111 | GetMessageEncoding() != GetACPEncoding()) |
| 2112 | { |
| 2113 | utf16 = pgwin32_message_to_UTF16(line, len, NULL); |
| 2114 | if (utf16) |
| 2115 | { |
| 2116 | ReportEventW(evtHandle, |
| 2117 | eventlevel, |
| 2118 | 0, |
| 2119 | 0, /* All events are Id 0 */ |
| 2120 | NULL, |
| 2121 | 1, |
| 2122 | 0, |
| 2123 | (LPCWSTR *) &utf16, |
| 2124 | NULL); |
| 2125 | /* XXX Try ReportEventA() when ReportEventW() fails? */ |
| 2126 | |
| 2127 | pfree(utf16); |
| 2128 | return; |
| 2129 | } |
| 2130 | } |
| 2131 | ReportEventA(evtHandle, |
| 2132 | eventlevel, |
| 2133 | 0, |
| 2134 | 0, /* All events are Id 0 */ |
| 2135 | NULL, |
| 2136 | 1, |
| 2137 | 0, |
| 2138 | &line, |
| 2139 | NULL); |
| 2140 | } |
| 2141 | #endif /* WIN32 */ |
| 2142 | |
| 2143 | static void |
| 2144 | write_console(const char *line, int len) |
| 2145 | { |
| 2146 | int rc; |
| 2147 | |
| 2148 | #ifdef WIN32 |
| 2149 | |
| 2150 | /* |
| 2151 | * Try to convert the message to UTF16 and write it with WriteConsoleW(). |
| 2152 | * Fall back on write() if anything fails. |
| 2153 | * |
| 2154 | * In contrast to write_eventlog(), don't skip straight to write() based |
| 2155 | * on the applicable encodings. Unlike WriteConsoleW(), write() depends |
| 2156 | * on the suitability of the console output code page. Since we put |
| 2157 | * stderr into binary mode in SubPostmasterMain(), write() skips the |
| 2158 | * necessary translation anyway. |
| 2159 | * |
| 2160 | * WriteConsoleW() will fail if stderr is redirected, so just fall through |
| 2161 | * to writing unconverted to the logfile in this case. |
| 2162 | * |
| 2163 | * Since we palloc the structure required for conversion, also fall |
| 2164 | * through to writing unconverted if we have not yet set up |
| 2165 | * CurrentMemoryContext. |
| 2166 | */ |
| 2167 | if (!in_error_recursion_trouble() && |
| 2168 | !redirection_done && |
| 2169 | CurrentMemoryContext != NULL) |
| 2170 | { |
| 2171 | WCHAR *utf16; |
| 2172 | int utf16len; |
| 2173 | |
| 2174 | utf16 = pgwin32_message_to_UTF16(line, len, &utf16len); |
| 2175 | if (utf16 != NULL) |
| 2176 | { |
| 2177 | HANDLE stdHandle; |
| 2178 | DWORD written; |
| 2179 | |
| 2180 | stdHandle = GetStdHandle(STD_ERROR_HANDLE); |
| 2181 | if (WriteConsoleW(stdHandle, utf16, utf16len, &written, NULL)) |
| 2182 | { |
| 2183 | pfree(utf16); |
| 2184 | return; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * In case WriteConsoleW() failed, fall back to writing the |
| 2189 | * message unconverted. |
| 2190 | */ |
| 2191 | pfree(utf16); |
| 2192 | } |
| 2193 | } |
| 2194 | #else |
| 2195 | |
| 2196 | /* |
| 2197 | * Conversion on non-win32 platforms is not implemented yet. It requires |
| 2198 | * non-throw version of pg_do_encoding_conversion(), that converts |
| 2199 | * unconvertable characters to '?' without errors. |
| 2200 | */ |
| 2201 | #endif |
| 2202 | |
| 2203 | /* |
| 2204 | * We ignore any error from write() here. We have no useful way to report |
| 2205 | * it ... certainly whining on stderr isn't likely to be productive. |
| 2206 | */ |
| 2207 | rc = write(fileno(stderr), line, len); |
| 2208 | (void) rc; |
| 2209 | } |
| 2210 | |
| 2211 | /* |
| 2212 | * setup formatted_log_time, for consistent times between CSV and regular logs |
| 2213 | */ |
| 2214 | static void |
| 2215 | setup_formatted_log_time(void) |
| 2216 | { |
| 2217 | pg_time_t stamp_time; |
| 2218 | char msbuf[13]; |
| 2219 | |
| 2220 | if (!saved_timeval_set) |
| 2221 | { |
| 2222 | gettimeofday(&saved_timeval, NULL); |
| 2223 | saved_timeval_set = true; |
| 2224 | } |
| 2225 | |
| 2226 | stamp_time = (pg_time_t) saved_timeval.tv_sec; |
| 2227 | |
| 2228 | /* |
| 2229 | * Note: we expect that guc.c will ensure that log_timezone is set up (at |
| 2230 | * least with a minimal GMT value) before Log_line_prefix can become |
| 2231 | * nonempty or CSV mode can be selected. |
| 2232 | */ |
| 2233 | pg_strftime(formatted_log_time, FORMATTED_TS_LEN, |
| 2234 | /* leave room for milliseconds... */ |
| 2235 | "%Y-%m-%d %H:%M:%S %Z" , |
| 2236 | pg_localtime(&stamp_time, log_timezone)); |
| 2237 | |
| 2238 | /* 'paste' milliseconds into place... */ |
| 2239 | sprintf(msbuf, ".%03d" , (int) (saved_timeval.tv_usec / 1000)); |
| 2240 | memcpy(formatted_log_time + 19, msbuf, 4); |
| 2241 | } |
| 2242 | |
| 2243 | /* |
| 2244 | * setup formatted_start_time |
| 2245 | */ |
| 2246 | static void |
| 2247 | setup_formatted_start_time(void) |
| 2248 | { |
| 2249 | pg_time_t stamp_time = (pg_time_t) MyStartTime; |
| 2250 | |
| 2251 | /* |
| 2252 | * Note: we expect that guc.c will ensure that log_timezone is set up (at |
| 2253 | * least with a minimal GMT value) before Log_line_prefix can become |
| 2254 | * nonempty or CSV mode can be selected. |
| 2255 | */ |
| 2256 | pg_strftime(formatted_start_time, FORMATTED_TS_LEN, |
| 2257 | "%Y-%m-%d %H:%M:%S %Z" , |
| 2258 | pg_localtime(&stamp_time, log_timezone)); |
| 2259 | } |
| 2260 | |
| 2261 | /* |
| 2262 | * process_log_prefix_padding --- helper function for processing the format |
| 2263 | * string in log_line_prefix |
| 2264 | * |
| 2265 | * Note: This function returns NULL if it finds something which |
| 2266 | * it deems invalid in the format string. |
| 2267 | */ |
| 2268 | static const char * |
| 2269 | process_log_prefix_padding(const char *p, int *ppadding) |
| 2270 | { |
| 2271 | int paddingsign = 1; |
| 2272 | int padding = 0; |
| 2273 | |
| 2274 | if (*p == '-') |
| 2275 | { |
| 2276 | p++; |
| 2277 | |
| 2278 | if (*p == '\0') /* Did the buf end in %- ? */ |
| 2279 | return NULL; |
| 2280 | paddingsign = -1; |
| 2281 | } |
| 2282 | |
| 2283 | /* generate an int version of the numerical string */ |
| 2284 | while (*p >= '0' && *p <= '9') |
| 2285 | padding = padding * 10 + (*p++ - '0'); |
| 2286 | |
| 2287 | /* format is invalid if it ends with the padding number */ |
| 2288 | if (*p == '\0') |
| 2289 | return NULL; |
| 2290 | |
| 2291 | padding *= paddingsign; |
| 2292 | *ppadding = padding; |
| 2293 | return p; |
| 2294 | } |
| 2295 | |
| 2296 | /* |
| 2297 | * Format tag info for log lines; append to the provided buffer. |
| 2298 | */ |
| 2299 | static void |
| 2300 | log_line_prefix(StringInfo buf, ErrorData *edata) |
| 2301 | { |
| 2302 | /* static counter for line numbers */ |
| 2303 | static long log_line_number = 0; |
| 2304 | |
| 2305 | /* has counter been reset in current process? */ |
| 2306 | static int log_my_pid = 0; |
| 2307 | int padding; |
| 2308 | const char *p; |
| 2309 | |
| 2310 | /* |
| 2311 | * This is one of the few places where we'd rather not inherit a static |
| 2312 | * variable's value from the postmaster. But since we will, reset it when |
| 2313 | * MyProcPid changes. MyStartTime also changes when MyProcPid does, so |
| 2314 | * reset the formatted start timestamp too. |
| 2315 | */ |
| 2316 | if (log_my_pid != MyProcPid) |
| 2317 | { |
| 2318 | log_line_number = 0; |
| 2319 | log_my_pid = MyProcPid; |
| 2320 | formatted_start_time[0] = '\0'; |
| 2321 | } |
| 2322 | log_line_number++; |
| 2323 | |
| 2324 | if (Log_line_prefix == NULL) |
| 2325 | return; /* in case guc hasn't run yet */ |
| 2326 | |
| 2327 | for (p = Log_line_prefix; *p != '\0'; p++) |
| 2328 | { |
| 2329 | if (*p != '%') |
| 2330 | { |
| 2331 | /* literal char, just copy */ |
| 2332 | appendStringInfoChar(buf, *p); |
| 2333 | continue; |
| 2334 | } |
| 2335 | |
| 2336 | /* must be a '%', so skip to the next char */ |
| 2337 | p++; |
| 2338 | if (*p == '\0') |
| 2339 | break; /* format error - ignore it */ |
| 2340 | else if (*p == '%') |
| 2341 | { |
| 2342 | /* string contains %% */ |
| 2343 | appendStringInfoChar(buf, '%'); |
| 2344 | continue; |
| 2345 | } |
| 2346 | |
| 2347 | |
| 2348 | /* |
| 2349 | * Process any formatting which may exist after the '%'. Note that |
| 2350 | * process_log_prefix_padding moves p past the padding number if it |
| 2351 | * exists. |
| 2352 | * |
| 2353 | * Note: Since only '-', '0' to '9' are valid formatting characters we |
| 2354 | * can do a quick check here to pre-check for formatting. If the char |
| 2355 | * is not formatting then we can skip a useless function call. |
| 2356 | * |
| 2357 | * Further note: At least on some platforms, passing %*s rather than |
| 2358 | * %s to appendStringInfo() is substantially slower, so many of the |
| 2359 | * cases below avoid doing that unless non-zero padding is in fact |
| 2360 | * specified. |
| 2361 | */ |
| 2362 | if (*p > '9') |
| 2363 | padding = 0; |
| 2364 | else if ((p = process_log_prefix_padding(p, &padding)) == NULL) |
| 2365 | break; |
| 2366 | |
| 2367 | /* process the option */ |
| 2368 | switch (*p) |
| 2369 | { |
| 2370 | case 'a': |
| 2371 | if (MyProcPort) |
| 2372 | { |
| 2373 | const char *appname = application_name; |
| 2374 | |
| 2375 | if (appname == NULL || *appname == '\0') |
| 2376 | appname = _("[unknown]" ); |
| 2377 | if (padding != 0) |
| 2378 | appendStringInfo(buf, "%*s" , padding, appname); |
| 2379 | else |
| 2380 | appendStringInfoString(buf, appname); |
| 2381 | } |
| 2382 | else if (padding != 0) |
| 2383 | appendStringInfoSpaces(buf, |
| 2384 | padding > 0 ? padding : -padding); |
| 2385 | |
| 2386 | break; |
| 2387 | case 'u': |
| 2388 | if (MyProcPort) |
| 2389 | { |
| 2390 | const char *username = MyProcPort->user_name; |
| 2391 | |
| 2392 | if (username == NULL || *username == '\0') |
| 2393 | username = _("[unknown]" ); |
| 2394 | if (padding != 0) |
| 2395 | appendStringInfo(buf, "%*s" , padding, username); |
| 2396 | else |
| 2397 | appendStringInfoString(buf, username); |
| 2398 | } |
| 2399 | else if (padding != 0) |
| 2400 | appendStringInfoSpaces(buf, |
| 2401 | padding > 0 ? padding : -padding); |
| 2402 | break; |
| 2403 | case 'd': |
| 2404 | if (MyProcPort) |
| 2405 | { |
| 2406 | const char *dbname = MyProcPort->database_name; |
| 2407 | |
| 2408 | if (dbname == NULL || *dbname == '\0') |
| 2409 | dbname = _("[unknown]" ); |
| 2410 | if (padding != 0) |
| 2411 | appendStringInfo(buf, "%*s" , padding, dbname); |
| 2412 | else |
| 2413 | appendStringInfoString(buf, dbname); |
| 2414 | } |
| 2415 | else if (padding != 0) |
| 2416 | appendStringInfoSpaces(buf, |
| 2417 | padding > 0 ? padding : -padding); |
| 2418 | break; |
| 2419 | case 'c': |
| 2420 | if (padding != 0) |
| 2421 | { |
| 2422 | char strfbuf[128]; |
| 2423 | |
| 2424 | snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x" , |
| 2425 | (long) (MyStartTime), MyProcPid); |
| 2426 | appendStringInfo(buf, "%*s" , padding, strfbuf); |
| 2427 | } |
| 2428 | else |
| 2429 | appendStringInfo(buf, "%lx.%x" , (long) (MyStartTime), MyProcPid); |
| 2430 | break; |
| 2431 | case 'p': |
| 2432 | if (padding != 0) |
| 2433 | appendStringInfo(buf, "%*d" , padding, MyProcPid); |
| 2434 | else |
| 2435 | appendStringInfo(buf, "%d" , MyProcPid); |
| 2436 | break; |
| 2437 | case 'l': |
| 2438 | if (padding != 0) |
| 2439 | appendStringInfo(buf, "%*ld" , padding, log_line_number); |
| 2440 | else |
| 2441 | appendStringInfo(buf, "%ld" , log_line_number); |
| 2442 | break; |
| 2443 | case 'm': |
| 2444 | setup_formatted_log_time(); |
| 2445 | if (padding != 0) |
| 2446 | appendStringInfo(buf, "%*s" , padding, formatted_log_time); |
| 2447 | else |
| 2448 | appendStringInfoString(buf, formatted_log_time); |
| 2449 | break; |
| 2450 | case 't': |
| 2451 | { |
| 2452 | pg_time_t stamp_time = (pg_time_t) time(NULL); |
| 2453 | char strfbuf[128]; |
| 2454 | |
| 2455 | pg_strftime(strfbuf, sizeof(strfbuf), |
| 2456 | "%Y-%m-%d %H:%M:%S %Z" , |
| 2457 | pg_localtime(&stamp_time, log_timezone)); |
| 2458 | if (padding != 0) |
| 2459 | appendStringInfo(buf, "%*s" , padding, strfbuf); |
| 2460 | else |
| 2461 | appendStringInfoString(buf, strfbuf); |
| 2462 | } |
| 2463 | break; |
| 2464 | case 'n': |
| 2465 | { |
| 2466 | char strfbuf[128]; |
| 2467 | |
| 2468 | if (!saved_timeval_set) |
| 2469 | { |
| 2470 | gettimeofday(&saved_timeval, NULL); |
| 2471 | saved_timeval_set = true; |
| 2472 | } |
| 2473 | |
| 2474 | snprintf(strfbuf, sizeof(strfbuf), "%ld.%03d" , |
| 2475 | (long) saved_timeval.tv_sec, |
| 2476 | (int) (saved_timeval.tv_usec / 1000)); |
| 2477 | |
| 2478 | if (padding != 0) |
| 2479 | appendStringInfo(buf, "%*s" , padding, strfbuf); |
| 2480 | else |
| 2481 | appendStringInfoString(buf, strfbuf); |
| 2482 | } |
| 2483 | break; |
| 2484 | case 's': |
| 2485 | if (formatted_start_time[0] == '\0') |
| 2486 | setup_formatted_start_time(); |
| 2487 | if (padding != 0) |
| 2488 | appendStringInfo(buf, "%*s" , padding, formatted_start_time); |
| 2489 | else |
| 2490 | appendStringInfoString(buf, formatted_start_time); |
| 2491 | break; |
| 2492 | case 'i': |
| 2493 | if (MyProcPort) |
| 2494 | { |
| 2495 | const char *psdisp; |
| 2496 | int displen; |
| 2497 | |
| 2498 | psdisp = get_ps_display(&displen); |
| 2499 | if (padding != 0) |
| 2500 | appendStringInfo(buf, "%*s" , padding, psdisp); |
| 2501 | else |
| 2502 | appendBinaryStringInfo(buf, psdisp, displen); |
| 2503 | |
| 2504 | } |
| 2505 | else if (padding != 0) |
| 2506 | appendStringInfoSpaces(buf, |
| 2507 | padding > 0 ? padding : -padding); |
| 2508 | break; |
| 2509 | case 'r': |
| 2510 | if (MyProcPort && MyProcPort->remote_host) |
| 2511 | { |
| 2512 | if (padding != 0) |
| 2513 | { |
| 2514 | if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0') |
| 2515 | { |
| 2516 | /* |
| 2517 | * This option is slightly special as the port |
| 2518 | * number may be appended onto the end. Here we |
| 2519 | * need to build 1 string which contains the |
| 2520 | * remote_host and optionally the remote_port (if |
| 2521 | * set) so we can properly align the string. |
| 2522 | */ |
| 2523 | |
| 2524 | char *hostport; |
| 2525 | |
| 2526 | hostport = psprintf("%s(%s)" , MyProcPort->remote_host, MyProcPort->remote_port); |
| 2527 | appendStringInfo(buf, "%*s" , padding, hostport); |
| 2528 | pfree(hostport); |
| 2529 | } |
| 2530 | else |
| 2531 | appendStringInfo(buf, "%*s" , padding, MyProcPort->remote_host); |
| 2532 | } |
| 2533 | else |
| 2534 | { |
| 2535 | /* padding is 0, so we don't need a temp buffer */ |
| 2536 | appendStringInfoString(buf, MyProcPort->remote_host); |
| 2537 | if (MyProcPort->remote_port && |
| 2538 | MyProcPort->remote_port[0] != '\0') |
| 2539 | appendStringInfo(buf, "(%s)" , |
| 2540 | MyProcPort->remote_port); |
| 2541 | } |
| 2542 | |
| 2543 | } |
| 2544 | else if (padding != 0) |
| 2545 | appendStringInfoSpaces(buf, |
| 2546 | padding > 0 ? padding : -padding); |
| 2547 | break; |
| 2548 | case 'h': |
| 2549 | if (MyProcPort && MyProcPort->remote_host) |
| 2550 | { |
| 2551 | if (padding != 0) |
| 2552 | appendStringInfo(buf, "%*s" , padding, MyProcPort->remote_host); |
| 2553 | else |
| 2554 | appendStringInfoString(buf, MyProcPort->remote_host); |
| 2555 | } |
| 2556 | else if (padding != 0) |
| 2557 | appendStringInfoSpaces(buf, |
| 2558 | padding > 0 ? padding : -padding); |
| 2559 | break; |
| 2560 | case 'q': |
| 2561 | /* in postmaster and friends, stop if %q is seen */ |
| 2562 | /* in a backend, just ignore */ |
| 2563 | if (MyProcPort == NULL) |
| 2564 | return; |
| 2565 | break; |
| 2566 | case 'v': |
| 2567 | /* keep VXID format in sync with lockfuncs.c */ |
| 2568 | if (MyProc != NULL && MyProc->backendId != InvalidBackendId) |
| 2569 | { |
| 2570 | if (padding != 0) |
| 2571 | { |
| 2572 | char strfbuf[128]; |
| 2573 | |
| 2574 | snprintf(strfbuf, sizeof(strfbuf) - 1, "%d/%u" , |
| 2575 | MyProc->backendId, MyProc->lxid); |
| 2576 | appendStringInfo(buf, "%*s" , padding, strfbuf); |
| 2577 | } |
| 2578 | else |
| 2579 | appendStringInfo(buf, "%d/%u" , MyProc->backendId, MyProc->lxid); |
| 2580 | } |
| 2581 | else if (padding != 0) |
| 2582 | appendStringInfoSpaces(buf, |
| 2583 | padding > 0 ? padding : -padding); |
| 2584 | break; |
| 2585 | case 'x': |
| 2586 | if (padding != 0) |
| 2587 | appendStringInfo(buf, "%*u" , padding, GetTopTransactionIdIfAny()); |
| 2588 | else |
| 2589 | appendStringInfo(buf, "%u" , GetTopTransactionIdIfAny()); |
| 2590 | break; |
| 2591 | case 'e': |
| 2592 | if (padding != 0) |
| 2593 | appendStringInfo(buf, "%*s" , padding, unpack_sql_state(edata->sqlerrcode)); |
| 2594 | else |
| 2595 | appendStringInfoString(buf, unpack_sql_state(edata->sqlerrcode)); |
| 2596 | break; |
| 2597 | default: |
| 2598 | /* format error - ignore it */ |
| 2599 | break; |
| 2600 | } |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | /* |
| 2605 | * append a CSV'd version of a string to a StringInfo |
| 2606 | * We use the PostgreSQL defaults for CSV, i.e. quote = escape = '"' |
| 2607 | * If it's NULL, append nothing. |
| 2608 | */ |
| 2609 | static inline void |
| 2610 | appendCSVLiteral(StringInfo buf, const char *data) |
| 2611 | { |
| 2612 | const char *p = data; |
| 2613 | char c; |
| 2614 | |
| 2615 | /* avoid confusing an empty string with NULL */ |
| 2616 | if (p == NULL) |
| 2617 | return; |
| 2618 | |
| 2619 | appendStringInfoCharMacro(buf, '"'); |
| 2620 | while ((c = *p++) != '\0') |
| 2621 | { |
| 2622 | if (c == '"') |
| 2623 | appendStringInfoCharMacro(buf, '"'); |
| 2624 | appendStringInfoCharMacro(buf, c); |
| 2625 | } |
| 2626 | appendStringInfoCharMacro(buf, '"'); |
| 2627 | } |
| 2628 | |
| 2629 | /* |
| 2630 | * Constructs the error message, depending on the Errordata it gets, in a CSV |
| 2631 | * format which is described in doc/src/sgml/config.sgml. |
| 2632 | */ |
| 2633 | static void |
| 2634 | write_csvlog(ErrorData *edata) |
| 2635 | { |
| 2636 | StringInfoData buf; |
| 2637 | bool print_stmt = false; |
| 2638 | |
| 2639 | /* static counter for line numbers */ |
| 2640 | static long log_line_number = 0; |
| 2641 | |
| 2642 | /* has counter been reset in current process? */ |
| 2643 | static int log_my_pid = 0; |
| 2644 | |
| 2645 | /* |
| 2646 | * This is one of the few places where we'd rather not inherit a static |
| 2647 | * variable's value from the postmaster. But since we will, reset it when |
| 2648 | * MyProcPid changes. |
| 2649 | */ |
| 2650 | if (log_my_pid != MyProcPid) |
| 2651 | { |
| 2652 | log_line_number = 0; |
| 2653 | log_my_pid = MyProcPid; |
| 2654 | formatted_start_time[0] = '\0'; |
| 2655 | } |
| 2656 | log_line_number++; |
| 2657 | |
| 2658 | initStringInfo(&buf); |
| 2659 | |
| 2660 | /* |
| 2661 | * timestamp with milliseconds |
| 2662 | * |
| 2663 | * Check if the timestamp is already calculated for the syslog message, |
| 2664 | * and use it if so. Otherwise, get the current timestamp. This is done |
| 2665 | * to put same timestamp in both syslog and csvlog messages. |
| 2666 | */ |
| 2667 | if (formatted_log_time[0] == '\0') |
| 2668 | setup_formatted_log_time(); |
| 2669 | |
| 2670 | appendStringInfoString(&buf, formatted_log_time); |
| 2671 | appendStringInfoChar(&buf, ','); |
| 2672 | |
| 2673 | /* username */ |
| 2674 | if (MyProcPort) |
| 2675 | appendCSVLiteral(&buf, MyProcPort->user_name); |
| 2676 | appendStringInfoChar(&buf, ','); |
| 2677 | |
| 2678 | /* database name */ |
| 2679 | if (MyProcPort) |
| 2680 | appendCSVLiteral(&buf, MyProcPort->database_name); |
| 2681 | appendStringInfoChar(&buf, ','); |
| 2682 | |
| 2683 | /* Process id */ |
| 2684 | if (MyProcPid != 0) |
| 2685 | appendStringInfo(&buf, "%d" , MyProcPid); |
| 2686 | appendStringInfoChar(&buf, ','); |
| 2687 | |
| 2688 | /* Remote host and port */ |
| 2689 | if (MyProcPort && MyProcPort->remote_host) |
| 2690 | { |
| 2691 | appendStringInfoChar(&buf, '"'); |
| 2692 | appendStringInfoString(&buf, MyProcPort->remote_host); |
| 2693 | if (MyProcPort->remote_port && MyProcPort->remote_port[0] != '\0') |
| 2694 | { |
| 2695 | appendStringInfoChar(&buf, ':'); |
| 2696 | appendStringInfoString(&buf, MyProcPort->remote_port); |
| 2697 | } |
| 2698 | appendStringInfoChar(&buf, '"'); |
| 2699 | } |
| 2700 | appendStringInfoChar(&buf, ','); |
| 2701 | |
| 2702 | /* session id */ |
| 2703 | appendStringInfo(&buf, "%lx.%x" , (long) MyStartTime, MyProcPid); |
| 2704 | appendStringInfoChar(&buf, ','); |
| 2705 | |
| 2706 | /* Line number */ |
| 2707 | appendStringInfo(&buf, "%ld" , log_line_number); |
| 2708 | appendStringInfoChar(&buf, ','); |
| 2709 | |
| 2710 | /* PS display */ |
| 2711 | if (MyProcPort) |
| 2712 | { |
| 2713 | StringInfoData msgbuf; |
| 2714 | const char *psdisp; |
| 2715 | int displen; |
| 2716 | |
| 2717 | initStringInfo(&msgbuf); |
| 2718 | |
| 2719 | psdisp = get_ps_display(&displen); |
| 2720 | appendBinaryStringInfo(&msgbuf, psdisp, displen); |
| 2721 | appendCSVLiteral(&buf, msgbuf.data); |
| 2722 | |
| 2723 | pfree(msgbuf.data); |
| 2724 | } |
| 2725 | appendStringInfoChar(&buf, ','); |
| 2726 | |
| 2727 | /* session start timestamp */ |
| 2728 | if (formatted_start_time[0] == '\0') |
| 2729 | setup_formatted_start_time(); |
| 2730 | appendStringInfoString(&buf, formatted_start_time); |
| 2731 | appendStringInfoChar(&buf, ','); |
| 2732 | |
| 2733 | /* Virtual transaction id */ |
| 2734 | /* keep VXID format in sync with lockfuncs.c */ |
| 2735 | if (MyProc != NULL && MyProc->backendId != InvalidBackendId) |
| 2736 | appendStringInfo(&buf, "%d/%u" , MyProc->backendId, MyProc->lxid); |
| 2737 | appendStringInfoChar(&buf, ','); |
| 2738 | |
| 2739 | /* Transaction id */ |
| 2740 | appendStringInfo(&buf, "%u" , GetTopTransactionIdIfAny()); |
| 2741 | appendStringInfoChar(&buf, ','); |
| 2742 | |
| 2743 | /* Error severity */ |
| 2744 | appendStringInfoString(&buf, _(error_severity(edata->elevel))); |
| 2745 | appendStringInfoChar(&buf, ','); |
| 2746 | |
| 2747 | /* SQL state code */ |
| 2748 | appendStringInfoString(&buf, unpack_sql_state(edata->sqlerrcode)); |
| 2749 | appendStringInfoChar(&buf, ','); |
| 2750 | |
| 2751 | /* errmessage */ |
| 2752 | appendCSVLiteral(&buf, edata->message); |
| 2753 | appendStringInfoChar(&buf, ','); |
| 2754 | |
| 2755 | /* errdetail or errdetail_log */ |
| 2756 | if (edata->detail_log) |
| 2757 | appendCSVLiteral(&buf, edata->detail_log); |
| 2758 | else |
| 2759 | appendCSVLiteral(&buf, edata->detail); |
| 2760 | appendStringInfoChar(&buf, ','); |
| 2761 | |
| 2762 | /* errhint */ |
| 2763 | appendCSVLiteral(&buf, edata->hint); |
| 2764 | appendStringInfoChar(&buf, ','); |
| 2765 | |
| 2766 | /* internal query */ |
| 2767 | appendCSVLiteral(&buf, edata->internalquery); |
| 2768 | appendStringInfoChar(&buf, ','); |
| 2769 | |
| 2770 | /* if printed internal query, print internal pos too */ |
| 2771 | if (edata->internalpos > 0 && edata->internalquery != NULL) |
| 2772 | appendStringInfo(&buf, "%d" , edata->internalpos); |
| 2773 | appendStringInfoChar(&buf, ','); |
| 2774 | |
| 2775 | /* errcontext */ |
| 2776 | if (!edata->hide_ctx) |
| 2777 | appendCSVLiteral(&buf, edata->context); |
| 2778 | appendStringInfoChar(&buf, ','); |
| 2779 | |
| 2780 | /* user query --- only reported if not disabled by the caller */ |
| 2781 | if (is_log_level_output(edata->elevel, log_min_error_statement) && |
| 2782 | debug_query_string != NULL && |
| 2783 | !edata->hide_stmt) |
| 2784 | print_stmt = true; |
| 2785 | if (print_stmt) |
| 2786 | appendCSVLiteral(&buf, debug_query_string); |
| 2787 | appendStringInfoChar(&buf, ','); |
| 2788 | if (print_stmt && edata->cursorpos > 0) |
| 2789 | appendStringInfo(&buf, "%d" , edata->cursorpos); |
| 2790 | appendStringInfoChar(&buf, ','); |
| 2791 | |
| 2792 | /* file error location */ |
| 2793 | if (Log_error_verbosity >= PGERROR_VERBOSE) |
| 2794 | { |
| 2795 | StringInfoData msgbuf; |
| 2796 | |
| 2797 | initStringInfo(&msgbuf); |
| 2798 | |
| 2799 | if (edata->funcname && edata->filename) |
| 2800 | appendStringInfo(&msgbuf, "%s, %s:%d" , |
| 2801 | edata->funcname, edata->filename, |
| 2802 | edata->lineno); |
| 2803 | else if (edata->filename) |
| 2804 | appendStringInfo(&msgbuf, "%s:%d" , |
| 2805 | edata->filename, edata->lineno); |
| 2806 | appendCSVLiteral(&buf, msgbuf.data); |
| 2807 | pfree(msgbuf.data); |
| 2808 | } |
| 2809 | appendStringInfoChar(&buf, ','); |
| 2810 | |
| 2811 | /* application name */ |
| 2812 | if (application_name) |
| 2813 | appendCSVLiteral(&buf, application_name); |
| 2814 | |
| 2815 | appendStringInfoChar(&buf, '\n'); |
| 2816 | |
| 2817 | /* If in the syslogger process, try to write messages direct to file */ |
| 2818 | if (am_syslogger) |
| 2819 | write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_CSVLOG); |
| 2820 | else |
| 2821 | write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_CSVLOG); |
| 2822 | |
| 2823 | pfree(buf.data); |
| 2824 | } |
| 2825 | |
| 2826 | /* |
| 2827 | * Unpack MAKE_SQLSTATE code. Note that this returns a pointer to a |
| 2828 | * static buffer. |
| 2829 | */ |
| 2830 | char * |
| 2831 | unpack_sql_state(int sql_state) |
| 2832 | { |
| 2833 | static char buf[12]; |
| 2834 | int i; |
| 2835 | |
| 2836 | for (i = 0; i < 5; i++) |
| 2837 | { |
| 2838 | buf[i] = PGUNSIXBIT(sql_state); |
| 2839 | sql_state >>= 6; |
| 2840 | } |
| 2841 | |
| 2842 | buf[i] = '\0'; |
| 2843 | return buf; |
| 2844 | } |
| 2845 | |
| 2846 | |
| 2847 | /* |
| 2848 | * Write error report to server's log |
| 2849 | */ |
| 2850 | static void |
| 2851 | send_message_to_server_log(ErrorData *edata) |
| 2852 | { |
| 2853 | StringInfoData buf; |
| 2854 | |
| 2855 | initStringInfo(&buf); |
| 2856 | |
| 2857 | saved_timeval_set = false; |
| 2858 | formatted_log_time[0] = '\0'; |
| 2859 | |
| 2860 | log_line_prefix(&buf, edata); |
| 2861 | appendStringInfo(&buf, "%s: " , _(error_severity(edata->elevel))); |
| 2862 | |
| 2863 | if (Log_error_verbosity >= PGERROR_VERBOSE) |
| 2864 | appendStringInfo(&buf, "%s: " , unpack_sql_state(edata->sqlerrcode)); |
| 2865 | |
| 2866 | if (edata->message) |
| 2867 | append_with_tabs(&buf, edata->message); |
| 2868 | else |
| 2869 | append_with_tabs(&buf, _("missing error text" )); |
| 2870 | |
| 2871 | if (edata->cursorpos > 0) |
| 2872 | appendStringInfo(&buf, _(" at character %d" ), |
| 2873 | edata->cursorpos); |
| 2874 | else if (edata->internalpos > 0) |
| 2875 | appendStringInfo(&buf, _(" at character %d" ), |
| 2876 | edata->internalpos); |
| 2877 | |
| 2878 | appendStringInfoChar(&buf, '\n'); |
| 2879 | |
| 2880 | if (Log_error_verbosity >= PGERROR_DEFAULT) |
| 2881 | { |
| 2882 | if (edata->detail_log) |
| 2883 | { |
| 2884 | log_line_prefix(&buf, edata); |
| 2885 | appendStringInfoString(&buf, _("DETAIL: " )); |
| 2886 | append_with_tabs(&buf, edata->detail_log); |
| 2887 | appendStringInfoChar(&buf, '\n'); |
| 2888 | } |
| 2889 | else if (edata->detail) |
| 2890 | { |
| 2891 | log_line_prefix(&buf, edata); |
| 2892 | appendStringInfoString(&buf, _("DETAIL: " )); |
| 2893 | append_with_tabs(&buf, edata->detail); |
| 2894 | appendStringInfoChar(&buf, '\n'); |
| 2895 | } |
| 2896 | if (edata->hint) |
| 2897 | { |
| 2898 | log_line_prefix(&buf, edata); |
| 2899 | appendStringInfoString(&buf, _("HINT: " )); |
| 2900 | append_with_tabs(&buf, edata->hint); |
| 2901 | appendStringInfoChar(&buf, '\n'); |
| 2902 | } |
| 2903 | if (edata->internalquery) |
| 2904 | { |
| 2905 | log_line_prefix(&buf, edata); |
| 2906 | appendStringInfoString(&buf, _("QUERY: " )); |
| 2907 | append_with_tabs(&buf, edata->internalquery); |
| 2908 | appendStringInfoChar(&buf, '\n'); |
| 2909 | } |
| 2910 | if (edata->context && !edata->hide_ctx) |
| 2911 | { |
| 2912 | log_line_prefix(&buf, edata); |
| 2913 | appendStringInfoString(&buf, _("CONTEXT: " )); |
| 2914 | append_with_tabs(&buf, edata->context); |
| 2915 | appendStringInfoChar(&buf, '\n'); |
| 2916 | } |
| 2917 | if (Log_error_verbosity >= PGERROR_VERBOSE) |
| 2918 | { |
| 2919 | /* assume no newlines in funcname or filename... */ |
| 2920 | if (edata->funcname && edata->filename) |
| 2921 | { |
| 2922 | log_line_prefix(&buf, edata); |
| 2923 | appendStringInfo(&buf, _("LOCATION: %s, %s:%d\n" ), |
| 2924 | edata->funcname, edata->filename, |
| 2925 | edata->lineno); |
| 2926 | } |
| 2927 | else if (edata->filename) |
| 2928 | { |
| 2929 | log_line_prefix(&buf, edata); |
| 2930 | appendStringInfo(&buf, _("LOCATION: %s:%d\n" ), |
| 2931 | edata->filename, edata->lineno); |
| 2932 | } |
| 2933 | } |
| 2934 | } |
| 2935 | |
| 2936 | /* |
| 2937 | * If the user wants the query that generated this error logged, do it. |
| 2938 | */ |
| 2939 | if (is_log_level_output(edata->elevel, log_min_error_statement) && |
| 2940 | debug_query_string != NULL && |
| 2941 | !edata->hide_stmt) |
| 2942 | { |
| 2943 | log_line_prefix(&buf, edata); |
| 2944 | appendStringInfoString(&buf, _("STATEMENT: " )); |
| 2945 | append_with_tabs(&buf, debug_query_string); |
| 2946 | appendStringInfoChar(&buf, '\n'); |
| 2947 | } |
| 2948 | |
| 2949 | #ifdef HAVE_SYSLOG |
| 2950 | /* Write to syslog, if enabled */ |
| 2951 | if (Log_destination & LOG_DESTINATION_SYSLOG) |
| 2952 | { |
| 2953 | int syslog_level; |
| 2954 | |
| 2955 | switch (edata->elevel) |
| 2956 | { |
| 2957 | case DEBUG5: |
| 2958 | case DEBUG4: |
| 2959 | case DEBUG3: |
| 2960 | case DEBUG2: |
| 2961 | case DEBUG1: |
| 2962 | syslog_level = LOG_DEBUG; |
| 2963 | break; |
| 2964 | case LOG: |
| 2965 | case LOG_SERVER_ONLY: |
| 2966 | case INFO: |
| 2967 | syslog_level = LOG_INFO; |
| 2968 | break; |
| 2969 | case NOTICE: |
| 2970 | case WARNING: |
| 2971 | syslog_level = LOG_NOTICE; |
| 2972 | break; |
| 2973 | case ERROR: |
| 2974 | syslog_level = LOG_WARNING; |
| 2975 | break; |
| 2976 | case FATAL: |
| 2977 | syslog_level = LOG_ERR; |
| 2978 | break; |
| 2979 | case PANIC: |
| 2980 | default: |
| 2981 | syslog_level = LOG_CRIT; |
| 2982 | break; |
| 2983 | } |
| 2984 | |
| 2985 | write_syslog(syslog_level, buf.data); |
| 2986 | } |
| 2987 | #endif /* HAVE_SYSLOG */ |
| 2988 | |
| 2989 | #ifdef WIN32 |
| 2990 | /* Write to eventlog, if enabled */ |
| 2991 | if (Log_destination & LOG_DESTINATION_EVENTLOG) |
| 2992 | { |
| 2993 | write_eventlog(edata->elevel, buf.data, buf.len); |
| 2994 | } |
| 2995 | #endif /* WIN32 */ |
| 2996 | |
| 2997 | /* Write to stderr, if enabled */ |
| 2998 | if ((Log_destination & LOG_DESTINATION_STDERR) || whereToSendOutput == DestDebug) |
| 2999 | { |
| 3000 | /* |
| 3001 | * Use the chunking protocol if we know the syslogger should be |
| 3002 | * catching stderr output, and we are not ourselves the syslogger. |
| 3003 | * Otherwise, just do a vanilla write to stderr. |
| 3004 | */ |
| 3005 | if (redirection_done && !am_syslogger) |
| 3006 | write_pipe_chunks(buf.data, buf.len, LOG_DESTINATION_STDERR); |
| 3007 | #ifdef WIN32 |
| 3008 | |
| 3009 | /* |
| 3010 | * In a win32 service environment, there is no usable stderr. Capture |
| 3011 | * anything going there and write it to the eventlog instead. |
| 3012 | * |
| 3013 | * If stderr redirection is active, it was OK to write to stderr above |
| 3014 | * because that's really a pipe to the syslogger process. |
| 3015 | */ |
| 3016 | else if (pgwin32_is_service()) |
| 3017 | write_eventlog(edata->elevel, buf.data, buf.len); |
| 3018 | #endif |
| 3019 | else |
| 3020 | write_console(buf.data, buf.len); |
| 3021 | } |
| 3022 | |
| 3023 | /* If in the syslogger process, try to write messages direct to file */ |
| 3024 | if (am_syslogger) |
| 3025 | write_syslogger_file(buf.data, buf.len, LOG_DESTINATION_STDERR); |
| 3026 | |
| 3027 | /* Write to CSV log if enabled */ |
| 3028 | if (Log_destination & LOG_DESTINATION_CSVLOG) |
| 3029 | { |
| 3030 | if (redirection_done || am_syslogger) |
| 3031 | { |
| 3032 | /* |
| 3033 | * send CSV data if it's safe to do so (syslogger doesn't need the |
| 3034 | * pipe). First get back the space in the message buffer. |
| 3035 | */ |
| 3036 | pfree(buf.data); |
| 3037 | write_csvlog(edata); |
| 3038 | } |
| 3039 | else |
| 3040 | { |
| 3041 | /* |
| 3042 | * syslogger not up (yet), so just dump the message to stderr, |
| 3043 | * unless we already did so above. |
| 3044 | */ |
| 3045 | if (!(Log_destination & LOG_DESTINATION_STDERR) && |
| 3046 | whereToSendOutput != DestDebug) |
| 3047 | write_console(buf.data, buf.len); |
| 3048 | pfree(buf.data); |
| 3049 | } |
| 3050 | } |
| 3051 | else |
| 3052 | { |
| 3053 | pfree(buf.data); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | /* |
| 3058 | * Send data to the syslogger using the chunked protocol |
| 3059 | * |
| 3060 | * Note: when there are multiple backends writing into the syslogger pipe, |
| 3061 | * it's critical that each write go into the pipe indivisibly, and not |
| 3062 | * get interleaved with data from other processes. Fortunately, the POSIX |
| 3063 | * spec requires that writes to pipes be atomic so long as they are not |
| 3064 | * more than PIPE_BUF bytes long. So we divide long messages into chunks |
| 3065 | * that are no more than that length, and send one chunk per write() call. |
| 3066 | * The collector process knows how to reassemble the chunks. |
| 3067 | * |
| 3068 | * Because of the atomic write requirement, there are only two possible |
| 3069 | * results from write() here: -1 for failure, or the requested number of |
| 3070 | * bytes. There is not really anything we can do about a failure; retry would |
| 3071 | * probably be an infinite loop, and we can't even report the error usefully. |
| 3072 | * (There is noplace else we could send it!) So we might as well just ignore |
| 3073 | * the result from write(). However, on some platforms you get a compiler |
| 3074 | * warning from ignoring write()'s result, so do a little dance with casting |
| 3075 | * rc to void to shut up the compiler. |
| 3076 | */ |
| 3077 | static void |
| 3078 | write_pipe_chunks(char *data, int len, int dest) |
| 3079 | { |
| 3080 | PipeProtoChunk p; |
| 3081 | int fd = fileno(stderr); |
| 3082 | int rc; |
| 3083 | |
| 3084 | Assert(len > 0); |
| 3085 | |
| 3086 | p.proto.nuls[0] = p.proto.nuls[1] = '\0'; |
| 3087 | p.proto.pid = MyProcPid; |
| 3088 | |
| 3089 | /* write all but the last chunk */ |
| 3090 | while (len > PIPE_MAX_PAYLOAD) |
| 3091 | { |
| 3092 | p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'F' : 'f'); |
| 3093 | p.proto.len = PIPE_MAX_PAYLOAD; |
| 3094 | memcpy(p.proto.data, data, PIPE_MAX_PAYLOAD); |
| 3095 | rc = write(fd, &p, PIPE_HEADER_SIZE + PIPE_MAX_PAYLOAD); |
| 3096 | (void) rc; |
| 3097 | data += PIPE_MAX_PAYLOAD; |
| 3098 | len -= PIPE_MAX_PAYLOAD; |
| 3099 | } |
| 3100 | |
| 3101 | /* write the last chunk */ |
| 3102 | p.proto.is_last = (dest == LOG_DESTINATION_CSVLOG ? 'T' : 't'); |
| 3103 | p.proto.len = len; |
| 3104 | memcpy(p.proto.data, data, len); |
| 3105 | rc = write(fd, &p, PIPE_HEADER_SIZE + len); |
| 3106 | (void) rc; |
| 3107 | } |
| 3108 | |
| 3109 | |
| 3110 | /* |
| 3111 | * Append a text string to the error report being built for the client. |
| 3112 | * |
| 3113 | * This is ordinarily identical to pq_sendstring(), but if we are in |
| 3114 | * error recursion trouble we skip encoding conversion, because of the |
| 3115 | * possibility that the problem is a failure in the encoding conversion |
| 3116 | * subsystem itself. Code elsewhere should ensure that the passed-in |
| 3117 | * strings will be plain 7-bit ASCII, and thus not in need of conversion, |
| 3118 | * in such cases. (In particular, we disable localization of error messages |
| 3119 | * to help ensure that's true.) |
| 3120 | */ |
| 3121 | static void |
| 3122 | err_sendstring(StringInfo buf, const char *str) |
| 3123 | { |
| 3124 | if (in_error_recursion_trouble()) |
| 3125 | pq_send_ascii_string(buf, str); |
| 3126 | else |
| 3127 | pq_sendstring(buf, str); |
| 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Write error report to client |
| 3132 | */ |
| 3133 | static void |
| 3134 | send_message_to_frontend(ErrorData *edata) |
| 3135 | { |
| 3136 | StringInfoData msgbuf; |
| 3137 | |
| 3138 | /* 'N' (Notice) is for nonfatal conditions, 'E' is for errors */ |
| 3139 | pq_beginmessage(&msgbuf, (edata->elevel < ERROR) ? 'N' : 'E'); |
| 3140 | |
| 3141 | if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 3) |
| 3142 | { |
| 3143 | /* New style with separate fields */ |
| 3144 | const char *sev; |
| 3145 | char tbuf[12]; |
| 3146 | int ssval; |
| 3147 | int i; |
| 3148 | |
| 3149 | sev = error_severity(edata->elevel); |
| 3150 | pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY); |
| 3151 | err_sendstring(&msgbuf, _(sev)); |
| 3152 | pq_sendbyte(&msgbuf, PG_DIAG_SEVERITY_NONLOCALIZED); |
| 3153 | err_sendstring(&msgbuf, sev); |
| 3154 | |
| 3155 | /* unpack MAKE_SQLSTATE code */ |
| 3156 | ssval = edata->sqlerrcode; |
| 3157 | for (i = 0; i < 5; i++) |
| 3158 | { |
| 3159 | tbuf[i] = PGUNSIXBIT(ssval); |
| 3160 | ssval >>= 6; |
| 3161 | } |
| 3162 | tbuf[i] = '\0'; |
| 3163 | |
| 3164 | pq_sendbyte(&msgbuf, PG_DIAG_SQLSTATE); |
| 3165 | err_sendstring(&msgbuf, tbuf); |
| 3166 | |
| 3167 | /* M field is required per protocol, so always send something */ |
| 3168 | pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_PRIMARY); |
| 3169 | if (edata->message) |
| 3170 | err_sendstring(&msgbuf, edata->message); |
| 3171 | else |
| 3172 | err_sendstring(&msgbuf, _("missing error text" )); |
| 3173 | |
| 3174 | if (edata->detail) |
| 3175 | { |
| 3176 | pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_DETAIL); |
| 3177 | err_sendstring(&msgbuf, edata->detail); |
| 3178 | } |
| 3179 | |
| 3180 | /* detail_log is intentionally not used here */ |
| 3181 | |
| 3182 | if (edata->hint) |
| 3183 | { |
| 3184 | pq_sendbyte(&msgbuf, PG_DIAG_MESSAGE_HINT); |
| 3185 | err_sendstring(&msgbuf, edata->hint); |
| 3186 | } |
| 3187 | |
| 3188 | if (edata->context) |
| 3189 | { |
| 3190 | pq_sendbyte(&msgbuf, PG_DIAG_CONTEXT); |
| 3191 | err_sendstring(&msgbuf, edata->context); |
| 3192 | } |
| 3193 | |
| 3194 | if (edata->schema_name) |
| 3195 | { |
| 3196 | pq_sendbyte(&msgbuf, PG_DIAG_SCHEMA_NAME); |
| 3197 | err_sendstring(&msgbuf, edata->schema_name); |
| 3198 | } |
| 3199 | |
| 3200 | if (edata->table_name) |
| 3201 | { |
| 3202 | pq_sendbyte(&msgbuf, PG_DIAG_TABLE_NAME); |
| 3203 | err_sendstring(&msgbuf, edata->table_name); |
| 3204 | } |
| 3205 | |
| 3206 | if (edata->column_name) |
| 3207 | { |
| 3208 | pq_sendbyte(&msgbuf, PG_DIAG_COLUMN_NAME); |
| 3209 | err_sendstring(&msgbuf, edata->column_name); |
| 3210 | } |
| 3211 | |
| 3212 | if (edata->datatype_name) |
| 3213 | { |
| 3214 | pq_sendbyte(&msgbuf, PG_DIAG_DATATYPE_NAME); |
| 3215 | err_sendstring(&msgbuf, edata->datatype_name); |
| 3216 | } |
| 3217 | |
| 3218 | if (edata->constraint_name) |
| 3219 | { |
| 3220 | pq_sendbyte(&msgbuf, PG_DIAG_CONSTRAINT_NAME); |
| 3221 | err_sendstring(&msgbuf, edata->constraint_name); |
| 3222 | } |
| 3223 | |
| 3224 | if (edata->cursorpos > 0) |
| 3225 | { |
| 3226 | snprintf(tbuf, sizeof(tbuf), "%d" , edata->cursorpos); |
| 3227 | pq_sendbyte(&msgbuf, PG_DIAG_STATEMENT_POSITION); |
| 3228 | err_sendstring(&msgbuf, tbuf); |
| 3229 | } |
| 3230 | |
| 3231 | if (edata->internalpos > 0) |
| 3232 | { |
| 3233 | snprintf(tbuf, sizeof(tbuf), "%d" , edata->internalpos); |
| 3234 | pq_sendbyte(&msgbuf, PG_DIAG_INTERNAL_POSITION); |
| 3235 | err_sendstring(&msgbuf, tbuf); |
| 3236 | } |
| 3237 | |
| 3238 | if (edata->internalquery) |
| 3239 | { |
| 3240 | pq_sendbyte(&msgbuf, PG_DIAG_INTERNAL_QUERY); |
| 3241 | err_sendstring(&msgbuf, edata->internalquery); |
| 3242 | } |
| 3243 | |
| 3244 | if (edata->filename) |
| 3245 | { |
| 3246 | pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_FILE); |
| 3247 | err_sendstring(&msgbuf, edata->filename); |
| 3248 | } |
| 3249 | |
| 3250 | if (edata->lineno > 0) |
| 3251 | { |
| 3252 | snprintf(tbuf, sizeof(tbuf), "%d" , edata->lineno); |
| 3253 | pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_LINE); |
| 3254 | err_sendstring(&msgbuf, tbuf); |
| 3255 | } |
| 3256 | |
| 3257 | if (edata->funcname) |
| 3258 | { |
| 3259 | pq_sendbyte(&msgbuf, PG_DIAG_SOURCE_FUNCTION); |
| 3260 | err_sendstring(&msgbuf, edata->funcname); |
| 3261 | } |
| 3262 | |
| 3263 | pq_sendbyte(&msgbuf, '\0'); /* terminator */ |
| 3264 | } |
| 3265 | else |
| 3266 | { |
| 3267 | /* Old style --- gin up a backwards-compatible message */ |
| 3268 | StringInfoData buf; |
| 3269 | |
| 3270 | initStringInfo(&buf); |
| 3271 | |
| 3272 | appendStringInfo(&buf, "%s: " , _(error_severity(edata->elevel))); |
| 3273 | |
| 3274 | if (edata->show_funcname && edata->funcname) |
| 3275 | appendStringInfo(&buf, "%s: " , edata->funcname); |
| 3276 | |
| 3277 | if (edata->message) |
| 3278 | appendStringInfoString(&buf, edata->message); |
| 3279 | else |
| 3280 | appendStringInfoString(&buf, _("missing error text" )); |
| 3281 | |
| 3282 | if (edata->cursorpos > 0) |
| 3283 | appendStringInfo(&buf, _(" at character %d" ), |
| 3284 | edata->cursorpos); |
| 3285 | else if (edata->internalpos > 0) |
| 3286 | appendStringInfo(&buf, _(" at character %d" ), |
| 3287 | edata->internalpos); |
| 3288 | |
| 3289 | appendStringInfoChar(&buf, '\n'); |
| 3290 | |
| 3291 | err_sendstring(&msgbuf, buf.data); |
| 3292 | |
| 3293 | pfree(buf.data); |
| 3294 | } |
| 3295 | |
| 3296 | pq_endmessage(&msgbuf); |
| 3297 | |
| 3298 | /* |
| 3299 | * This flush is normally not necessary, since postgres.c will flush out |
| 3300 | * waiting data when control returns to the main loop. But it seems best |
| 3301 | * to leave it here, so that the client has some clue what happened if the |
| 3302 | * backend dies before getting back to the main loop ... error/notice |
| 3303 | * messages should not be a performance-critical path anyway, so an extra |
| 3304 | * flush won't hurt much ... |
| 3305 | */ |
| 3306 | pq_flush(); |
| 3307 | } |
| 3308 | |
| 3309 | |
| 3310 | /* |
| 3311 | * Support routines for formatting error messages. |
| 3312 | */ |
| 3313 | |
| 3314 | |
| 3315 | /* |
| 3316 | * error_severity --- get string representing elevel |
| 3317 | * |
| 3318 | * The string is not localized here, but we mark the strings for translation |
| 3319 | * so that callers can invoke _() on the result. |
| 3320 | */ |
| 3321 | static const char * |
| 3322 | error_severity(int elevel) |
| 3323 | { |
| 3324 | const char *prefix; |
| 3325 | |
| 3326 | switch (elevel) |
| 3327 | { |
| 3328 | case DEBUG1: |
| 3329 | case DEBUG2: |
| 3330 | case DEBUG3: |
| 3331 | case DEBUG4: |
| 3332 | case DEBUG5: |
| 3333 | prefix = gettext_noop("DEBUG" ); |
| 3334 | break; |
| 3335 | case LOG: |
| 3336 | case LOG_SERVER_ONLY: |
| 3337 | prefix = gettext_noop("LOG" ); |
| 3338 | break; |
| 3339 | case INFO: |
| 3340 | prefix = gettext_noop("INFO" ); |
| 3341 | break; |
| 3342 | case NOTICE: |
| 3343 | prefix = gettext_noop("NOTICE" ); |
| 3344 | break; |
| 3345 | case WARNING: |
| 3346 | prefix = gettext_noop("WARNING" ); |
| 3347 | break; |
| 3348 | case ERROR: |
| 3349 | prefix = gettext_noop("ERROR" ); |
| 3350 | break; |
| 3351 | case FATAL: |
| 3352 | prefix = gettext_noop("FATAL" ); |
| 3353 | break; |
| 3354 | case PANIC: |
| 3355 | prefix = gettext_noop("PANIC" ); |
| 3356 | break; |
| 3357 | default: |
| 3358 | prefix = "???" ; |
| 3359 | break; |
| 3360 | } |
| 3361 | |
| 3362 | return prefix; |
| 3363 | } |
| 3364 | |
| 3365 | |
| 3366 | /* |
| 3367 | * append_with_tabs |
| 3368 | * |
| 3369 | * Append the string to the StringInfo buffer, inserting a tab after any |
| 3370 | * newline. |
| 3371 | */ |
| 3372 | static void |
| 3373 | append_with_tabs(StringInfo buf, const char *str) |
| 3374 | { |
| 3375 | char ch; |
| 3376 | |
| 3377 | while ((ch = *str++) != '\0') |
| 3378 | { |
| 3379 | appendStringInfoCharMacro(buf, ch); |
| 3380 | if (ch == '\n') |
| 3381 | appendStringInfoCharMacro(buf, '\t'); |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | |
| 3386 | /* |
| 3387 | * Write errors to stderr (or by equal means when stderr is |
| 3388 | * not available). Used before ereport/elog can be used |
| 3389 | * safely (memory context, GUC load etc) |
| 3390 | */ |
| 3391 | void |
| 3392 | write_stderr(const char *fmt,...) |
| 3393 | { |
| 3394 | va_list ap; |
| 3395 | |
| 3396 | #ifdef WIN32 |
| 3397 | char errbuf[2048]; /* Arbitrary size? */ |
| 3398 | #endif |
| 3399 | |
| 3400 | fmt = _(fmt); |
| 3401 | |
| 3402 | va_start(ap, fmt); |
| 3403 | #ifndef WIN32 |
| 3404 | /* On Unix, we just fprintf to stderr */ |
| 3405 | vfprintf(stderr, fmt, ap); |
| 3406 | fflush(stderr); |
| 3407 | #else |
| 3408 | vsnprintf(errbuf, sizeof(errbuf), fmt, ap); |
| 3409 | |
| 3410 | /* |
| 3411 | * On Win32, we print to stderr if running on a console, or write to |
| 3412 | * eventlog if running as a service |
| 3413 | */ |
| 3414 | if (pgwin32_is_service()) /* Running as a service */ |
| 3415 | { |
| 3416 | write_eventlog(ERROR, errbuf, strlen(errbuf)); |
| 3417 | } |
| 3418 | else |
| 3419 | { |
| 3420 | /* Not running as service, write to stderr */ |
| 3421 | write_console(errbuf, strlen(errbuf)); |
| 3422 | fflush(stderr); |
| 3423 | } |
| 3424 | #endif |
| 3425 | va_end(ap); |
| 3426 | } |
| 3427 | |
| 3428 | |
| 3429 | /* |
| 3430 | * is_log_level_output -- is elevel logically >= log_min_level? |
| 3431 | * |
| 3432 | * We use this for tests that should consider LOG to sort out-of-order, |
| 3433 | * between ERROR and FATAL. Generally this is the right thing for testing |
| 3434 | * whether a message should go to the postmaster log, whereas a simple >= |
| 3435 | * test is correct for testing whether the message should go to the client. |
| 3436 | */ |
| 3437 | static bool |
| 3438 | is_log_level_output(int elevel, int log_min_level) |
| 3439 | { |
| 3440 | if (elevel == LOG || elevel == LOG_SERVER_ONLY) |
| 3441 | { |
| 3442 | if (log_min_level == LOG || log_min_level <= ERROR) |
| 3443 | return true; |
| 3444 | } |
| 3445 | else if (log_min_level == LOG) |
| 3446 | { |
| 3447 | /* elevel != LOG */ |
| 3448 | if (elevel >= FATAL) |
| 3449 | return true; |
| 3450 | } |
| 3451 | /* Neither is LOG */ |
| 3452 | else if (elevel >= log_min_level) |
| 3453 | return true; |
| 3454 | |
| 3455 | return false; |
| 3456 | } |
| 3457 | |
| 3458 | /* |
| 3459 | * Adjust the level of a recovery-related message per trace_recovery_messages. |
| 3460 | * |
| 3461 | * The argument is the default log level of the message, eg, DEBUG2. (This |
| 3462 | * should only be applied to DEBUGn log messages, otherwise it's a no-op.) |
| 3463 | * If the level is >= trace_recovery_messages, we return LOG, causing the |
| 3464 | * message to be logged unconditionally (for most settings of |
| 3465 | * log_min_messages). Otherwise, we return the argument unchanged. |
| 3466 | * The message will then be shown based on the setting of log_min_messages. |
| 3467 | * |
| 3468 | * Intention is to keep this for at least the whole of the 9.0 production |
| 3469 | * release, so we can more easily diagnose production problems in the field. |
| 3470 | * It should go away eventually, though, because it's an ugly and |
| 3471 | * hard-to-explain kluge. |
| 3472 | */ |
| 3473 | int |
| 3474 | trace_recovery(int trace_level) |
| 3475 | { |
| 3476 | if (trace_level < LOG && |
| 3477 | trace_level >= trace_recovery_messages) |
| 3478 | return LOG; |
| 3479 | |
| 3480 | return trace_level; |
| 3481 | } |
| 3482 | |