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