chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / log.c
index c5760d26d6acf7327103cee3c1a4743d4688e230..7c04f23772b3c47cab7e2f9a47c3bd7158aff2cf 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -10,6 +10,18 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.5  2008/02/27 01:31:14  james
+ * *** empty log message ***
+ *
+ * Revision 1.4  2008/02/27 00:54:16  james
+ * *** empty log message ***
+ *
+ * Revision 1.3  2008/02/23 11:48:37  james
+ * *** empty log message ***
+ *
+ * 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 +32,7 @@ static char rcsid[] = "$Id$";
 typedef struct
 {
   LOG_SIGNATURE;
+  int do_close;
   FILE *fp;
 } File_Log;
 
@@ -53,11 +66,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 +83,73 @@ file_log_new (char *fn)
 {
   File_Log *l;
   FILE *f;
+  int dc = 1;
 
-  f = fopen (fn, "a+");
-  if (!f)
-    return NULL;
+  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;
+
+  fput_cp (f, 0xffef);
 
   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;
+    }
+}