chiark / gitweb /
debian: Clean noadns build directory.
[mLib] / trace.c
1 /* -*-c-*-
2  *
3  * $Id: trace.c,v 1.8 2004/04/08 01:36:13 mdw Exp $
4  *
5  * Tracing functions for debugging
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
14  * mLib is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU Library General Public License as
16  * published by the Free Software Foundation; either version 2 of the
17  * License, or (at your option) any later version.
18  * 
19  * mLib is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Library General Public License for more details.
23  * 
24  * You should have received a copy of the GNU Library General Public
25  * License along with mLib; if not, write to the Free
26  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Header files ------------------------------------------------------*/
31
32 /* --- ANSI headers --- */
33
34 #include <ctype.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 /* --- Local headers --- */
41
42 #include "dstr.h"
43 #include "quis.h"
44 #include "trace.h"
45
46 /*----- Private state information -----------------------------------------*/
47
48 static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
49 static void *tracearg;
50 static unsigned tracelvl = 0;           /* How much tracing gets done? */
51
52 /*----- Functions provided ------------------------------------------------*/
53
54 /* --- @t_file@ --- *
55  *
56  * Arguments:   @const char *buf@ = buffer to print
57  *              @size_t sz@ = buffer size
58  *              @void *v@ = file handle
59  *
60  * Returns:     ---
61  *
62  * Use:         Dumps tracing information to a file.
63  */
64
65 static void t_file(const char *buf, size_t sz, void *v)
66 {
67   FILE *fp = v;
68   fprintf(fp, "+ %s: ", QUIS);
69   fwrite(buf, 1, sz, fp);
70   fputc('\n', fp);
71 }
72
73 /* --- @trace@ --- *
74  *
75  * Arguments:   @unsigned l@ = trace level for output
76  *              @const char *f@ = a @printf@-style format string
77  *              @...@ = other arguments
78  *
79  * Returns:     ---
80  *
81  * Use:         Reports a message to the trace output.
82  */
83
84 void trace(unsigned l, const char *f, ...)
85 {
86   va_list ap;
87   dstr d = DSTR_INIT;
88   if ((l & tracing()) == 0)
89     return;
90   va_start(ap, f);
91   dstr_vputf(&d, f, &ap);
92   va_end(ap);
93   tracefunc(d.buf, d.len, tracearg);
94   dstr_destroy(&d);
95 }
96
97 /* --- @trace_block@ --- *
98  *
99  * Arguments:   @unsigned l@ = trace level for output
100  *              @const char *s@ = some header string to write
101  *              @const void *b@ = pointer to a block of memory to dump
102  *              @size_t sz@ = size of the block of memory
103  *
104  * Returns:     ---
105  *
106  * Use:         Dumps the contents of a block to the trace output.
107  */
108
109 void trace_block(unsigned l, const char *s, const void *b, size_t sz)
110 {
111   const unsigned char *p = b;
112   size_t i;
113   unsigned long o = 0;
114   dstr d = DSTR_INIT;
115   size_t c;
116
117   /* --- Skip if the trace level is too high --- */
118
119   if ((l & tracing()) == 0)
120     return;
121
122   /* --- Now start work --- */
123
124   tracefunc(s, strlen(s), tracearg);
125   while (sz) {
126     dstr_reset(&d);
127     dstr_putf(&d, "   %08lx : ", o);
128     for (i = 0; i < 8; i++) {
129       if (i < sz)
130         dstr_putf(&d, "%02x ", p[i]);
131       else
132         dstr_puts(&d, "** ");
133     }
134     dstr_puts(&d, ": ");
135     for (i = 0; i < 8; i++) {
136       if (i < sz)
137         dstr_putc(&d, isprint(p[i]) ? p[i] : '.');
138       else
139         dstr_putc(&d, '*');
140     }
141     dstr_putz(&d);
142     tracefunc(d.buf, d.len, tracearg);
143     c = (sz >= 8) ? 8 : sz;
144     sz -= c, p += c, o += c;
145   }
146   dstr_destroy(&d);
147 }
148
149 /* --- @trace_on@ --- *
150  *
151  * Arguments:   @FILE *fp@ = a file to trace on
152  *              @unsigned l@ = trace level to set
153  *
154  * Returns:     ---
155  *
156  * Use:         Enables tracing to a file.
157  */
158
159 void trace_on(FILE *fp, unsigned l)
160 {
161   tracefunc = t_file;
162   tracearg = fp;
163   if (!tracelvl)
164     tracelvl = l;
165 }
166
167 /* --- @trace_custom@ --- *
168  *
169  * Arguments:   @void (*func)(const char *buf, size_t sz, void *v)@ =
170  *                      output function
171  *              @void *v@ = magic handle to give to function
172  *
173  * Returns:     ---
174  *
175  * Use:         Sets up a custom trace handler.
176  */
177
178 void trace_custom(void (*func)(const char */*buf*/,
179                                size_t /*sz*/, void */*v*/),
180                   void *v)
181 {
182   tracefunc = func;
183   tracearg = v;
184 }
185
186 /* --- @trace_level@ --- *
187  *
188  * Arguments:   @unsigned l@ = trace level to set
189  *
190  * Returns:     ---
191  *
192  * Use:         Sets the tracing level.
193  */
194
195 void trace_level(unsigned l)
196 {
197   tracelvl = l;
198 }
199
200 /* --- @tracing@ --- *
201  *
202  * Arguments:   ---
203  *
204  * Returns:     Zero if not tracing, tracing level if tracing.
205  *
206  * Use:         Informs the caller whether tracing is enabled.
207  */
208
209 unsigned tracing(void)
210 {
211   return (tracefunc ? tracelvl : 0u);
212 }
213
214 /*----- That's all, folks -------------------------------------------------*/