Skip to content

Commit 0432ce9

Browse files
committed
Fix elog logic so that error messages displayed during initdb (or
other standalone-backend situations) are not duplicated. Remove some dead code, add some comments, too.
1 parent fc6603f commit 0432ce9

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

src/backend/utils/error/elog.c

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.68 2000/11/25 04:38:00 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.69 2000/11/25 19:09:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -77,7 +77,6 @@ static const char * print_timestamp(void);
7777
static const char * print_pid(void);
7878

7979
static int Debugfile = -1;
80-
static int Err_file = -1;
8180
static int ElogDebugIndentLevel = 0;
8281

8382
/*--------------------
@@ -329,6 +328,7 @@ elog(int lev, const char *fmt, ...)
329328
*/
330329

331330
#ifdef ENABLE_SYSLOG
331+
/* Write to syslog, if enabled */
332332
if (Use_syslog >= 1)
333333
{
334334
int syslog_level;
@@ -364,31 +364,11 @@ elog(int lev, const char *fmt, ...)
364364

365365
len = strlen(msg_buf);
366366

367+
/* Write to debug file, if open and enabled */
368+
/* NOTE: debug file is typically pointed at stderr */
367369
if (Debugfile >= 0 && Use_syslog <= 1)
368370
write(Debugfile, msg_buf, len);
369371

370-
/*
371-
* If there's an error log file other than our channel to the
372-
* front-end program, write to it first. This is important because
373-
* there's a bug in the socket code on ultrix. If the front end has
374-
* gone away (so the channel to it has been closed at the other end),
375-
* then writing here can cause this backend to exit without warning
376-
* that is, write() does an exit(). In this case, our only hope of
377-
* finding out what's going on is if Err_file was set to some disk
378-
* log. This is a major pain. (It's probably also long-dead code...
379-
* does anyone still use ultrix?)
380-
*/
381-
if (lev > DEBUG && Err_file >= 0 &&
382-
Debugfile != Err_file && Use_syslog <= 1)
383-
{
384-
if (write(Err_file, msg_buf, len) < 0)
385-
{
386-
write(open("/dev/console", O_WRONLY, 0666), msg_buf, len);
387-
lev = REALLYFATAL;
388-
}
389-
fsync(Err_file);
390-
}
391-
392372
#ifndef PG_STANDALONE
393373

394374
if (lev > DEBUG && whereToSendOutput == Remote)
@@ -429,9 +409,11 @@ elog(int lev, const char *fmt, ...)
429409

430410
/*
431411
* We are running as an interactive backend, so just send the
432-
* message to stderr.
412+
* message to stderr. But don't send a duplicate if Debugfile
413+
* write, above, already sent to stderr.
433414
*/
434-
fputs(msg_buf, stderr);
415+
if (Debugfile != fileno(stderr))
416+
fputs(msg_buf, stderr);
435417
}
436418

437419
#endif /* !PG_STANDALONE */
@@ -511,33 +493,40 @@ DebugFileOpen(void)
511493
int fd,
512494
istty;
513495

514-
Err_file = Debugfile = -1;
496+
Debugfile = -1;
515497
ElogDebugIndentLevel = 0;
516498

517499
if (OutputFileName[0])
518500
{
501+
/*
502+
* A debug-output file name was given.
503+
*
504+
* Make sure we can write the file, and find out if it's a tty.
505+
*/
519506
if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY,
520507
0666)) < 0)
521508
elog(FATAL, "DebugFileOpen: open of %s: %m",
522509
OutputFileName);
523510
istty = isatty(fd);
524511
close(fd);
525512

513+
/*
514+
* Redirect our stderr to the debug output file.
515+
*/
516+
if (!freopen(OutputFileName, "a", stderr))
517+
elog(FATAL, "DebugFileOpen: %s reopen as stderr: %m",
518+
OutputFileName);
519+
Debugfile = fileno(stderr);
526520
/*
527521
* If the file is a tty and we're running under the postmaster,
528522
* try to send stdout there as well (if it isn't a tty then stderr
529523
* will block out stdout, so we may as well let stdout go wherever
530524
* it was going before).
531525
*/
532-
if (istty &&
533-
IsUnderPostmaster &&
534-
!freopen(OutputFileName, "a", stdout))
535-
elog(FATAL, "DebugFileOpen: %s reopen as stdout: %m",
536-
OutputFileName);
537-
if (!freopen(OutputFileName, "a", stderr))
538-
elog(FATAL, "DebugFileOpen: %s reopen as stderr: %m",
539-
OutputFileName);
540-
Err_file = Debugfile = fileno(stderr);
526+
if (istty && IsUnderPostmaster)
527+
if (!freopen(OutputFileName, "a", stdout))
528+
elog(FATAL, "DebugFileOpen: %s reopen as stdout: %m",
529+
OutputFileName);
541530
return Debugfile;
542531
}
543532

@@ -555,7 +544,7 @@ DebugFileOpen(void)
555544
if (fd < 0)
556545
elog(FATAL, "DebugFileOpen: could not open debugging file");
557546

558-
Err_file = Debugfile = fd;
547+
Debugfile = fd;
559548
return Debugfile;
560549
}
561550

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy