Subversion Repositories planix.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#include "dat.h"
2
 
3
void
4
logbufproc(Logbuf *lb)
5
{
6
	char *s;
7
	int n;
8
	Req *r;
9
 
10
	while(lb->wait && lb->rp != lb->wp){
11
		r = lb->wait;
12
		lb->wait = r->aux;
13
		if(lb->wait == nil)
14
			lb->waitlast = &lb->wait;
15
		r->aux = nil;
16
		if(r->ifcall.count < 5){
17
			respond(r, "factotum: read request count too short");
18
			continue;
19
		}
20
		s = lb->msg[lb->rp];
21
		lb->msg[lb->rp] = nil;
22
		if(++lb->rp == nelem(lb->msg))
23
			lb->rp = 0;
24
		n = r->ifcall.count;
25
		if(n < strlen(s)+1+1){
26
			memmove(r->ofcall.data, s, n-5);
27
			n -= 5;
28
			r->ofcall.data[n] = '\0';
29
			/* look for first byte of UTF-8 sequence by skipping continuation bytes */
30
			while(n>0 && (r->ofcall.data[--n]&0xC0)==0x80)
31
				;
32
			strcpy(r->ofcall.data+n, "...\n");
33
		}else{
34
			strcpy(r->ofcall.data, s);
35
			strcat(r->ofcall.data, "\n");
36
		}
37
		r->ofcall.count = strlen(r->ofcall.data);
38
		free(s);
39
		respond(r, nil);
40
	}
41
}
42
 
43
void
44
logbufread(Logbuf *lb, Req *r)
45
{
46
	if(lb->waitlast == nil)
47
		lb->waitlast = &lb->wait;
48
	*(lb->waitlast) = r;
49
	lb->waitlast = &r->aux;
50
	r->aux = nil;
51
	logbufproc(lb);
52
}
53
 
54
void
55
logbufflush(Logbuf *lb, Req *r)
56
{
57
	Req **l;
58
 
59
	for(l=&lb->wait; *l; l=&(*l)->aux){
60
		if(*l == r){
61
			*l = r->aux;
62
			r->aux = nil;
63
			if(*l == nil)
64
				lb->waitlast = l;
65
			respond(r, "interrupted");
66
			break;
67
		}
68
	}
69
}
70
 
71
void
72
logbufappend(Logbuf *lb, char *buf)
73
{
74
	if(debug)
75
		fprint(2, "%s\n", buf);
76
 
77
	if(lb->msg[lb->wp])
78
		free(lb->msg[lb->wp]);
79
	lb->msg[lb->wp] = estrdup9p(buf);
80
	if(++lb->wp == nelem(lb->msg))
81
		lb->wp = 0;
82
	logbufproc(lb);
83
}
84
 
85
Logbuf logbuf;
86
 
87
void
88
logread(Req *r)
89
{
90
	logbufread(&logbuf, r);
91
}
92
 
93
void
94
logflush(Req *r)
95
{
96
	logbufflush(&logbuf, r);
97
}
98
 
99
void
100
flog(char *fmt, ...)
101
{
102
	char buf[1024];
103
	va_list arg;
104
 
105
	va_start(arg, fmt);
106
	vseprint(buf, buf+sizeof buf, fmt, arg);
107
	va_end(arg);
108
	logbufappend(&logbuf, buf);
109
}
110