/*
* $Log$
+ * 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 ***
*
typedef struct
{
LOG_SIGNATURE;
+ int do_close;
FILE *fp;
} File_Log;
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);
}
{
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;
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;
+ }
+}