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