]> mj.ucw.cz Git - libucw.git/commitdiff
Replaced safe_msg() with a flag L_SIGHANDLER to msg().
authorMartin Mares <mj@ucw.cz>
Wed, 4 Jun 2008 17:00:46 +0000 (19:00 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 4 Jun 2008 17:00:46 +0000 (19:00 +0200)
Updated all callers.

lib/lib.h
lib/log.c

index 6fdefd642adc5cc0f0a4229df30f39761953ca82..1bd0b35402247b9f197ed6183ece2b43793db3ca 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -1,7 +1,7 @@
 /*
  *     The UCW Library -- Miscellaneous Functions
  *
- *     (c) 1997--2007 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2008 Martin Mares <mj@ucw.cz>
  *     (c) 2005 Tomas Valla <tom@ucw.cz>
  *     (c) 2006 Robert Spalek <robert@ucw.cz>
  *     (c) 2007 Pavel Charvat <pchar@ucw.cz>
@@ -90,6 +90,8 @@
 #define L_ERROR_R      'e'
 #define L_FATAL                '!'             /* die() */
 
+#define L_SIGHANDLER   0x10000         /* Avoid things which are unsafe in signal handlers */
+
 extern char *log_title;                        /* NULL - print no title, default is log_progname */
 extern char *log_filename;             /* Expanded name of the current log file */
 extern volatile int log_switch_nest;   /* log_switch() nesting counter, increment to disable automatic switches */
@@ -101,8 +103,6 @@ extern void (*log_switch_hook)(struct tm *tm);
 
 void msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3);
 void vmsg(uns cat, const char *fmt, va_list args);
-void safe_msg(uns cat, const char *fmt, ...) FORMAT_CHECK(printf,2,3);
-void safe_vmsg(uns cat, const char *fmt, va_list args);
 void die(const char *, ...) NONRET FORMAT_CHECK(printf,1,2);
 void log_init(const char *argv0);
 void log_file(const char *name);
index 4d91b315bec14b43f40ae8d903bb3aecbc96efcc..f3ab51dbfa0dd5d651e1bc637c3b0cb54b075891 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- Logging
  *
- *     (c) 1997--2006 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2008 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -29,88 +29,39 @@ void
 vmsg(unsigned int cat, const char *fmt, va_list args)
 {
   struct timeval tv;
+  int have_tm = 0;
   struct tm tm;
   byte *buf, *p;
   int buflen = 256;
   int l, l0, r;
   va_list args2;
 
-  gettimeofday(&tv, NULL);
-  if (!localtime_r(&tv.tv_sec, &tm))
-    bzero(&tm, sizeof(tm));
+  if (!(cat & L_SIGHANDLER))
+    {
+      /* CAVEAT: These calls are not safe in signal handlers. */
+      gettimeofday(&tv, NULL);
+      if (localtime_r(&tv.tv_sec, &tm))
+       have_tm = 1;
+      if (log_switch_hook)
+       log_switch_hook(&tm);
+    }
 
-  if (log_switch_hook)
-    log_switch_hook(&tm);
   while (1)
     {
       p = buf = alloca(buflen);
-      *p++ = cat;
-      /* We cannot use strftime() here, because it's not re-entrant */
-      p += sprintf(p, " %4d-%02d-%02d %02d:%02d:%02d", tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
-                  tm.tm_hour, tm.tm_min, tm.tm_sec);
-      if (log_precise_timings)
-        p += sprintf(p, ".%06d", (int)tv.tv_usec);
-      *p++ = ' ';
-      if (log_title)
+      *p++ = cat & 0xff;
+      if (have_tm)
        {
-         if (log_pid)
-           p += sprintf(p, "[%s (%d)] ", log_title, log_pid);
-         else
-           p += sprintf(p, "[%s] ", log_title);
+         p += strftime(p, buflen, " %Y-%m-%d %H:%M:%S", &tm);
+         if (log_precise_timings)
+           p += sprintf(p, ".%06d", (int)tv.tv_usec);
        }
       else
        {
-         if (log_pid)
-           p += sprintf(p, "[%d] ", log_pid);
+         p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
+         if (log_precise_timings)
+           p += sprintf(p, ".\?\?\?\?\?\?");
        }
-      l0 = p - buf + 1;
-      r = buflen - l0;
-      va_copy(args2, args);
-      l = vsnprintf(p, r, fmt, args2);
-      va_end(args2);
-      if (l < 0)
-       l = r;
-      else if (l < r)
-       {
-         while (*p)
-           {
-             if (*p < 0x20 && *p != '\t')
-               *p = 0x7f;
-             p++;
-           }
-         *p = '\n';
-         write(2, buf, l + l0);
-         return;
-       }
-      buflen = l + l0 + 1;
-    }
-}
-
-void
-msg(unsigned int cat, const char *fmt, ...)
-{
-  va_list args;
-
-  va_start(args, fmt);
-  vmsg(cat, fmt, args);
-  va_end(args);
-}
-
-void
-safe_vmsg(unsigned int cat, const char *fmt, va_list args)
-{
-  byte *buf, *p;
-  int buflen = 256;
-  int l, l0, r;
-  va_list args2;
-
-  while (1)
-    {
-      p = buf = alloca(buflen);
-      *p++ = cat;
-      p += sprintf(p, " \?\?\?\?-\?\?-\?\? \?\?:\?\?:\?\?");
-      if (log_precise_timings)
-        p += sprintf(p, ".\?\?\?\?\?\?");
       *p++ = ' ';
       if (log_title)
        {
@@ -148,12 +99,12 @@ safe_vmsg(unsigned int cat, const char *fmt, va_list args)
 }
 
 void
-safe_msg(unsigned int cat, const char *fmt, ...)
+msg(unsigned int cat, const char *fmt, ...)
 {
   va_list args;
 
   va_start(args, fmt);
-  safe_vmsg(cat, fmt, args);
+  vmsg(cat, fmt, args);
   va_end(args);
 }