]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add fr_log_init_fp()
authorAlan T. DeKok <aland@freeradius.org>
Thu, 19 Jan 2023 14:39:48 +0000 (09:39 -0500)
committerAlan T. DeKok <aland@freeradius.org>
Thu, 19 Jan 2023 14:39:48 +0000 (09:39 -0500)
for pre-existing FILE* handles

src/lib/util/log.c
src/lib/util/log.h

index feca02e5eba420f66b6e8aecafcaea94e47adc99..79712742bf7d3ad8ed576d67ca59a8b4e7b175e0 100644 (file)
@@ -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
  *
  */
index f1624f6df524f273d8b549a480eab709a32f781e..5be480d5b42d29fdcfed693d3ca4664d9f6620e6 100644 (file)
@@ -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)