3 * Tracing functions for debugging
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 /* --- ANSI headers --- */
36 /* --- Local headers --- */
43 /*----- Private state information -----------------------------------------*/
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? */
49 /*----- Functions provided ------------------------------------------------*/
53 * Arguments: @const char *buf@ = buffer to print
54 * @size_t sz@ = buffer size
55 * @void *v@ = file handle
59 * Use: Dumps tracing information to a file.
62 static void t_file(const char *buf, size_t sz, void *v)
65 fprintf(fp, "+ %s: ", QUIS);
66 fwrite(buf, 1, sz, fp);
72 * Arguments: @unsigned l@ = trace level for output
73 * @const char *f@ = a @printf@-style format string
74 * @...@ = other arguments
78 * Use: Reports a message to the trace output.
81 void trace(unsigned l, const char *f, ...)
85 if ((l & tracing()) == 0)
88 dstr_vputf(&d, f, &ap);
90 tracefunc(d.buf, d.len, tracearg);
94 /* --- @trace_block@ --- *
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
103 * Use: Dumps the contents of a block to the trace output.
106 void trace_block(unsigned l, const char *s, const void *b, size_t sz)
108 const unsigned char *p = b;
114 /* --- Skip if the trace level is too high --- */
116 if ((l & tracing()) == 0)
119 /* --- Now start work --- */
121 tracefunc(s, strlen(s), tracearg);
124 dstr_putf(&d, " %08lx : ", o);
125 for (i = 0; i < 8; i++) {
127 dstr_putf(&d, "%02x ", p[i]);
129 dstr_puts(&d, "** ");
132 for (i = 0; i < 8; i++) {
134 dstr_putc(&d, ISPRINT(p[i]) ? p[i] : '.');
139 tracefunc(d.buf, d.len, tracearg);
140 c = (sz >= 8) ? 8 : sz;
141 sz -= c, p += c, o += c;
146 /* --- @trace_on@ --- *
148 * Arguments: @FILE *fp@ = a file to trace on
149 * @unsigned l@ = trace level to set
153 * Use: Enables tracing to a file.
156 void trace_on(FILE *fp, unsigned l)
164 /* --- @trace_custom@ --- *
166 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
168 * @void *v@ = magic handle to give to function
172 * Use: Sets up a custom trace handler.
175 void trace_custom(void (*func)(const char */*buf*/,
176 size_t /*sz*/, void */*v*/),
183 /* --- @trace_level@ --- *
185 * Arguments: @unsigned l@ = trace level to set
189 * Use: Sets the tracing level.
192 void trace_level(unsigned l)
197 /* --- @tracing@ --- *
201 * Returns: Zero if not tracing, tracing level if tracing.
203 * Use: Informs the caller whether tracing is enabled.
206 unsigned tracing(void)
208 return (tracefunc ? tracelvl : 0u);
211 /*----- That's all, folks -------------------------------------------------*/