From 9aa671e874bcd0b3d05cb9e74e3c80d96422e620 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 13 Feb 2009 19:32:48 +0100 Subject: [PATCH] Logging: Resurrected the log_file() interface. It is still useful for programs that use just a single log file. It creates a new file stream and connects it as a substream of stream #0, replacing the previous substreams. Also keep fd #2 as a duplicate of the log file fd, so that errors of exec'd programs will not be lost. --- ucw/log-file.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ucw/log-file.c b/ucw/log-file.c index fe85a270..015b8df2 100644 --- a/ucw/log-file.c +++ b/ucw/log-file.c @@ -12,6 +12,7 @@ #include "ucw/log.h" #include "ucw/lfs.h" #include "ucw/threads.h" +#include "ucw/simple-lists.h" #include #include @@ -95,13 +96,25 @@ do_log_switch(struct log_stream *ls, struct tm *tm) return switched; } +/* Emulate the old single-file interface: close the existing log file and open a new one. */ void log_file(const char *name) { if (!name) return; - // FIXME + struct log_stream *ls = log_new_file(name); + struct log_stream *def = log_stream_by_flags(0); + simp_node *s; + while (s = clist_head(&def->substreams)) + { + struct log_stream *old = s->p; + log_rm_substream(def, old); + if (old != (struct log_stream *) &log_stream_default) + log_close_stream(old); + } + dup2(ls->idata, 2); // Let fd2 be an alias for the log file + log_add_substream(def, ls); } int @@ -196,11 +209,11 @@ log_new_file(const char *path) int main(int argc, char **argv) { log_init(argv[0]); - // log_file("/proc/self/fd/1"); + log_file("/proc/self/fd/1"); // struct log_stream *ls = log_new_fd(1); - struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S"); + // struct log_stream *ls = log_new_file("/tmp/quork-%Y%m%d-%H%M%S"); for (int i=1; iregnum, argv[i]); + msg(L_INFO, argv[i]); return 0; } -- 2.39.2