3 * $Id: trace.c,v 1.4 1999/05/19 20:27:11 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/05/19 20:27:11 mdw
34 * Change naming to match newer mLib conventions.
36 * Revision 1.3 1999/05/06 19:51:35 mdw
37 * Reformatted the LGPL notice a little bit.
39 * Revision 1.2 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
42 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
43 * Initial version of mLib
47 /*----- Header files ------------------------------------------------------*/
49 /* --- ANSI headers --- */
57 /* --- Local headers --- */
62 /*----- Private state information -----------------------------------------*/
64 static FILE *tracefp = 0; /* Where does debugging go? */
65 static unsigned int tracelvl = 0; /* How much tracing gets done? */
67 /*----- Functions provided ------------------------------------------------*/
71 * Arguments: @unsigned int l@ = trace level for output
72 * @const char *f@ = a @printf@-style format string
73 * @...@ = other arguments
77 * Use: Reports a message to the trace output.
80 void trace(unsigned int l, const char *f, ...)
83 if ((l & tracing()) == 0)
86 fprintf(tracefp, "*** %s: ", QUIS);
87 vfprintf(tracefp, f, ap);
93 /* --- @trace_block@ --- *
95 * Arguments: @unsigned int l@ = trace level for output
96 * @const char *s@ = some header string to write
97 * @const void *b@ = pointer to a block of memory to dump
98 * @size_t sz@ = size of the block of memory
102 * Use: Dumps the contents of a block to the trace output.
105 void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
107 const unsigned char *p = b;
112 /* --- Skip if the trace level is too high --- */
114 if ((l & tracing()) == 0)
117 /* --- Now start work --- */
119 fprintf(tracefp, "*** %s: %s\n", QUIS, s);
122 fprintf(tracefp, "*** %s: %08lx : ", QUIS, o);
123 for (i = 0; i < 8; i++) {
125 fprintf(tracefp, "%02x ", p[i]);
127 fputs("** ", tracefp);
129 fputs(": ", tracefp);
130 for (i = 0; i < 8; i++) {
132 fputc(isprint(p[i]) ? p[i] : '.', tracefp);
136 fputc('\n', tracefp);
137 c = (sz >= 8) ? 8 : sz;
138 sz -= c, p += c, o += c;
143 /* --- @trace_on@ --- *
145 * Arguments: @FILE *fp@ = a file to trace on
146 * @unsigned int l@ = trace level to set
150 * Use: Enables tracing to a file.
153 void trace_on(FILE *fp, unsigned int l)
160 /* --- @trace_setLevel@ --- *
162 * Arguments: @unsigned int l@ = trace level to set
166 * Use: Sets the tracing level.
169 void trace_setLevel(unsigned int l)
174 /* --- @tracing@ --- *
178 * Returns: Zero if not tracing, tracing level if tracing.
180 * Use: Informs the caller whether tracing is enabled.
183 unsigned int tracing(void)
185 return (tracefp ? tracelvl : 0u);
188 /*----- That's all, folks -------------------------------------------------*/