elog.c 5.8 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * elog.c--
4
 *	  error logger
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/utils/error/elog.c,v 1.29 1998/06/15 19:29:41 momjian Exp $
11 12 13 14 15 16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#ifndef O_RDONLY
#include <sys/file.h>
20
#endif							/* O_RDONLY */
21 22 23
#include <sys/types.h>
#include <stdarg.h>
#include <errno.h>
24 25
#include <unistd.h>
#include <signal.h>
26 27 28 29 30 31

#include "postgres.h"
#include "miscadmin.h"
#include "libpq/libpq.h"
#include "storage/proc.h"

32 33 34
static int	Debugfile = -1;
static int	Err_file = -1;
static int	ElogDebugIndentLevel = 0;
35

36
extern char OutputFileName[];
37 38 39

/*
 * elog --
40
 *		Old error logging function.
41 42
 */
void
43
elog(int lev, const char *fmt,...)
44
{
45 46 47
	va_list		ap;
	char		buf[ELOG_MAXLEN],
				line[ELOG_MAXLEN];
48
	char	   *bp;
49
	const char *cp;
50 51
	extern int	errno,
				sys_nerr;
52

53
#ifndef PG_STANDALONE
54
	extern FILE *Pfout;
55 56

#endif							/* !PG_STANDALONE */
B
Bruce Momjian 已提交
57
#ifdef ELOG_TIMESTAMPS
58
	time_t		tim;
59

B
Bruce Momjian 已提交
60
#endif
61 62
	int			len;
	int			i = 0;
63 64 65 66 67 68

	va_start(ap, fmt);
	if (lev == DEBUG && Debugfile < 0)
		return;
	switch (lev)
	{
69 70 71 72 73 74
		case NOIND:
			i = ElogDebugIndentLevel - 1;
			if (i < 0)
				i = 0;
			if (i > 30)
				i = i % 30;
75
			cp = "DEBUG:  ";
76 77 78 79 80 81 82
			break;
		case DEBUG:
			i = ElogDebugIndentLevel;
			if (i < 0)
				i = 0;
			if (i > 30)
				i = i % 30;
83
			cp = "DEBUG:  ";
84 85
			break;
		case NOTICE:
86
			cp = "NOTICE:  ";
87
			break;
88 89 90
		case ERROR:
			cp = "ERROR:  ";
			break;
91
		default:
92
			sprintf(line, "FATAL %d:  ", lev);
93
			cp = line;
94
	}
95
#ifdef ELOG_TIMESTAMPS
96 97 98 99
	time(&tim);
	strcat(strcpy(buf, cp), ctime(&tim) + 4);
	bp = buf + strlen(buf) - 6;
	*bp++ = ':';
100
#else
101 102
	strcpy(buf, cp);
	bp = buf + strlen(buf);
103
#endif
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	while (i-- > 0)
		*bp++ = ' ';
	for (cp = fmt; *cp; cp++)
		if (*cp == '%' && *(cp + 1) == 'm')
		{
			if (errno < sys_nerr && errno >= 0)
				strcpy(bp, strerror(errno));
			else
				sprintf(bp, "error %d", errno);
			bp += strlen(bp);
			cp++;
		}
		else
			*bp++ = *cp;
	*bp = '\0';
	vsprintf(line, buf, ap);
	va_end(ap);
	len = strlen(strcat(line, "\n"));
	if (Debugfile > -1)
		write(Debugfile, line, len);
	if (lev == DEBUG || lev == NOIND)
		return;

127
	/*
128 129 130 131 132 133 134 135
	 * If there's an error log file other than our channel to the
	 * front-end program, write to it first.  This is important because
	 * there's a bug in the socket code on ultrix.  If the front end has
	 * gone away (so the channel to it has been closed at the other end),
	 * then writing here can cause this backend to exit without warning --
	 * that is, write() does an exit(). In this case, our only hope of
	 * finding out what's going on is if Err_file was set to some disk
	 * log.  This is a major pain.
136
	 */
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	if (Err_file > -1 && Debugfile != Err_file)
	{
		if (write(Err_file, line, len) < 0)
		{
			write(open("/dev/console", O_WRONLY, 0666), line, len);
			fflush(stdout);
			fflush(stderr);
			exitpg(lev);
		}
		fsync(Err_file);
	}

#ifndef PG_STANDALONE
	/* Send IPC message to the front-end program */
	if (Pfout != NULL && lev > DEBUG)
	{
		/* notices are not exactly errors, handle it differently */
		if (lev == NOTICE)
			pq_putnchar("N", 1);
		else
			pq_putnchar("E", 1);
		/* pq_putint(-101, 4); *//* should be query id */
		pq_putstr(line);
		pq_flush();
	}
163 164 165 166 167 168 169
	if (Pfout == NULL)
	{

		/*
		 * There is no socket.	One explanation for this is we are running
		 * as the Postmaster.  So we'll write the message to stderr.
		 */
170 171
		fputs(line, stderr);
	}
172 173
#endif							/* !PG_STANDALONE */

174
	if (lev == ERROR)
175
	{
176
		extern bool	InError;
177 178

		ProcReleaseSpins(NULL); /* get rid of spinlocks we hold */
179
		if (!InError)
180
		{
181
			kill(MyProcPid, 1); /* abort to traffic cop */
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			pause();
		}

		/*
		 * The pause(3) is just to avoid race conditions where the thread
		 * of control on an MP system gets past here (i.e., the signal is
		 * not received instantaneously).
		 */
	}

	if (lev == FATAL)
	{

		/*
		 * Assume that if we have detected the failure we can exit with a
		 * normal exit status.	This will prevent the postmaster from
		 * cleaning up when it's not needed.
		 */
		fflush(stdout);
		fflush(stderr);
		ProcReleaseSpins(NULL); /* get rid of spinlocks we hold */
		ProcReleaseLocks();		/* get rid of real locks we hold */
		exitpg(0);
	}

	if (lev > FATAL)
	{
		fflush(stdout);
		fflush(stderr);
		exitpg(lev);
	}
213 214 215 216
}

