chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / log.c
index c5760d26d6acf7327103cee3c1a4743d4688e230..65860f38142c68f4c073ecda9fbcee12149dbae2 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -10,6 +10,9 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.2  2008/02/22 14:51:54  james
+ * *** empty log message ***
+ *
  * Revision 1.1  2008/02/14 12:14:50  james
  * *** empty log message ***
  *
@@ -20,6 +23,7 @@ static char rcsid[] = "$Id$";
 typedef struct
 {
   LOG_SIGNATURE;
+  int do_close;
   FILE *fp;
 } File_Log;
 
@@ -53,11 +57,14 @@ flog_log (Log * _l, char *buf)
   fflush (l->fp);
 }
 
+
+
+
 static void
 flog_close (Log * _l)
 {
   File_Log *l = (File_Log *) _l;
-  if (l->fp)
+  if (l->fp && l->do_close)
     fclose (l->fp);
   free (l);
 }
@@ -67,16 +74,64 @@ file_log_new (char *fn)
 {
   File_Log *l;
   FILE *f;
+  int dc=1;
 
+  if (fn && strcmp(fn,"-")) {
   f = fopen (fn, "a+");
   if (!f)
     return NULL;
+  } else {
+       f=stderr;
+       dc=0;
+  }
 
   l = malloc (sizeof (File_Log));
 
   l->log = flog_log;
   l->close = flog_close;
   l->fp = f;
+  l->do_close=dc;
 
   return (Log *) l;
 }
+void 
+log_f (Log *log,char *fmt, ...)
+{
+
+  int n;
+  static char *buf;
+  va_list ap;
+  static int size;
+
+  if (!log) return;
+
+  if (!size) {
+  size = 128;
+  buf = malloc (size);
+  }
+
+  if (!buf)
+    return;
+
+  while (1)
+    {
+      va_start (ap, fmt);
+      n = vsnprintf (buf , size, fmt, ap);
+      va_end (ap);
+
+      if (n > -1 && n < size) {
+       log->log(log,buf);
+       return; 
+      }
+
+      if (n > -1)               /* glibc 2.1 */
+        size = n + 1;
+      else                      /* glibc 2.0 */
+        size *= 2;              /* twice the old size */
+
+      buf = realloc (buf, size);
+
+      if (!buf)
+       return;
+    }
+}