0875b58f |
1 | /* -*-c-*- |
2 | * |
ff0f0220 |
3 | * $Id: trace.c,v 1.4 1999/05/19 20:27:11 mdw Exp $ |
0875b58f |
4 | * |
5 | * Tracing functions for debugging |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
c846879c |
10 | /*----- Licensing notice --------------------------------------------------* |
0875b58f |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
c846879c |
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 | * |
0875b58f |
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 |
c846879c |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
0bd98442 |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
0875b58f |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: trace.c,v $ |
ff0f0220 |
33 | * Revision 1.4 1999/05/19 20:27:11 mdw |
34 | * Change naming to match newer mLib conventions. |
35 | * |
0bd98442 |
36 | * Revision 1.3 1999/05/06 19:51:35 mdw |
37 | * Reformatted the LGPL notice a little bit. |
38 | * |
c846879c |
39 | * Revision 1.2 1999/05/05 18:50:31 mdw |
40 | * Change licensing conditions to LGPL. |
41 | * |
42 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
43 | * Initial version of mLib |
0875b58f |
44 | * |
45 | */ |
46 | |
47 | /*----- Header files ------------------------------------------------------*/ |
48 | |
49 | /* --- ANSI headers --- */ |
50 | |
51 | #include <ctype.h> |
52 | #include <stdarg.h> |
53 | #include <stdio.h> |
54 | #include <stdlib.h> |
55 | #include <string.h> |
56 | |
57 | /* --- Local headers --- */ |
58 | |
59 | #include "quis.h" |
60 | #include "trace.h" |
61 | |
62 | /*----- Private state information -----------------------------------------*/ |
63 | |
ff0f0220 |
64 | static FILE *tracefp = 0; /* Where does debugging go? */ |
65 | static unsigned int tracelvl = 0; /* How much tracing gets done? */ |
0875b58f |
66 | |
67 | /*----- Functions provided ------------------------------------------------*/ |
68 | |
69 | /* --- @trace@ --- * |
70 | * |
71 | * Arguments: @unsigned int l@ = trace level for output |
72 | * @const char *f@ = a @printf@-style format string |
73 | * @...@ = other arguments |
74 | * |
75 | * Returns: --- |
76 | * |
77 | * Use: Reports a message to the trace output. |
78 | */ |
79 | |
80 | void trace(unsigned int l, const char *f, ...) |
81 | { |
82 | va_list ap; |
83 | if ((l & tracing()) == 0) |
84 | return; |
85 | va_start(ap, f); |
ff0f0220 |
86 | fprintf(tracefp, "*** %s: ", QUIS); |
87 | vfprintf(tracefp, f, ap); |
0875b58f |
88 | va_end(ap); |
ff0f0220 |
89 | putc('\n', tracefp); |
90 | fflush(tracefp); |
0875b58f |
91 | } |
92 | |
93 | /* --- @trace_block@ --- * |
94 | * |
95 | * Arguments: @unsigned int l@ = trace level for output |
96 | * @const char *s@ = some header string to write |
97 | * @const void *b@ = pointer to a block of memory to dump |
98 | * @size_t sz@ = size of the block of memory |
99 | * |
100 | * Returns: --- |
101 | * |
102 | * Use: Dumps the contents of a block to the trace output. |
103 | */ |
104 | |
105 | void trace_block(unsigned int l, const char *s, const void *b, size_t sz) |
106 | { |
107 | const unsigned char *p = b; |
108 | size_t i; |
109 | unsigned long o = 0; |
110 | size_t c; |
111 | |
112 | /* --- Skip if the trace level is too high --- */ |
113 | |
114 | if ((l & tracing()) == 0) |
115 | return; |
116 | |
117 | /* --- Now start work --- */ |
118 | |
ff0f0220 |
119 | fprintf(tracefp, "*** %s: %s\n", QUIS, s); |
0875b58f |
120 | |
121 | while (sz) { |
ff0f0220 |
122 | fprintf(tracefp, "*** %s: %08lx : ", QUIS, o); |
0875b58f |
123 | for (i = 0; i < 8; i++) { |
124 | if (i < sz) |
ff0f0220 |
125 | fprintf(tracefp, "%02x ", p[i]); |
0875b58f |
126 | else |
ff0f0220 |
127 | fputs("** ", tracefp); |
0875b58f |
128 | } |
ff0f0220 |
129 | fputs(": ", tracefp); |
0875b58f |
130 | for (i = 0; i < 8; i++) { |
131 | if (i < sz) |
ff0f0220 |
132 | fputc(isprint(p[i]) ? p[i] : '.', tracefp); |
0875b58f |
133 | else |
ff0f0220 |
134 | fputc('*', tracefp); |
0875b58f |
135 | } |
ff0f0220 |
136 | fputc('\n', tracefp); |
0875b58f |
137 | c = (sz >= 8) ? 8 : sz; |
138 | sz -= c, p += c, o += c; |
139 | } |
ff0f0220 |
140 | fflush(tracefp); |
0875b58f |
141 | } |
142 | |
143 | /* --- @trace_on@ --- * |
144 | * |
145 | * Arguments: @FILE *fp@ = a file to trace on |
146 | * @unsigned int l@ = trace level to set |
147 | * |
148 | * Returns: --- |
149 | * |
150 | * Use: Enables tracing to a file. |
151 | */ |
152 | |
153 | void trace_on(FILE *fp, unsigned int l) |
154 | { |
ff0f0220 |
155 | tracefp = fp; |
156 | if (!tracelvl) |
157 | tracelvl = l; |
0875b58f |
158 | } |
159 | |
160 | /* --- @trace_setLevel@ --- * |
161 | * |
162 | * Arguments: @unsigned int l@ = trace level to set |
163 | * |
164 | * Returns: --- |
165 | * |
166 | * Use: Sets the tracing level. |
167 | */ |
168 | |
169 | void trace_setLevel(unsigned int l) |
170 | { |
ff0f0220 |
171 | tracelvl = l; |
0875b58f |
172 | } |
173 | |
174 | /* --- @tracing@ --- * |
175 | * |
176 | * Arguments: --- |
177 | * |
178 | * Returns: Zero if not tracing, tracing level if tracing. |
179 | * |
180 | * Use: Informs the caller whether tracing is enabled. |
181 | */ |
182 | |
183 | unsigned int tracing(void) |
184 | { |
ff0f0220 |
185 | return (tracefp ? tracelvl : 0u); |
0875b58f |
186 | } |
187 | |
188 | /*----- That's all, folks -------------------------------------------------*/ |