chiark / gitweb /
Various manual fixes.
[mLib] / trace.c
diff --git a/trace.c b/trace.c
index f67c9cb5ed98b9acde3dc728fbcb082195062a06..f7ef7674b93319053b7b8b8c677708ac615e9dc6 100644 (file)
--- a/trace.c
+++ b/trace.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: trace.c,v 1.2 1999/05/05 18:50:31 mdw Exp $
+ * $Id: trace.c,v 1.8 2004/04/08 01:36:13 mdw Exp $
  *
  * Tracing functions for debugging
  *
  * GNU Library General Public License for more details.
  * 
  * You should have received a copy of the GNU Library General Public
- * License along with mLib; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: trace.c,v $
- * Revision 1.2  1999/05/05 18:50:31  mdw
- * Change licensing conditions to LGPL.
- *
- * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
- * Initial version of mLib
- *
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
  */
 
 /*----- Header files ------------------------------------------------------*/
 
 /* --- Local headers --- */
 
+#include "dstr.h"
 #include "quis.h"
 #include "trace.h"
 
 /*----- Private state information -----------------------------------------*/
 
-static FILE *trace__fp = 0;            /* Where does debugging go? */
-static unsigned int trace__lvl = 0;    /* How much tracing gets done? */
+static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
+static void *tracearg;
+static unsigned tracelvl = 0;          /* How much tracing gets done? */
 
 /*----- Functions provided ------------------------------------------------*/
 
+/* --- @t_file@ --- *
+ *
+ * Arguments:  @const char *buf@ = buffer to print
+ *             @size_t sz@ = buffer size
+ *             @void *v@ = file handle
+ *
+ * Returns:    ---
+ *
+ * Use:                Dumps tracing information to a file.
+ */
+
+static void t_file(const char *buf, size_t sz, void *v)
+{
+  FILE *fp = v;
+  fprintf(fp, "+ %s: ", QUIS);
+  fwrite(buf, 1, sz, fp);
+  fputc('\n', fp);
+}
+
 /* --- @trace@ --- *
  *
- * Arguments:  @unsigned int l@ = trace level for output
+ * Arguments:  @unsigned l@ = trace level for output
  *             @const char *f@ = a @printf@-style format string
  *             @...@ = other arguments
  *
@@ -70,22 +81,22 @@ static unsigned int trace__lvl = 0; /* How much tracing gets done? */
  * Use:                Reports a message to the trace output.
  */
 
-void trace(unsigned int l, const char *f, ...)
+void trace(unsigned l, const char *f, ...)
 {
   va_list ap;
+  dstr d = DSTR_INIT;
   if ((l & tracing()) == 0)
     return;
   va_start(ap, f);
-  fprintf(trace__fp, "*** %s: ", QUIS);
-  vfprintf(trace__fp, f, ap);
+  dstr_vputf(&d, f, &ap);
   va_end(ap);
-  putc('\n', trace__fp);
-  fflush(trace__fp);
+  tracefunc(d.buf, d.len, tracearg);
+  dstr_destroy(&d);
 }
 
 /* --- @trace_block@ --- *
  *
- * Arguments:  @unsigned int l@ = trace level for output
+ * Arguments:  @unsigned l@ = trace level for output
  *             @const char *s@ = some header string to write
  *             @const void *b@ = pointer to a block of memory to dump
  *             @size_t sz@ = size of the block of memory
@@ -95,11 +106,12 @@ void trace(unsigned int l, const char *f, ...)
  * Use:                Dumps the contents of a block to the trace output.
  */
 
-void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
+void trace_block(unsigned l, const char *s, const void *b, size_t sz)
 {
   const unsigned char *p = b;
   size_t i;
   unsigned long o = 0;
+  dstr d = DSTR_INIT;
   size_t c;
 
   /* --- Skip if the trace level is too high --- */
@@ -109,59 +121,80 @@ void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
 
   /* --- Now start work --- */
 
-  fprintf(trace__fp, "*** %s: %s\n", QUIS, s);
-
+  tracefunc(s, strlen(s), tracearg);
   while (sz) {
-    fprintf(trace__fp, "*** %s:   %08lx : ", QUIS, o);
+    dstr_reset(&d);
+    dstr_putf(&d, "   %08lx : ", o);
     for (i = 0; i < 8; i++) {
       if (i < sz)
-       fprintf(trace__fp, "%02x ", p[i]);
+       dstr_putf(&d, "%02x ", p[i]);
       else
-       fputs("** ", trace__fp);
+       dstr_puts(&d, "** ");
     }
-    fputs(": ", trace__fp);
+    dstr_puts(&d, ": ");
     for (i = 0; i < 8; i++) {
       if (i < sz)
-       fputc(isprint(p[i]) ? p[i] : '.', trace__fp);
+       dstr_putc(&d, isprint(p[i]) ? p[i] : '.');
       else
-       fputc('*', trace__fp);
+       dstr_putc(&d, '*');
     }
-    fputc('\n', trace__fp);
+    dstr_putz(&d);
+    tracefunc(d.buf, d.len, tracearg);
     c = (sz >= 8) ? 8 : sz;
     sz -= c, p += c, o += c;
   }
-  fflush(trace__fp);
+  dstr_destroy(&d);
 }
 
 /* --- @trace_on@ --- *
  *
  * Arguments:  @FILE *fp@ = a file to trace on
- *             @unsigned int l@ = trace level to set
+ *             @unsigned l@ = trace level to set
  *
  * Returns:    ---
  *
  * Use:                Enables tracing to a file.
  */
 
-void trace_on(FILE *fp, unsigned int l)
+void trace_on(FILE *fp, unsigned l)
+{
+  tracefunc = t_file;
+  tracearg = fp;
+  if (!tracelvl)
+    tracelvl = l;
+}
+
+/* --- @trace_custom@ --- *
+ *
+ * Arguments:  @void (*func)(const char *buf, size_t sz, void *v)@ =
+ *                     output function
+ *             @void *v@ = magic handle to give to function
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets up a custom trace handler.
+ */
+
+void trace_custom(void (*func)(const char */*buf*/,
+                              size_t /*sz*/, void */*v*/),
+                 void *v)
 {
-  trace__fp = fp;
-  if (!trace__lvl)
-    trace__lvl = l;
+  tracefunc = func;
+  tracearg = v;
 }
 
-/* --- @trace_setLevel@ --- *
+/* --- @trace_level@ --- *
  *
- * Arguments:  @unsigned int l@ = trace level to set
+ * Arguments:  @unsigned l@ = trace level to set
  *
  * Returns:    ---
  *
  * Use:                Sets the tracing level.
  */
 
-void trace_setLevel(unsigned int l)
+void trace_level(unsigned l)
 {
-  trace__lvl = l;
+  tracelvl = l;
 }
 
 /* --- @tracing@ --- *
@@ -173,9 +206,9 @@ void trace_setLevel(unsigned int l)
  * Use:                Informs the caller whether tracing is enabled.
  */
 
-unsigned int tracing(void)
+unsigned tracing(void)
 {
-  return (trace__fp ? trace__lvl : 0u);
+  return (tracefunc ? tracelvl : 0u);
 }
 
 /*----- That's all, folks -------------------------------------------------*/