1 /*
2  * debug.c
3  * (C)1998-2011 by Marc Huber <Marc.Huber@web.de>
4  *
5  * $Id: debug.c,v 1.18 2015/03/14 06:11:27 marc Exp $
6  *
7  */
8 
9 #ifndef __GNUC__
10 #define __attribute__(A)
11 #endif				/* __GNUC__ */
12 
13 static const char debugrcsid[] __attribute__ ((used)) = "$Id: debug.c,v 1.18 2015/03/14 06:11:27 marc Exp $";
14 
15 #define _DEBUG_MAIN_
16 #include <stdio.h>
17 #include <stdarg.h>
18 #include <unistd.h>
19 #include <ctype.h>
20 #include <errno.h>
21 
22 #include "debug.h"
23 #include "misc/memops.h"
24 #include "mavis.h"
25 
debug_setpid()26 void debug_setpid()
27 {
28     common_data.pid = getpid();
29 }
30 
debug(u_long level,char * format,...)31 void debug(u_long level, char *format, ...)
32 {
33     if (level & common_data.debug) {
34 	static int indent = 0;
35 	va_list ap;
36 	int olderrno;
37 	size_t len = 1024, nlen;
38 	char *tmpbuf = alloca(len);
39 	char *t;
40 	char spaces[] = "                                        " "                                        " "                                        ";
41 
42 	olderrno = errno;
43 
44 	va_start(ap, format);
45 	nlen = vsnprintf(tmpbuf, len, format, ap);
46 	va_end(ap);
47 	if (nlen >= len) {
48 	    tmpbuf = alloca(++nlen);
49 	    va_start(ap, format);
50 	    vsnprintf(tmpbuf, nlen, format, ap);
51 	    va_end(ap);
52 	}
53 
54 	if (tmpbuf[0] == '-' && indent > 0)
55 	    indent--;
56 
57 	for (t = tmpbuf; t[0] && t[1]; t++)
58 	    if (iscntrl((int) *t))
59 		*t = '^';
60 
61 	fprintf(stderr, "%6ld: %.*s%s", (long) common_data.pid, indent, spaces, tmpbuf);
62 
63 	if (tmpbuf[0] == '+' && indent < (int) sizeof(spaces) - 2)
64 	    indent++;
65 
66 	fflush(stderr);
67 	errno = olderrno;
68     }
69 }
70