3 * $Id: trace.c,v 1.2 1999/05/05 18:50:31 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 Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
50 /* --- Local headers --- */
55 /*----- Private state information -----------------------------------------*/
57 static FILE *trace__fp = 0; /* Where does debugging go? */
58 static unsigned int trace__lvl = 0; /* How much tracing gets done? */
60 /*----- Functions provided ------------------------------------------------*/
64 * Arguments: @unsigned int l@ = trace level for output
65 * @const char *f@ = a @printf@-style format string
66 * @...@ = other arguments
70 * Use: Reports a message to the trace output.
73 void trace(unsigned int l, const char *f, ...)
76 if ((l & tracing()) == 0)
79 fprintf(trace__fp, "*** %s: ", QUIS);
80 vfprintf(trace__fp, f, ap);
82 putc('\n', trace__fp);
86 /* --- @trace_block@ --- *
88 * Arguments: @unsigned int l@ = trace level for output
89 * @const char *s@ = some header string to write
90 * @const void *b@ = pointer to a block of memory to dump
91 * @size_t sz@ = size of the block of memory
95 * Use: Dumps the contents of a block to the trace output.
98 void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
100 const unsigned char *p = b;
105 /* --- Skip if the trace level is too high --- */
107 if ((l & tracing()) == 0)
110 /* --- Now start work --- */
112 fprintf(trace__fp, "*** %s: %s\n", QUIS, s);
115 fprintf(trace__fp, "*** %s: %08lx : ", QUIS, o);
116 for (i = 0; i < 8; i++) {
118 fprintf(trace__fp, "%02x ", p[i]);
120 fputs("** ", trace__fp);
122 fputs(": ", trace__fp);
123 for (i = 0; i < 8; i++) {
125 fputc(isprint(p[i]) ? p[i] : '.', trace__fp);
127 fputc('*', trace__fp);
129 fputc('\n', trace__fp);
130 c = (sz >= 8) ? 8 : sz;
131 sz -= c, p += c, o += c;
136 /* --- @trace_on@ --- *
138 * Arguments: @FILE *fp@ = a file to trace on
139 * @unsigned int l@ = trace level to set
143 * Use: Enables tracing to a file.
146 void trace_on(FILE *fp, unsigned int l)
153 /* --- @trace_setLevel@ --- *
155 * Arguments: @unsigned int l@ = trace level to set
159 * Use: Sets the tracing level.
162 void trace_setLevel(unsigned int l)
167 /* --- @tracing@ --- *
171 * Returns: Zero if not tracing, tracing level if tracing.
173 * Use: Informs the caller whether tracing is enabled.
176 unsigned int tracing(void)
178 return (trace__fp ? trace__lvl : 0u);
181 /*----- That's all, folks -------------------------------------------------*/