3 * $Id: trace.c,v 1.8 2004/04/08 01:36:13 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 /*----- Header files ------------------------------------------------------*/
32 /* --- ANSI headers --- */
40 /* --- Local headers --- */
46 /*----- Private state information -----------------------------------------*/
48 static void (*tracefunc
)(const char *buf
, size_t sz
, void *v
) = 0;
49 static void *tracearg
;
50 static unsigned tracelvl
= 0; /* How much tracing gets done? */
52 /*----- Functions provided ------------------------------------------------*/
56 * Arguments: @const char *buf@ = buffer to print
57 * @size_t sz@ = buffer size
58 * @void *v@ = file handle
62 * Use: Dumps tracing information to a file.
65 static void t_file(const char *buf
, size_t sz
, void *v
)
68 fprintf(fp
, "+ %s: ", QUIS
);
69 fwrite(buf
, 1, sz
, fp
);
75 * Arguments: @unsigned l@ = trace level for output
76 * @const char *f@ = a @printf@-style format string
77 * @...@ = other arguments
81 * Use: Reports a message to the trace output.
84 void trace(unsigned l
, const char *f
, ...)
88 if ((l
& tracing()) == 0)
91 dstr_vputf(&d
, f
, &ap
);
93 tracefunc(d
.buf
, d
.len
, tracearg
);
97 /* --- @trace_block@ --- *
99 * Arguments: @unsigned l@ = trace level for output
100 * @const char *s@ = some header string to write
101 * @const void *b@ = pointer to a block of memory to dump
102 * @size_t sz@ = size of the block of memory
106 * Use: Dumps the contents of a block to the trace output.
109 void trace_block(unsigned l
, const char *s
, const void *b
, size_t sz
)
111 const unsigned char *p
= b
;
117 /* --- Skip if the trace level is too high --- */
119 if ((l
& tracing()) == 0)
122 /* --- Now start work --- */
124 tracefunc(s
, strlen(s
), tracearg
);
127 dstr_putf(&d
, " %08lx : ", o
);
128 for (i
= 0; i
< 8; i
++) {
130 dstr_putf(&d
, "%02x ", p
[i
]);
132 dstr_puts(&d
, "** ");
135 for (i
= 0; i
< 8; i
++) {
137 dstr_putc(&d
, isprint(p
[i
]) ? p
[i
] : '.');
142 tracefunc(d
.buf
, d
.len
, tracearg
);
143 c
= (sz
>= 8) ?
8 : sz
;
144 sz
-= c
, p
+= c
, o
+= c
;
149 /* --- @trace_on@ --- *
151 * Arguments: @FILE *fp@ = a file to trace on
152 * @unsigned l@ = trace level to set
156 * Use: Enables tracing to a file.
159 void trace_on(FILE *fp
, unsigned l
)
167 /* --- @trace_custom@ --- *
169 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
171 * @void *v@ = magic handle to give to function
175 * Use: Sets up a custom trace handler.
178 void trace_custom(void (*func
)(const char */
*buf*/
,
179 size_t /*sz*/, void */
*v*/
),
186 /* --- @trace_level@ --- *
188 * Arguments: @unsigned l@ = trace level to set
192 * Use: Sets the tracing level.
195 void trace_level(unsigned l
)
200 /* --- @tracing@ --- *
204 * Returns: Zero if not tracing, tracing level if tracing.
206 * Use: Informs the caller whether tracing is enabled.
209 unsigned tracing(void)
211 return (tracefunc ? tracelvl
: 0u);
214 /*----- That's all, folks -------------------------------------------------*/