From: Alan T. DeKok Date: Thu, 19 Jan 2023 14:39:48 +0000 (-0500) Subject: add fr_log_init_fp() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7ed1cd5e67c422338e405ca1db12a79d8291c80d;p=thirdparty%2Ffreeradius-server.git add fr_log_init_fp() for pre-existing FILE* handles --- diff --git a/src/lib/util/log.c b/src/lib/util/log.c index feca02e5eba..79712742bf7 100644 --- a/src/lib/util/log.c +++ b/src/lib/util/log.c @@ -1080,24 +1080,20 @@ int fr_log_init_std(fr_log_t *log, fr_log_dst_t dst_type) return 0; } -/** Initialise a file logging destination +/** Initialise a file logging destination to a FILE* * * @param[out] log Destination to initialise. - * @param[in] file to open handle for. + * @param[in] fp pre-existing handle * @return * - 0 on success. * - -1 on failure. */ -int fr_log_init_file(fr_log_t *log, char const *file) +int fr_log_init_fp(fr_log_t *log, FILE *fp) { memset(log, 0, sizeof(*log)); log->dst = L_DST_FILES; - - if (unlikely((log->handle = fopen(file, "a")) == NULL)) { - fr_strerror_printf("Failed opening log file \"%s\": %s", file, fr_syserror(errno)); - return -1; - } + log->handle = fp; setlinebuf(log->handle); log->fd = fileno(log->handle); @@ -1105,6 +1101,26 @@ int fr_log_init_file(fr_log_t *log, char const *file) return 0; } +/** Initialise a file logging destination + * + * @param[out] log Destination to initialise. + * @param[in] file to open handle for. + * @return + * - 0 on success. + * - -1 on failure. + */ +int fr_log_init_file(fr_log_t *log, char const *file) +{ + FILE *fp; + + if (unlikely((fp = fopen(file, "a")) == NULL)) { + fr_strerror_printf("Failed opening log file \"%s\": %s", file, fr_syserror(errno)); + return -1; + } + + return fr_log_init_fp(log, fp); +} + /** Write complete lines to syslog * */ diff --git a/src/lib/util/log.h b/src/lib/util/log.h index f1624f6df52..5be480d5b42 100644 --- a/src/lib/util/log.h +++ b/src/lib/util/log.h @@ -182,6 +182,8 @@ int fr_log_init_std(fr_log_t *log, fr_log_dst_t dst_type) CC_HINT(nonnull); int fr_log_init_file(fr_log_t *log, char const *file) CC_HINT(nonnull); +int fr_log_init_fp(fr_log_t *log, FILE *fp) CC_HINT(nonnull); + int fr_log_init_syslog(fr_log_t *log) CC_HINT(nonnull); int fr_log_init_func(fr_log_t *log, cookie_write_function_t write, cookie_close_function_t close, void *uctx)