2
0

logging.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*-------------------------------------------------------------------------
  2. * Logging framework for frontend programs
  3. *
  4. * Copyright (c) 2018-2022, PostgreSQL Global Development Group
  5. *
  6. * src/include/common/logging.h
  7. *
  8. *-------------------------------------------------------------------------
  9. */
  10. #ifndef COMMON_LOGGING_H
  11. #define COMMON_LOGGING_H
  12. /*
  13. * Log levels are informational only. They do not affect program flow.
  14. */
  15. enum pg_log_level
  16. {
  17. /*
  18. * Not initialized yet (not to be used as an actual message log level).
  19. */
  20. PG_LOG_NOTSET = 0,
  21. /*
  22. * Low level messages that are normally off by default.
  23. */
  24. PG_LOG_DEBUG,
  25. /*
  26. * Any program messages that go to stderr, shown by default. (The
  27. * program's normal output should go to stdout and not use the logging
  28. * system.)
  29. */
  30. PG_LOG_INFO,
  31. /*
  32. * Warnings and "almost" errors, depends on the program
  33. */
  34. PG_LOG_WARNING,
  35. /*
  36. * Errors
  37. */
  38. PG_LOG_ERROR,
  39. /*
  40. * Turn all logging off (not to be used as an actual message log level).
  41. */
  42. PG_LOG_OFF,
  43. };
  44. /*
  45. * __pg_log_level is the minimum log level that will actually be shown.
  46. */
  47. extern enum pg_log_level __pg_log_level;
  48. /*
  49. * A log message can have several parts. The primary message is required,
  50. * others are optional. When emitting multiple parts, do so in the order of
  51. * this enum, for consistency.
  52. */
  53. enum pg_log_part
  54. {
  55. /*
  56. * The primary message. Try to keep it to one line; follow the backend's
  57. * style guideline for primary messages.
  58. */
  59. PG_LOG_PRIMARY,
  60. /*
  61. * Additional detail. Follow the backend's style guideline for detail
  62. * messages.
  63. */
  64. PG_LOG_DETAIL,
  65. /*
  66. * Hint (not guaranteed correct) about how to fix the problem. Follow the
  67. * backend's style guideline for hint messages.
  68. */
  69. PG_LOG_HINT,
  70. };
  71. /*
  72. * Kind of a hack to be able to produce the psql output exactly as required by
  73. * the regression tests.
  74. */
  75. #define PG_LOG_FLAG_TERSE 1
  76. void pg_logging_init(const char *argv0);
  77. void pg_logging_config(int new_flags);
  78. void pg_logging_set_level(enum pg_log_level new_level);
  79. void pg_logging_increase_verbosity(void);
  80. void pg_logging_set_pre_callback(void (*cb) (void));
  81. void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno));
  82. void pg_log_generic(enum pg_log_level level, enum pg_log_part part,
  83. const char *pg_restrict fmt,...)
  84. pg_attribute_printf(3, 4);
  85. void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
  86. const char *pg_restrict fmt, va_list ap)
  87. pg_attribute_printf(3, 0);
  88. /*
  89. * Preferred style is to use these macros to perform logging; don't call
  90. * pg_log_generic[_v] directly, except perhaps in error interface code.
  91. */
  92. #define pg_log_error(...) \
  93. pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__)
  94. #define pg_log_error_detail(...) \
  95. pg_log_generic(PG_LOG_ERROR, PG_LOG_DETAIL, __VA_ARGS__)
  96. #define pg_log_error_hint(...) \
  97. pg_log_generic(PG_LOG_ERROR, PG_LOG_HINT, __VA_ARGS__)
  98. #define pg_log_warning(...) \
  99. pg_log_generic(PG_LOG_WARNING, PG_LOG_PRIMARY, __VA_ARGS__)
  100. #define pg_log_warning_detail(...) \
  101. pg_log_generic(PG_LOG_WARNING, PG_LOG_DETAIL, __VA_ARGS__)
  102. #define pg_log_warning_hint(...) \
  103. pg_log_generic(PG_LOG_WARNING, PG_LOG_HINT, __VA_ARGS__)
  104. #define pg_log_info(...) \
  105. pg_log_generic(PG_LOG_INFO, PG_LOG_PRIMARY, __VA_ARGS__)
  106. #define pg_log_info_detail(...) \
  107. pg_log_generic(PG_LOG_INFO, PG_LOG_DETAIL, __VA_ARGS__)
  108. #define pg_log_info_hint(...) \
  109. pg_log_generic(PG_LOG_INFO, PG_LOG_HINT, __VA_ARGS__)
  110. #define pg_log_debug(...) do { \
  111. if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
  112. pg_log_generic(PG_LOG_DEBUG, PG_LOG_PRIMARY, __VA_ARGS__); \
  113. } while(0)
  114. #define pg_log_debug_detail(...) do { \
  115. if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
  116. pg_log_generic(PG_LOG_DEBUG, PG_LOG_DETAIL, __VA_ARGS__); \
  117. } while(0)
  118. #define pg_log_debug_hint(...) do { \
  119. if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) \
  120. pg_log_generic(PG_LOG_DEBUG, PG_LOG_HINT, __VA_ARGS__); \
  121. } while(0)
  122. /*
  123. * A common shortcut: pg_log_error() and immediately exit(1).
  124. */
  125. #define pg_fatal(...) do { \
  126. pg_log_generic(PG_LOG_ERROR, PG_LOG_PRIMARY, __VA_ARGS__); \
  127. exit(1); \
  128. } while(0)
  129. #endif /* COMMON_LOGGING_H */