elog.h 9.25 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * elog.h
4
 *	  POSTGRES error reporting/logging definitions.
5 6
 *
 *
Bruce Momjian's avatar
Bruce Momjian committed
7
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/include/utils/elog.h,v 1.77 2004/09/16 21:59:17 tgl Exp $
11 12 13
 *
 *-------------------------------------------------------------------------
 */
14
#ifndef ELOG_H
15 16
#define ELOG_H

17 18
#include <setjmp.h>

19
/* Error level codes */
Bruce Momjian's avatar
Bruce Momjian committed
20 21
#define DEBUG5		10			/* Debugging messages, in categories of
								 * decreasing detail. */
22
#define DEBUG4		11
Bruce Momjian's avatar
Bruce Momjian committed
23 24
#define DEBUG3		12
#define DEBUG2		13
25
#define DEBUG1		14			/* used by GUC debug_* variables */
Bruce Momjian's avatar
Bruce Momjian committed
26 27 28 29 30 31 32 33 34 35 36
#define LOG			15			/* Server operational messages; sent only
								 * to server log by default. */
#define COMMERROR	16			/* Client communication problems; same as
								 * LOG for server reporting, but never
								 * sent to client. */
#define INFO		17			/* Informative messages that are always
								 * sent to client;	is not affected by
								 * client_min_messages */
#define NOTICE		18			/* Helpful messages to users about query
								 * operation;  sent to client and server
								 * log by default. */
Bruce Momjian's avatar
Bruce Momjian committed
37 38 39 40
#define WARNING		19			/* Warnings.  NOTICE is for expected
								 * messages like implicit sequence
								 * creation by SERIAL. WARNING is for
								 * unexpected messages. */
Bruce Momjian's avatar
Bruce Momjian committed
41 42
#define ERROR		20			/* user error - abort transaction; return
								 * to known state */
43
/* Save ERROR value in PGERROR so it can be restored when Win32 includes
44 45 46 47 48 49
 * modify it.  We have to use a constant rather than ERROR because macros
 * are expanded only when referenced outside macros.
 */
#ifdef WIN32
#define PGERROR		20
#endif
Bruce Momjian's avatar
Bruce Momjian committed
50 51
#define FATAL		21			/* fatal error - abort process */
#define PANIC		22			/* take down the other backends with me */
52

Bruce Momjian's avatar
Bruce Momjian committed
53
 /* #define DEBUG DEBUG1 */	/* Backward compatibility with pre-7.3 */
54

55 56 57

/* macros for representing SQLSTATE strings compactly */
#define PGSIXBIT(ch)	(((ch) - '0') & 0x3F)
Bruce Momjian's avatar
Bruce Momjian committed
58
#define PGUNSIXBIT(val) (((val) & 0x3F) + '0')
59 60 61 62 63

#define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5)	\
	(PGSIXBIT(ch1) + (PGSIXBIT(ch2) << 6) + (PGSIXBIT(ch3) << 12) + \
	 (PGSIXBIT(ch4) << 18) + (PGSIXBIT(ch5) << 24))

64 65 66 67
/* These macros depend on the fact that '0' becomes a zero in SIXBIT */
#define ERRCODE_TO_CATEGORY(ec)  ((ec) & ((1 << 12) - 1))
#define ERRCODE_IS_CATEGORY(ec)  (((ec) & ~((1 << 12) - 1)) == 0)

68 69
/* SQLSTATE codes for errors are defined in a separate file */
#include "utils/errcodes.h"
70

71 72 73 74 75 76 77 78 79

/* Which __func__ symbol do we have, if any? */
#ifdef HAVE_FUNCNAME__FUNC
#define PG_FUNCNAME_MACRO	__func__
#else
#ifdef HAVE_FUNCNAME__FUNCTION
#define PG_FUNCNAME_MACRO	__FUNCTION__
#else
#define PG_FUNCNAME_MACRO	NULL
80
#endif
81 82 83 84 85 86
#endif


