3 * $Id: trace.c,v 1.6 2001/02/03 16:23:55 mdw Exp $
5 * Tracing functions for debugging
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2001/02/03 16:23:55 mdw
34 * New custom trace output interface.
36 * Revision 1.5 1999/10/22 22:39:52 mdw
37 * New documented interface for tracing.
39 * Revision 1.4 1999/05/19 20:27:11 mdw
40 * Change naming to match newer mLib conventions.
42 * Revision 1.3 1999/05/06 19:51:35 mdw
43 * Reformatted the LGPL notice a little bit.
45 * Revision 1.2 1999/05/05 18:50:31 mdw
46 * Change licensing conditions to LGPL.
48 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
49 * Initial version of mLib
53 /*----- Header files ------------------------------------------------------*/
55 /* --- ANSI headers --- */
63 /* --- Local headers --- */
69 /*----- Private state information -----------------------------------------*/
71 static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
72 static void *tracearg;
73 static unsigned tracelvl = 0; /* How much tracing gets done? */
75 /*----- Functions provided ------------------------------------------------*/
79 * Arguments: @const char *buf@ = buffer to print
80 * @size_t sz@ = buffer size
81 * @void *v@ = file handle
85 * Use: Dumps tracing information to a file.
88 static void t_file(const char *buf, size_t sz, void *v)
91 fprintf(fp, "+ %s: ", QUIS);
92 fwrite(buf, 1, sz, fp);
98 * Arguments: @unsigned l@ = trace level for output
99 * @const char *f@ = a @printf@-style format string
100 * @...@ = other arguments
104 * Use: Reports a message to the trace output.
107 void trace(unsigned l, const char *f, ...)
111 if ((l & tracing()) == 0)
114 dstr_vputf(&d, f, ap);
116 tracefunc(d.buf, d.len, tracearg);
120 /* --- @trace_block@ --- *
122 * Arguments: @unsigned l@ = trace level for output
123 * @const char *s@ = some header string to write
124 * @const void *b@ = pointer to a block of memory to dump
125 * @size_t sz@ = size of the block of memory
129 * Use: Dumps the contents of a block to the trace output.
132 void trace_block(unsigned l, const char *s, const void *b, size_t sz)
134 const unsigned char *p = b;
140 /* --- Skip if the trace level is too high --- */
142 if ((l & tracing()) == 0)
145 /* --- Now start work --- */
147 tracefunc(s, strlen(s), tracearg);
150 dstr_putf(&d, " %08lx : ", o);
151 for (i = 0; i < 8; i++) {
153 dstr_putf(&d, "%02x ", p[i]);
155 dstr_puts(&d, "** ");
158 for (i = 0; i < 8; i++) {
160 dstr_putc(&d, isprint(p[i]) ? p[i] : '.');
165 tracefunc(d.buf, d.len, tracearg);
166 c = (sz >= 8) ? 8 : sz;
167 sz -= c, p += c, o += c;
172 /* --- @trace_on@ --- *
174 * Arguments: @FILE *fp@ = a file to trace on
175 * @unsigned l@ = trace level to set
179 * Use: Enables tracing to a file.
182 void trace_on(FILE *fp, unsigned l)
190 /* --- @trace_custom@ --- *
192 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
194 * @void *v@ = magic handle to give to function
198 * Use: Sets up a custom trace handler.
201 void trace_custom(void (*func)(const char */*buf*/,
202 size_t /*sz*/, void */*v*/),
209 /* --- @trace_level@ --- *
211 * Arguments: @unsigned l@ = trace level to set
215 * Use: Sets the tracing level.
218 void trace_level(unsigned l)
223 /* --- @tracing@ --- *
227 * Returns: Zero if not tracing, tracing level if tracing.
229 * Use: Informs the caller whether tracing is enabled.
232 unsigned tracing(void)
234 return (tracefunc ? tracelvl : 0u);
237 /*----- That's all, folks -------------------------------------------------*/