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