3 * $Id: trace.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
47 /* --- Local headers --- */
52 /*----- Private state information -----------------------------------------*/
54 static FILE *trace__fp = 0; /* Where does debugging go? */
55 static unsigned int trace__lvl = 0; /* How much tracing gets done? */
57 /*----- Functions provided ------------------------------------------------*/
61 * Arguments: @unsigned int l@ = trace level for output
62 * @const char *f@ = a @printf@-style format string
63 * @...@ = other arguments
67 * Use: Reports a message to the trace output.
70 void trace(unsigned int l, const char *f, ...)
73 if ((l & tracing()) == 0)
76 fprintf(trace__fp, "*** %s: ", QUIS);
77 vfprintf(trace__fp, f, ap);
79 putc('\n', trace__fp);
83 /* --- @trace_block@ --- *
85 * Arguments: @unsigned int l@ = trace level for output
86 * @const char *s@ = some header string to write
87 * @const void *b@ = pointer to a block of memory to dump
88 * @size_t sz@ = size of the block of memory
92 * Use: Dumps the contents of a block to the trace output.
95 void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
97 const unsigned char *p = b;
102 /* --- Skip if the trace level is too high --- */
104 if ((l & tracing()) == 0)
107 /* --- Now start work --- */
109 fprintf(trace__fp, "*** %s: %s\n", QUIS, s);
112 fprintf(trace__fp, "*** %s: %08lx : ", QUIS, o);
113 for (i = 0; i < 8; i++) {
115 fprintf(trace__fp, "%02x ", p[i]);
117 fputs("** ", trace__fp);
119 fputs(": ", trace__fp);
120 for (i = 0; i < 8; i++) {
122 fputc(isprint(p[i]) ? p[i] : '.', trace__fp);
124 fputc('*', trace__fp);
126 fputc('\n', trace__fp);
127 c = (sz >= 8) ? 8 : sz;
128 sz -= c, p += c, o += c;
133 /* --- @trace_on@ --- *
135 * Arguments: @FILE *fp@ = a file to trace on
136 * @unsigned int l@ = trace level to set
140 * Use: Enables tracing to a file.
143 void trace_on(FILE *fp, unsigned int l)
150 /* --- @trace_setLevel@ --- *
152 * Arguments: @unsigned int l@ = trace level to set
156 * Use: Sets the tracing level.
159 void trace_setLevel(unsigned int l)
164 /* --- @tracing@ --- *
168 * Returns: Zero if not tracing, tracing level if tracing.
170 * Use: Informs the caller whether tracing is enabled.
173 unsigned int tracing(void)
175 return (trace__fp ? trace__lvl : 0u);
178 /*----- That's all, folks -------------------------------------------------*/