gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

437 line
16KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * elog.h
  4. * POSTGRES error reporting/logging definitions.
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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 as
  29. * LOG for server reporting, but never
  30. * 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. * Provide a way to prevent "errno" from being accidentally used inside an
  66. * elog() or ereport() invocation. Since we know that some operating systems
  67. * define errno as something involving a function call, we'll put a local
  68. * variable of the same name as that function in the local scope to force a
  69. * compile error. On platforms that don't define errno in that way, nothing
  70. * happens, so we get no warning ... but we can live with that as long as it
  71. * happens on some popular platforms.
  72. */
  73. #if defined(errno) && defined(__linux__)
  74. #define pg_prevent_errno_in_scope() int __errno_location pg_attribute_unused()
  75. #elif defined(errno) && (defined(__darwin__) || defined(__freebsd__))
  76. #define pg_prevent_errno_in_scope() int __error pg_attribute_unused()
  77. #else
  78. #define pg_prevent_errno_in_scope()
  79. #endif
  80. /*----------
  81. * New-style error reporting API: to be used in this way:
  82. * ereport(ERROR,
  83. * errcode(ERRCODE_UNDEFINED_CURSOR),
  84. * errmsg("portal \"%s\" not found", stmt->portalname),
  85. * ... other errxxx() fields as needed ...);
  86. *
  87. * The error level is required, and so is a primary error message (errmsg
  88. * or errmsg_internal). All else is optional. errcode() defaults to
  89. * ERRCODE_INTERNAL_ERROR if elevel is ERROR or more, ERRCODE_WARNING
  90. * if elevel is WARNING, or ERRCODE_SUCCESSFUL_COMPLETION if elevel is
  91. * NOTICE or below.
  92. *
  93. * Before Postgres v12, extra parentheses were required around the
  94. * list of auxiliary function calls; that's now optional.
  95. *
  96. * ereport_domain() allows a message domain to be specified, for modules that
  97. * wish to use a different message catalog from the backend's. To avoid having
  98. * one copy of the default text domain per .o file, we define it as NULL here
  99. * and have errstart insert the default text domain. Modules can either use
  100. * ereport_domain() directly, or preferably they can override the TEXTDOMAIN
  101. * macro.
  102. *
  103. * If elevel >= ERROR, the call will not return; we try to inform the compiler
  104. * of that via pg_unreachable(). However, no useful optimization effect is
  105. * obtained unless the compiler sees elevel as a compile-time constant, else
  106. * we're just adding code bloat. So, if __builtin_constant_p is available,
  107. * use that to cause the second if() to vanish completely for non-constant
  108. * cases. We avoid using a local variable because it's not necessary and
  109. * prevents gcc from making the unreachability deduction at optlevel -O0.
  110. *----------
  111. */
  112. #ifdef HAVE__BUILTIN_CONSTANT_P
  113. #define ereport_domain(elevel, domain, ...) \
  114. do { \
  115. pg_prevent_errno_in_scope(); \
  116. if (errstart(elevel, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
  117. __VA_ARGS__, errfinish(0); \
  118. if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
  119. pg_unreachable(); \
  120. } while(0)
  121. #else /* !HAVE__BUILTIN_CONSTANT_P */
  122. #define ereport_domain(elevel, domain, ...) \
  123. do { \
  124. const int elevel_ = (elevel); \
  125. pg_prevent_errno_in_scope(); \
  126. if (errstart(elevel_, __FILE__, __LINE__, PG_FUNCNAME_MACRO, domain)) \
  127. __VA_ARGS__, errfinish(0); \
  128. if (elevel_ >= ERROR) \
  129. pg_unreachable(); \
  130. } while(0)
  131. #endif /* HAVE__BUILTIN_CONSTANT_P */
  132. #define ereport(elevel, ...) \
  133. ereport_domain(elevel, TEXTDOMAIN, __VA_ARGS__)
  134. #define TEXTDOMAIN NULL
  135. extern bool errstart(int elevel, const char *filename, int lineno,
  136. const char *funcname, const char *domain);
  137. extern void errfinish(int dummy,...);
  138. extern int errcode(int sqlerrcode);
  139. extern int errcode_for_file_access(void);
  140. extern int errcode_for_socket_access(void);
  141. extern int errmsg(const char *fmt,...) pg_attribute_printf(1, 2);
  142. extern int errmsg_internal(const char *fmt,...) pg_attribute_printf(1, 2);
  143. extern int errmsg_plural(const char *fmt_singular, const char *fmt_plural,
  144. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  145. extern int errdetail(const char *fmt,...) pg_attribute_printf(1, 2);
  146. extern int errdetail_internal(const char *fmt,...) pg_attribute_printf(1, 2);
  147. extern int errdetail_log(const char *fmt,...) pg_attribute_printf(1, 2);
  148. extern int errdetail_log_plural(const char *fmt_singular,
  149. const char *fmt_plural,
  150. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  151. extern int errdetail_plural(const char *fmt_singular, const char *fmt_plural,
  152. unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4);
  153. extern int errhint(const char *fmt,...) pg_attribute_printf(1, 2);
  154. /*
  155. * errcontext() is typically called in error context callback functions, not
  156. * within an ereport() invocation. The callback function can be in a different
  157. * module than the ereport() call, so the message domain passed in errstart()
  158. * is not usually the correct domain for translating the context message.
  159. * set_errcontext_domain() first sets the domain to be used, and
  160. * errcontext_msg() passes the actual message.
  161. */
  162. #define errcontext set_errcontext_domain(TEXTDOMAIN), errcontext_msg
  163. extern int set_errcontext_domain(const char *domain);
  164. extern int errcontext_msg(const char *fmt,...) pg_attribute_printf(1, 2);
  165. extern int errhidestmt(bool hide_stmt);
  166. extern int errhidecontext(bool hide_ctx);
  167. extern int errfunction(const char *funcname);
  168. extern int errposition(int cursorpos);
  169. extern int internalerrposition(int cursorpos);
  170. extern int internalerrquery(const char *query);
  171. extern int err_generic_string(int field, const char *str);
  172. extern int geterrcode(void);
  173. extern int geterrposition(void);
  174. extern int getinternalerrposition(void);
  175. /*----------
  176. * Old-style error reporting API: to be used in this way:
  177. * elog(ERROR, "portal \"%s\" not found", stmt->portalname);
  178. *----------
  179. */
  180. /*
  181. * Using variadic macros, we can give the compiler a hint about the
  182. * call not returning when elevel >= ERROR. See comments for ereport().
  183. * Note that historically elog() has called elog_start (which saves errno)
  184. * before evaluating "elevel", so we preserve that behavior here.
  185. */
  186. #ifdef HAVE__BUILTIN_CONSTANT_P
  187. #define elog(elevel, ...) \
  188. do { \
  189. pg_prevent_errno_in_scope(); \
  190. elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
  191. elog_finish(elevel, __VA_ARGS__); \
  192. if (__builtin_constant_p(elevel) && (elevel) >= ERROR) \
  193. pg_unreachable(); \
  194. } while(0)
  195. #else /* !HAVE__BUILTIN_CONSTANT_P */
  196. #define elog(elevel, ...) \
  197. do { \
  198. pg_prevent_errno_in_scope(); \
  199. elog_start(__FILE__, __LINE__, PG_FUNCNAME_MACRO); \
  200. { \
  201. const int elevel_ = (elevel); \
  202. elog_finish(elevel_, __VA_ARGS__); \
  203. if (elevel_ >= ERROR) \
  204. pg_unreachable(); \
  205. } \
  206. } while(0)
  207. #endif /* HAVE__BUILTIN_CONSTANT_P */
  208. extern void elog_start(const char *filename, int lineno, const char *funcname);
  209. extern void elog_finish(int elevel, const char *fmt,...) pg_attribute_printf(2, 3);
  210. /* Support for constructing error strings separately from ereport() calls */
  211. extern void pre_format_elog_string(int errnumber, const char *domain);
  212. extern char *format_elog_string(const char *fmt,...) pg_attribute_printf(1, 2);
  213. /* Support for attaching context information to error reports */
  214. typedef struct ErrorContextCallback
  215. {
  216. struct ErrorContextCallback *previous;
  217. void (*callback) (void *arg);
  218. void *arg;
  219. } ErrorContextCallback;
  220. extern PGDLLIMPORT ErrorContextCallback *error_context_stack;
  221. /*----------
  222. * API for catching ereport(ERROR) exits. Use these macros like so:
  223. *
  224. * PG_TRY();
  225. * {
  226. * ... code that might throw ereport(ERROR) ...
  227. * }
  228. * PG_CATCH();
  229. * {
  230. * ... error recovery code ...
  231. * }
  232. * PG_END_TRY();
  233. *
  234. * (The braces are not actually necessary, but are recommended so that
  235. * pgindent will indent the construct nicely.) The error recovery code
  236. * can either do PG_RE_THROW to propagate the error outwards, or do a
  237. * (sub)transaction abort. Failure to do so may leave the system in an
  238. * inconsistent state for further processing.
  239. *
  240. * Note: while the system will correctly propagate any new ereport(ERROR)
  241. * occurring in the recovery section, there is a small limit on the number
  242. * of levels this will work for. It's best to keep the error recovery
  243. * section simple enough that it can't generate any new errors, at least
  244. * not before popping the error stack.
  245. *
  246. * Note: an ereport(FATAL) will not be caught by this construct; control will
  247. * exit straight through proc_exit(). Therefore, do NOT put any cleanup
  248. * of non-process-local resources into the error recovery section, at least
  249. * not without taking thought for what will happen during ereport(FATAL).
  250. * The PG_ENSURE_ERROR_CLEANUP macros provided by storage/ipc.h may be
  251. * helpful in such cases.
  252. *
  253. * Note: if a local variable of the function containing PG_TRY is modified
  254. * in the PG_TRY section and used in the PG_CATCH section, that variable
  255. * must be declared "volatile" for POSIX compliance. This is not mere
  256. * pedantry; we have seen bugs from compilers improperly optimizing code
  257. * away when such a variable was not marked. Beware that gcc's -Wclobbered
  258. * warnings are just about entirely useless for catching such oversights.
  259. *----------
  260. */
  261. #define PG_TRY() \
  262. do { \
  263. sigjmp_buf *save_exception_stack = PG_exception_stack; \
  264. ErrorContextCallback *save_context_stack = error_context_stack; \
  265. sigjmp_buf local_sigjmp_buf; \
  266. if (sigsetjmp(local_sigjmp_buf, 0) == 0) \
  267. { \
  268. PG_exception_stack = &local_sigjmp_buf
  269. #define PG_CATCH() \
  270. } \
  271. else \
  272. { \
  273. PG_exception_stack = save_exception_stack; \
  274. error_context_stack = save_context_stack
  275. #define PG_END_TRY() \
  276. } \
  277. PG_exception_stack = save_exception_stack; \
  278. error_context_stack = save_context_stack; \
  279. } while (0)
  280. /*
  281. * Some compilers understand pg_attribute_noreturn(); for other compilers,
  282. * insert pg_unreachable() so that the compiler gets the point.
  283. */
  284. #ifdef HAVE_PG_ATTRIBUTE_NORETURN
  285. #define PG_RE_THROW() \
  286. pg_re_throw()
  287. #else
  288. #define PG_RE_THROW() \
  289. (pg_re_throw(), pg_unreachable())
  290. #endif
  291. extern PGDLLIMPORT sigjmp_buf *PG_exception_stack;
  292. /* Stuff that error handlers might want to use */
  293. /*
  294. * ErrorData holds the data accumulated during any one ereport() cycle.
  295. * Any non-NULL pointers must point to palloc'd data.
  296. * (The const pointers are an exception; we assume they point at non-freeable
  297. * constant strings.)
  298. */
  299. typedef struct ErrorData
  300. {
  301. int elevel; /* error level */
  302. bool output_to_server; /* will report to server log? */
  303. bool output_to_client; /* will report to client? */
  304. bool show_funcname; /* true to force funcname inclusion */
  305. bool hide_stmt; /* true to prevent STATEMENT: inclusion */
  306. bool hide_ctx; /* true to prevent CONTEXT: inclusion */
  307. const char *filename; /* __FILE__ of ereport() call */
  308. int lineno; /* __LINE__ of ereport() call */
  309. const char *funcname; /* __func__ of ereport() call */
  310. const char *domain; /* message domain */
  311. const char *context_domain; /* message domain for context message */
  312. int sqlerrcode; /* encoded ERRSTATE */
  313. char *message; /* primary error message (translated) */
  314. char *detail; /* detail error message */
  315. char *detail_log; /* detail error message for server log only */
  316. char *hint; /* hint message */
  317. char *context; /* context message */
  318. const char *message_id; /* primary message's id (original string) */
  319. char *schema_name; /* name of schema */
  320. char *table_name; /* name of table */
  321. char *column_name; /* name of column */
  322. char *datatype_name; /* name of datatype */
  323. char *constraint_name; /* name of constraint */
  324. int cursorpos; /* cursor index into query string */
  325. int internalpos; /* cursor index into internalquery */
  326. char *internalquery; /* text of internally-generated query */
  327. int saved_errno; /* errno at entry */
  328. /* context containing associated non-constant strings */
  329. struct MemoryContextData *assoc_context;
  330. } ErrorData;
  331. extern void EmitErrorReport(void);
  332. extern ErrorData *CopyErrorData(void);
  333. extern void FreeErrorData(ErrorData *edata);
  334. extern void FlushErrorState(void);
  335. extern void ReThrowError(ErrorData *edata) pg_attribute_noreturn();
  336. extern void ThrowErrorData(ErrorData *edata);
  337. extern void pg_re_throw(void) pg_attribute_noreturn();
  338. extern char *GetErrorContextStack(void);
  339. /* Hook for intercepting messages before they are sent to the server log */
  340. typedef void (*emit_log_hook_type) (ErrorData *edata);
  341. extern PGDLLIMPORT emit_log_hook_type emit_log_hook;
  342. /* GUC-configurable parameters */
  343. typedef enum
  344. {
  345. PGERROR_TERSE, /* single-line error messages */
  346. PGERROR_DEFAULT, /* recommended style */
  347. PGERROR_VERBOSE /* all the facts, ma'am */
  348. } PGErrorVerbosity;
  349. extern int Log_error_verbosity;
  350. extern char *Log_line_prefix;
  351. extern int Log_destination;
  352. extern char *Log_destination_string;
  353. extern bool syslog_sequence_numbers;
  354. extern bool syslog_split_messages;
  355. /* Log destination bitmap */
  356. #define LOG_DESTINATION_STDERR 1
  357. #define LOG_DESTINATION_SYSLOG 2
  358. #define LOG_DESTINATION_EVENTLOG 4
  359. #define LOG_DESTINATION_CSVLOG 8
  360. /* Other exported functions */
  361. extern void DebugFileOpen(void);
  362. extern char *unpack_sql_state(int sql_state);
  363. extern bool in_error_recursion_trouble(void);
  364. #ifdef HAVE_SYSLOG
  365. extern void set_syslog_parameters(const char *ident, int facility);
  366. #endif
  367. /*
  368. * Write errors to stderr (or by equal means when stderr is
  369. * not available). Used before ereport/elog can be used
  370. * safely (memory context, GUC load etc)
  371. */
  372. extern void write_stderr(const char *fmt,...) pg_attribute_printf(1, 2);
  373. #endif /* ELOG_H */
上海开阖软件有限公司 沪ICP备12045867号-1