From: Martin Mares Date: Fri, 13 Feb 2009 18:32:48 +0000 (+0100) Subject: Logging: Resurrected the log_file() interface. X-Git-Tag: holmes-import~97 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=9aa671e874bcd0b3d05cb9e74e3c80d96422e620;p=libucw.git 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. --- 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; }