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