/*----------
 * New-style error reporting API: to be used in this way:
 *		ereport(ERROR,
87
 *				(errcode(ERRCODE_UNDEFINED_CURSOR),
88 89 90 91
 *				 errmsg("portal \"%s\" not found", stmt->portalname),
 *				 ... other errxxx() fields as needed ...));
 *
 * The error level is required, and so is a primary error message (errmsg
Bruce Momjian's avatar
Bruce Momjian committed
92
 * or errmsg_internal).  All else is optional.	errcode() defaults to
93 94 95
 * ERRCODE_INTERNAL_ERROR if elevel is ERROR or more, ERRCODE_WARNING
 * if elevel is WARNING, or ERRCODE_SUCCESSFUL_COMPLETION if elevel is
 * NOTICE or below.
96 97 98 99 100 101 102
 *----------
 */
#define ereport(elevel, rest)  \
	(errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO) ? \
	 (errfinish rest) : (void) 0)

extern bool errstart(int elevel, const char *filename, int lineno,
Bruce Momjian's avatar
Bruce Momjian committed
103 104
		 const char *funcname);
extern void errfinish(int dummy,...);
105

Bruce Momjian's avatar
Bruce Momjian committed
106
extern int	errcode(int sqlerrcode);
107

Bruce Momjian's avatar
Bruce Momjian committed
108 109
extern int	errcode_for_file_access(void);
extern int	errcode_for_socket_access(void);
110

Bruce Momjian's avatar
Bruce Momjian committed
111 112
extern int
errmsg(const char *fmt,...)
113 114 115 116
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

Bruce Momjian's avatar
Bruce Momjian committed
117 118
extern int
errmsg_internal(const char *fmt,...)
119 120 121 122
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

Bruce Momjian's avatar
Bruce Momjian committed
123 124
extern int
errdetail(const char *fmt,...)
125 126 127 128
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

Bruce Momjian's avatar
Bruce Momjian committed
129 130
extern int
errhint(const char *fmt,...)
131 132 133 134
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

Bruce Momjian's avatar
Bruce Momjian committed
135 136
extern int
errcontext(const char *fmt,...)
137 138 139 140
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

Bruce Momjian's avatar
Bruce Momjian committed
141 142
extern int	errfunction(const char *funcname);
extern int	errposition(int cursorpos);
143

144 145 146 147 148 149
extern int	internalerrposition(int cursorpos);
extern int	internalerrquery(const char *query);

extern int	geterrposition(void);
extern int	getinternalerrposition(void);

150 151 152 153 154 155

/*----------
 * Old-style error reporting API: to be used in this way:
 *		elog(ERROR, "portal \"%s\" not found", stmt->portalname);
 *----------
 */
156
#define elog	elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO), elog_finish
157

158
extern void elog_start(const char *filename, int lineno, const char *funcname);
159
extern void
Bruce Momjian's avatar
Bruce Momjian committed
160
elog_finish(int elevel, const char *fmt,...)
161 162
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
163
__attribute__((format(printf, 2, 3)));
164

165 166 167 168 169 170

/* Support for attaching context information to error reports */

typedef struct ErrorContextCallback
{
	struct ErrorContextCallback *previous;
Bruce Momjian's avatar
Bruce Momjian committed
171 172
	void		(*callback) (void *arg);
	void	   *arg;
173
} ErrorContextCallback;
174

175
extern DLLIMPORT ErrorContextCallback *error_context_stack;
176 177


178 179 180 181 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
/*----------
 * API for catching ereport(ERROR) exits.  Use these macros like so:
 *
 *		PG_TRY();
 *		{
 *			... code that might throw ereport(ERROR) ...
 *		}
 *		PG_CATCH();
 *		{
 *			... error recovery code ...
 *		}
 *		PG_END_TRY();
 *
 * (The braces are not actually necessary, but are recommended so that
 * pg_indent will indent the construct nicely.)  The error recovery code
 * can optionally do PG_RE_THROW() to propagate the same error outwards.
 *
 * Note: while the system will correctly propagate any new ereport(ERROR)
 * occurring in the recovery section, there is a small limit on the number
 * of levels this will work for.  It's best to keep the error recovery
 * section simple enough that it can't generate any new errors, at least
 * not before popping the error stack.
 *----------
 */
