3 * $Id: trace.c,v 1.7 2002/01/13 13:34:32 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.7 2002/01/13 13:34:32 mdw
34 * Track @dstr_vputf@ interface change.
36 * Revision 1.6 2001/02/03 16:23:55 mdw
37 * New custom trace output interface.
39 * Revision 1.5 1999/10/22 22:39:52 mdw
40 * New documented interface for tracing.
42 * Revision 1.4 1999/05/19 20:27:11 mdw
43 * Change naming to match newer mLib conventions.
45 * Revision 1.3 1999/05/06 19:51:35 mdw
46 * Reformatted the LGPL notice a little bit.
48 * Revision 1.2 1999/05/05 18:50:31 mdw
49 * Change licensing conditions to LGPL.
51 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
52 * Initial version of mLib
56 /*----- Header files ------------------------------------------------------*/
58 /* --- ANSI headers --- */
66 /* --- Local headers --- */
72 /*----- Private state information -----------------------------------------*/
74 static void (*tracefunc
)(const char *buf
, size_t sz
, void *v
) = 0;
75 static void *tracearg
;
76 static unsigned tracelvl
= 0; /* How much tracing gets done? */
78 /*----- Functions provided ------------------------------------------------*/
82 * Arguments: @const char *buf@ = buffer to print
83 * @size_t sz@ = buffer size
84 * @void *v@ = file handle
88 * Use: Dumps tracing information to a file.
91 static void t_file(const char *buf
, size_t sz
, void *v
)
94 fprintf(fp
, "+ %s: ", QUIS
);
95 fwrite(buf
, 1, sz
, fp
);
101 * Arguments: @unsigned l@ = trace level for output
102 * @const char *f@ = a @printf@-style format string
103 * @...@ = other arguments
107 * Use: Reports a message to the trace output.
110 void trace(unsigned l
, const char *f
, ...)
114 if ((l
& tracing()) == 0)
117 dstr_vputf(&d
, f
, &ap
);
119 tracefunc(d
.buf
, d
.len
, tracearg
);
123 /* --- @trace_block@ --- *
125 * Arguments: @unsigned l@ = trace level for output
126 * @const char *s@ = some header string to write
127 * @const void *b@ = pointer to a block of memory to dump
128 * @size_t sz@ = size of the block of memory
132 * Use: Dumps the contents of a block to the trace output.
135 void trace_block(unsigned l
, const char *s
, const void *b
, size_t sz
)
137 const unsigned char *p
= b
;
143 /* --- Skip if the trace level is too high --- */
145 if ((l
& tracing()) == 0)
148 /* --- Now start work --- */
150 tracefunc(s
, strlen(s
), tracearg
);
153 dstr_putf(&d
, " %08lx : ", o
);
154 for (i
= 0; i
< 8; i
++) {
156 dstr_putf(&d
, "%02x ", p
[i
]);
158 dstr_puts(&d
, "** ");
161 for (i
= 0; i
< 8; i
++) {
163 dstr_putc(&d
, isprint(p
[i
]) ? p
[i
] : '.');
168 tracefunc(d
.buf
, d
.len
, tracearg
);
169 c
= (sz
>= 8) ?
8 : sz
;
170 sz
-= c
, p
+= c
, o
+= c
;
175 /* --- @trace_on@ --- *
177 * Arguments: @FILE *fp@ = a file to trace on
178 * @unsigned l@ = trace level to set
182 * Use: Enables tracing to a file.
185 void trace_on(FILE *fp
, unsigned l
)
193 /* --- @trace_custom@ --- *
195 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
197 * @void *v@ = magic handle to give to function
201 * Use: Sets up a custom trace handler.
204 void trace_custom(void (*func
)(const char */
*buf*/
,
205 size_t /*sz*/, void */
*v*/
),
212 /* --- @trace_level@ --- *
214 * Arguments: @unsigned l@ = trace level to set
218 * Use: Sets the tracing level.
221 void trace_level(unsigned l
)
226 /* --- @tracing@ --- *
230 * Returns: Zero if not tracing, tracing level if tracing.
232 * Use: Informs the caller whether tracing is enabled.
235 unsigned tracing(void)
237 return (tracefunc ? tracelvl
: 0u);
240 /*----- That's all, folks -------------------------------------------------*/