elog.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*-------------------------------------------------------------------------
  2. *
  3. * elog.h
  4. * POSTGRES error reporting/logging definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/utils/elog.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. #ifndef ELOG_H
  15. #define ELOG_H
  16. #include <setjmp.h>
  17. /* Error level codes */
  18. #define DEBUG5 10 /* Debugging messages, in categories of
  19. * decreasing detail. */
  20. #define DEBUG4 11
  21. #define DEBUG3 12
  22. #define DEBUG2 13
  23. #define DEBUG1 14 /* used by GUC debug_* variables */
  24. #define LOG 15 /* Server operational messages; sent only to
  25. * server log by default. */
  26. #define LOG_SERVER_ONLY 16 /* Same as LOG for server reporting, but never
  27. * sent to client. */
  28. #define COMMERROR LOG_SERVER_ONLY /* Client communication problems; same
  29. * as LOG for server reporting, but
  30. * never sent to client. */
  31. #define INFO 17 /* Messages specifically requested by user (eg
  32. * VACUUM VERBOSE output); always sent to
  33. * client regardless of client_min_messages,
  34. * but by default not sent to server log. */
  35. #define NOTICE 18 /* Helpful messages to users about query
  36. * operation; sent to client and not to server
  37. * log by default. */
  38. #define WARNING 19 /* Warnings. NOTICE is for expected messages
  39. * like implicit sequence creation by SERIAL.
  40. * WARNING is for unexpected messages. */
  41. #define ERROR 20 /* user error - abort transaction; return to
  42. * known state */
  43. /* Save ERROR value in PGERROR so it can be restored when Win32 includes
  44. * modify it. We have to use a constant rather than ERROR because macros
  45. * are expanded only when referenced outside macros.
  46. */
  47. #ifdef WIN32
  48. #define PGERROR 20
  49. #endif
  50. #define FATAL 21 /* fatal error - abort process */
  51. #define PANIC 22 /* take down the other backends with me */
  52. /* #define DEBUG DEBUG1 */ /* Backward compatibility with pre-7.3 */
  53. /* macros for representing SQLSTATE strings compactly */
  54. #define PGSIXBIT(ch) (((ch) - '0') & 0x3F)
  55. #define PGUNSIXBIT(val) (((val) & 0x3F) + '0')
  56. #define MAKE_SQLSTATE(ch1,ch2,ch3,ch4,ch5) \
  57. (PGSIXBIT(ch1) + (PGSIXBIT(ch2) << 6) + (PGSIXBIT(ch3) << 12) + \
  58. (PGSIXBIT(ch4) << 18) + (PGSIXBIT(ch5) << 24))
  59. /* These macros depend on the fact that '0' becomes a zero in SIXBIT */
  60. #define ERRCODE_TO_CATEGORY(ec) ((ec) & ((1 << 12) - 1))
  61. #define ERRCODE_IS_CATEGORY(ec) (((ec) & ~((1 << 12) - 1)) == 0)
  62. /* SQLSTATE codes for errors are defined in a separate file */
  63. #include "utils/errcodes.h"
  64. /*----------
  65. * New-style error reporting API: to be used in this way:
  66. * ereport(ERROR,
  67. * (errcode(ERRCODE_UNDEFINED_CURSOR),
  68. * errmsg("portal \"%s\" not found", stmt->portalname),
  69. * ... other errxxx() fields as needed ...));
  70. *
  71. * The error level is required, and so is a primary error message (errmsg
  72. * or errmsg_internal). All else is optional. errcode() defaults to
  73. * ERRCODE_INTERNAL_ERROR if elevel is ERROR or more, ERRCODE_WARNING
  74. * if elevel is WARNING, or ERRCODE_SUCCESSFUL_COMPLETION if elevel is
  75. * NOTICE or below.
  76. *
  77. * ereport_domain() allows a message domain to be specified, for modules that
  78. * wish to use a different message catalog from the backend's. To avoid having
  79. * one copy of the default text domain per .o file, we define it as NULL here
  80. * and have errstart insert the default text domain. Modules can either use
  81. * ereport_domain() directly, or preferably they can override the TEXTDOMAIN
  82. * macro.
  83. *
  84. * If elevel >= ERROR, the call will not return; we try to inform the compiler
  85. * of that via pg_unreachable(). However, no useful optimization effect is
  86. * obtained unless the compiler sees elevel as a compile-time constant, else
  87. * we're just adding code bloat. So, if __builtin_constant_p is available,
  88. * use that to cause the second if() to vanish completely for non-constant
  89. * cases. We avoid using a local variable because it's not necessary and
  90. * prevents gcc from making the unreachability deduction at optlevel -O0.
  91. *----------
  92. */
  93. #ifdef HAVE__BUILTIN_CONSTANT_P
  94. #define ereport_domain(elevel, domain, rest) \
  95. do { \
  96. if (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
  97. errfinish rest; \
  98. if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
  99. pg_unreachable(); \
  100. } while(0)
  101. #else /* !HAVE__BUILTIN_CONSTANT_P */
  102. #define ereport_domain(elevel, domain, rest) \
  103. do { \
  104. const int elevel_ = (elevel); \
  105. if (errstart(elevel_, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
  106. errfinish rest; \
  107. if (elevel_ >= ERROR) \
  108. pg_unreachable(); \
  109. } while(0)
  110. #endif /* HAVE__BUILTIN_CONSTANT_P */
  111. #define ereport(elevel, rest) \
  112. ereport_domain(elevel, TEXTDOMAIN, rest)
  113. #define TEXTDOMAIN NULL
  114. extern bool errstart(int elevel, const char *filename, int lineno,
  115. const char *funcname, const char *domain);
  116. extern void errfinish(int dummy,...);
  117. extern int errcode(int sqlerrcode);
  118. extern int errcode_for_file_access(void);
  119. extern int errcode_for_socket_access(void);
  120. extern int errmsg(const char *fmt,...) pg_attribute_printf(1, 2);
  121. extern int errmsg_internal(const char *fmt,...) pg_attribute_printf(1, 2);
  122. extern int errmsg_plural(const char *fmt_singular, const char *fmt_plural,
  123. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  124. extern int errdetail(const char *fmt,...) pg_attribute_printf(1, 2);
  125. extern int errdetail_internal(const char *fmt,...) pg_attribute_printf(1, 2);
  126. extern int errdetail_log(const char *fmt,...) pg_attribute_printf(1, 2);
  127. extern int errdetail_log_plural(const char *fmt_singular,
  128. const char *fmt_plural,
  129. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  130. extern int errdetail_plural(const char *fmt_singular, const char *fmt_plural,
  131. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  132. extern int errhint(const char *fmt,...) pg_attribute_printf(1, 2);
  133. /*
  134. * errcontext() is typically called in error context callback functions, not
  135. * within an ereport() invocation. The callback function can be in a different
  136. * module than the ereport() call, so the message domain passed in errstart()
  137. * is not usually the correct domain for translating the context message.
  138. * set_errcontext_domain() first sets the domain to be used, and
  139. * errcontext_msg() passes the actual message.
  140. */
  141. #define errcontext set_errcontext_domain(TEXTDOMAIN), errcontext_msg
  142. extern int set_errcontext_domain(const char *domain);
  143. extern int errcontext_msg(const char *fmt,...) pg_attribute_printf(1, 2);
  144. extern int errhidestmt(bool hide_stmt);
  145. extern int errhidecontext(bool hide_ctx);
  146. extern int errfunction(const char *funcname);
  147. extern int errposition(int cursorpos);
  148. extern int internalerrposition(int cursorpos);
  149. extern int internalerrquery(const char *query);
  150. extern int err_generic_string(int field, const char *str);
  151. extern int geterrcode(void);
  152. extern int geterrposition(void);
  153. extern int getinternalerrposition(void);
  154. /*----------
  155. * Old-style error reporting API: to be used in this way:
  156. * elog(ERROR, "portal \"%s\" not found", stmt->portalname);
  157. *----------
  158. */
  159. #ifdef HAVE__VA_ARGS
  160. /*
  161. * If we have variadic macros, we can give the compiler a hint about the
  162. * call not returning when elevel >= ERROR. See comments for ereport().
  163. * Note that historically elog() has called elog_start (which saves errno)
  164. * before evaluating "elevel", so we preserve that behavior here.
  165. */
  166. #ifdef HAVE__BUILTIN_CONSTANT_P
  167. #define elog(elevel, ...) \
  168. do { \
  169. elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
  170. elog_finish(elevel, __VA_ARGS__); \
  171. if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
  172. pg_unreachable(); \
  173. } while(0)
  174. #else /* !HAVE__BUILTIN_CONSTANT_P */
  175. #define elog(elevel, ...) \
  176. do { \
  177. elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
  178. { \
  179. const int elevel_ = (elevel); \
  180. elog_finish(elevel_, __VA_ARGS__); \
  181. if (elevel_ >= ERROR) \
  182. pg_unreachable(); \
  183. } \
  184. } while(0)
  185. #endif /* HAVE__BUILTIN_CONSTANT_P */
  186. #else /* !HAVE__VA_ARGS */
  187. #define elog \
  188. elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO), \
  189. elog_finish
  190. #endif /* HAVE__VA_ARGS */
  191. extern void elog_start(const char *filename, int lineno, const char *funcname);
  192. extern void elog_finish(int elevel, const char *fmt,...) pg_attribute_printf(2, 3);
  193. /* Support for constructing error strings separately from ereport() calls */
  194. extern void pre_format_elog_string(int errnumber, const char *domain);
  195. extern char *format_elog_string(const char *fmt,...) pg_attribute_printf(1, 2);
  196. /* Support for attaching context information to error reports */
  197. typedef struct ErrorContextCallback
  198. {
  199. struct ErrorContextCallback *previous;
  200. void (*callback) (void *arg);
  201. void *arg;
  202. } ErrorContextCallback;
  203. extern PGDLLIMPORT ErrorContextCallback *error_context_stack;
  204. /*----------
  205. * API for catching ereport(ERROR) exits. Use these macros like so:
  206. *
  207. * PG_TRY();
  208. * {
  209. * ... code that might throw ereport(ERROR) ...
  210. * }
  211. * PG_CATCH();
  212. * {
  213. * ... error recovery code ...
  214. * }
  215. * PG_END_TRY();
  216. *
  217. * (The braces are not actually necessary, but are recommended so that
  218. * pgindent will indent the construct nicely.) The error recovery code
  219. * can optionally do PG_RE_THROW() to propagate the same error outwards.
  220. *
  221. * Note: while the system will correctly propagate any new ereport(ERROR)
  222. * occurring in the recovery section, there is a small limit on the number
  223. * of levels this will work for. It's best to keep the error recovery
  224. * section simple enough that it can't generate any new errors, at least
  225. * not before popping the error stack.
  226. *
  227. * Note: an ereport(FATAL) will not be caught by this construct; control will
  228. * exit straight through proc_exit(). Therefore, do NOT put any cleanup
  229. * of non-process-local resources into the error recovery section, at least
  230. * not without taking thought for what will happen during ereport(FATAL).
  231. * The PG_ENSURE_ERROR_CLEANUP macros provided by storage/ipc.h may be
  232. * helpful in such cases.
  233. *
  234. * Note: if a local variable of the function containing PG_TRY is modified
  235. * in the PG_TRY section and used in the PG_CATCH section, that variable
  236. * must be declared "volatile" for POSIX compliance. This is not mere
  237. * pedantry; we have seen bugs from compilers improperly optimizing code
  238. * away when such a variable was not marked. Beware that gcc's -Wclobbered
  239. * warnings are just about entirely useless for catching such oversights.
  240. *----------
  241. */
  242. #define PG_TRY() \
  243. do { \
  244. sigjmp_buf *save_exception_stack = PG_exception_stack; \
  245. ErrorContextCallback *save_context_stack = error_context_stack; \
  246. sigjmp_buf local_sigjmp_buf; \
  247. if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
  248. { \
  249. PG_exception_stack = &local_sigjmp_buf
  250. #define PG_CATCH() \
  251. } \
  252. else \
  253. { \
  254. PG_exception_stack = save_exception_stack; \
  255. error_context_stack = save_context_stack
  256. #define PG_END_TRY() \
  257. } \
  258. PG_exception_stack = save_exception_stack; \
  259. error_context_stack = save_context_stack; \
  260. } while (0)
  261. /*
  262. * Some compilers understand pg_attribute_noreturn(); for other compilers,
  263. * insert pg_unreachable() so that the compiler gets the point.
  264. */
  265. #ifdef HAVE_PG_ATTRIBUTE_NORETURN
  266. #define PG_RE_THROW() \
  267. pg_re_throw()
  268. #else
  269. #define PG_RE_THROW() \
  270. (pg_re_throw(), pg_unreachable())
  271. #endif
  272. extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
  273. /* Stuff that error handlers might want to use */
  274. /*
  275. * ErrorData holds the data accumulated during any one ereport() cycle.
  276. * Any non-NULL pointers must point to palloc'd data.
  277. * (The const pointers are an exception; we assume they point at non-freeable
  278. * constant strings.)
  279. */
  280. typedef struct ErrorData
  281. {
  282. int elevel; /* error level */
  283. bool output_to_server; /* will report to server log? */
  284. bool output_to_client; /* will report to client? */
  285. bool show_funcname; /* true to force funcname inclusion */
  286. bool hide_stmt; /* true to prevent STATEMENT: inclusion */
  287. bool hide_ctx; /* true to prevent CONTEXT: inclusion */
  288. const char *filename; /* __FILE__ of ereport() call */
  289. int lineno; /* __LINE__ of ereport() call */
  290. const char *funcname; /* __func__ of ereport() call */
  291. const char *domain; /* message domain */
  292. const char *context_domain; /* message domain for context message */
  293. int sqlerrcode; /* encoded ERRSTATE */
  294. char *message; /* primary error message (translated) */
  295. char *detail; /* detail error message */
  296. char *detail_log; /* detail error message for server log only */
  297. char *hint; /* hint message */
  298. char *context; /* context message */
  299. const char *message_id; /* primary message's id (original string) */
  300. char *schema_name; /* name of schema */
  301. char *table_name; /* name of table */
  302. char *column_name; /* name of column */
  303. char *datatype_name; /* name of datatype */
  304. char *constraint_name; /* name of constraint */
  305. int cursorpos; /* cursor index into query string */
  306. int internalpos; /* cursor index into internalquery */
  307. char *internalquery; /* text of internally-generated query */
  308. int saved_errno; /* errno at entry */
  309. /* context containing associated non-constant strings */
  310. struct MemoryContextData *assoc_context;
  311. } ErrorData;
  312. extern void EmitErrorReport(void);
  313. extern ErrorData *CopyErrorData(void);
  314. extern void FreeErrorData(ErrorData *edata);
  315. extern void FlushErrorState(void);
  316. extern void ReThrowError(ErrorData *edata) pg_attribute_noreturn();
  317. extern void ThrowErrorData(ErrorData *edata);
  318. extern void pg_re_throw(void) pg_attribute_noreturn();
  319. extern char *GetErrorContextStack(void);
  320. /* Hook for intercepting messages before they are sent to the server log */
  321. typedef void (*emit_log_hook_type) (ErrorData *edata);
  322. extern PGDLLIMPORT emit_log_hook_type emit_log_hook;
  323. /* GUC-configurable parameters */
  324. typedef enum
  325. {
  326. PGERROR_TERSE, /* single-line error messages */
  327. PGERROR_DEFAULT, /* recommended style */
  328. PGERROR_VERBOSE /* all the facts, ma'am */
  329. } PGErrorVerbosity;
  330. extern int Log_error_verbosity;
  331. extern char *Log_line_prefix;
  332. extern int Log_destination;
  333. extern char *Log_destination_string;
  334. extern bool syslog_sequence_numbers;
  335. extern bool syslog_split_messages;
  336. /* Log destination bitmap */
  337. #define LOG_DESTINATION_STDERR 1
  338. #define LOG_DESTINATION_SYSLOG 2
  339. #define LOG_DESTINATION_EVENTLOG 4
  340. #define LOG_DESTINATION_CSVLOG 8
  341. /* Other exported functions */
  342. extern void DebugFileOpen(void);
  343. extern char *unpack_sql_state(int sql_state);
  344. extern bool in_error_recursion_trouble(void);
  345. #ifdef HAVE_SYSLOG
  346. extern void set_syslog_parameters(const char *ident, int facility);
  347. #endif
  348. /*
  349. * Write errors to stderr (or by equal means when stderr is
  350. * not available). Used before ereport/elog can be used
  351. * safely (memory context, GUC load etc)
  352. */
  353. extern void write_stderr(const char *fmt,...) pg_attribute_printf(1, 2);
  354. #endif /* ELOG_H */