#define PG_TRY()  \
	do { \
		sigjmp_buf *save_exception_stack = PG_exception_stack; \
		ErrorContextCallback *save_context_stack = error_context_stack; \
		sigjmp_buf local_sigjmp_buf; \
207
		if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
208 209 210
		{ \
			PG_exception_stack = &local_sigjmp_buf

Bruce Momjian's avatar
Bruce Momjian committed
211
#define PG_CATCH()	\
212 213 214 215 216 217 218 219 220 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 256 257 258 259 260 261 262 263 264
		} \
		else \
		{ \
			PG_exception_stack = save_exception_stack; \
			error_context_stack = save_context_stack

#define PG_END_TRY()  \
		} \
		PG_exception_stack = save_exception_stack; \
		error_context_stack = save_context_stack; \
	} while (0)

#define PG_RE_THROW()  \
	siglongjmp(*PG_exception_stack, 1)

extern DLLIMPORT sigjmp_buf *PG_exception_stack;


/* Stuff that error handlers might want to use */

/*
 * ErrorData holds the data accumulated during any one ereport() cycle.
 * Any non-NULL pointers must point to palloc'd data.
 * (The const pointers are an exception; we assume they point at non-freeable
 * constant strings.)
 */
typedef struct ErrorData
{
	int			elevel;			/* error level */
	bool		output_to_server;		/* will report to server log? */
	bool		output_to_client;		/* will report to client? */
	bool		show_funcname;	/* true to force funcname inclusion */
	const char *filename;		/* __FILE__ of ereport() call */
	int			lineno;			/* __LINE__ of ereport() call */
	const char *funcname;		/* __func__ of ereport() call */
	int			sqlerrcode;		/* encoded ERRSTATE */
	char	   *message;		/* primary error message */
	char	   *detail;			/* detail error message */
	char	   *hint;			/* hint message */
	char	   *context;		/* context message */
	int			cursorpos;		/* cursor index into query string */
	int			internalpos;	/* cursor index into internalquery */
	char	   *internalquery;	/* text of internally-generated query */
	int			saved_errno;	/* errno at entry */
} ErrorData;

extern void EmitErrorReport(void);
extern ErrorData *CopyErrorData(void);
extern void FreeErrorData(ErrorData *edata);
extern void FlushErrorState(void);
extern void ReThrowError(ErrorData *edata);


265
/* GUC-configurable parameters */
266 267 268 269 270 271

typedef enum
{
	PGERROR_TERSE,				/* single-line error messages */
	PGERROR_DEFAULT,			/* recommended style */
	PGERROR_VERBOSE				/* all the facts, ma'am */
272
} PGErrorVerbosity;
273 274

extern PGErrorVerbosity Log_error_verbosity;
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
275
extern char *Log_line_prefix;
Bruce Momjian's avatar
Bruce Momjian committed
276
extern int	Log_destination;
Bruce Momjian's avatar
Bruce Momjian committed
277

278
/* Log destination bitmap */
Bruce Momjian's avatar
Bruce Momjian committed
279 280
#define LOG_DESTINATION_STDERR	 1
#define LOG_DESTINATION_SYSLOG	 2
281
#define LOG_DESTINATION_EVENTLOG 4
282 283 284

/* Other exported functions */
extern void DebugFileOpen(void);
285

286 287 288 289 290
/*
 * Write errors to stderr (or by equal means when stderr is
 * not available). Used before ereport/elog can be used
 * safely (memory context, GUC load etc)
 */
Bruce Momjian's avatar
Bruce Momjian committed
291 292
extern void
write_stderr(const char *fmt,...)
293 294 295 296
/* This extension allows gcc to check the format string for consistency with
   the supplied arguments. */
__attribute__((format(printf, 1, 2)));

297
#endif   /* ELOG_H */