xref: /plan9/sys/src/cmd/ip/httpd/log.c (revision a84536681645e23c630ce4ef2e5c3b284d4c590b)
1 #include <u.h>
2 #include <libc.h>
3 #include "httpd.h"
4 #include "httpsrv.h"
5 
6 int		logall[2];
7 
8 void
9 logit(HConnect *c, char *fmt, ...)
10 {
11 	char buf[4096];
12 	va_list arg;
13 	HSPriv *p;
14 
15 	va_start(arg, fmt);
16 	vseprint(buf, buf+sizeof(buf), fmt, arg);
17 	va_end(arg);
18 	p = nil;
19 	if(c != nil)
20 		p = c->private;
21 	if(p != nil && p->remotesys != nil)
22 		syslog(0, HTTPLOG, "%s %s", p->remotesys, buf);
23 	else
24 		syslog(0, HTTPLOG, "%s", buf);
25 }
26 
27 void
28 writelog(HConnect *c, char *fmt, ...)
29 {
30 	HSPriv *p;
31 	char buf[HBufSize+500], *bufp, *bufe;
32 	ulong now, today;
33 	int logfd;
34 	va_list arg;
35 
36 	bufe = buf + sizeof(buf);
37 	now = time(nil);
38 	today = now / (24*60*60);
39 	logfd = logall[today & 1];
40 	if(c == nil || logfd <= 0)
41 		return;
42 	p = c->private;
43 	if(c->hstop == c->header || c->hstop[-1] != '\n')
44 		*c->hstop = '\n';
45 	*c->hstop = '\0';
46 	bufp = seprint(buf, bufe, "==========\n");
47 	bufp = seprint(bufp, bufe, "LogTime:  %D\n", now);
48 	bufp = seprint(bufp, bufe, "ConnTime: %D\n", c->reqtime);
49 	bufp = seprint(bufp, bufe, "RemoteIP: %s\n", p->remotesys);
50 	bufp = seprint(bufp, bufe, "Port: %s\n", p->remoteserv);
51 	va_start(arg, fmt);
52 	bufp = vseprint(bufp, bufe, fmt, arg);
53 	va_end(arg);
54 	if(c->req.uri != nil && c->req.uri[0] != 0)
55 		bufp = seprint(bufp, bufe, "FinalURI: %s\n", c->req.uri);
56 	bufp = seprint(bufp, bufe, "----------\n%s\n", (char*)c->header);
57 	write(logfd, buf, bufp-buf);   /* append-only file */
58 }
59