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