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 --- */
38 /* --- Local headers --- */
45 /*----- Private state information -----------------------------------------*/
47 static void (*tracefunc
)(const char *buf
, size_t sz
, void *v
) = 0;
48 static void *tracearg
;
49 static unsigned tracelvl
= 0; /* How much tracing gets done? */
51 /*----- Functions provided ------------------------------------------------*/
55 * Arguments: @const char *buf@ = buffer to print
56 * @size_t sz@ = buffer size
57 * @void *v@ = file handle
61 * Use: Dumps tracing information to a file.
64 static void t_file(const char *buf
, size_t sz
, void *v
)
67 fprintf(fp
, "+ %s: ", QUIS
);
68 fwrite(buf
, 1, sz
, fp
);
74 * Arguments: @unsigned l@ = trace level for output
75 * @const char *f@ = a @printf@-style format string
76 * @...@ = other arguments
80 * Use: Reports a message to the trace output.
83 void trace(unsigned l
, const char *f
, ...)
87 if ((l
& tracing()) == 0)
90 dstr_vputf(&d
, f
, &ap
);
92 tracefunc(d
.buf
, d
.len
, tracearg
);
96 /* --- @trace_block@ --- *
98 * Arguments: @unsigned l@ = trace level for output
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
105 * Use: Dumps the contents of a block to the trace output.
108 void trace_block(unsigned l
, const char *s
, const void *b
, size_t sz
)
110 const unsigned char *p
= b
;
116 /* --- Skip if the trace level is too high --- */
118 if ((l
& tracing()) == 0)
121 /* --- Now start work --- */
123 tracefunc(s
, strlen(s
), tracearg
);
126 dstr_putf(&d
, " %08lx : ", o
);
127 for (i
= 0; i
< 8; i
++) {
129 dstr_putf(&d
, "%02x ", p
[i
]);
131 dstr_puts(&d
, "** ");
134 for (i
= 0; i
< 8; i
++) {
136 dstr_putc(&d
, ISPRINT(p
[i
]) ? p
[i
] : '.');
141 tracefunc(d
.buf
, d
.len
, tracearg
);
142 c
= (sz
>= 8) ?
8 : sz
;
143 sz
-= c
, p
+= c
, o
+= c
;
148 /* --- @trace_on@ --- *
150 * Arguments: @FILE *fp@ = a file to trace on
151 * @unsigned l@ = trace level to set
155 * Use: Enables tracing to a file.
158 void trace_on(FILE *fp
, unsigned l
)
166 /* --- @trace_custom@ --- *
168 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
170 * @void *v@ = magic handle to give to function
174 * Use: Sets up a custom trace handler.
177 void trace_custom(void (*func
)(const char */
*buf*/
,
178 size_t /*sz*/, void */
*v*/
),
185 /* --- @trace_level@ --- *
187 * Arguments: @unsigned l@ = trace level to set
191 * Use: Sets the tracing level.
194 void trace_level(unsigned l
)
199 /* --- @tracing@ --- *
203 * Returns: Zero if not tracing, tracing level if tracing.
205 * Use: Informs the caller whether tracing is enabled.
208 unsigned tracing(void)
210 return (tracefunc ? tracelvl
: 0u);
213 /*----- That's all, folks -------------------------------------------------*/