00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef ELOG_H
00015 #define ELOG_H
00016
00017 #include <setjmp.h>
00018
00019
00020 #define DEBUG5 10
00021
00022 #define DEBUG4 11
00023 #define DEBUG3 12
00024 #define DEBUG2 13
00025 #define DEBUG1 14
00026 #define LOG 15
00027
00028 #define COMMERROR 16
00029
00030
00031 #define INFO 17
00032
00033
00034
00035 #define NOTICE 18
00036
00037
00038 #define WARNING 19
00039
00040
00041 #define ERROR 20
00042
00043
00044
00045
00046
00047 #ifdef WIN32
00048 #define PGERROR 20
00049 #endif
00050 #define FATAL 21
00051 #define PANIC 22
00052
00053
00054
00055
00056
00057 #define PGSIXBIT(ch) (((ch) - '0') & 0x3F)
00058 #define PGUNSIXBIT(val) (((val) & 0x3F) + '0')
00059
00060 #define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) \
00061 (PGSIXBIT(ch1) + (PGSIXBIT(ch2) << 6) + (PGSIXBIT(ch3) << 12) + \
00062 (PGSIXBIT(ch4) << 18) + (PGSIXBIT(ch5) << 24))
00063
00064
00065 #define ERRCODE_TO_CATEGORY(ec) ((ec) & ((1 << 12) - 1))
00066 #define ERRCODE_IS_CATEGORY(ec) (((ec) & ~((1 << 12) - 1)) == 0)
00067
00068
00069 #include "utils/errcodes.h"
00070
00071
00072
00073 #ifdef HAVE_FUNCNAME__FUNC
00074 #define PG_FUNCNAME_MACRO __func__
00075 #else
00076 #ifdef HAVE_FUNCNAME__FUNCTION
00077 #define PG_FUNCNAME_MACRO __FUNCTION__
00078 #else
00079 #define PG_FUNCNAME_MACRO NULL
00080 #endif
00081 #endif
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 #ifdef HAVE__BUILTIN_CONSTANT_P
00114 #define ereport_domain(elevel, domain, rest) \
00115 do { \
00116 if (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
00117 errfinish rest; \
00118 if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
00119 pg_unreachable(); \
00120 } while(0)
00121 #else
00122 #define ereport_domain(elevel, domain, rest) \
00123 do { \
00124 const int elevel_ = (elevel); \
00125 if (errstart(elevel_, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
00126 errfinish rest; \
00127 if (elevel_ >= ERROR) \
00128 pg_unreachable(); \
00129 } while(0)
00130 #endif
00131
00132 #define ereport(elevel, rest) \
00133 ereport_domain(elevel, TEXTDOMAIN, rest)
00134
00135 #define TEXTDOMAIN NULL
00136
00137 extern bool errstart(int elevel, const char *filename, int lineno,
00138 const char *funcname, const char *domain);
00139 extern void errfinish(int dummy,...);
00140
00141 extern int errcode(int sqlerrcode);
00142
00143 extern int errcode_for_file_access(void);
00144 extern int errcode_for_socket_access(void);
00145
00146 extern int
00147 errmsg(const char *fmt,...)
00148
00149
00150 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00151
00152 extern int
00153 errmsg_internal(const char *fmt,...)
00154
00155
00156 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00157
00158 extern int
00159 errmsg_plural(const char *fmt_singular, const char *fmt_plural,
00160 unsigned long n,...)
00161
00162
00163 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 4)))
00164 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 4)));
00165
00166 extern int
00167 errdetail(const char *fmt,...)
00168
00169
00170 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00171
00172 extern int
00173 errdetail_internal(const char *fmt,...)
00174
00175
00176 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00177
00178 extern int
00179 errdetail_log(const char *fmt,...)
00180
00181
00182 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00183
00184 extern int
00185 errdetail_plural(const char *fmt_singular, const char *fmt_plural,
00186 unsigned long n,...)
00187
00188
00189 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 4)))
00190 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 4)));
00191
00192 extern int
00193 errhint(const char *fmt,...)
00194
00195
00196 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 #define errcontext set_errcontext_domain(TEXTDOMAIN), errcontext_msg
00207
00208 extern int set_errcontext_domain(const char *domain);
00209 extern int
00210 errcontext_msg(const char *fmt,...)
00211
00212
00213 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00214
00215 extern int errhidestmt(bool hide_stmt);
00216
00217 extern int errfunction(const char *funcname);
00218 extern int errposition(int cursorpos);
00219
00220 extern int internalerrposition(int cursorpos);
00221 extern int internalerrquery(const char *query);
00222
00223 extern int err_generic_string(int field, const char *str);
00224
00225 extern int geterrcode(void);
00226 extern int geterrposition(void);
00227 extern int getinternalerrposition(void);
00228
00229
00230
00231
00232
00233
00234
00235 #ifdef HAVE__VA_ARGS
00236
00237
00238
00239
00240
00241
00242 #ifdef HAVE__BUILTIN_CONSTANT_P
00243 #define elog(elevel, ...) \
00244 do { \
00245 elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
00246 elog_finish(elevel, __VA_ARGS__); \
00247 if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
00248 pg_unreachable(); \
00249 } while(0)
00250 #else
00251 #define elog(elevel, ...) \
00252 do { \
00253 int elevel_; \
00254 elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
00255 elevel_ = (elevel); \
00256 elog_finish(elevel_, __VA_ARGS__); \
00257 if (elevel_ >= ERROR) \
00258 pg_unreachable(); \
00259 } while(0)
00260 #endif
00261 #else
00262 #define elog \
00263 elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO), \
00264 elog_finish
00265 #endif
00266
00267 extern void elog_start(const char *filename, int lineno, const char *funcname);
00268 extern void
00269 elog_finish(int elevel, const char *fmt,...)
00270
00271
00272 __attribute__((format(PG_PRINTF_ATTRIBUTE, 2, 3)));
00273
00274
00275
00276
00277 extern void pre_format_elog_string(int errnumber, const char *domain);
00278 extern char *
00279 format_elog_string(const char *fmt,...)
00280
00281
00282 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00283
00284
00285
00286
00287 typedef struct ErrorContextCallback
00288 {
00289 struct ErrorContextCallback *previous;
00290 void (*callback) (void *arg);
00291 void *arg;
00292 } ErrorContextCallback;
00293
00294 extern PGDLLIMPORT ErrorContextCallback *error_context_stack;
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 #define PG_TRY() \
00329 do { \
00330 sigjmp_buf *save_exception_stack = PG_exception_stack; \
00331 ErrorContextCallback *save_context_stack = error_context_stack; \
00332 sigjmp_buf local_sigjmp_buf; \
00333 if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
00334 { \
00335 PG_exception_stack = &local_sigjmp_buf
00336
00337 #define PG_CATCH() \
00338 } \
00339 else \
00340 { \
00341 PG_exception_stack = save_exception_stack; \
00342 error_context_stack = save_context_stack
00343
00344 #define PG_END_TRY() \
00345 } \
00346 PG_exception_stack = save_exception_stack; \
00347 error_context_stack = save_context_stack; \
00348 } while (0)
00349
00350
00351
00352
00353
00354 #ifdef __GNUC__
00355 #define PG_RE_THROW() \
00356 pg_re_throw()
00357 #else
00358 #define PG_RE_THROW() \
00359 (pg_re_throw(), pg_unreachable())
00360 #endif
00361
00362 extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 typedef struct ErrorData
00374 {
00375 int elevel;
00376 bool output_to_server;
00377 bool output_to_client;
00378 bool show_funcname;
00379 bool hide_stmt;
00380 const char *filename;
00381 int lineno;
00382 const char *funcname;
00383 const char *domain;
00384 const char *context_domain;
00385 int sqlerrcode;
00386 char *message;
00387 char *detail;
00388 char *detail_log;
00389 char *hint;
00390 char *context;
00391 char *schema_name;
00392 char *table_name;
00393 char *column_name;
00394 char *datatype_name;
00395 char *constraint_name;
00396 int cursorpos;
00397 int internalpos;
00398 char *internalquery;
00399 int saved_errno;
00400 } ErrorData;
00401
00402 extern void EmitErrorReport(void);
00403 extern ErrorData *CopyErrorData(void);
00404 extern void FreeErrorData(ErrorData *edata);
00405 extern void FlushErrorState(void);
00406 extern void ReThrowError(ErrorData *edata) __attribute__((noreturn));
00407 extern void pg_re_throw(void) __attribute__((noreturn));
00408
00409
00410 typedef void (*emit_log_hook_type) (ErrorData *edata);
00411 extern PGDLLIMPORT emit_log_hook_type emit_log_hook;
00412
00413
00414
00415
00416 typedef enum
00417 {
00418 PGERROR_TERSE,
00419 PGERROR_DEFAULT,
00420 PGERROR_VERBOSE
00421 } PGErrorVerbosity;
00422
00423 extern int Log_error_verbosity;
00424 extern char *Log_line_prefix;
00425 extern int Log_destination;
00426
00427
00428 #define LOG_DESTINATION_STDERR 1
00429 #define LOG_DESTINATION_SYSLOG 2
00430 #define LOG_DESTINATION_EVENTLOG 4
00431 #define LOG_DESTINATION_CSVLOG 8
00432
00433
00434 extern void DebugFileOpen(void);
00435 extern char *unpack_sql_state(int sql_state);
00436 extern bool in_error_recursion_trouble(void);
00437
00438 #ifdef HAVE_SYSLOG
00439 extern void set_syslog_parameters(const char *ident, int facility);
00440 #endif
00441
00442
00443
00444
00445
00446
00447 extern void
00448 write_stderr(const char *fmt,...)
00449
00450
00451 __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
00452
00453 #endif