#ifndef PG_STANDALONE
int
217
DebugFileOpen(void)
218
{
219 220
	int			fd,
				istty;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

	Err_file = Debugfile = -1;
	ElogDebugIndentLevel = 0;

	if (OutputFileName[0])
	{
		OutputFileName[MAXPGPATH - 1] = '\0';
		if ((fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY,
					   0666)) < 0)
			elog(FATAL, "DebugFileOpen: open of %s: %m",
				 OutputFileName);
		istty = isatty(fd);
		close(fd);

		/*
		 * If the file is a tty and we're running under the postmaster,
		 * try to send stdout there as well (if it isn't a tty then stderr
		 * will block out stdout, so we may as well let stdout go wherever
		 * it was going before).
		 */
		if (istty &&
			IsUnderPostmaster &&
			!freopen(OutputFileName, "a", stdout))
			elog(FATAL, "DebugFileOpen: %s reopen as stdout: %m",
				 OutputFileName);
		if (!freopen(OutputFileName, "a", stderr))
			elog(FATAL, "DebugFileOpen: %s reopen as stderr: %m",
				 OutputFileName);
		Err_file = Debugfile = fileno(stderr);
		return (Debugfile);
	}

	/*
	 * If no filename was specified, send debugging output to stderr. If
	 * stderr has been hosed, try to open a file.
256
	 */
257 258 259 260
	fd = fileno(stderr);
	if (fcntl(fd, F_GETFD, 0) < 0)
	{
		sprintf(OutputFileName, "%s/pg.errors.%d",
B
Bruce Momjian 已提交
261
				DataDir, (int) MyProcPid);
262 263 264 265 266 267 268
		fd = open(OutputFileName, O_CREAT | O_APPEND | O_WRONLY, 0666);
	}
	if (fd < 0)
		elog(FATAL, "DebugFileOpen: could not open debugging file");

	Err_file = Debugfile = fd;
	return (Debugfile);
269
}
270

271
#endif