]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Resurrected the log_file() interface.
authorMartin Mares <mj@ucw.cz>
Fri, 13 Feb 2009 18:32:48 +0000 (19:32 +0100)
committerMartin Mares <mj@ucw.cz>
Fri, 13 Feb 2009 18:32:48 +0000 (19:32 +0100)
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

index fe85a270f94b1624090cda009c2d418b57c0c19b..015b8df2075700621e46b6ee2efb8144f3224317 100644 (file)
@@ -12,6 +12,7 @@
 #include "ucw/log.h"
 #include "ucw/lfs.h"
 #include "ucw/threads.h"
+#include "ucw/simple-lists.h"
 
 #include <stdio.h>
 #include <string.h>
@@ -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; i<argc; i++)
-    msg(L_INFO | ls->regnum, argv[i]);
+    msg(L_INFO, argv[i]);
   return 0;
 }