chiark / gitweb /
Script to transform CVS sources into buildable source tree.
[mLib] / trace.c
1 /* -*-c-*-
2  *
3  * $Id: trace.c,v 1.2 1999/05/05 18:50:31 mdw Exp $
4  *
5  * Tracing functions for debugging
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the mLib utilities library.
13  *
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.
18  * 
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.
23  * 
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.
27  */
28
29 /*----- Revision history --------------------------------------------------*
30  *
31  * $Log: trace.c,v $
32  * Revision 1.2  1999/05/05 18:50:31  mdw
33  * Change licensing conditions to LGPL.
34  *
35  * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
36  * Initial version of mLib
37  *
38  */
39
40 /*----- Header files ------------------------------------------------------*/
41
42 /* --- ANSI headers --- */
43
44 #include <ctype.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 /* --- Local headers --- */
51
52 #include "quis.h"
53 #include "trace.h"
54
55 /*----- Private state information -----------------------------------------*/
56
57 static FILE *trace__fp = 0;             /* Where does debugging go? */
58 static unsigned int trace__lvl = 0;     /* How much tracing gets done? */
59
60 /*----- Functions provided ------------------------------------------------*/
61
62 /* --- @trace@ --- *
63  *
64  * Arguments:   @unsigned int l@ = trace level for output
65  *              @const char *f@ = a @printf@-style format string
66  *              @...@ = other arguments
67  *
68  * Returns:     ---
69  *
70  * Use:         Reports a message to the trace output.
71  */
72
73 void trace(unsigned int l, const char *f, ...)
74 {
75   va_list ap;
76   if ((l & tracing()) == 0)
77     return;
78   va_start(ap, f);
79   fprintf(trace__fp, "*** %s: ", QUIS);
80   vfprintf(trace__fp, f, ap);
81   va_end(ap);
82   putc('\n', trace__fp);
83   fflush(trace__fp);
84 }
85
86 /* --- @trace_block@ --- *
87  *
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
92  *
93  * Returns:     ---
94  *
95  * Use:         Dumps the contents of a block to the trace output.
96  */
97
98 void trace_block(unsigned int l, const char *s, const void *b, size_t sz)
99 {
100   const unsigned char *p = b;
101   size_t i;
102   unsigned long o = 0;
103   size_t c;
104
105   /* --- Skip if the trace level is too high --- */
106
107   if ((l & tracing()) == 0)
108     return;
109
110   /* --- Now start work --- */
111
112   fprintf(trace__fp, "*** %s: %s\n", QUIS, s);
113
114   while (sz) {
115     fprintf(trace__fp, "*** %s:   %08lx : ", QUIS, o);
116     for (i = 0; i < 8; i++) {
117       if (i < sz)
118         fprintf(trace__fp, "%02x ", p[i]);
119       else
120         fputs("** ", trace__fp);
121     }
122     fputs(": ", trace__fp);
123     for (i = 0; i < 8; i++) {
124       if (i < sz)
125         fputc(isprint(p[i]) ? p[i] : '.', trace__fp);
126       else
127         fputc('*', trace__fp);
128     }
129     fputc('\n', trace__fp);
130     c = (sz >= 8) ? 8 : sz;
131     sz -= c, p += c, o += c;
132   }
133   fflush(trace__fp);
134 }
135
136 /* --- @trace_on@ --- *
137  *
138  * Arguments:   @FILE *fp@ = a file to trace on
139  *              @unsigned int l@ = trace level to set
140  *
141  * Returns:     ---
142  *
143  * Use:         Enables tracing to a file.
144  */
145
146 void trace_on(FILE *fp, unsigned int l)
147 {
148   trace__fp = fp;
149   if (!trace__lvl)
150     trace__lvl = l;
151 }
152
153 /* --- @trace_setLevel@ --- *
154  *
155  * Arguments:   @unsigned int l@ = trace level to set
156  *
157  * Returns:     ---
158  *
159  * Use:         Sets the tracing level.
160  */
161
162 void trace_setLevel(unsigned int l)
163 {
164   trace__lvl = l;
165 }
166
167 /* --- @tracing@ --- *
168  *
169  * Arguments:   ---
170  *
171  * Returns:     Zero if not tracing, tracing level if tracing.
172  *
173  * Use:         Informs the caller whether tracing is enabled.
174  */
175
176 unsigned int tracing(void)
177 {
178   return (trace__fp ? trace__lvl : 0u);
179 }
180
181 /*----- That's all, folks -------------------------------------------------*/