From 6a4ddd24b8a07f6d324e7bfcd415ca0e9295a45d Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 31 Mar 2026 14:01:15 +0200 Subject: [PATCH v20 1/4] Add log file support to logging.c This adds the ability for users of logging.c to provide a file handle for a log file, where log messages are also written in addition to stderr. --- src/common/logging.c | 27 +++++++++++++++++++++++++++ src/include/common/logging.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/src/common/logging.c b/src/common/logging.c index 5206949e5d8..4a69d96281b 100644 --- a/src/common/logging.c +++ b/src/common/logging.c @@ -26,6 +26,8 @@ static int log_flags; static void (*log_pre_callback) (void); static void (*log_locus_callback) (const char **, uint64 *); +static FILE *log_logfile; + static const char *sgr_error = NULL; static const char *sgr_warning = NULL; static const char *sgr_note = NULL; @@ -204,6 +206,18 @@ pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno) log_locus_callback = cb; } +void +pg_logging_set_logfile(FILE *logfile) +{ + log_logfile = logfile; +} + +void +pg_logging_unset_logfile(void) +{ + log_logfile = NULL; +} + void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...) @@ -277,6 +291,8 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, if (sgr_error) fprintf(stderr, ANSI_ESCAPE_FMT, sgr_error); fprintf(stderr, _("error: ")); + if (log_logfile) + fprintf(log_logfile, _("error: ")); if (sgr_error) fprintf(stderr, ANSI_ESCAPE_RESET); break; @@ -284,6 +300,8 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, if (sgr_warning) fprintf(stderr, ANSI_ESCAPE_FMT, sgr_warning); fprintf(stderr, _("warning: ")); + if (log_logfile) + fprintf(log_logfile, _("warning: ")); if (sgr_warning) fprintf(stderr, ANSI_ESCAPE_RESET); break; @@ -295,6 +313,8 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, if (sgr_note) fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note); fprintf(stderr, _("detail: ")); + if (log_logfile) + fprintf(log_logfile, _("detail: ")); if (sgr_note) fprintf(stderr, ANSI_ESCAPE_RESET); break; @@ -302,6 +322,8 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, if (sgr_note) fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note); fprintf(stderr, _("hint: ")); + if (log_logfile) + fprintf(log_logfile, _("hint: ")); if (sgr_note) fprintf(stderr, ANSI_ESCAPE_RESET); break; @@ -332,6 +354,11 @@ pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, buf[required_len - 2] = '\0'; fprintf(stderr, "%s\n", buf); + if (log_logfile) + { + fprintf(log_logfile, "%s\n", buf); + fflush(log_logfile); + } free(buf); } diff --git a/src/include/common/logging.h b/src/include/common/logging.h index bccba4ac07b..06c202dbe2d 100644 --- a/src/include/common/logging.h +++ b/src/include/common/logging.h @@ -91,6 +91,8 @@ void pg_logging_set_level(enum pg_log_level new_level); void pg_logging_increase_verbosity(void); void pg_logging_set_pre_callback(void (*cb) (void)); void pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno)); +void pg_logging_set_logfile(FILE *logfile); +void pg_logging_unset_logfile(void); void pg_log_generic(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt,...) -- 2.53.0