utils/macros.h: Add <ctype.h> and `foocmp' helper macros.
[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 #include <stdlib.h>
36 #include <string.h>
37
38 /* --- Local headers --- */
39
40 #include "dstr.h"
41 #include "macros.h"
42 #include "quis.h"
43 #include "trace.h"
44
45 /*----- Private state information -----------------------------------------*/
46
47 static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
48 static void *tracearg;
49 static unsigned tracelvl = 0; /* How much tracing gets done? */
50
51 /*----- Functions provided ------------------------------------------------*/
52
53 /* --- @t_file@ --- *
54 *
55 * Arguments: @const char *buf@ = buffer to print
56 * @size_t sz@ = buffer size
57 * @void *v@ = file handle
58 *
59 * Returns: ---
60 *
61 * Use: Dumps tracing information to a file.
62 */
63
64 static void t_file(const char *buf, size_t sz, void *v)
65 {
66 FILE *fp = v;
67 fprintf(fp, "+ %s: ", QUIS);
68 fwrite(buf, 1, sz, fp);
69 fputc('\n', fp);
70 }
71
72 /* --- @trace@ --- *
73 *
74 * Arguments: @unsigned l@ = trace level for output
75 * @const char *f@ = a @printf@-style format string
76 * @...@ = other arguments
77 *
78 * Returns: ---
79 *
80 * Use: Reports a message to the trace output.
81 */
82
83 void trace(unsigned l, const char *f, ...)
84 {
85 va_list ap;
86 dstr d = DSTR_INIT;
87 if ((l & tracing()) == 0)
88 return;
89 va_start(ap, f);
90 dstr_vputf(&d, f, &ap);
91 va_end(ap);
92 tracefunc(d.buf, d.len, tracearg);
93 dstr_destroy(&d);
94 }
95
96 /* --- @trace_block@ --- *
97 *
98 * Arguments: @unsigned l@ = trace level for output
99 * @const char *s@ = some header string to write
100 * @const void *b@ = pointer to a block of memory to dump
101 * @size_t sz@ = size of the block of memory
102 *
103 * Returns: ---
104 *
105 * Use: Dumps the contents of a block to the trace output.
106 */
107
108 void trace_block(unsigned l, const char *s, const void *b, size_t sz)
109 {
110 const unsigned char *p = b;
111 size_t i;
112 unsigned long o = 0;
113 dstr d = DSTR_INIT;
114 size_t c;
115
116 /* --- Skip if the trace level is too high --- */
117
118 if ((l & tracing()) == 0)
119 return;
120
121 /* --- Now start work --- */
122
123 tracefunc(s, strlen(s), tracearg);
124 while (sz) {
125 dstr_reset(&d);
126 dstr_putf(&d, " %08lx : ", o);
127 for (i = 0; i < 8; i++) {
128 if (i < sz)
129 dstr_putf(&d, "%02x ", p[i]);
130 else
131 dstr_puts(&d, "** ");
132 }
133 dstr_puts(&d, ": ");
134 for (i = 0; i < 8; i++) {
135 if (i < sz)
136 dstr_putc(&d, ISPRINT(p[i]) ? p[i] : '.');
137 else
138 dstr_putc(&d, '*');
139 }
140 dstr_putz(&d);
141 tracefunc(d.buf, d.len, tracearg);
142 c = (sz >= 8) ? 8 : sz;
143 sz -= c, p += c, o += c;
144 }
145 dstr_destroy(&d);
146 }
147
148 /* --- @trace_on@ --- *
149 *
150 * Arguments: @FILE *fp@ = a file to trace on
151 * @unsigned l@ = trace level to set
152 *
153 * Returns: ---
154 *
155 * Use: Enables tracing to a file.
156 */
157
158 void trace_on(FILE *fp, unsigned l)
159 {
160 tracefunc = t_file;
161 tracearg = fp;
162 if (!tracelvl)
163 tracelvl = l;
164 }
165
166 /* --- @trace_custom@ --- *
167 *
168 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
169 * output function
170 * @void *v@ = magic handle to give to function
171 *
172 * Returns: ---
173 *
174 * Use: Sets up a custom trace handler.
175 */
176
177 void trace_custom(void (*func)(const char */*buf*/,
178 size_t /*sz*/, void */*v*/),
179 void *v)
180 {
181 tracefunc = func;
182 tracearg = v;
183 }
184
185 /* --- @trace_level@ --- *
186 *
187 * Arguments: @unsigned l@ = trace level to set
188 *
189 * Returns: ---
190 *
191 * Use: Sets the tracing level.
192 */
193
194 void trace_level(unsigned l)
195 {
196 tracelvl = l;
197 }
198
199 /* --- @tracing@ --- *
200 *
201 * Arguments: ---
202 *
203 * Returns: Zero if not tracing, tracing level if tracing.
204 *
205 * Use: Informs the caller whether tracing is enabled.
206 */
207
208 unsigned tracing(void)
209 {
210 return (tracefunc ? tracelvl : 0u);
211 }
212
213 /*----- That's all, folks -------------------------------------------------*/