utils/macros.h: Add <ctype.h> and `foocmp' helper macros.
[mLib] / trace / trace.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
0875b58f 3 * Tracing functions for debugging
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
0875b58f 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
d4efbcd9 16 *
0875b58f 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
c846879c 20 * GNU Library General Public License for more details.
d4efbcd9 21 *
c846879c 22 * You should have received a copy of the GNU Library General Public
0bd98442 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.
0875b58f 26 */
27
0875b58f 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
e4452aaa 40#include "dstr.h"
36188114 41#include "macros.h"
0875b58f 42#include "quis.h"
43#include "trace.h"
44
45/*----- Private state information -----------------------------------------*/
46
e4452aaa 47static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
48static void *tracearg;
de3ceebe 49static unsigned tracelvl = 0; /* How much tracing gets done? */
0875b58f 50
51/*----- Functions provided ------------------------------------------------*/
52
e4452aaa 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
64static 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
0875b58f 72/* --- @trace@ --- *
73 *
de3ceebe 74 * Arguments: @unsigned l@ = trace level for output
0875b58f 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
de3ceebe 83void trace(unsigned l, const char *f, ...)
0875b58f 84{
85 va_list ap;
e4452aaa 86 dstr d = DSTR_INIT;
0875b58f 87 if ((l & tracing()) == 0)
88 return;
89 va_start(ap, f);
7d0dd9ff 90 dstr_vputf(&d, f, &ap);
0875b58f 91 va_end(ap);
e4452aaa 92 tracefunc(d.buf, d.len, tracearg);
93 dstr_destroy(&d);
0875b58f 94}
95
96/* --- @trace_block@ --- *
97 *
de3ceebe 98 * Arguments: @unsigned l@ = trace level for output
0875b58f 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
de3ceebe 108void trace_block(unsigned l, const char *s, const void *b, size_t sz)
0875b58f 109{
110 const unsigned char *p = b;
111 size_t i;
112 unsigned long o = 0;
e4452aaa 113 dstr d = DSTR_INIT;
0875b58f 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
e4452aaa 123 tracefunc(s, strlen(s), tracearg);
0875b58f 124 while (sz) {
e4452aaa 125 dstr_reset(&d);
126 dstr_putf(&d, " %08lx : ", o);
0875b58f 127 for (i = 0; i < 8; i++) {
128 if (i < sz)
e4452aaa 129 dstr_putf(&d, "%02x ", p[i]);
0875b58f 130 else
e4452aaa 131 dstr_puts(&d, "** ");
0875b58f 132 }
e4452aaa 133 dstr_puts(&d, ": ");
0875b58f 134 for (i = 0; i < 8; i++) {
135 if (i < sz)
36188114 136 dstr_putc(&d, ISPRINT(p[i]) ? p[i] : '.');
0875b58f 137 else
e4452aaa 138 dstr_putc(&d, '*');
0875b58f 139 }
e4452aaa 140 dstr_putz(&d);
141 tracefunc(d.buf, d.len, tracearg);
0875b58f 142 c = (sz >= 8) ? 8 : sz;
143 sz -= c, p += c, o += c;
144 }
e4452aaa 145 dstr_destroy(&d);
0875b58f 146}
147
148/* --- @trace_on@ --- *
149 *
150 * Arguments: @FILE *fp@ = a file to trace on
de3ceebe 151 * @unsigned l@ = trace level to set
0875b58f 152 *
153 * Returns: ---
154 *
155 * Use: Enables tracing to a file.
156 */
157
de3ceebe 158void trace_on(FILE *fp, unsigned l)
0875b58f 159{
e4452aaa 160 tracefunc = t_file;
161 tracearg = fp;
ff0f0220 162 if (!tracelvl)
163 tracelvl = l;
0875b58f 164}
165
e4452aaa 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
177void 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@ --- *
0875b58f 186 *
de3ceebe 187 * Arguments: @unsigned l@ = trace level to set
0875b58f 188 *
189 * Returns: ---
190 *
191 * Use: Sets the tracing level.
192 */
193
e4452aaa 194void trace_level(unsigned l)
0875b58f 195{
ff0f0220 196 tracelvl = l;
0875b58f 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
de3ceebe 208unsigned tracing(void)
0875b58f 209{
e4452aaa 210 return (tracefunc ? tracelvl : 0u);
0875b58f 211}
212
213/*----- That's all, folks -------------------------------------------------*/