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 --- */
44 /*----- Private state information -----------------------------------------*/
46 static void (*tracefunc
)(const char *buf
, size_t sz
, void *v
) = 0;
47 static void *tracearg
;
48 static unsigned tracelvl
= 0; /* How much tracing gets done? */
50 /*----- Functions provided ------------------------------------------------*/
54 * Arguments: @const char *buf@ = buffer to print
55 * @size_t sz@ = buffer size
56 * @void *v@ = file handle
60 * Use: Dumps tracing information to a file.
63 static void t_file(const char *buf
, size_t sz
, void *v
)
66 fprintf(fp
, "+ %s: ", QUIS
);
67 fwrite(buf
, 1, sz
, fp
);
73 * Arguments: @unsigned l@ = trace level for output
74 * @const char *f@ = a @printf@-style format string
75 * @...@ = other arguments
79 * Use: Reports a message to the trace output.
82 void trace(unsigned l
, const char *f
, ...)
86 if ((l
& tracing()) == 0)
89 dstr_vputf(&d
, f
, &ap
);
91 tracefunc(d
.buf
, d
.len
, tracearg
);
95 /* --- @trace_block@ --- *
97 * Arguments: @unsigned l@ = trace level for output
98 * @const char *s@ = some header string to write
99 * @const void *b@ = pointer to a block of memory to dump
100 * @size_t sz@ = size of the block of memory
104 * Use: Dumps the contents of a block to the trace output.
107 void trace_block(unsigned l
, const char *s
, const void *b
, size_t sz
)
109 const unsigned char *p
= b
;
115 /* --- Skip if the trace level is too high --- */
117 if ((l
& tracing()) == 0)
120 /* --- Now start work --- */
122 tracefunc(s
, strlen(s
), tracearg
);
125 dstr_putf(&d
, " %08lx : ", o
);
126 for (i
= 0; i
< 8; i
++) {
128 dstr_putf(&d
, "%02x ", p
[i
]);
130 dstr_puts(&d
, "** ");
133 for (i
= 0; i
< 8; i
++) {
135 dstr_putc(&d
, isprint(p
[i
]) ? p
[i
] : '.');
140 tracefunc(d
.buf
, d
.len
, tracearg
);
141 c
= (sz
>= 8) ?
8 : sz
;
142 sz
-= c
, p
+= c
, o
+= c
;
147 /* --- @trace_on@ --- *
149 * Arguments: @FILE *fp@ = a file to trace on
150 * @unsigned l@ = trace level to set
154 * Use: Enables tracing to a file.
157 void trace_on(FILE *fp
, unsigned l
)
165 /* --- @trace_custom@ --- *
167 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
169 * @void *v@ = magic handle to give to function
173 * Use: Sets up a custom trace handler.
176 void trace_custom(void (*func
)(const char */
*buf*/
,
177 size_t /*sz*/, void */
*v*/
),
184 /* --- @trace_level@ --- *
186 * Arguments: @unsigned l@ = trace level to set
190 * Use: Sets the tracing level.
193 void trace_level(unsigned l
)
198 /* --- @tracing@ --- *
202 * Returns: Zero if not tracing, tracing level if tracing.
204 * Use: Informs the caller whether tracing is enabled.
207 unsigned tracing(void)
209 return (tracefunc ? tracelvl
: 0u);
212 /*----- That's all, folks -------------------------------------------------*/