]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/log-file.c
Tests: xtypes-test sets an explicit timezone
[libucw.git] / ucw / log-file.c
index 2693269d3378afebaf96ad5a85755d56f6c96c61..2e7a8d26873b306b9734877ed6d2f2497303fae6 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- Logging to Files
  *
- *     (c) 1997--2009 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2015 Martin Mares <mj@ucw.cz>
  *     (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
@@ -32,6 +32,7 @@ struct file_stream {
 #define MAX_EXPAND 64          // Maximum size of expansion of strftime escapes
 
 static int log_switch_nest;
+static bool log_stderr_replaced;
 
 static void
 do_log_reopen(struct file_stream *fs, const char *name)
@@ -43,7 +44,10 @@ do_log_reopen(struct file_stream *fs, const char *name)
     close(fs->fd);
   fs->fd = fd;
   if (fs->flags & FF_FD2_FOLLOWS)
-    dup2(fd, 2);
+    {
+      dup2(fd, 2);
+      log_stderr_replaced = 1;
+    }
   if (fs->ls.name)
     {
       xfree(fs->ls.name);
@@ -115,6 +119,8 @@ log_new_fd(int fd, uint flags)
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
   fs->fd = fd;
+  if (fd == 2)
+    log_stderr_replaced = 1;
   fs->flags = flags;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
@@ -130,10 +136,10 @@ log_new_file(const char *path, uint flags)
   struct log_stream *ls = log_new_stream(sizeof(struct file_stream));
   struct file_stream *fs = (struct file_stream *) ls;
   fs->fd = -1;
+  fs->flags = FF_CLOSE_FD | flags;
   fs->orig_name = xstrdup(path);
   if (strchr(path, '%'))
-    fs->flags = FF_FORMAT_NAME;
-  fs->flags |= FF_CLOSE_FD | flags;
+    fs->flags |= FF_FORMAT_NAME;
   ls->msgfmt = LSFMT_DEFAULT;
   ls->handler = file_handler;
   ls->close = file_close;
@@ -181,6 +187,17 @@ log_file(const char *name)
   log_set_default_stream(log_new_file(name, FF_FD2_FOLLOWS));
 }
 
+void
+log_drop_stderr(void)
+{
+  if (!log_stderr_replaced)
+    {
+      if (dup2(1, 2) < 0)
+       die("Cannot get rid of stderr: %m");
+      log_stderr_replaced = 1;
+    }
+}
+
 #ifdef TEST
 
 int main(int argc, char **argv)