LRN's patch argument order
[oweals/gnunet.git] / src / util / common_logging.c
index 22901bf268bc44b3358b231b9514df55f0b5e0a3..18b03d0ebbd15d90f7202c6bbe45761223594ec2 100644 (file)
@@ -546,6 +546,7 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
   int dirwarn;
   char *fn;
   const char *env_logfile = NULL;
+  int altlog_fd;
 
   min_level = get_type (loglevel);
 #if !defined(GNUNET_CULL_LOGGING)
@@ -569,8 +570,39 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
   if (NULL == fn)
     return GNUNET_SYSERR;
   dirwarn = (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn));
-  altlog = FOPEN (fn, "a");
-  if (altlog == NULL)
+  altlog_fd = OPEN (fn, O_APPEND |
+#if WINDOWS
+                        O_BINARY |
+#endif
+                        O_WRONLY | O_CREAT,
+#if WINDOWS
+                        _S_IREAD | _S_IWRITE
+#else
+                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
+#endif
+                   );
+  if (altlog_fd != -1)
+  {
+    int dup_return;
+    if (GNUNET_stderr != NULL)
+      fclose (GNUNET_stderr);
+    dup_return = dup2 (altlog_fd, 2);
+    close (altlog_fd);
+    if (dup_return != -1)
+    {
+      altlog = fdopen (2, "ab");
+      if (altlog == NULL)
+      {
+        close (2);
+        altlog_fd = -1;
+      }
+    }
+    else
+    {
+      altlog_fd = -1;
+    }
+  }
+  if (altlog_fd == -1)
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "fopen", fn);
     if (dirwarn)
@@ -581,8 +613,6 @@ GNUNET_log_setup (const char *comp, const char *loglevel, const char *logfile)
     return GNUNET_SYSERR;
   }
   GNUNET_free (fn);
-  if (GNUNET_stderr != NULL)
-    fclose (GNUNET_stderr);
   GNUNET_stderr = altlog;
   return GNUNET_OK;
 }