| 1 | /*------------------------------------------------------------------------- |
| 2 | * Logging framework for frontend programs |
| 3 | * |
| 4 | * Copyright (c) 2018-2019, 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 | /* |
| 14 | * Log levels are informational only. They do not affect program flow. |
| 15 | */ |
| 16 | enum pg_log_level |
| 17 | { |
| 18 | /* |
| 19 | * Not initialized yet |
| 20 | */ |
| 21 | PG_LOG_NOTSET = 0, |
| 22 | |
| 23 | /* |
| 24 | * Low level messages that are normally off by default. |
| 25 | */ |
| 26 | PG_LOG_DEBUG, |
| 27 | |
| 28 | /* |
| 29 | * Any program messages that go to stderr, shown by default. (The |
| 30 | * program's normal output should go to stdout and not use the logging |
| 31 | * system.) |
| 32 | */ |
| 33 | PG_LOG_INFO, |
| 34 | |
| 35 | /* |
| 36 | * Warnings and "almost" errors, depends on the program |
| 37 | */ |
| 38 | PG_LOG_WARNING, |
| 39 | |
| 40 | /* |
| 41 | * Errors |
| 42 | */ |
| 43 | PG_LOG_ERROR, |
| 44 | |
| 45 | /* |
| 46 | * Severe errors that cause program termination. (One-shot programs may |
| 47 | * chose to label even fatal errors as merely "errors". The distinction |
| 48 | * is up to the program.) |
| 49 | */ |
| 50 | PG_LOG_FATAL, |
| 51 | |
| 52 | /* |
| 53 | * Turn all logging off. |
| 54 | */ |
| 55 | PG_LOG_OFF, |
| 56 | }; |
| 57 | |
| 58 | extern enum pg_log_level __pg_log_level; |
| 59 | |
| 60 | /* |
| 61 | * Kind of a hack to be able to produce the psql output exactly as required by |
| 62 | * the regression tests. |
| 63 | */ |
| 64 | #define PG_LOG_FLAG_TERSE 1 |
| 65 | |
| 66 | void pg_logging_init(const char *argv0); |
| 67 | void pg_logging_config(int new_flags); |
| 68 | void pg_logging_set_level(enum pg_log_level new_level); |
| 69 | void pg_logging_set_pre_callback(void (*cb) (void)); |
| 70 | void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno)); |
| 71 | |
| 72 | void pg_log_generic(enum pg_log_level level, const char *pg_restrict fmt,...) pg_attribute_printf(2, 3); |
| 73 | void pg_log_generic_v(enum pg_log_level level, const char *pg_restrict fmt, va_list ap) pg_attribute_printf(2, 0); |
| 74 | |
| 75 | #define pg_log_fatal(...) do { \ |
| 76 | if (likely(__pg_log_level <= PG_LOG_FATAL)) pg_log_generic(PG_LOG_FATAL, __VA_ARGS__); \ |
| 77 | } while(0) |
| 78 | |
| 79 | #define pg_log_error(...) do { \ |
| 80 | if (likely(__pg_log_level <= PG_LOG_ERROR)) pg_log_generic(PG_LOG_ERROR, __VA_ARGS__); \ |
| 81 | } while(0) |
| 82 | |
| 83 | #define pg_log_warning(...) do { \ |
| 84 | if (likely(__pg_log_level <= PG_LOG_WARNING)) pg_log_generic(PG_LOG_WARNING, __VA_ARGS__); \ |
| 85 | } while(0) |
| 86 | |
| 87 | #define pg_log_info(...) do { \ |
| 88 | if (likely(__pg_log_level <= PG_LOG_INFO)) pg_log_generic(PG_LOG_INFO, __VA_ARGS__); \ |
| 89 | } while(0) |
| 90 | |
| 91 | #define pg_log_debug(...) do { \ |
| 92 | if (unlikely(__pg_log_level <= PG_LOG_DEBUG)) pg_log_generic(PG_LOG_DEBUG, __VA_ARGS__); \ |
| 93 | } while(0) |
| 94 | |
| 95 | #endif /* COMMON_LOGGING_H */ |
| 96 | |