chiark / gitweb /
New custom trace output interface.
[mLib] / trace.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
e4452aaa 3 * $Id: trace.c,v 1.6 2001/02/03 16:23:55 mdw Exp $
0875b58f 4 *
5 * Tracing functions for debugging
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
18 *
0875b58f 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
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: trace.c,v $
e4452aaa 33 * Revision 1.6 2001/02/03 16:23:55 mdw
34 * New custom trace output interface.
35 *
de3ceebe 36 * Revision 1.5 1999/10/22 22:39:52 mdw
37 * New documented interface for tracing.
38 *
ff0f0220 39 * Revision 1.4 1999/05/19 20:27:11 mdw
40 * Change naming to match newer mLib conventions.
41 *
0bd98442 42 * Revision 1.3 1999/05/06 19:51:35 mdw
43 * Reformatted the LGPL notice a little bit.
44 *
c846879c 45 * Revision 1.2 1999/05/05 18:50:31 mdw
46 * Change licensing conditions to LGPL.
47 *
48 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
49 * Initial version of mLib
0875b58f 50 *
51 */
52
53/*----- Header files ------------------------------------------------------*/
54
55/* --- ANSI headers --- */
56
57#include <ctype.h>
58#include <stdarg.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62
63/* --- Local headers --- */
64
e4452aaa 65#include "dstr.h"
0875b58f 66#include "quis.h"
67#include "trace.h"
68
69/*----- Private state information -----------------------------------------*/
70
e4452aaa 71static void (*tracefunc)(const char *buf, size_t sz, void *v) = 0;
72static void *tracearg;
de3ceebe 73static unsigned tracelvl = 0; /* How much tracing gets done? */
0875b58f 74
75/*----- Functions provided ------------------------------------------------*/
76
e4452aaa 77/* --- @t_file@ --- *
78 *
79 * Arguments: @const char *buf@ = buffer to print
80 * @size_t sz@ = buffer size
81 * @void *v@ = file handle
82 *
83 * Returns: ---
84 *
85 * Use: Dumps tracing information to a file.
86 */
87
88static void t_file(const char *buf, size_t sz, void *v)
89{
90 FILE *fp = v;
91 fprintf(fp, "+ %s: ", QUIS);
92 fwrite(buf, 1, sz, fp);
93 fputc('\n', fp);
94}
95
0875b58f 96/* --- @trace@ --- *
97 *
de3ceebe 98 * Arguments: @unsigned l@ = trace level for output
0875b58f 99 * @const char *f@ = a @printf@-style format string
100 * @...@ = other arguments
101 *
102 * Returns: ---
103 *
104 * Use: Reports a message to the trace output.
105 */
106
de3ceebe 107void trace(unsigned l, const char *f, ...)
0875b58f 108{
109 va_list ap;
e4452aaa 110 dstr d = DSTR_INIT;
0875b58f 111 if ((l & tracing()) == 0)
112 return;
113 va_start(ap, f);
e4452aaa 114 dstr_vputf(&d, f, ap);
0875b58f 115 va_end(ap);
e4452aaa 116 tracefunc(d.buf, d.len, tracearg);
117 dstr_destroy(&d);
0875b58f 118}
119
120/* --- @trace_block@ --- *
121 *
de3ceebe 122 * Arguments: @unsigned l@ = trace level for output
0875b58f 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
126 *
127 * Returns: ---
128 *
129 * Use: Dumps the contents of a block to the trace output.
130 */
131
de3ceebe 132void trace_block(unsigned l, const char *s, const void *b, size_t sz)
0875b58f 133{
134 const unsigned char *p = b;
135 size_t i;
136 unsigned long o = 0;
e4452aaa 137 dstr d = DSTR_INIT;
0875b58f 138 size_t c;
139
140 /* --- Skip if the trace level is too high --- */
141
142 if ((l & tracing()) == 0)
143 return;
144
145 /* --- Now start work --- */
146
e4452aaa 147 tracefunc(s, strlen(s), tracearg);
0875b58f 148 while (sz) {
e4452aaa 149 dstr_reset(&d);
150 dstr_putf(&d, " %08lx : ", o);
0875b58f 151 for (i = 0; i < 8; i++) {
152 if (i < sz)
e4452aaa 153 dstr_putf(&d, "%02x ", p[i]);
0875b58f 154 else
e4452aaa 155 dstr_puts(&d, "** ");
0875b58f 156 }
e4452aaa 157 dstr_puts(&d, ": ");
0875b58f 158 for (i = 0; i < 8; i++) {
159 if (i < sz)
e4452aaa 160 dstr_putc(&d, isprint(p[i]) ? p[i] : '.');
0875b58f 161 else
e4452aaa 162 dstr_putc(&d, '*');
0875b58f 163 }
e4452aaa 164 dstr_putz(&d);
165 tracefunc(d.buf, d.len, tracearg);
0875b58f 166 c = (sz >= 8) ? 8 : sz;
167 sz -= c, p += c, o += c;
168 }
e4452aaa 169 dstr_destroy(&d);
0875b58f 170}
171
172/* --- @trace_on@ --- *
173 *
174 * Arguments: @FILE *fp@ = a file to trace on
de3ceebe 175 * @unsigned l@ = trace level to set
0875b58f 176 *
177 * Returns: ---
178 *
179 * Use: Enables tracing to a file.
180 */
181
de3ceebe 182void trace_on(FILE *fp, unsigned l)
0875b58f 183{
e4452aaa 184 tracefunc = t_file;
185 tracearg = fp;
ff0f0220 186 if (!tracelvl)
187 tracelvl = l;
0875b58f 188}
189
e4452aaa 190/* --- @trace_custom@ --- *
191 *
192 * Arguments: @void (*func)(const char *buf, size_t sz, void *v)@ =
193 * output function
194 * @void *v@ = magic handle to give to function
195 *
196 * Returns: ---
197 *
198 * Use: Sets up a custom trace handler.
199 */
200
201void trace_custom(void (*func)(const char */*buf*/,
202 size_t /*sz*/, void */*v*/),
203 void *v)
204{
205 tracefunc = func;
206 tracearg = v;
207}
208
209/* --- @trace_level@ --- *
0875b58f 210 *
de3ceebe 211 * Arguments: @unsigned l@ = trace level to set
0875b58f 212 *
213 * Returns: ---
214 *
215 * Use: Sets the tracing level.
216 */
217
e4452aaa 218void trace_level(unsigned l)
0875b58f 219{
ff0f0220 220 tracelvl = l;
0875b58f 221}
222
223/* --- @tracing@ --- *
224 *
225 * Arguments: ---
226 *
227 * Returns: Zero if not tracing, tracing level if tracing.
228 *
229 * Use: Informs the caller whether tracing is enabled.
230 */
231
de3ceebe 232unsigned tracing(void)
0875b58f 233{
e4452aaa 234 return (tracefunc ? tracelvl : 0u);
0875b58f 235}
236
237/*----- That's all, folks -------------------------------------------------*/