Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/port/log.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname port/log.c 1999/0316
## diff -e /dev/null /n/emeliedump/1999/0316/sys/src/brazil/port/log.c
0a
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"

static char Ebadlogctl[] = "unknown log ctl message";

enum {
	Nlog		= 4*1024,
};

void
logopen(Log *alog)
{
	lock(alog);
	if(waserror()){
		unlock(alog);
		nexterror();
	}
	if(alog->opens == 0){
		if(alog->buf == nil)
			alog->buf = malloc(Nlog);
		alog->rptr = alog->buf;
		alog->end = alog->buf + Nlog;
	}
	alog->opens++;
	unlock(alog);
	poperror();
}

void
logclose(Log *alog)
{
	lock(alog);
	if(waserror()){
		unlock(alog);
		nexterror();
	}
	alog->opens--;
	if(alog->opens == 0){
		free(alog->buf);
		alog->buf = nil;
	}
	unlock(alog);
	poperror();
}

static int
logready(void *a)
{
	Log *alog = a;

	return alog->len;
}

long
logread(Log *alog, void *a, ulong, long n)
{
	int i, d;
	char *p, *rptr;

	qlock(&alog->readq);
	if(waserror()){
		qunlock(&alog->readq);
		nexterror();
	}

	for(;;){
		lock(alog);
		if(alog->len){
			if(n > alog->len)
				n = alog->len;
			d = 0;
			rptr = alog->rptr;
			alog->rptr += n;
			if(alog->rptr >= alog->end){
				d = alog->rptr - alog->end;
				alog->rptr = alog->buf + d;
			}
			alog->len -= n;
			unlock(alog);

			i = n;
			p = a;
			if(d){
				memmove(p, rptr, d);
				i -= d;
				p += d;
				rptr = alog->buf;
			}
			memmove(p, rptr, i);
			break;
		}
		else
			unlock(alog);

		sleep(&alog->readr, logready, alog);
	}

	qunlock(&alog->readq);
	poperror();

	return n;
}

char*
logctl(Log *alog, int argc, char *argv[], Logflag *flags)
{
	int i, n, set;
	Logflag *fp;

	if(argc < 2)
		return Ebadlogctl;

	if(strcmp("set", argv[0]) == 0)
		set = 1;
	else if(strcmp("clear", argv[0]) == 0)
		set = 0;
	else
		return Ebadlogctl;

	for(i = 1; i < argc; i++){
		for(fp = flags; fp->name; fp++)
			if(strcmp(fp->name, argv[i]) == 0)
				break;
		if(fp->name == nil)
			continue;
		if(set)
			alog->logmask |= fp->mask;
		else
			alog->logmask &= ~fp->mask;
	}

	return nil;
}

void
log(Log *alog, int mask, char *fmt, ...)
{
	char buf[128], *t, *fp;
	int i, n;
	va_list arg;

	if(!(alog->logmask & mask))
		return;

	va_start(arg, fmt);
	n = doprint(buf, buf+sizeof(buf), fmt, arg) - buf;
	va_end(arg);

	if(alog->opens == 0)
		return;

	lock(alog);
	i = alog->len + n - Nlog;
	if(i > 0){
		alog->len -= i;
		alog->rptr += i;
		if(alog->rptr >= alog->end)
			alog->rptr = alog->buf + (alog->rptr - alog->end);
	}
	t = alog->rptr + alog->len;
	fp = buf;
	alog->len += n;
	while(n-- > 0){
		if(t >= alog->end)
			t = alog->buf + (t - alog->end);
		*t++ = *fp++;
	}
	unlock(alog);

	wakeup(&alog->readr);
}
.
## diffname port/log.c 1999/0630
## diff -e /n/emeliedump/1999/0316/sys/src/brazil/port/log.c /n/emeliedump/1999/0630/sys/src/brazil/port/log.c
111c
	int i, set;
.
## diffname port/log.c 1999/0731
## diff -e /n/emeliedump/1999/0630/sys/src/brazil/port/log.c /n/emeliedump/1999/0731/sys/src/brazil/port/log.c
93a
			memmove(p+i, alog->buf, d);
.
87,92d
85c
			i = n-d;
.
## diffname port/log.c 2000/1018
## diff -e /n/emeliedump/1999/0731/sys/src/brazil/port/log.c /n/emeliedump/2000/1018/sys/src/9/port/log.c
170a

void
vlog(Log *alog, int mask, char *fmt, va_list arg)
{
	char buf[128];
	int n;

	if(!(alog->logmask & mask))
		return;

	if(alog->opens == 0)
		return;

	n = doprint(buf, buf+sizeof(buf), fmt, arg) - buf;

	logn(alog, mask, buf, n);
}

void
log(Log *alog, int mask, char *fmt, ...)
{
	va_list arg;

	va_start(arg, fmt);
	vlog(alog, mask, fmt, arg);
	va_end(arg);
}

.
169c
	if(dowake)
		wakeup(&alog->readr);
.
166a
	dowake = alog->len >= alog->minread;
.
152c
	i = alog->len + n - alog->nlog;
.
150a
	if(n > alog->nlog)
		return;

.
144,147d
137,139c
	char *fp, *t;
	int dowake, i;
.
135c
logn(Log *alog, int mask, void *buf, int n)
.
72c
		if(alog->len >= alog->minread || alog->len >= n){
.
55c
	return alog->len >= alog->minread;
.
26c
		alog->end = alog->buf + alog->nlog;
		alog->len = 0;
.
24c
			alog->buf = malloc(alog->nlog);
.
22a
		if(alog->nlog == 0)
			alog->nlog = 4*1024;
		if(alog->minread == 0)
			alog->minread = 1;
.
10,13d
## diffname port/log.c 2001/0503
## diff -e /n/emeliedump/2000/1018/sys/src/9/port/log.c /n/emeliedump/2001/0503/sys/src/9/port/log.c
48d
38,41d
## diffname port/log.c 2002/0217
## diff -e /n/emeliedump/2001/0503/sys/src/9/port/log.c /n/emeliedump/2002/0217/sys/src/9/port/log.c
194a
	logn(alog, mask, buf, n);
}
.
193d
191c
	n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
.
180,189d
172a
	va_list arg;
	char buf[128];
.
171d
169c
log(Log *alog, int mask, char *fmt, ...)
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.