3 * $Id: trace.c,v 1.5 1999/10/22 22:39:52 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.5 1999/10/22 22:39:52 mdw
34 * New documented interface for tracing.
36 * Revision 1.4 1999/05/19 20:27:11 mdw
37 * Change naming to match newer mLib conventions.
39 * Revision 1.3 1999/05/06 19:51:35 mdw
40 * Reformatted the LGPL notice a little bit.
42 * Revision 1.2 1999/05/05 18:50:31 mdw
43 * Change licensing conditions to LGPL.
45 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
46 * Initial version of mLib
50 /*----- Header files ------------------------------------------------------*/
52 /* --- ANSI headers --- */
60 /* --- Local headers --- */
65 /*----- Private state information -----------------------------------------*/
67 static FILE *tracefp
= 0; /* Where does debugging go? */
68 static unsigned tracelvl
= 0; /* How much tracing gets done? */
70 /*----- Functions provided ------------------------------------------------*/
74 * Arguments: @unsigned l@ = trace level for output
75 * @const char *f@ = a @printf@-style format string
76 * @...@ = other arguments
80 * Use: Reports a message to the trace output.
83 void trace(unsigned l
, const char *f
, ...)
86 if ((l
& tracing()) == 0)
89 fprintf(tracefp
, "*** %s: ", QUIS
);
90 vfprintf(tracefp
, f
, ap
);
96 /* --- @trace_block@ --- *
98 * Arguments: @unsigned l@ = trace level for output
99 * @const char *s@ = some header string to write
100 * @const void *b@ = pointer to a block of memory to dump
101 * @size_t sz@ = size of the block of memory
105 * Use: Dumps the contents of a block to the trace output.
108 void trace_block(unsigned l
, const char *s
, const void *b
, size_t sz
)
110 const unsigned char *p
= b
;
115 /* --- Skip if the trace level is too high --- */
117 if ((l
& tracing()) == 0)
120 /* --- Now start work --- */
122 fprintf(tracefp
, "*** %s: %s\n", QUIS
, s
);
125 fprintf(tracefp
, "*** %s: %08lx : ", QUIS
, o
);
126 for (i
= 0; i
< 8; i
++) {
128 fprintf(tracefp
, "%02x ", p
[i
]);
130 fputs("** ", tracefp
);
132 fputs(": ", tracefp
);
133 for (i
= 0; i
< 8; i
++) {
135 fputc(isprint(p
[i
]) ? p
[i
] : '.', tracefp
);
139 fputc('\n', tracefp
);
140 c
= (sz
>= 8) ?
8 : sz
;
141 sz
-= c
, p
+= c
, o
+= c
;
146 /* --- @trace_on@ --- *
148 * Arguments: @FILE *fp@ = a file to trace on
149 * @unsigned l@ = trace level to set
153 * Use: Enables tracing to a file.
156 void trace_on(FILE *fp
, unsigned l
)
163 /* --- @trace_setLevel@ --- *
165 * Arguments: @unsigned l@ = trace level to set
169 * Use: Sets the tracing level.
172 void trace_setLevel(unsigned l
)
177 /* --- @tracing@ --- *
181 * Returns: Zero if not tracing, tracing level if tracing.
183 * Use: Informs the caller whether tracing is enabled.
186 unsigned tracing(void)
188 return (tracefp ? tracelvl
: 0u);
191 /*----- That's all, folks -------------------------------------------------*/