File: debug.c

package info (click to toggle)
qstat 2.11-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,276 kB
  • ctags: 1,313
  • sloc: ansic: 18,279; sh: 3,372; makefile: 38
file content (142 lines) | stat: -rw-r--r-- 2,688 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * qstat 2.6
 * by Steve Jankowski
 *
 * debug helper functions
 * Copyright 2004 Ludwig Nussel
 *
 * Licensed under the Artistic License, see LICENSE.txt for license terms
 */

#include "debug.h"

#include <stdio.h>
#include <stdlib.h>
#ifndef _WIN32
#include <unistd.h>
#include <sys/param.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <fcntl.h>
#include <stdarg.h>

#ifdef DEBUG
void _debug(const char* file, int line, const char* function, int level, const char* fmt, ...)
{
	va_list ap;
	char buf[9];
	time_t now;

	now = time(NULL);
	strftime(buf,9,"%T",localtime(&now));

	fprintf(stderr, "debug(%d) %s %s:%d %s() - ", level, buf, file, line, function);

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
	if ( '\n' != fmt[strlen(fmt)-1]  )
	{
		fputs("\n", stderr);
	}
	return;
}
#endif

static int debug_level = 0;

void set_debug_level (int level)
{
  debug_level = level;
}

int get_debug_level (void)
{
  return (debug_level);
}

void malformed_packet(const struct qserver* server, const char* fmt, ...)
{
	va_list ap;

	if(!show_errors) return;

	fputs("malformed packet", stderr);

	if(server)
	{
		fprintf(stderr, " from %d.%d.%d.%d:%hu",
			server->ipaddr&0xff,
			(server->ipaddr>>8)&0xff,
			(server->ipaddr>>16)&0xff,
			(server->ipaddr>>24)&0xff,
			server->port);
	}
	fputs(": ", stderr);

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if(*fmt && fmt[strlen(fmt)-1] != '\n')
	{
		fputc('\n',stderr);
	}
}

#ifdef ENABLE_DUMP
static unsigned count = 0;
#endif
void dump_packet(const char* buf, int buflen)
{
#ifdef ENABLE_DUMP
	char fn[PATH_MAX] = {0};
	int fd;
	sprintf(fn, "dump%03u", count++);
	fprintf(stderr, "dumping to %s\n", fn);
	fd = open(fn, O_WRONLY|O_CREAT|O_EXCL, 0644);
	if(fd == -1) { perror("open"); return; }
	if(write(fd, buf, buflen) == -1)
		perror("write");
	close(fd);
#endif
}

void
print_packet( struct qserver *server, const char *buf, int buflen)
{
	static char *hex= "0123456789abcdef";
	unsigned char *p= (unsigned char*)buf;
	int i, h, a, b, astart, offset= 0;
	char line[256];

	if ( server != NULL)
	fprintf( stderr, "FROM %s len %d\n", server->arg, buflen );

	for ( i= buflen; i ; offset+= 16)  {
	memset( line, ' ', 256);
	h= 0;
	h+= sprintf( line, "%5d:", offset);
	a= astart = h + 16*2 + 16/4 + 2;
	for ( b=16; b && i; b--, i--, p++)  {
		if ( (b & 3) == 0)
		line[h++]= ' ';
		line[h++]= hex[*p >> 4];
		line[h++]= hex[*p & 0xf];
		if ( isprint( *p))
		line[a++]= *p;
		else
		line[a++]= '.';
		if((a-astart)==8) line[a++] = ' ';
	}
	line[a]= '\0';
	fputs( line, stderr);
	fputs( "\n", stderr);
	}
	fputs( "\n", stderr);
}