summaryrefslogtreecommitdiff
path: root/sys/src/cmd/aquarela/smblog.c
blob: 763ac268a07ab6bddf263050df713314f10a51eb (plain)
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
#include "headers.h"

static QLock logreflock, logprintlock;
static int locked;

void
smbloglock(void)
{
	qlock(&logreflock);
	if (locked++ == 0)
		qlock(&logprintlock);
	qunlock(&logreflock);
}

void
smblogunlock(void)
{
	qlock(&logreflock);
	if (locked && --locked == 0)
		qunlock(&logprintlock);
	qunlock(&logreflock);
}

static int
smbloglockedvprint(char *fmt, va_list ap)
{
	if (smbglobals.log.fd >= 0)
		vfprint(smbglobals.log.fd, fmt, ap);
	if (smbglobals.log.print)
		vfprint(2, fmt, ap);
	return 0;
}

int
smblogvprint(int cmd, char *fmt, va_list ap)
{
	if (cmd < 0 || smboptable[cmd].debug) {
		smbloglock();
		smbloglockedvprint(fmt, ap);
		smblogunlock();
	}
	return 0;
}

int
smblogprint(int cmd, char *fmt, ...)
{
	if (cmd < 0 || smbtrans2optable[cmd].debug) {
		va_list ap;
		va_start(ap, fmt);
		smblogvprint(cmd, fmt, ap);
		va_end(ap);
	}
	return 0;
}

int
translogprint(int cmd, char *fmt, ...)
{
	if (cmd < 0 || smboptable[cmd].debug) {
		va_list ap;
		va_start(ap, fmt);
		smblogvprint(cmd, fmt, ap);
		va_end(ap);
	}
	return 0;
}

int
smblogprintif(int v, char *fmt, ...)
{
	if (v) {
		va_list ap;
		va_start(ap, fmt);
		smbloglock();
		smbloglockedvprint(fmt, ap);
		smblogunlock();
		va_end(ap);
	}
	return 0;
}

void
smblogdata(int cmd, int (*print)(int cmd, char *fmt, ...), void *ap, long n, long limit)
{
	uchar *p = ap;
	long i;
	long saven;
	i = 0;
	saven = n;
	if (saven > limit)
		n = limit;
	while (i < n) {
		int l = n - i < 16 ? n - i : 16;
		int b;
		(*print)(cmd, "0x%.4lux  ", i);
		for (b = 0; b < l; b += 2) {
			(*print)(cmd, " %.2ux", p[i + b]);
			if (b < l - 1)
				(*print)(cmd, "%.2ux", p[i + b + 1]);
			else
				(*print)(cmd, "  ");
		}
		while (b < 16) {
			(*print)(cmd, "     ");
			b += 2;
		}
		(*print)(cmd, "        ");
		for (b = 0; b < l; b++)
			if (p[i + b] >= ' ' && p[i + b] <= '~')
				(*print)(cmd, "%c", p[i + b]);
			else
				(*print)(cmd, ".");
		(*print)(cmd, "\n");
		i += l;
	}
	if (saven > limit)
		(*print)(cmd, "0x%.4ux   ...\n0x%.4ux\n", limit, saven);
}