1 /*-------------------------------------------------------------------------
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.24 1998/01/07 21:06:23 momjian Exp $
12 *-------------------------------------------------------------------------
21 #include <sys/types.h>
28 #include "miscadmin.h"
29 #include "libpq/libpq.h"
30 #include "storage/proc.h"
32 static int Debugfile = -1;
33 static int Err_file = -1;
34 static int ElogDebugIndentLevel = 0;
36 extern char OutputFileName[];
40 * Old error logging function.
43 elog(int lev, const char *fmt,...)
46 char buf[ELOG_MAXLEN],
49 register const char *cp;
56 #endif /* !PG_STANDALONE */
57 #ifdef ELOG_TIMESTAMPS
65 if (lev == DEBUG && Debugfile < 0)
72 i = ElogDebugIndentLevel - 1;
80 i = ElogDebugIndentLevel;
94 sprintf(line, "FATAL %d: ", lev);
97 #ifdef ELOG_TIMESTAMPS
99 strcat(strcpy(buf, cp), ctime(&tim) + 4);
100 bp = buf + strlen(buf) - 6;
104 bp = buf + strlen(buf);
108 for (cp = fmt; *cp; cp++)
109 if (*cp == '%' && *(cp + 1) == 'm')
111 if (errno < sys_nerr && errno >= 0)
112 strcpy(bp, strerror(errno));
114 sprintf(bp, "error %d", errno);
121 vsprintf(line, buf, ap);
123 len = strlen(strcat(line, "\n"));
125 write(Debugfile, line, len);
126 if (lev == DEBUG || lev == NOIND)
130 * If there's an error log file other than our channel to the
131 * front-end program, write to it first. This is important because
132 * there's a bug in the socket code on ultrix. If the front end has
133 * gone away (so the channel to it has been closed at the other end),
134 * then writing here can cause this backend to exit without warning --
135 * that is, write() does an exit(). In this case, our only hope of
136 * finding out what's going on is if Err_file was set to some disk
137 * log. This is a major pain.
140 if (Err_file > -1 && Debugfile != Err_file)
142 if (write(Err_file, line, len) < 0)
144 write(open("/dev/console", O_WRONLY, 0666), line, len);
152 #ifndef PG_STANDALONE
153 /* Send IPC message to the front-end program */
154 if (Pfout != NULL && lev > DEBUG)
156 /* notices are not exactly errors, handle it differently */
161 /* pq_putint(-101, 4); *//* should be query id */
166 /* There is no socket. One explanation for this is we are running
167 as the Postmaster. So we'll write the message to stderr.
171 #endif /* !PG_STANDALONE */
177 ProcReleaseSpins(NULL); /* get rid of spinlocks we hold */
180 kill(getpid(), 1); /* abort to traffic cop */
185 * The pause(3) is just to avoid race conditions where the thread
186 * of control on an MP system gets past here (i.e., the signal is
187 * not received instantaneously).
195 * Assume that if we have detected the failure we can exit with a
196 * normal exit status. This will prevent the postmaster from
197 * cleaning up when it's not needed.
201 ProcReleaseSpins(NULL); /* get rid of spinlocks we hold */
202 ProcReleaseLocks(); /* get rid of real locks we hold */
214 #ifndef PG_STANDALONE
221 Err_file = Debugfile = -1;
222 ElogDebugIndentLevel = 0;
224 if (OutputFileName[0])
226 OutputFileName[MAXPGPATH - 1] = '\0';
227 if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY,
229 elog(FATAL, "DebugFileOpen: open of %s: %m",
235 * If the file is a tty and we're running under the postmaster,
236 * try to send stdout there as well (if it isn't a tty then stderr
237 * will block out stdout, so we may as well let stdout go wherever
238 * it was going before).
242 !freopen(OutputFileName, "a", stdout))
243 elog(FATAL, "DebugFileOpen: %s reopen as stdout: %m",
245 if (!freopen(OutputFileName, "a", stderr))
246 elog(FATAL, "DebugFileOpen: %s reopen as stderr: %m",
248 Err_file = Debugfile = fileno(stderr);
253 * If no filename was specified, send debugging output to stderr. If
254 * stderr has been hosed, try to open a file.
257 if (fcntl(fd, F_GETFD, 0) < 0)
259 sprintf(OutputFileName, "%s/pg.errors.%d",
260 DataDir, (int) getpid());
261 fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY, 0666);
264 elog(FATAL, "DebugFileOpen: could not open debugging file");
266 Err_file = Debugfile = fd;