]> mj.ucw.cz Git - libucw.git/blobdiff - lib/log.c
Fix escaping of "+" characters in outgoing parameters. (BTW: when Galeon
[libucw.git] / lib / log.c
index bc74de5b93e30e250b5b345e7d4b699e794e3639..bcbce64b6115e010059e1788e16070f3f367e63a 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -2,6 +2,9 @@
  *     Sherlock Library -- Logging
  *
  *     (c) 1997--2001 Martin Mares <mj@ucw.cz>
  *     Sherlock Library -- Logging
  *
  *     (c) 1997--2001 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
 #include "lib/lib.h"
  */
 
 #include "lib/lib.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <sys/time.h>
+#include <time.h>
 
 
-static byte *log_progname;
+static char *log_progname, log_name_patt[64], log_name[64];
 static pid_t log_pid;
 static pid_t log_pid;
+static int log_params;
 
 void
 log_fork(void)
 {
 
 void
 log_fork(void)
 {
-  log_pid = 0;
+  log_pid = getpid();
+}
+
+static void
+log_switch(struct tm *tm)
+{
+  int fd;
+  char name[64];
+
+  if (!log_name_patt[0] ||
+      log_name[0] && !log_params)
+    return;
+  strftime(name, sizeof(name), log_name_patt, tm);
+  if (!strcmp(name, log_name))
+    return;
+  strcpy(log_name, name);
+  fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
+  if (fd < 0)
+    die("Unable to open log file %s: %m", name);
+  close(2);
+  dup(fd);
+  close(fd);
 }
 
 static void
 }
 
 static void
@@ -29,12 +55,21 @@ vlog(unsigned int cat, const char *msg, va_list args)
   struct tm *tm = localtime(&tim);
   char buf[32];
 
   struct tm *tm = localtime(&tim);
   char buf[32];
 
-  if (!log_pid)
-    log_pid = getpid();
+  log_switch(tm);
   strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
   fprintf(stderr, "%c %s ", cat, buf);
   if (log_progname)
   strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
   fprintf(stderr, "%c %s ", cat, buf);
   if (log_progname)
-    fprintf(stderr, "[%s (%d)] ", log_progname, log_pid);
+    {
+      if (log_pid)
+       fprintf(stderr, "[%s (%d)] ", log_progname, log_pid);
+      else
+       fprintf(stderr, "[%s] ", log_progname);
+    }
+  else
+    {
+      if (log_pid)
+       fprintf(stderr, "[%d] ", log_pid);
+    }
   vfprintf(stderr, msg, args);
   fputc('\n', stderr);
   fflush(stderr);
   vfprintf(stderr, msg, args);
   fputc('\n', stderr);
   fflush(stderr);
@@ -61,8 +96,23 @@ die(byte *msg, ...)
   exit(1);
 }
 
   exit(1);
 }
 
+#ifdef DEBUG
+void
+assert_failed(char *assertion, char *file, int line)
+{
+  log(L_FATAL, "Assertion `%s' failed at %s:%d", assertion, file, line);
+  abort();
+}
+#else
+void
+assert_failed(void)
+{
+  die("Internal error: Assertion failed.");
+}
+#endif
+
 static byte *
 static byte *
-basename(byte *n)
+log_basename(byte *n)
 {
   byte *p = n;
 
 {
   byte *p = n;
 
@@ -76,7 +126,7 @@ void
 log_init(byte *argv0)
 {
   if (argv0)
 log_init(byte *argv0)
 {
   if (argv0)
-    log_progname = basename(argv0);
+    log_progname = log_basename(argv0);
 }
 
 void
 }
 
 void
@@ -84,11 +134,11 @@ log_file(byte *name)
 {
   if (name)
     {
 {
   if (name)
     {
-      int fd = open(name, O_WRONLY | O_CREAT | O_APPEND, 0666);
-      if (fd < 0)
-       die("Unable to open log file %s: %m", name);
-      close(2);
-      dup(fd);
-      close(fd);
+      time_t tim = time(NULL);
+      struct tm *tm = localtime(&tim);
+      strcpy(log_name_patt, name);
+      log_params = !!strchr(name, '%');
+      log_name[0] = 0;
+      log_switch(tm);
     }
 }
     }
 }