1
0
mirror of https://github.com/openbsd/src.git synced 2024-12-21 23:18:00 -08:00

sndiod: Switch to printf(3)-style logging

With this change, any line is logged using a single function call
(instead of one function call per logged token). This makes the
sources easier to read and reduces the binary size. Debug strings that
would be too difficult to produce with snprintf(3) are simplified.

ok tb, dlg
This commit is contained in:
ratchov 2024-12-20 07:35:56 +00:00
parent 9e4e626151
commit 7b6392009e
19 changed files with 526 additions and 1597 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: abuf.c,v 1.5 2022/12/26 19:16:03 jmc Exp $ */
/* $OpenBSD: abuf.c,v 1.6 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -29,18 +29,6 @@
#include "abuf.h"
#include "utils.h"
#ifdef DEBUG
void
abuf_log(struct abuf *buf)
{
log_putu(buf->start);
log_puts("+");
log_putu(buf->used);
log_puts("/");
log_putu(buf->len);
}
#endif
void
abuf_init(struct abuf *buf, unsigned int len)
{
@ -54,13 +42,8 @@ void
abuf_done(struct abuf *buf)
{
#ifdef DEBUG
if (buf->used > 0) {
if (log_level >= 3) {
log_puts("deleting non-empty buffer, used = ");
log_putu(buf->used);
log_puts("\n");
}
}
if (buf->used > 0)
logx(3, "deleting non-empty buffer, used = %d", buf->used);
#endif
xfree(buf->data);
buf->data = (void *)0xdeadbeef;
@ -89,9 +72,7 @@ abuf_rdiscard(struct abuf *buf, int count)
{
#ifdef DEBUG
if (count < 0 || count > buf->used) {
log_puts("abuf_rdiscard: bad count = ");
log_putu(count);
log_puts("\n");
logx(0, "%s: bad count = %d", __func__, count);
panic();
}
#endif
@ -109,9 +90,7 @@ abuf_wcommit(struct abuf *buf, int count)
{
#ifdef DEBUG
if (count < 0 || count > (buf->len - buf->used)) {
log_puts("abuf_wcommit: bad count = ");
log_putu(count);
log_puts("\n");
logx(0, "%s: bad count = %d", __func__, count);
panic();
}
#endif

View File

@ -1,4 +1,4 @@
/* $OpenBSD: abuf.h,v 1.2 2016/03/23 06:11:52 ratchov Exp $ */
/* $OpenBSD: abuf.h,v 1.3 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -26,7 +26,6 @@ struct abuf {
void abuf_init(struct abuf *, unsigned int);
void abuf_done(struct abuf *);
void abuf_log(struct abuf *);
unsigned char *abuf_rgetblk(struct abuf *, int *);
unsigned char *abuf_wgetblk(struct abuf *, int *);
void abuf_rdiscard(struct abuf *, int);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: dev.c,v 1.118 2024/08/23 01:19:33 jsg Exp $ */
/* $OpenBSD: dev.c,v 1.119 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -54,7 +54,6 @@ unsigned int dev_roundof(struct dev *, unsigned int);
void dev_wakeup(struct dev *);
void slot_ctlname(struct slot *, char *, size_t);
void slot_log(struct slot *);
void slot_del(struct slot *);
void slot_setvol(struct slot *, unsigned int);
void slot_ready(struct slot *);
@ -65,9 +64,6 @@ void slot_write(struct slot *);
void slot_read(struct slot *);
int slot_skip(struct slot *);
void ctl_node_log(struct ctl_node *);
void ctl_log(struct ctl *);
struct slotops zomb_slotops = {
zomb_onmove,
zomb_onvol,
@ -107,53 +103,12 @@ slot_array_init(void)
}
}
void
dev_log(struct dev *d)
{
#ifdef DEBUG
static char *pstates[] = {
"cfg", "ini", "run"
};
#endif
log_puts("snd");
log_putu(d->num);
#ifdef DEBUG
if (log_level >= 3) {
log_puts(" pst=");
log_puts(pstates[d->pstate]);
}
#endif
}
void
slot_ctlname(struct slot *s, char *name, size_t size)
{
snprintf(name, size, "%s%u", s->name, s->unit);
}
void
slot_log(struct slot *s)
{
char name[CTL_NAMEMAX];
#ifdef DEBUG
static char *pstates[] = {
"ini", "sta", "rdy", "run", "stp", "mid"
};
#endif
slot_ctlname(s, name, CTL_NAMEMAX);
log_puts(name);
#ifdef DEBUG
if (log_level >= 3) {
log_puts(" vol=");
log_putu(s->vol);
if (s->ops) {
log_puts(",pst=");
log_puts(pstates[s->pstate]);
}
}
#endif
}
void
zomb_onmove(void *arg)
{
@ -180,10 +135,7 @@ zomb_eof(void *arg)
struct slot *s = arg;
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": zomb_eof\n");
}
logx(3, "%s%u: %s", s->name, s->unit, __func__);
#endif
s->ops = NULL;
}
@ -194,13 +146,29 @@ zomb_exit(void *arg)
#ifdef DEBUG
struct slot *s = arg;
if (log_level >= 3) {
slot_log(s);
log_puts(": zomb_exit\n");
}
logx(3, "%s%u: %s", s->name, s->unit, __func__);
#endif
}
size_t
chans_fmt(char *buf, size_t size, int mode, int pmin, int pmax, int rmin, int rmax)
{
const char *sep = "";
char *end = buf + size;
char *p = buf;
if (mode & MODE_PLAY) {
p += snprintf(p, p < end ? end - p : 0, "play %d:%d", pmin, pmax);
sep = ", ";
}
if (mode & MODE_RECMASK) {
p += snprintf(p, p < end ? end - p : 0, "%s%s %d:%d", sep,
(mode & MODE_MON) ? "mon" : "rec", rmin, rmax);
}
return p - buf;
}
/*
* Broadcast MIDI data to all opts using this device
*/
@ -307,14 +275,7 @@ mtc_midi_full(struct mtc *mtc)
mtc->fps = 24;
}
#ifdef DEBUG
if (log_level >= 3) {
dev_log(mtc->dev);
log_puts(": mtc full frame at ");
log_puti(mtc->delta);
log_puts(", ");
log_puti(mtc->fps);
log_puts(" fps\n");
}
logx(3, "%s: mtc full frame at %d, %d fps", mtc->dev->path, mtc->delta, mtc->fps);
#endif
fps = mtc->fps;
mtc->hr = (mtc->origin / (MTC_SEC * 3600)) % 24;
@ -452,10 +413,7 @@ slot_skip(struct slot *s)
break;
}
#ifdef DEBUG
if (log_level >= 4) {
slot_log(s);
log_puts(": skipped a cycle\n");
}
logx(4, "%s%u: skipped a cycle", s->name, s->unit);
#endif
if (s->pstate != SLOT_STOP && (s->mode & MODE_RECMASK)) {
if (s->sub.encbuf)
@ -485,10 +443,8 @@ dev_mix_badd(struct dev *d, struct slot *s)
idata = (adata_t *)abuf_rgetblk(&s->mix.buf, &icount);
#ifdef DEBUG
if (icount < s->round * s->mix.bpf) {
slot_log(s);
log_puts(": not enough data to mix (");
log_putu(icount);
log_puts("bytes)\n");
logx(0, "%s%u: not enough data to mix (%u bytes)",
s->name, s->unit, icount);
panic();
}
#endif
@ -577,14 +533,8 @@ dev_mix_adjvol(struct dev *d)
i->mix.weight = d->master_enabled ?
ADATA_MUL(weight, MIDI_TO_ADATA(d->master)) : weight;
#ifdef DEBUG
if (log_level >= 3) {
slot_log(i);
log_puts(": set weight: ");
log_puti(i->mix.weight);
log_puts("/");
log_puti(i->opt->maxweight);
log_puts("\n");
}
logx(3, "%s%u: set weight: %d / %d", i->name, i->unit, i->mix.weight,
i->opt->maxweight);
#endif
}
}
@ -605,7 +555,7 @@ dev_sub_bcopy(struct dev *d, struct slot *s)
odata = (adata_t *)abuf_wgetblk(&s->sub.buf, &ocount);
#ifdef DEBUG
if (ocount < s->round * s->sub.bpf) {
log_puts("dev_sub_bcopy: not enough space\n");
logx(0, "dev_sub_bcopy: not enough space");
panic();
}
#endif
@ -681,10 +631,7 @@ dev_cycle(struct dev *d)
*/
if (d->slot_list == NULL && d->idle >= d->bufsz &&
(mtc_array[0].dev != d || mtc_array[0].tstate != MTC_RUN)) {
if (log_level >= 2) {
dev_log(d);
log_puts(": device stopped\n");
}
logx(2, "%s: device stopped", d->path);
dev_sio_stop(d);
d->pstate = DEV_INIT;
if (d->refcnt == 0)
@ -694,12 +641,7 @@ dev_cycle(struct dev *d)
if (d->prime > 0) {
#ifdef DEBUG
if (log_level >= 4) {
dev_log(d);
log_puts(": empty cycle, prime = ");
log_putu(d->prime);
log_puts("\n");
}
logx(4, "%s: empty cycle, prime = %u", d->path, d->prime);
#endif
base = (unsigned char *)DEV_PBUF(d);
nsamp = d->round * d->pchan;
@ -714,16 +656,7 @@ dev_cycle(struct dev *d)
d->delta -= d->round;
#ifdef DEBUG
if (log_level >= 4) {
dev_log(d);
log_puts(": full cycle: delta = ");
log_puti(d->delta);
if (d->mode & MODE_PLAY) {
log_puts(", poffs = ");
log_puti(d->poffs);
}
log_puts("\n");
}
logx(4, "%s: full cycle: delta = %d", d->path, d->delta);
#endif
if (d->mode & MODE_PLAY) {
base = (unsigned char *)DEV_PBUF(d);
@ -735,13 +668,7 @@ dev_cycle(struct dev *d)
ps = &d->slot_list;
while ((s = *ps) != NULL) {
#ifdef DEBUG
if (log_level >= 4) {
slot_log(s);
log_puts(": running");
log_puts(", skip = ");
log_puti(s->skip);
log_puts("\n");
}
logx(4, "%s%u: running, skip = %d", s->name, s->unit, s->skip);
#endif
d->idle = 0;
@ -757,8 +684,8 @@ dev_cycle(struct dev *d)
#ifdef DEBUG
if (s->pstate == SLOT_STOP && !(s->mode & MODE_PLAY)) {
slot_log(s);
log_puts(": rec-only slots can't be drained\n");
logx(0, "%s%u: rec-only slots can't be drained",
s->name, s->unit);
panic();
}
#endif
@ -778,10 +705,7 @@ dev_cycle(struct dev *d)
slot_freebufs(s);
dev_mix_adjvol(d);
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": drained\n");
}
logx(3, "%s%u: drained", s->name, s->unit);
#endif
continue;
}
@ -796,10 +720,7 @@ dev_cycle(struct dev *d)
s->round * s->sub.bpf)) {
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": xrun, pause cycle\n");
}
logx(3, "%s%u: xrun, pause cycle", s->name, s->unit);
#endif
if (s->xrun == XRUN_IGNORE) {
s->delta -= s->round;
@ -812,8 +733,7 @@ dev_cycle(struct dev *d)
*ps = s->next;
} else {
#ifdef DEBUG
slot_log(s);
log_puts(": bad xrun mode\n");
logx(0, "%s%u: bad xrun mode", s->name, s->unit);
panic();
#endif
}
@ -825,12 +745,8 @@ dev_cycle(struct dev *d)
s->ops->flush(s->arg);
} else {
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": prime = ");
log_puti(s->sub.prime);
log_puts("\n");
}
logx(3, "%s%u: prime = %d", s->name, s->unit,
s->sub.prime);
#endif
s->sub.prime--;
}
@ -890,12 +806,8 @@ dev_master(struct dev *d, unsigned int master)
struct ctl *c;
unsigned int v;
if (log_level >= 2) {
dev_log(d);
log_puts(": master volume set to ");
log_putu(master);
log_puts("\n");
}
logx(2, "%s: master volume set to %u", d->path, master);
if (d->master_enabled) {
d->master = master;
if (d->mode & MODE_PLAY)
@ -926,8 +838,7 @@ dev_new(char *path, struct aparams *par,
struct dev *d, **pd;
if (dev_sndnum == DEV_NMAX) {
if (log_level >= 1)
log_puts("too many devices\n");
logx(1, "too many devices");
return NULL;
}
d = xmalloc(sizeof(struct dev));
@ -984,6 +895,8 @@ dev_adjpar(struct dev *d, int mode,
int
dev_allocbufs(struct dev *d)
{
char enc_str[ENCMAX], chans_str[64];
/*
* Create record buffer.
*/
@ -1022,29 +935,14 @@ dev_allocbufs(struct dev *d)
*/
memset(d->rbuf, 0, d->round * d->rchan * sizeof(adata_t));
if (log_level >= 2) {
dev_log(d);
log_puts(": ");
log_putu(d->rate);
log_puts("Hz, ");
aparams_log(&d->par);
if (d->mode & MODE_PLAY) {
log_puts(", play 0:");
log_puti(d->pchan - 1);
}
if (d->mode & MODE_REC) {
log_puts(", rec 0:");
log_puti(d->rchan - 1);
}
log_puts(", ");
log_putu(d->bufsz / d->round);
log_puts(" blocks of ");
log_putu(d->round);
log_puts(" frames");
if (d == mtc_array[0].dev)
log_puts(", mtc");
log_puts("\n");
}
logx(2, "%s: %dHz, %s, %s, %d blocks of %d frames",
d->path, d->rate,
(aparams_enctostr(&d->par, enc_str), enc_str),
(chans_fmt(chans_str, sizeof(chans_str),
d->mode & (MODE_PLAY | MODE_REC),
0, d->pchan - 1, 0, d->rchan - 1), chans_str),
d->bufsz / d->round, d->round);
return 1;
}
@ -1066,10 +964,7 @@ dev_open(struct dev *d)
if (d->rchan == 0)
d->rchan = 2;
if (!dev_sio_open(d)) {
if (log_level >= 1) {
dev_log(d);
log_puts(": failed to open audio device\n");
}
logx(1, "%s: failed to open audio device", d->path);
return 0;
}
if (!dev_allocbufs(d))
@ -1127,10 +1022,7 @@ void
dev_freebufs(struct dev *d)
{
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": closing\n");
}
logx(3, "%s: closing", d->path);
#endif
if (d->mode & MODE_PLAY) {
if (d->encbuf != NULL)
@ -1164,10 +1056,7 @@ int
dev_ref(struct dev *d)
{
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": device requested\n");
}
logx(3, "%s: device requested", d->path);
#endif
if (d->pstate == DEV_CFG && !dev_open(d))
return 0;
@ -1179,10 +1068,7 @@ void
dev_unref(struct dev *d)
{
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": device released\n");
}
logx(3, "%s: device released", d->path);
#endif
d->refcnt--;
if (d->refcnt == 0 && d->pstate == DEV_INIT)
@ -1197,10 +1083,9 @@ dev_init(struct dev *d)
{
if ((d->reqmode & MODE_AUDIOMASK) == 0) {
#ifdef DEBUG
dev_log(d);
log_puts(": has no streams\n");
logx(1, "%s: has no streams", d->path);
#endif
return 0;
return 0;
}
if (d->hold && !dev_ref(d))
return 0;
@ -1214,10 +1099,7 @@ void
dev_done(struct dev *d)
{
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": draining\n");
}
logx(3, "%s: draining", d->path);
#endif
if (mtc_array[0].dev == d && mtc_array[0].tstate != MTC_STOP)
mtc_stop(&mtc_array[0]);
@ -1246,18 +1128,14 @@ dev_del(struct dev *d)
struct dev **p;
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": deleting\n");
}
logx(3, "%s: deleting", d->path);
#endif
if (d->pstate != DEV_CFG)
dev_close(d);
for (p = &dev_list; *p != d; p = &(*p)->next) {
#ifdef DEBUG
if (*p == NULL) {
dev_log(d);
log_puts(": device to delete not on the list\n");
logx(0, "%s: not on the list", d->path);
panic();
}
#endif
@ -1279,10 +1157,8 @@ void
dev_wakeup(struct dev *d)
{
if (d->pstate == DEV_INIT) {
if (log_level >= 2) {
dev_log(d);
log_puts(": device started\n");
}
logx(2, "%s: started", d->path);
if (d->mode & MODE_PLAY) {
d->prime = d->bufsz;
} else {
@ -1311,12 +1187,7 @@ dev_iscompat(struct dev *o, struct dev *n)
{
if (((long long)o->round * n->rate != (long long)n->round * o->rate) ||
((long long)o->bufsz * n->rate != (long long)n->bufsz * o->rate)) {
if (log_level >= 1) {
log_puts(n->name);
log_puts(": not compatible with ");
log_puts(o->name);
log_puts("\n");
}
logx(1, "%s: not compatible with %s", n->name, o->name);
return 0;
}
return 1;
@ -1343,10 +1214,7 @@ dev_migrate(struct dev *odev)
/* try next one, circulating through the list */
ndev = ndev->alt_next;
if (ndev == odev) {
if (log_level >= 1) {
dev_log(odev);
log_puts(": no fall-back device found\n");
}
logx(1, "%s: no fall-back device found", odev->path);
return NULL;
}
@ -1364,12 +1232,7 @@ dev_migrate(struct dev *odev)
break;
}
if (log_level >= 1) {
dev_log(odev);
log_puts(": switching to ");
dev_log(ndev);
log_puts("\n");
}
logx(1, "%s: switching to %s", odev->path, ndev->path);
if (mtc_array[0].dev == odev)
mtc_setdev(&mtc_array[0], ndev);
@ -1410,10 +1273,7 @@ mtc_trigger(struct mtc *mtc)
struct slot *s;
if (mtc->tstate != MTC_START) {
if (log_level >= 2) {
dev_log(mtc->dev);
log_puts(": not started by mmc yet, waiting...\n");
}
logx(2, "%s: not started by mmc yet, waiting.", mtc->dev->path);
return;
}
@ -1422,10 +1282,7 @@ mtc_trigger(struct mtc *mtc)
continue;
if (s->pstate != SLOT_READY) {
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": not ready, start delayed\n");
}
logx(3, "%s%u: not ready, start delayed", s->name, s->unit);
#endif
return;
}
@ -1455,10 +1312,7 @@ mtc_start(struct mtc *mtc)
mtc_trigger(mtc);
#ifdef DEBUG
} else {
if (log_level >= 3) {
dev_log(mtc->dev);
log_puts(": ignoring mmc start\n");
}
logx(3, "%s: ignoring mmc start", mtc->dev->path);
#endif
}
}
@ -1479,10 +1333,7 @@ mtc_stop(struct mtc *mtc)
break;
default:
#ifdef DEBUG
if (log_level >= 3) {
dev_log(mtc->dev);
log_puts(": ignored mmc stop\n");
}
logx(3, "%s: ignored mmc stop", mtc->dev->path);
#endif
return;
}
@ -1494,12 +1345,8 @@ mtc_stop(struct mtc *mtc)
void
mtc_loc(struct mtc *mtc, unsigned int origin)
{
if (log_level >= 2) {
dev_log(mtc->dev);
log_puts(": relocated to ");
log_putu(origin);
log_puts("\n");
}
logx(2, "%s: relocated to %u", mtc->dev->path, origin);
if (mtc->tstate == MTC_RUN)
mtc_stop(mtc);
mtc->origin = origin;
@ -1518,10 +1365,7 @@ mtc_setdev(struct mtc *mtc, struct dev *d)
if (mtc->dev == d)
return;
if (log_level >= 2) {
dev_log(d);
log_puts(": set to be MIDI clock source\n");
}
logx(2, "%s: set to be MIDI clock source", d->path);
/* adjust clock and ref counter, if needed */
if (mtc->tstate == MTC_RUN) {
@ -1655,14 +1499,8 @@ slot_allocbufs(struct slot *s)
}
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": allocated ");
log_putu(s->appbufsz);
log_puts("/");
log_putu(SLOT_BUFSZ(s));
log_puts(" fr buffers\n");
}
logx(3, "%s%u: allocated %u/%u fr buffers",
s->name, s->unit, s->appbufsz, SLOT_BUFSZ(s));
#endif
}
@ -1757,10 +1595,7 @@ slot_new(struct opt *opt, unsigned int id, char *who,
}
if (bestidx == DEV_NSLOT) {
if (log_level >= 1) {
log_puts(name);
log_puts(": out of sub-device slots\n");
}
logx(1, "%s: out of sub-device slots", name);
return NULL;
}
@ -1800,14 +1635,7 @@ found:
dev_midi_slotdesc(s->opt->dev, s);
dev_midi_vol(s->opt->dev, s);
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": using ");
log_puts(s->opt->name);
log_puts(", mode = ");
log_putx(mode);
log_puts("\n");
}
logx(3, "%s%u: using %s, mode = %x", s->name, s->unit, s->opt->name, mode);
#endif
return s;
}
@ -1841,12 +1669,7 @@ void
slot_setvol(struct slot *s, unsigned int vol)
{
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": setting volume ");
log_putu(vol);
log_puts("\n");
}
logx(3, "%s%u: setting volume %u", s->name, s->unit, vol);
#endif
s->vol = vol;
s->mix.vol = MIDI_TO_ADATA(s->vol);
@ -1865,12 +1688,7 @@ slot_setopt(struct slot *s, struct opt *o)
if (s->opt == NULL || s->opt == o)
return;
if (log_level >= 2) {
slot_log(s);
log_puts(": moving to opt ");
log_puts(o->name);
log_puts("\n");
}
logx(2, "%s%u: moving to opt %s", s->name, s->unit, o->name);
odev = s->opt->dev;
if (s->ops != NULL) {
@ -1919,12 +1737,8 @@ slot_attach(struct slot *s)
if (((s->mode & MODE_PLAY) && !(s->opt->mode & MODE_PLAY)) ||
((s->mode & MODE_RECMASK) && !(s->opt->mode & MODE_RECMASK))) {
if (log_level >= 1) {
slot_log(s);
log_puts(" at ");
log_puts(s->opt->name);
log_puts(": mode not allowed on this sub-device\n");
}
logx(1, "%s%u at %s: mode not allowed", s->name, s->unit, s->opt->name);
return;
}
/*
@ -1951,16 +1765,8 @@ slot_attach(struct slot *s)
}
#ifdef DEBUG
if (log_level >= 2) {
slot_log(s);
log_puts(": attached at ");
log_puti(s->delta);
log_puts(" + ");
log_puti(s->delta_rem);
log_puts("/");
log_puti(s->round);
log_puts("\n");
}
logx(2, "%s%u: attached at %d + %d / %d",
s->name, s->unit, s->delta, s->delta_rem, s->round);
#endif
/*
@ -2006,31 +1812,20 @@ slot_start(struct slot *s)
{
struct dev *d = s->opt->dev;
#ifdef DEBUG
char enc_str[ENCMAX], chans_str[64];
if (s->pstate != SLOT_INIT) {
slot_log(s);
log_puts(": slot_start: wrong state\n");
logx(0, "%s%u: slot_start: wrong state", s->name, s->unit);
panic();
}
if (s->mode & MODE_PLAY) {
if (log_level >= 3) {
slot_log(s);
log_puts(": playing ");
aparams_log(&s->par);
log_puts(" -> ");
aparams_log(&d->par);
log_puts("\n");
}
}
if (s->mode & MODE_RECMASK) {
if (log_level >= 3) {
slot_log(s);
log_puts(": recording ");
aparams_log(&s->par);
log_puts(" <- ");
aparams_log(&d->par);
log_puts("\n");
}
}
logx(2, "%s%u: %dHz, %s, %s, %d blocks of %d frames",
s->name, s->unit, s->rate,
(aparams_enctostr(&s->par, enc_str), enc_str),
(chans_fmt(chans_str, sizeof(chans_str), s->mode,
s->opt->pmin, s->opt->pmin + s->mix.nch - 1,
s->opt->rmin, s->opt->rmin + s->sub.nch - 1), chans_str),
s->appbufsz / s->round, s->round);
#endif
slot_allocbufs(s);
@ -2070,8 +1865,7 @@ slot_detach(struct slot *s)
for (ps = &d->slot_list; *ps != s; ps = &(*ps)->next) {
#ifdef DEBUG
if (*ps == NULL) {
slot_log(s);
log_puts(": can't detach, not on list\n");
logx(0, "%s%u: can't detach, not on list", s->name, s->unit);
panic();
}
#endif
@ -2093,16 +1887,8 @@ slot_detach(struct slot *s)
}
#ifdef DEBUG
if (log_level >= 2) {
slot_log(s);
log_puts(": detached at ");
log_puti(s->delta);
log_puts(" + ");
log_puti(s->delta_rem);
log_puts("/");
log_puti(d->round);
log_puts("\n");
}
logx(2, "%s%u: detached at %d + %d / %d",
s->name, s->unit, s->delta, s->delta_rem, d->round);
#endif
if (s->mode & MODE_PLAY)
dev_mix_adjvol(d);
@ -2138,10 +1924,7 @@ void
slot_stop(struct slot *s, int drain)
{
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": stopping\n");
}
logx(3, "%s%u: stopping (drain = %d)", s->name, s->unit, drain);
#endif
if (s->pstate == SLOT_START) {
/*
@ -2167,10 +1950,7 @@ slot_stop(struct slot *s, int drain)
slot_detach(s);
} else {
#ifdef DEBUG
if (log_level >= 3) {
slot_log(s);
log_puts(": not drained (blocked by mmc)\n");
}
logx(3, "%s%u: not drained (blocked by mmc)", s->name, s->unit);
#endif
}
@ -2187,10 +1967,7 @@ slot_skip_update(struct slot *s)
skip = slot_skip(s);
while (skip > 0) {
#ifdef DEBUG
if (log_level >= 4) {
slot_log(s);
log_puts(": catching skipped block\n");
}
logx(4, "%s%u: catching skipped block", s->name, s->unit);
#endif
if (s->mode & MODE_RECMASK)
s->ops->flush(s->arg);
@ -2209,10 +1986,7 @@ slot_write(struct slot *s)
{
if (s->pstate == SLOT_START && s->mix.buf.used == s->mix.buf.len) {
#ifdef DEBUG
if (log_level >= 4) {
slot_log(s);
log_puts(": switching to READY state\n");
}
logx(4, "%s%u: switching to READY state", s->name, s->unit);
#endif
s->pstate = SLOT_READY;
slot_ready(s);
@ -2351,99 +2125,81 @@ ctlslot_update(struct ctlslot *s)
s->ops->sync(s->arg);
}
void
ctl_node_log(struct ctl_node *c)
size_t
ctl_node_fmt(char *buf, size_t size, struct ctl_node *c)
{
log_puts(c->name);
char *end = buf + size;
char *p = buf;
p += snprintf(buf, size, "%s", c->name);
if (c->unit >= 0)
log_putu(c->unit);
p += snprintf(p, p < end ? end - p : 0, "%d", c->unit);
return p - buf;
}
void
ctl_log(struct ctl *c)
size_t
ctl_scope_fmt(char *buf, size_t size, struct ctl *c)
{
if (c->group[0] != 0) {
log_puts(c->group);
log_puts("/");
switch (c->scope) {
case CTL_HW:
return snprintf(buf, size, "hw:%s/%u",
c->u.hw.dev->name, c->u.hw.addr);
case CTL_DEV_MASTER:
return snprintf(buf, size, "dev_master:%s",
c->u.dev_master.dev->name);
case CTL_SLOT_LEVEL:
return snprintf(buf, size, "slot_level:%s%u",
c->u.slot_level.slot->name, c->u.slot_level.slot->unit);
case CTL_OPT_DEV:
return snprintf(buf, size, "opt_dev:%s/%s",
c->u.opt_dev.opt->name, c->u.opt_dev.dev->name);
default:
return snprintf(buf, size, "unknown");
}
ctl_node_log(&c->node0);
log_puts(".");
log_puts(c->func);
log_puts("=");
}
size_t
ctl_fmt(char *buf, size_t size, struct ctl *c)
{
char *end = buf + size;
char *p = buf;
p += snprintf(p, size, "%s/", c->group);
p += ctl_node_fmt(p, p < end ? end - p : 0, &c->node0);
p += snprintf(p, p < end ? end - p : 0, ".%s", c->func);
switch (c->type) {
case CTL_NONE:
log_puts("none");
break;
case CTL_NUM:
case CTL_SW:
log_putu(c->curval);
break;
case CTL_VEC:
case CTL_LIST:
case CTL_SEL:
ctl_node_log(&c->node1);
log_puts(":");
log_putu(c->curval);
}
log_puts(" at ");
log_putu(c->addr);
log_puts(" -> ");
switch (c->scope) {
case CTL_HW:
log_puts("hw:");
log_puts(c->u.hw.dev->name);
log_puts("/");
log_putu(c->u.hw.addr);
break;
case CTL_DEV_MASTER:
log_puts("dev_master:");
log_puts(c->u.dev_master.dev->name);
break;
case CTL_SLOT_LEVEL:
log_puts("slot_level:");
log_puts(c->u.slot_level.slot->name);
log_putu(c->u.slot_level.slot->unit);
break;
case CTL_OPT_DEV:
log_puts("opt_dev:");
log_puts(c->u.opt_dev.opt->name);
log_puts("/");
log_puts(c->u.opt_dev.dev->name);
break;
default:
log_puts("unknown");
}
if (c->display[0] != 0) {
log_puts(" (");
log_puts(c->display);
log_puts(")");
p += snprintf(p, p < end ? end - p : 0, "[");
p += ctl_node_fmt(p, p < end ? end - p : 0, &c->node1);
p += snprintf(p, p < end ? end - p : 0, "]");
}
if (c->display[0] != 0)
p += snprintf(p, size, " (%s)", c->display);
return p - buf;
}
int
ctl_setval(struct ctl *c, int val)
{
if (c->curval == val) {
if (log_level >= 3) {
ctl_log(c);
log_puts(": already set\n");
}
logx(3, "ctl%u: already set", c->addr);
return 1;
}
if (val < 0 || val > c->maxval) {
if (log_level >= 3) {
log_putu(val);
log_puts(": ctl val out of bounds\n");
}
logx(3, "ctl%u: %d: out of range", c->addr, val);
return 0;
}
switch (c->scope) {
case CTL_HW:
if (log_level >= 3) {
ctl_log(c);
log_puts(": marked as dirty\n");
}
logx(3, "ctl%u: marked as dirty", c->addr);
c->curval = val;
c->dirty = 1;
return dev_ref(c->u.hw.dev);
@ -2467,10 +2223,7 @@ ctl_setval(struct ctl *c, int val)
c->u.opt_dev.opt->alt_first = c->u.opt_dev.dev;
return 1;
default:
if (log_level >= 2) {
ctl_log(c);
log_puts(": not writable\n");
}
logx(2, "ctl%u: not writable", c->addr);
return 1;
}
}
@ -2484,6 +2237,9 @@ ctl_new(int scope, void *arg0, void *arg1,
char *str0, int unit0, char *func,
char *str1, int unit1, int maxval, int val)
{
#ifdef DEBUG
char ctl_str[64], scope_str[32];
#endif
struct ctl *c, **pc;
struct ctlslot *s;
int addr;
@ -2540,10 +2296,9 @@ ctl_new(int scope, void *arg0, void *arg1,
c->next = *pc;
*pc = c;
#ifdef DEBUG
if (log_level >= 2) {
ctl_log(c);
log_puts(": added\n");
}
logx(2, "ctl%u: %s = %d at %s: added", c->addr,
(ctl_fmt(ctl_str, sizeof(ctl_str), c), ctl_str), c->curval,
(ctl_scope_fmt(scope_str, sizeof(scope_str), c), scope_str));
#endif
return c;
}
@ -2620,6 +2375,9 @@ ctl_onval(int scope, void *arg0, void *arg1, int val)
int
ctl_del(int scope, void *arg0, void *arg1)
{
#ifdef DEBUG
char str[64];
#endif
struct ctl *c, **pc;
int found;
@ -2631,10 +2389,8 @@ ctl_del(int scope, void *arg0, void *arg1)
return found;
if (ctl_match(c, scope, arg0, arg1)) {
#ifdef DEBUG
if (log_level >= 2) {
ctl_log(c);
log_puts(": removed\n");
}
logx(2, "ctl%u: %s: removed", c->addr,
(ctl_fmt(str, sizeof(str), c), str));
#endif
found++;
c->refs_mask &= ~CTL_DEVMASK;
@ -2690,17 +2446,11 @@ dev_ctlsync(struct dev *d)
}
if (d->master_enabled && found) {
if (log_level >= 2) {
dev_log(d);
log_puts(": software master level control disabled\n");
}
logx(2, "%s: software master level control disabled", d->path);
d->master_enabled = 0;
ctl_del(CTL_DEV_MASTER, d, NULL);
} else if (!d->master_enabled && !found) {
if (log_level >= 2) {
dev_log(d);
log_puts(": software master level control enabled\n");
}
logx(2, "%s: software master level control enabled", d->path);
d->master_enabled = 1;
ctl_new(CTL_DEV_MASTER, d, NULL,
CTL_NUM, "", d->name, "output", -1, "level",

View File

@ -1,4 +1,4 @@
/* $OpenBSD: dev.h,v 1.46 2024/05/24 15:16:09 ratchov Exp $ */
/* $OpenBSD: dev.h,v 1.47 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -288,8 +288,7 @@ extern struct ctlslot ctlslot_array[DEV_NCTLSLOT];
extern struct mtc mtc_array[1];
void slot_array_init(void);
void dev_log(struct dev *);
size_t chans_fmt(char *, size_t, int, int, int, int, int);
int dev_open(struct dev *);
void dev_close(struct dev *);
void dev_abort(struct dev *);
@ -333,7 +332,6 @@ void mtc_setdev(struct mtc *, struct dev *);
/*
* sio_open(3) like interface for clients
*/
void slot_log(struct slot *);
struct slot *slot_new(struct opt *, unsigned int, char *,
struct slotops *, void *, int);
void slot_del(struct slot *);
@ -354,7 +352,9 @@ void slot_detach(struct slot *);
struct ctl *ctl_new(int, void *, void *,
int, char *, char *, char *, int, char *, char *, int, int, int);
int ctl_del(int, void *, void *);
void ctl_log(struct ctl *);
size_t ctl_node_fmt(char *, size_t, struct ctl_node *);
size_t ctl_scope_fmt(char *, size_t, struct ctl *);
size_t ctl_fmt(char *, size_t, struct ctl *);
int ctl_setval(struct ctl *c, int val);
int ctl_match(struct ctl *, int, void *, void *);
struct ctl *ctl_find(int, void *, void *);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: dev_sioctl.c,v 1.10 2024/05/24 15:16:09 ratchov Exp $ */
/* $OpenBSD: dev_sioctl.c,v 1.11 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2014-2020 Alexandre Ratchov <alex@caoua.org>
*
@ -78,29 +78,19 @@ dev_sioctl_ondesc(void *arg, struct sioctl_desc *desc, int val)
void
dev_sioctl_onval(void *arg, unsigned int addr, unsigned int val)
{
char str[64];
struct dev *d = arg;
struct ctl *c;
if (log_level >= 2) {
dev_log(d);
log_puts(": onctl: addr = ");
log_putu(addr);
log_puts(", val = ");
log_putu(val);
log_puts("\n");
}
logx(2, "%s: onctl: addr = %u, val = %u", d->path, addr, val);
for (c = ctl_list; c != NULL; c = c->next) {
if (c->scope != CTL_HW || c->u.hw.dev != d ||
c->u.hw.addr != addr)
continue;
if (log_level >= 2) {
ctl_log(c);
log_puts(": new value -> ");
log_putu(val);
log_puts("\n");
}
logx(2, "ctl%u: %s -> %u", c->addr,
(ctl_fmt(str, sizeof(str), c), str), val);
c->val_mask = ~0U;
c->curval = val;
@ -197,14 +187,9 @@ dev_sioctl_out(void *arg)
if (c->scope != CTL_HW || c->u.hw.dev != d || !c->dirty)
continue;
if (!sioctl_setval(d->sioctl.hdl, c->u.hw.addr, c->curval)) {
ctl_log(c);
log_puts(": set failed\n");
logx(1, "ctl%u: set failed", c->addr);
break;
}
if (log_level >= 2) {
ctl_log(c);
log_puts(": changed\n");
}
c->dirty = 0;
cnt++;
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: dsp.c,v 1.21 2024/04/22 14:11:35 ratchov Exp $ */
/* $OpenBSD: dsp.c,v 1.22 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -14,6 +14,7 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "dsp.h"
#include "utils.h"
@ -258,18 +259,6 @@ aparams_init(struct aparams *par)
par->msb = 0;
}
/*
* log the given format/channels/encoding
*/
void
aparams_log(struct aparams *par)
{
char enc[ENCMAX];
aparams_enctostr(par, enc);
log_puts(enc);
}
/*
* return true if encoding corresponds to what we store in adata_t
*/
@ -348,15 +337,7 @@ resamp_do(struct resamp *p, adata_t *in, adata_t *out, int icnt, int ocnt)
* Start conversion.
*/
#ifdef DEBUG
if (log_level >= 4) {
log_puts("resamp: copying ");
log_puti(ifr);
log_puts(" -> ");
log_putu(ofr);
log_puts(" frames, diff = ");
log_puti(diff);
log_puts("\n");
}
logx(4, "resamp: copying %d -> %d frames, diff = %d", ifr, ofr, diff);
#endif
for (;;) {
if (diff >= oblksz) {
@ -423,15 +404,11 @@ resamp_do(struct resamp *p, adata_t *in, adata_t *out, int icnt, int ocnt)
p->ctx_start = ctx_start;
#ifdef DEBUG
if (ifr != 0) {
log_puts("resamp_do: ");
log_puti(ifr);
log_puts(": too many input frames\n");
logx(0, "resamp_do: %d: too many input frames", ifr);
panic();
}
if (ofr != 0) {
log_puts("resamp_do: ");
log_puti(ofr);
log_puts(": too many output frames\n");
logx(0, "resamp_do: %d: too many output frames", ofr);
panic();
}
#endif
@ -488,13 +465,7 @@ resamp_init(struct resamp *p, unsigned int iblksz,
p->filt_step = RESAMP_UNIT / p->iblksz;
}
#ifdef DEBUG
if (log_level >= 3) {
log_puts("resamp: ");
log_putu(iblksz);
log_puts("/");
log_putu(oblksz);
log_puts("\n");
}
logx(3, "resamp_init: %u/%u", iblksz, oblksz);
#endif
}
@ -516,11 +487,7 @@ enc_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
int osnext;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("enc: copying ");
log_putu(todo);
log_puts(" frames\n");
}
logx(4, "enc: copying %u frames", todo);
#endif
/*
* Partially copy structures into local variables, to avoid
@ -574,11 +541,7 @@ enc_sil_do(struct conv *p, unsigned char *out, int todo)
int osnext;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("enc: silence ");
log_putu(todo);
log_puts(" frames\n");
}
logx(4, "enc: silence %u frames", todo);
#endif
/*
* Partially copy structures into local variables, to avoid
@ -613,6 +576,10 @@ enc_sil_do(struct conv *p, unsigned char *out, int todo)
void
enc_init(struct conv *p, struct aparams *par, int nch)
{
#ifdef DEBUG
char enc_str[ENCMAX];
#endif
p->nch = nch;
p->bps = par->bps;
if (par->msb) {
@ -635,13 +602,8 @@ enc_init(struct conv *p, struct aparams *par, int nch)
p->snext = 0;
}
#ifdef DEBUG
if (log_level >= 3) {
log_puts("enc: ");
aparams_log(par);
log_puts(", ");
log_puti(p->nch);
log_puts(" channels\n");
}
logx(3, "enc: %s, %d channels",
(aparams_enctostr(par, enc_str), enc_str), p->nch);
#endif
}
@ -663,11 +625,7 @@ dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
adata_t *odata;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("dec: copying ");
log_putu(todo);
log_puts(" frames\n");
}
logx(4, "dec: copying %u frames", todo);
#endif
/*
* Partially copy structures into local variables, to avoid
@ -706,6 +664,10 @@ dec_do(struct conv *p, unsigned char *in, unsigned char *out, int todo)
void
dec_init(struct conv *p, struct aparams *par, int nch)
{
#ifdef DEBUG
char enc_str[ENCMAX];
#endif
p->bps = par->bps;
p->nch = nch;
if (par->msb) {
@ -728,13 +690,8 @@ dec_init(struct conv *p, struct aparams *par, int nch)
p->snext = 0;
}
#ifdef DEBUG
if (log_level >= 3) {
log_puts("dec: ");
aparams_log(par);
log_puts(", ");
log_puti(p->nch);
log_puts(" channels\n");
}
logx(3, "dec: %s, %d channels",
(aparams_enctostr(par, enc_str), enc_str), p->nch);
#endif
}
@ -748,11 +705,7 @@ cmap_add(struct cmap *p, void *in, void *out, int vol, int todo)
int i, j, nch, istart, inext, onext, ostart, y, v;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("cmap: adding ");
log_puti(todo);
log_puts(" frames\n");
}
logx(4, "cmap: adding %d frames", todo);
#endif
idata = in;
odata = out;
@ -794,11 +747,7 @@ cmap_copy(struct cmap *p, void *in, void *out, int vol, int todo)
int i, j, nch, istart, inext, onext, ostart, v;
#ifdef DEBUG
if (log_level >= 4) {
log_puts("cmap: copying ");
log_puti(todo);
log_puts(" frames\n");
}
logx(4, "cmap: copying %d frames", todo);
#endif
idata = in;
odata = out;
@ -864,18 +813,7 @@ cmap_init(struct cmap *p,
p->inext = imax - isubmax;
p->nch = nch;
#ifdef DEBUG
if (log_level >= 3) {
log_puts("cmap: nch = ");
log_puti(p->nch);
log_puts(", ostart = ");
log_puti(p->ostart);
log_puts(", onext = ");
log_puti(p->onext);
log_puts(", istart = ");
log_puti(p->istart);
log_puts(", inext = ");
log_puti(p->inext);
log_puts("\n");
}
logx(3, "%s: nch = %d, ostart = %d, onext = %d, istart = %d, inext = %d",
__func__, p->nch, p->ostart, p->onext, p->istart, p->inext);
#endif
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: dsp.h,v 1.14 2024/04/22 11:07:42 ratchov Exp $ */
/* $OpenBSD: dsp.h,v 1.15 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2012 Alexandre Ratchov <alex@caoua.org>
*
@ -111,7 +111,6 @@ struct cmap {
extern const int aparams_ctltovol[128];
void aparams_init(struct aparams *);
void aparams_log(struct aparams *);
int aparams_strtoenc(struct aparams *, char *);
int aparams_enctostr(struct aparams *, char *);
int aparams_native(struct aparams *);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: fdpass.c,v 1.11 2021/11/01 14:43:25 ratchov Exp $ */
/* $OpenBSD: fdpass.c,v 1.12 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2015 Alexandre Ratchov <alex@caoua.org>
*
@ -69,12 +69,6 @@ struct fdpass {
int fd;
} *fdpass_peer = NULL;
static void
fdpass_log(struct fdpass *f)
{
log_puts(f->file->name);
}
static int
fdpass_send(struct fdpass *f, int cmd, int num, int mode, int fd)
{
@ -107,34 +101,18 @@ fdpass_send(struct fdpass *f, int cmd, int num, int mode, int fd)
}
n = sendmsg(f->fd, &msg, 0);
if (n == -1) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": sendmsg failed\n");
}
logx(1, "%s: sendmsg failed", f->file->name);
fdpass_close(f);
return 0;
}
if (n != sizeof(struct fdpass_msg)) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": short write\n");
}
logx(1, "%s: short write", f->file->name);
fdpass_close(f);
return 0;
}
#ifdef DEBUG
if (log_level >= 3) {
fdpass_log(f);
log_puts(": send: cmd = ");
log_puti(cmd);
log_puts(", num = ");
log_puti(num);
log_puts(", mode = ");
log_puti(mode);
log_puts(", fd = ");
log_puti(fd);
log_puts("\n");
}
logx(3, "%s: send: cmd = %d, num = %d, mode = %d, fd = %d",
f->file->name, cmd, num, mode, fd);
#endif
if (fd >= 0)
close(fd);
@ -163,10 +141,7 @@ fdpass_recv(struct fdpass *f, int *cmd, int *num, int *mode, int *fd)
msg.msg_iovlen = 1;
n = recvmsg(f->fd, &msg, MSG_WAITALL);
if (n == -1 && errno == EMSGSIZE) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": out of fds\n");
}
logx(1, "%s: out of fds", f->file->name);
/*
* ancillary data (ie the fd) is discarded,
* retrieve the message
@ -174,26 +149,17 @@ fdpass_recv(struct fdpass *f, int *cmd, int *num, int *mode, int *fd)
n = recvmsg(f->fd, &msg, MSG_WAITALL);
}
if (n == -1) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": recvmsg failed\n");
}
logx(1, "%s: recvmsg failed", f->file->name);
fdpass_close(f);
return 0;
}
if (n == 0) {
if (log_level >= 3) {
fdpass_log(f);
log_puts(": recvmsg eof\n");
}
logx(3, "%s: recvmsg eof", f->file->name);
fdpass_close(f);
return 0;
}
if (msg.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": truncated\n");
}
logx(1, "%s: truncated", f->file->name);
fdpass_close(f);
return 0;
}
@ -215,18 +181,8 @@ fdpass_recv(struct fdpass *f, int *cmd, int *num, int *mode, int *fd)
*num = data.num;
*mode = data.mode;
#ifdef DEBUG
if (log_level >= 3) {
fdpass_log(f);
log_puts(": recv: cmd = ");
log_puti(*cmd);
log_puts(", num = ");
log_puti(*num);
log_puts(", mode = ");
log_puti(*mode);
log_puts(", fd = ");
log_puti(*fd);
log_puts("\n");
}
logx(3, "%s: recv: cmd = %d, num = %d, mode = %d, fd = %d",
f->file->name, *cmd, *num, *mode, *fd);
#endif
return 1;
}
@ -239,10 +195,7 @@ fdpass_waitret(struct fdpass *f, int *retfd)
if (!fdpass_recv(fdpass_peer, &cmd, &unused, &unused, retfd))
return 0;
if (cmd != FDPASS_RETURN) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": expected RETURN message\n");
}
logx(1, "%s: expected RETURN message", f->file->name);
fdpass_close(f);
return 0;
}
@ -302,10 +255,7 @@ fdpass_in_worker(void *arg)
{
struct fdpass *f = arg;
if (log_level >= 3) {
fdpass_log(f);
log_puts(": exit\n");
}
logx(3, "%s: exit", f->file->name);
fdpass_close(f);
return;
}
@ -324,10 +274,7 @@ fdpass_in_helper(void *arg)
case FDPASS_OPEN_SND:
d = dev_bynum(num);
if (d == NULL || !(mode & (SIO_PLAY | SIO_REC))) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": bad audio device or mode\n");
}
logx(1, "%s: bad audio device or mode", f->file->name);
fdpass_close(f);
return;
}
@ -336,10 +283,7 @@ fdpass_in_helper(void *arg)
case FDPASS_OPEN_MIDI:
p = port_bynum(num);
if (p == NULL || !(mode & (MIO_IN | MIO_OUT))) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": bad midi port or mode\n");
}
logx(1, "%s: bad midi port or mode", f->file->name);
fdpass_close(f);
return;
}
@ -348,10 +292,7 @@ fdpass_in_helper(void *arg)
case FDPASS_OPEN_CTL:
d = dev_bynum(num);
if (d == NULL || !(mode & (SIOCTL_READ | SIOCTL_WRITE))) {
if (log_level >= 1) {
fdpass_log(f);
log_puts(": bad audio control device\n");
}
logx(1, "%s: bad control device", f->file->name);
fdpass_close(f);
return;
}
@ -374,10 +315,7 @@ fdpass_hup(void *arg)
{
struct fdpass *f = arg;
if (log_level >= 3) {
fdpass_log(f);
log_puts(": hup\n");
}
logx(3, "%s: hup", f->file->name);
fdpass_close(f);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: file.c,v 1.27 2024/07/23 06:34:03 ratchov Exp $ */
/* $OpenBSD: file.c,v 1.28 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -99,11 +99,11 @@ timo_add(struct timo *o, unsigned int delta)
#ifdef DEBUG
if (o->set) {
log_puts("timo_add: already set\n");
logx(0, "timo_add: already set");
panic();
}
if (delta == 0) {
log_puts("timo_add: zero timeout is evil\n");
logx(0, "timo_add: zero timeout is evil");
panic();
}
#endif
@ -136,8 +136,7 @@ timo_del(struct timo *o)
}
}
#ifdef DEBUG
if (log_level >= 4)
log_puts("timo_del: not found\n");
logx(4, "timo_del: not found");
#endif
}
@ -194,30 +193,13 @@ timo_done(void)
{
#ifdef DEBUG
if (timo_queue != NULL) {
log_puts("timo_done: timo_queue not empty!\n");
logx(0, "timo_done: timo_queue not empty!");
panic();
}
#endif
timo_queue = (struct timo *)0xdeadbeef;
}
#ifdef DEBUG
void
file_log(struct file *f)
{
static char *states[] = { "ini", "zom" };
log_puts(f->ops->name);
if (log_level >= 3) {
log_puts("(");
log_puts(f->name);
log_puts("|");
log_puts(states[f->state]);
log_puts(")");
}
}
#endif
struct file *
file_new(struct fileops *ops, void *arg, char *name, unsigned int nfds)
{
@ -225,10 +207,7 @@ file_new(struct fileops *ops, void *arg, char *name, unsigned int nfds)
if (file_nfds + nfds > MAXFDS) {
#ifdef DEBUG
if (log_level >= 1) {
log_puts(name);
log_puts(": too many polled files\n");
}
logx(1, "%s: too many polled files", name);
#endif
return NULL;
}
@ -242,10 +221,7 @@ file_new(struct fileops *ops, void *arg, char *name, unsigned int nfds)
f->next = file_list;
file_list = f;
#ifdef DEBUG
if (log_level >= 3) {
file_log(f);
log_puts(": created\n");
}
logx(3, "%s: created", f->name);
#endif
file_nfds += f->max_nfds;
return f;
@ -256,17 +232,14 @@ file_del(struct file *f)
{
#ifdef DEBUG
if (f->state == FILE_ZOMB) {
log_puts("bad state in file_del()\n");
logx(0, "%s: %s: bad state in file_del", __func__, f->name);
panic();
}
#endif
file_nfds -= f->max_nfds;
f->state = FILE_ZOMB;
#ifdef DEBUG
if (log_level >= 3) {
file_log(f);
log_puts(": destroyed\n");
}
logx(3, "%s: destroyed", f->name);
#endif
}
@ -303,17 +276,35 @@ file_process(struct file *f, struct pollfd *pfd)
clock_gettime(CLOCK_UPTIME, &ts1);
us = 1000000L * (ts1.tv_sec - ts0.tv_sec);
us += (ts1.tv_nsec - ts0.tv_nsec) / 1000;
if (log_level >= 4 || us >= 5000) {
file_log(f);
log_puts(": processed in ");
log_putu(us);
log_puts("us\n");
}
if (us >= 5000)
logx(4, "%s: processed in %luus", f->name, us);
}
#endif
return rc;
}
#ifdef DEBUG
size_t
filelist_fmt(char *buf, size_t size, struct pollfd *pfd, int ret)
{
struct file *f;
char *p = buf, *end = buf + size;
const char *sep = "";
int i;
for (f = file_list; f != NULL; f = f->next) {
p += snprintf(p, p < end ? end - p : 0, "%s%s:", sep, f->name);
for (i = 0; i < f->nfds; i++) {
p += snprintf(p, p < end ? end - p : 0, " 0x%x",
ret ? pfd->revents : pfd->events);
pfd++;
}
sep = ", ";
}
return p - buf;
}
#endif
int
file_poll(void)
{
@ -322,7 +313,7 @@ file_poll(void)
struct timespec ts;
#ifdef DEBUG
struct timespec sleepts;
int i;
char str[128];
#endif
long long delta_nsec;
int nfds, res, timo;
@ -341,8 +332,7 @@ file_poll(void)
if (file_list == NULL && timo_queue == NULL) {
#ifdef DEBUG
if (log_level >= 3)
log_puts("nothing to do...\n");
logx(3, "nothing to do...");
#endif
return 0;
}
@ -358,21 +348,7 @@ file_poll(void)
nfds += f->nfds;
}
#ifdef DEBUG
if (log_level >= 4) {
log_puts("poll:");
pfd = pfds;
for (f = file_list; f != NULL; f = f->next) {
log_puts(" ");
log_puts(f->ops->name);
log_puts(":");
for (i = 0; i < f->nfds; i++) {
log_puts(" ");
log_putx(pfd->events);
pfd++;
}
}
log_puts("\n");
}
logx(4, "poll [%s]", (filelist_fmt(str, sizeof(str), pfds, 0), str));
#endif
/*
@ -413,7 +389,7 @@ file_poll(void)
res = poll(pfds, nfds, timo);
if (res == -1) {
if (errno != EINTR) {
log_puts("poll failed");
logx(0, "poll failed");
panic();
}
return 1;
@ -432,10 +408,8 @@ file_poll(void)
delta_nsec += ts.tv_nsec - file_ts.tv_nsec;
if (delta_nsec >= 0 && delta_nsec < 60000000000LL)
timo_update(delta_nsec / 1000);
else {
if (log_level >= 2)
log_puts("out-of-bounds clock delta\n");
}
else
logx(2, "out-of-bounds clock delta");
}
file_ts = ts;
@ -458,7 +432,7 @@ filelist_init(void)
sigset_t set;
if (clock_gettime(CLOCK_UPTIME, &file_ts) == -1) {
log_puts("filelist_init: CLOCK_UPTIME unsupported\n");
logx(0, "filelist_init: CLOCK_UPTIME unsupported");
panic();
}
sigemptyset(&set);
@ -476,10 +450,8 @@ filelist_done(void)
struct file *f;
if (file_list != NULL) {
for (f = file_list; f != NULL; f = f->next) {
file_log(f);
log_puts(" not closed\n");
}
for (f = file_list; f != NULL; f = f->next)
logx(0, "%s: not closed", f->name);
panic();
}
log_sync = 1;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: file.h,v 1.6 2024/05/19 00:05:43 jsg Exp $ */
/* $OpenBSD: file.h,v 1.7 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -70,10 +70,10 @@ void timo_del(struct timo *);
void filelist_init(void);
void filelist_done(void);
size_t filelist_fmt(char *, size_t, struct pollfd *, int);
struct file *file_new(struct fileops *, void *, char *, unsigned int);
void file_del(struct file *);
void file_log(struct file *);
int file_poll(void);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: listen.c,v 1.14 2020/01/23 20:55:01 ratchov Exp $ */
/* $OpenBSD: listen.c,v 1.15 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
*
@ -60,7 +60,7 @@ listen_close(struct listen *f)
for (pf = &listen_list; *pf != f; pf = &(*pf)->next) {
#ifdef DEBUG
if (*pf == NULL) {
log_puts("listen_close: not on list\n");
logx(0, "%s: not on list", __func__);
panic();
}
#endif
@ -84,13 +84,11 @@ listen_new_un(char *path)
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
log_puts(path);
log_puts(": failed to create socket\n");
logx(0, "%s: failed to create socket", path);
return 0;
}
if (unlink(path) == -1 && errno != ENOENT) {
log_puts(path);
log_puts(": failed to unlink socket\n");
logx(0, "%s: failed to unlink socket", path);
goto bad_close;
}
sockname.sun_family = AF_UNIX;
@ -98,18 +96,16 @@ listen_new_un(char *path)
oldumask = umask(0111);
if (bind(sock, (struct sockaddr *)&sockname,
sizeof(struct sockaddr_un)) == -1) {
log_puts(path);
log_puts(": failed to bind socket\n");
logx(0, "%s: failed to bind socket", path);
goto bad_close;
}
if (listen(sock, 1) == -1) {
log_puts(path);
log_puts(": failed to listen\n");
logx(0, "%s: failed to listen", path);
goto bad_close;
}
umask(oldumask);
f = xmalloc(sizeof(struct listen));
f->file = file_new(&listen_fileops, f, path, 1);
f->file = file_new(&listen_fileops, f, "unix", 1);
if (f->file == NULL)
goto bad_close;
f->path = xstrdup(path);
@ -141,8 +137,7 @@ listen_new_tcp(char *addr, unsigned int port)
aihints.ai_protocol = IPPROTO_TCP;
error = getaddrinfo(host, serv, &aihints, &ailist);
if (error) {
log_puts(addr);
log_puts(": failed to resolve address\n");
logx(0, "%s: failed to resolve address", addr);
return 0;
}
@ -153,29 +148,25 @@ listen_new_tcp(char *addr, unsigned int port)
for (ai = ailist; ai != NULL; ai = ai->ai_next) {
s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (s == -1) {
log_puts(addr);
log_puts(": failed to create socket\n");
logx(0, "%s: failed to create socket", addr);
continue;
}
opt = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
&opt, sizeof(int)) == -1) {
log_puts(addr);
log_puts(": failed to set SO_REUSEADDR\n");
logx(0, "%s: failed to set SO_REUSEADDR", addr);
goto bad_close;
}
if (bind(s, ai->ai_addr, ai->ai_addrlen) == -1) {
log_puts(addr);
log_puts(": failed to bind socket\n");
logx(0, "%s: failed to bind socket", addr);
goto bad_close;
}
if (listen(s, 1) == -1) {
log_puts(addr);
log_puts(": failed to listen\n");
logx(0, "%s: failed to listen", addr);
goto bad_close;
}
f = xmalloc(sizeof(struct listen));
f->file = file_new(&listen_fileops, f, addr, 1);
f->file = file_new(&listen_fileops, f, "tcp", 1);
if (f == NULL) {
bad_close:
close(s);
@ -237,16 +228,14 @@ listen_in(void *arg)
return;
}
if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
file_log(f->file);
log_puts(": failed to set non-blocking mode\n");
logx(0, "%s: failed to set non-blocking mode", f->file->name);
goto bad_close;
}
if (f->path == NULL) {
opt = 1;
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
&opt, sizeof(int)) == -1) {
file_log(f->file);
log_puts(": failed to set TCP_NODELAY flag\n");
logx(0, "%s: failed to set TCP_NODELAY flag", f->file->name);
goto bad_close;
}
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: midi.c,v 1.31 2024/05/19 00:05:43 jsg Exp $ */
/* $OpenBSD: midi.c,v 1.32 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -56,11 +56,21 @@ struct midithru {
const unsigned int voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
const unsigned int common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
void
midi_log(struct midi *ep)
size_t
midiev_fmt(char *buf, size_t size, unsigned char *ev, size_t len)
{
log_puts("midi");
log_putu(ep - midi_ep);
const char *sep = "";
char *end = buf + size;
char *p = buf;
int i;
for (i = 0; i < len; i++) {
if (i == 1)
sep = " ";
p += snprintf(p, p < end ? end - p : 0, "%s%02x", sep, ev[i]);
}
return p - buf;
}
void
@ -93,6 +103,7 @@ midi_new(struct midiops *ops, void *arg, int mode)
ep->st = 0;
ep->last_st = 0;
ep->txmask = 0;
ep->num = i;
ep->self = 1 << i;
ep->tickets = 0;
ep->mode = mode;
@ -143,8 +154,7 @@ midi_link(struct midi *ep, struct midi *peer)
if (ep->mode & MODE_MIDIIN) {
#ifdef DEBUG
if (ep->obuf.used > 0) {
midi_log(ep);
log_puts(": linked with non-empty buffer\n");
logx(0, "midi%u: linked with non-empty buffer", ep->num);
panic();
}
#endif
@ -187,8 +197,7 @@ midi_tag(struct midi *ep, unsigned int tag)
if (ep->mode & MODE_MIDIIN) {
#ifdef DEBUG
if (ep->obuf.used > 0) {
midi_log(ep);
log_puts(": tagged with non-empty buffer\n");
logx(0, "midi%u: tagged with non-empty buffer", ep->num);
panic();
}
#endif
@ -230,19 +239,15 @@ midi_tags(struct midi *ep)
void
midi_send(struct midi *iep, unsigned char *msg, int size)
{
#ifdef DEBUG
char str[128];
#endif
struct midi *oep;
int i;
#ifdef DEBUG
if (log_level >= 4) {
midi_log(iep);
log_puts(": sending:");
for (i = 0; i < size; i++) {
log_puts(" ");
log_putx(msg[i]);
}
log_puts("\n");
}
logx(4, "midi%u: sending: %s", iep->num,
(midiev_fmt(str, sizeof(str), msg, size), str));
#endif
for (i = 0; i < MIDI_NEP ; i++) {
if ((iep->txmask & (1 << i)) == 0)
@ -254,12 +259,7 @@ midi_send(struct midi *iep, unsigned char *msg, int size)
} else if (msg[0] <= 0xf7)
oep->owner = iep;
#ifdef DEBUG
if (log_level >= 4) {
midi_log(iep);
log_puts(" -> ");
midi_log(oep);
log_puts("\n");
}
logx(4, "midi%u -> midi%u", iep->num, oep->num);
#endif
oep->ops->omsg(oep->arg, msg, size);
}
@ -392,21 +392,17 @@ midi_in(struct midi *iep, unsigned char *idata, int icount)
void
midi_out(struct midi *oep, unsigned char *idata, int icount)
{
#ifdef DEBUG
char str[128];
#endif
unsigned char *odata;
int ocount;
#ifdef DEBUG
int i;
#endif
while (icount > 0) {
if (oep->obuf.used == oep->obuf.len) {
#ifdef DEBUG
if (log_level >= 2) {
midi_log(oep);
log_puts(": too slow, discarding ");
log_putu(oep->obuf.used);
log_puts(" bytes\n");
}
logx(2, "midi%u: too slow, discarding %d bytes",
oep->num, oep->obuf.used);
#endif
abuf_rdiscard(&oep->obuf, oep->obuf.used);
oep->owner = NULL;
@ -417,15 +413,8 @@ midi_out(struct midi *oep, unsigned char *idata, int icount)
ocount = icount;
memcpy(odata, idata, ocount);
#ifdef DEBUG
if (log_level >= 4) {
midi_log(oep);
log_puts(": out: ");
for (i = 0; i < ocount; i++) {
log_puts(" ");
log_putx(odata[i]);
}
log_puts("\n");
}
logx(4, "midi%u: out: %s", oep->num,
(midiev_fmt(str, sizeof(str), odata, ocount), str));
#endif
abuf_wcommit(&oep->obuf, ocount);
icount -= ocount;
@ -488,12 +477,6 @@ midi_migrate(struct midi *oep, struct midi *nep)
}
}
void
port_log(struct port *p)
{
midi_log(p->midi);
}
void
port_imsg(void *arg, unsigned char *msg, int size)
{
@ -523,11 +506,8 @@ port_exit(void *arg)
#ifdef DEBUG
struct port *p = arg;
if (log_level >= 3) {
port_log(p);
log_puts(": port exit\n");
panic();
}
logx(0, "midi%u: port exit", p->midi->num);
panic();
#endif
}
@ -565,7 +545,7 @@ port_del(struct port *c)
for (p = &port_list; *p != c; p = &(*p)->next) {
#ifdef DEBUG
if (*p == NULL) {
log_puts("port to delete not on list\n");
logx(0, "port to delete not on list");
panic();
}
#endif
@ -578,10 +558,7 @@ int
port_ref(struct port *c)
{
#ifdef DEBUG
if (log_level >= 3) {
port_log(c);
log_puts(": port requested\n");
}
logx(3, "midi%u: port requested", c->midi->num);
#endif
if (c->state == PORT_CFG && !port_open(c))
return 0;
@ -594,10 +571,7 @@ port_unref(struct port *c)
int i, rxmask;
#ifdef DEBUG
if (log_level >= 3) {
port_log(c);
log_puts(": port released\n");
}
logx(3, "midi%u: port released", c->midi->num);
#endif
for (rxmask = 0, i = 0; i < MIDI_NEP; i++)
rxmask |= midi_ep[i].txmask;
@ -645,10 +619,7 @@ port_migrate(struct port *op)
/* try next one, circulating through the list */
np = np->alt_next;
if (np == op) {
if (log_level >= 2) {
port_log(op);
log_puts(": no fall-back port found\n");
}
logx(2, "midi%u: no fall-back port found", op->midi->num);
return op;
}
@ -656,12 +627,7 @@ port_migrate(struct port *op)
break;
}
if (log_level >= 2) {
port_log(op);
log_puts(": switching to ");
port_log(np);
log_puts("\n");
}
logx(2, "midi%u: switching to midi%u", op->midi->num, np->midi->num);
midi_migrate(op->midi, np->midi);
return np;
@ -683,10 +649,7 @@ int
port_open(struct port *c)
{
if (!port_mio_open(c)) {
if (log_level >= 1) {
port_log(c);
log_puts(": failed to open midi port\n");
}
logx(1, "midi%u: failed to open midi port", c->midi->num);
return 0;
}
c->state = PORT_INIT;
@ -698,13 +661,11 @@ port_close(struct port *c)
{
#ifdef DEBUG
if (c->state == PORT_CFG) {
port_log(c);
log_puts(": can't close port (not opened)\n");
logx(0, "midi%u: can't close port (not opened)", c->midi->num);
panic();
}
#endif
port_log(c);
log_puts(": closed\n");
logx(2, "midi%u: closed", c->midi->num);
c->state = PORT_CFG;
port_mio_close(c);
return 1;
@ -720,10 +681,7 @@ port_drain(struct port *c)
else {
c->state = PORT_DRAIN;
#ifdef DEBUG
if (log_level >= 3) {
port_log(c);
log_puts(": draining\n");
}
logx(3, "midi%u: draining", c->midi->num);
#endif
}
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: midi.h,v 1.16 2024/05/03 05:18:09 ratchov Exp $ */
/* $OpenBSD: midi.h,v 1.17 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -73,7 +73,8 @@ struct midi {
unsigned int idx; /* current ``msg'' size */
unsigned int len; /* expected ``msg'' length */
unsigned int txmask; /* list of ep we send to */
unsigned int self; /* equal (1 << index) */
unsigned int num; /* index in the midi_ep array */
unsigned int self; /* equal (1 << num) */
int tickets; /* max bytes we can process */
struct abuf obuf; /* output buffer */
};
@ -96,7 +97,7 @@ struct port {
};
/*
* midi control ports
* midi ports
*/
extern struct port *port_list;
@ -104,7 +105,7 @@ void midi_init(void);
void midi_done(void);
struct midi *midi_new(struct midiops *, void *, int);
void midi_del(struct midi *);
void midi_log(struct midi *);
size_t midiev_fmt(char *, size_t, unsigned char *, size_t);
void midi_tickets(struct midi *);
void midi_in(struct midi *, unsigned char *, int);
void midi_out(struct midi *, unsigned char *, int);
@ -117,7 +118,6 @@ void midi_link(struct midi *, struct midi *);
void midi_abort(struct midi *);
void midi_migrate(struct midi *, struct midi *);
void port_log(struct port *);
struct port *port_new(char *, unsigned int, int);
struct port *port_bynum(int);
void port_del(struct port *);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: opt.c,v 1.12 2024/05/24 15:21:35 ratchov Exp $ */
/* $OpenBSD: opt.c,v 1.13 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2011 Alexandre Ratchov <alex@caoua.org>
*
@ -42,8 +42,7 @@ opt_midi_imsg(void *arg, unsigned char *msg, int len)
#ifdef DEBUG
struct opt *o = arg;
log_puts(o->name);
log_puts(": can't receive midi messages\n");
logx(0, "%s: can't receive midi messages", o->name);
panic();
#endif
}
@ -91,10 +90,7 @@ opt_midi_omsg(void *arg, unsigned char *msg, int len)
if (o->mtc == NULL)
return;
mtc_setdev(o->mtc, o->dev);
if (log_level >= 2) {
log_puts(o->name);
log_puts(": mmc stop\n");
}
logx(2, "%s: mmc stop", o->name);
mtc_stop(o->mtc);
break;
case SYSEX_MMC_START:
@ -103,10 +99,7 @@ opt_midi_omsg(void *arg, unsigned char *msg, int len)
if (o->mtc == NULL)
return;
mtc_setdev(o->mtc, o->dev);
if (log_level >= 2) {
log_puts(o->name);
log_puts(": mmc start\n");
}
logx(2, "%s: mmc start", o->name);
mtc_start(o->mtc);
break;
case SYSEX_MMC_LOC:
@ -160,11 +153,7 @@ opt_midi_exit(void *arg)
{
struct opt *o = arg;
if (log_level >= 1) {
log_puts(o->name);
log_puts(": midi end point died\n");
panic();
}
logx(1, "%s: midi end point died", o->name);
}
/*
@ -177,6 +166,7 @@ opt_new(struct dev *d, char *name,
{
struct dev *a;
struct opt *o, **po;
char str[64];
unsigned int len, num;
char c;
@ -186,15 +176,13 @@ opt_new(struct dev *d, char *name,
} else {
for (len = 0; name[len] != '\0'; len++) {
if (len == OPT_NAMEMAX) {
log_puts(name);
log_puts(": too long\n");
logx(0, "%s: too long", name);
return NULL;
}
c = name[len];
if ((c < 'a' || c > 'z') &&
(c < 'A' || c > 'Z')) {
log_puts(name);
log_puts(": only alphabetic chars allowed\n");
logx(0, "%s: only alphabetic chars allowed", name);
return NULL;
}
}
@ -203,28 +191,22 @@ opt_new(struct dev *d, char *name,
for (po = &opt_list; *po != NULL; po = &(*po)->next)
num++;
if (num >= OPT_NMAX) {
log_puts(name);
log_puts(": too many opts\n");
logx(0, "%s: too many opts", name);
return NULL;
}
if (opt_byname(name)) {
log_puts(name);
log_puts(": already defined\n");
logx(1, "%s: already defined", name);
return NULL;
}
if (mmc) {
if (mtc_array[0].dev != NULL && mtc_array[0].dev != d) {
log_puts(name);
log_puts(": MTC already setup for another device\n");
logx(0, "%s: MTC already setup for another device", name);
return NULL;
}
mtc_array[0].dev = d;
if (log_level >= 2) {
dev_log(d);
log_puts(": initial MTC source, controlled by MMC\n");
}
logx(2, "%s: initial MTC source, controlled by MMC", d->path);
}
if (strcmp(d->name, name) == 0)
@ -265,39 +247,11 @@ opt_new(struct dev *d, char *name,
memcpy(o->name, name, len + 1);
o->next = *po;
*po = o;
if (log_level >= 2) {
dev_log(d);
log_puts(".");
log_puts(o->name);
log_puts(":");
if (o->mode & MODE_REC) {
log_puts(" rec=");
log_putu(o->rmin);
log_puts(":");
log_putu(o->rmax);
}
if (o->mode & MODE_PLAY) {
log_puts(" play=");
log_putu(o->pmin);
log_puts(":");
log_putu(o->pmax);
log_puts(" vol=");
log_putu(o->maxweight);
}
if (o->mode & MODE_MON) {
log_puts(" mon=");
log_putu(o->rmin);
log_puts(":");
log_putu(o->rmax);
}
if (o->mode & (MODE_RECMASK | MODE_PLAY)) {
if (o->mtc)
log_puts(" mtc");
if (o->dup)
log_puts(" dup");
}
log_puts("\n");
}
logx(2, "%s: %s%s, vol = %d", o->name, (chans_fmt(str, sizeof(str),
o->mode, o->pmin, o->pmax, o->rmin, o->rmax), str),
(o->dup) ? ", dup" : "", o->maxweight);
return o;
}
@ -333,7 +287,7 @@ opt_del(struct opt *o)
for (po = &opt_list; *po != o; po = &(*po)->next) {
#ifdef DEBUG
if (*po == NULL) {
log_puts("opt_del: not on list\n");
logx(0, "%s: not on list", __func__);
panic();
}
#endif
@ -355,8 +309,7 @@ opt_done(struct opt *o)
if (o->refcnt != 0) {
// XXX: all clients are already kicked, so this never happens
log_puts(o->name);
log_puts(": still has refs\n");
logx(0, "%s: still has refs", o->name);
}
for (d = dev_list; d != NULL; d = d->next)
ctl_del(CTL_OPT_DEV, o, d);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: siofile.c,v 1.27 2024/04/02 05:32:10 ratchov Exp $ */
/* $OpenBSD: siofile.c,v 1.28 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -59,12 +59,8 @@ dev_sio_onmove(void *arg, int delta)
struct dev *d = arg;
#ifdef DEBUG
if (log_level >= 4) {
dev_log(d);
log_puts(": tick, delta = ");
log_puti(delta);
log_puts("\n");
}
logx(4, "%s: tick, delta = %d", d->path, delta);
d->sio.sum_utime += file_utime - d->sio.utime;
d->sio.sum_wtime += file_wtime - d->sio.wtime;
d->sio.wtime = file_wtime;
@ -82,8 +78,7 @@ dev_sio_timeout(void *arg)
{
struct dev *d = arg;
dev_log(d);
log_puts(": watchdog timeout\n");
logx(1, "%s: watchdog timeout", d->path);
dev_migrate(d);
dev_abort(d);
}
@ -111,21 +106,14 @@ dev_sio_open(struct dev *d)
else
return 0;
}
if (log_level >= 1) {
log_puts("warning, device opened in ");
log_puts(mode == SIO_PLAY ? "play-only" : "rec-only");
log_puts(" mode\n");
}
logx(1, "%s: warning, device opened in %s mode",
d->path, mode == SIO_PLAY ? "play-only" : "rec-only");
}
d->mode = mode;
d->sioctl.hdl = fdpass_sioctl_open(d->num, SIOCTL_READ | SIOCTL_WRITE);
if (d->sioctl.hdl == NULL) {
if (log_level >= 1) {
dev_log(d);
log_puts(": no control device\n");
}
}
if (d->sioctl.hdl == NULL)
logx(1, "%s: no control device", d->path);
sio_initpar(&par);
par.bits = d->par.bits;
@ -181,53 +169,32 @@ dev_sio_open(struct dev *d)
*/
if (par.bits > BITS_MAX) {
dev_log(d);
log_puts(": ");
log_putu(par.bits);
log_puts(": unsupported number of bits\n");
logx(0, "%s: %u: unsupported number of bits", d->path, par.bits);
goto bad_close;
}
if (par.bps > SIO_BPS(BITS_MAX)) {
dev_log(d);
log_puts(": ");
log_putu(par.bps);
log_puts(": unsupported sample size\n");
logx(0, "%s: %u: unsupported sample size", d->path, par.bps);
goto bad_close;
}
if ((d->mode & SIO_PLAY) && par.pchan > NCHAN_MAX) {
dev_log(d);
log_puts(": ");
log_putu(par.pchan);
log_puts(": unsupported number of play channels\n");
logx(0, "%s: %u: unsupported number of play channels", d->path, par.pchan);
goto bad_close;
}
if ((d->mode & SIO_REC) && par.rchan > NCHAN_MAX) {
dev_log(d);
log_puts(": ");
log_putu(par.rchan);
log_puts(": unsupported number of rec channels\n");
logx(0, "%s: %u: unsupported number of rec channels", d->path, par.rchan);
goto bad_close;
}
if (par.bufsz == 0 || par.bufsz > RATE_MAX) {
dev_log(d);
log_puts(": ");
log_putu(par.bufsz);
log_puts(": unsupported buffer size\n");
logx(0, "%s: %u: unsupported buffer size", d->path, par.bufsz);
goto bad_close;
}
if (par.round == 0 || par.round > par.bufsz ||
par.bufsz % par.round != 0) {
dev_log(d);
log_puts(": ");
log_putu(par.round);
log_puts(": unsupported block size\n");
logx(0, "%s: %u: unsupported block size", d->path, par.round);
goto bad_close;
}
if (par.rate == 0 || par.rate > RATE_MAX) {
dev_log(d);
log_puts(": ");
log_putu(par.rate);
log_puts(": unsupported rate\n");
logx(0, "%s: %u: unsupported rate", d->path, par.rate);
goto bad_close;
}
#endif
@ -268,10 +235,7 @@ dev_sio_close(struct dev *d)
{
dev_sioctl_close(d);
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": closed\n");
}
logx(3, "%s: closed", d->path);
#endif
timo_del(&d->sio.watchdog);
file_del(d->sio.file);
@ -287,10 +251,7 @@ void
dev_sio_start(struct dev *d)
{
if (!sio_start(d->sio.hdl)) {
if (log_level >= 1) {
dev_log(d);
log_puts(": failed to start device\n");
}
logx(1, "%s: failed to start device", d->path);
return;
}
if (d->mode & MODE_PLAY) {
@ -307,10 +268,7 @@ dev_sio_start(struct dev *d)
d->sio.sum_wtime = 0;
d->sio.wtime = file_wtime;
d->sio.utime = file_utime;
if (log_level >= 3) {
dev_log(d);
log_puts(": started\n");
}
logx(3, "%s: started", d->path);
#endif
timo_add(&d->sio.watchdog, WATCHDOG_USEC);
}
@ -319,21 +277,12 @@ void
dev_sio_stop(struct dev *d)
{
if (!sio_eof(d->sio.hdl) && !sio_flush(d->sio.hdl)) {
if (log_level >= 1) {
dev_log(d);
log_puts(": failed to stop device\n");
}
logx(1, "%s: failed to stop device", d->path);
return;
}
#ifdef DEBUG
if (log_level >= 3) {
dev_log(d);
log_puts(": stopped, load avg = ");
log_puti(d->sio.sum_utime / 1000);
log_puts(" / ");
log_puti(d->sio.sum_wtime / 1000);
log_puts("\n");
}
logx(3, "%s: stopped, load avg = %lld / %lld",
d->path, d->sio.sum_utime / 1000, d->sio.sum_wtime / 1000);
#endif
timo_del(&d->sio.watchdog);
}
@ -381,18 +330,15 @@ dev_sio_run(void *arg)
case DEV_SIO_READ:
#ifdef DEBUG
if (!(d->sio.events & POLLIN)) {
dev_log(d);
log_puts(": recording, but POLLIN not set\n");
logx(0, "%s: recording, but POLLIN not set", d->path);
panic();
}
if (d->sio.todo == 0) {
dev_log(d);
log_puts(": can't read data\n");
logx(0, "%s: can't read data", d->path);
panic();
}
if (d->prime > 0) {
dev_log(d);
log_puts(": unexpected data\n");
logx(0, "%s: unexpected data", d->path);
panic();
}
#endif
@ -403,39 +349,16 @@ dev_sio_run(void *arg)
n = sio_read(d->sio.hdl, data, d->sio.todo);
d->sio.todo -= n;
#ifdef DEBUG
if (log_level >= 4) {
dev_log(d);
log_puts(": read ");
log_putu(n);
log_puts(": bytes, todo ");
log_putu(d->sio.todo);
log_puts("/");
log_putu(d->round * d->rchan * d->par.bps);
log_puts("\n");
}
logx(4, "%s: read %u bytes, todo %u / %u", d->path,
n, d->sio.todo, d->round * d->rchan * d->par.bps);
#endif
if (d->sio.todo > 0)
return;
#ifdef DEBUG
d->sio.rused -= d->round;
if (log_level >= 2) {
if (d->sio.rused >= d->round) {
dev_log(d);
log_puts(": rec hw xrun, rused = ");
log_puti(d->sio.rused);
log_puts("/");
log_puti(d->bufsz);
log_puts("\n");
}
if (d->sio.rused < 0 ||
d->sio.rused >= d->bufsz) {
dev_log(d);
log_puts(": out of bounds rused = ");
log_puti(d->sio.rused);
log_puts("/");
log_puti(d->bufsz);
log_puts("\n");
}
if (d->sio.rused >= d->round) {
logx(2, "%s: rec hw xrun, rused = %d / %d",
d->path, d->sio.rused, d->bufsz);
}
#endif
d->sio.cstate = DEV_SIO_CYCLE;
@ -452,8 +375,7 @@ dev_sio_run(void *arg)
*/
if (!((d->mode & MODE_REC) && d->prime == 0) &&
!(d->sio.events & POLLOUT)) {
dev_log(d);
log_puts(": cycle not at block boundary\n");
logx(0, "%s: cycle not at block boundary", d->path);
panic();
}
#endif
@ -470,8 +392,7 @@ dev_sio_run(void *arg)
case DEV_SIO_WRITE:
#ifdef DEBUG
if (d->sio.todo == 0) {
dev_log(d);
log_puts(": can't write data\n");
logx(0, "%s: can't write data", d->path);
panic();
}
#endif
@ -482,41 +403,23 @@ dev_sio_run(void *arg)
n = sio_write(d->sio.hdl, data, d->sio.todo);
d->sio.todo -= n;
#ifdef DEBUG
if (log_level >= 4) {
dev_log(d);
log_puts(": wrote ");
log_putu(n);
log_puts(" bytes, todo ");
log_putu(d->sio.todo);
log_puts("/");
log_putu(d->round * d->pchan * d->par.bps);
log_puts("\n");
}
logx(4, "%s: wrote %u bytes, todo %u / %u",
d->path, n, d->sio.todo, d->round * d->pchan * d->par.bps);
#endif
if (d->sio.todo > 0)
return;
#ifdef DEBUG
d->sio.pused += d->round;
if (log_level >= 2) {
if (d->prime == 0 &&
d->sio.pused <= d->bufsz - d->round) {
dev_log(d);
log_puts(": play hw xrun, pused = ");
log_puti(d->sio.pused);
log_puts("/");
log_puti(d->bufsz);
log_puts("\n");
}
if (d->sio.pused < 0 ||
d->sio.pused > d->bufsz) {
/* device driver or libsndio bug */
dev_log(d);
log_puts(": out of bounds pused = ");
log_puti(d->sio.pused);
log_puts("/");
log_puti(d->bufsz);
log_puts("\n");
}
if (d->prime == 0 &&
d->sio.pused <= d->bufsz - d->round) {
logx(2, "%s: play hw xrun, pused = %d / %d",
d->path, d->sio.pused, d->bufsz);
}
if (d->sio.pused < 0 ||
d->sio.pused > d->bufsz) {
/* device driver or libsndio bug */
logx(2, "%s: out of bounds pused = %d / %d",
d->path, d->sio.pused, d->bufsz);
}
#endif
d->poffs += d->round;
@ -538,10 +441,7 @@ dev_sio_hup(void *arg)
struct dev *d = arg;
#ifdef DEBUG
if (log_level >= 2) {
dev_log(d);
log_puts(": disconnected\n");
}
logx(2, "%s: disconnected", d->path);
#endif
dev_migrate(d);
dev_abort(d);

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sndiod.c,v 1.49 2024/05/03 05:18:09 ratchov Exp $ */
/* $OpenBSD: sndiod.c,v 1.50 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -489,7 +489,7 @@ start_helper(int background)
}
pid = fork();
if (pid == -1) {
log_puts("can't fork\n");
perror("fork");
return 0;
}
if (pid == 0) {

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* $OpenBSD: utils.c,v 1.8 2021/11/01 14:43:25 ratchov Exp $ */
/* $OpenBSD: utils.c,v 1.9 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2003-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -15,12 +15,16 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* log_xxx() routines are used to quickly store traces into a trace buffer.
* logx() quickly stores traces into a trace buffer.
* This allows traces to be collected during time sensitive operations without
* disturbing them. The buffer can be flushed on standard error later, when
* slow syscalls are no longer disruptive, e.g. at the end of the poll() loop.
*/
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
@ -32,16 +36,8 @@
*/
#define LOG_BUFSZ 8192
/*
* store a character in the log
*/
#define LOG_PUTC(c) do { \
if (log_used < LOG_BUFSZ) \
log_buf[log_used++] = (c); \
} while (0)
char log_buf[LOG_BUFSZ]; /* buffer where traces are stored */
unsigned int log_used = 0; /* bytes used in the buffer */
size_t log_used = 0; /* bytes used in the buffer */
unsigned int log_sync = 1; /* if true, flush after each '\n' */
/*
@ -57,75 +53,29 @@ log_flush(void)
}
/*
* store a string in the log
* log a single line to stderr
*/
void
log_puts(char *msg)
log_do(const char *fmt, ...)
{
char *p = msg;
int c;
va_list ap;
int n, save_errno = errno;
while ((c = *p++) != '\0') {
LOG_PUTC(c);
if (log_sync && c == '\n')
va_start(ap, fmt);
n = vsnprintf(log_buf + log_used, sizeof(log_buf) - log_used, fmt, ap);
va_end(ap);
if (n != -1) {
log_used += n;
if (log_used >= sizeof(log_buf))
log_used = sizeof(log_buf) - 1;
log_buf[log_used++] = '\n';
if (log_sync)
log_flush();
}
}
/*
* store a hex in the log
*/
void
log_putx(unsigned long num)
{
char dig[sizeof(num) * 2], *p = dig, c;
unsigned int ndig;
if (num != 0) {
for (ndig = 0; num != 0; ndig++) {
*p++ = num & 0xf;
num >>= 4;
}
for (; ndig != 0; ndig--) {
c = *(--p);
c += (c < 10) ? '0' : 'a' - 10;
LOG_PUTC(c);
}
} else
LOG_PUTC('0');
}
/*
* store an unsigned decimal in the log
*/
void
log_putu(unsigned long num)
{
char dig[sizeof(num) * 3], *p = dig;
unsigned int ndig;
if (num != 0) {
for (ndig = 0; num != 0; ndig++) {
*p++ = num % 10;
num /= 10;
}
for (; ndig != 0; ndig--)
LOG_PUTC(*(--p) + '0');
} else
LOG_PUTC('0');
}
/*
* store a signed decimal in the log
*/
void
log_puti(long num)
{
if (num < 0) {
LOG_PUTC('-');
num = -num;
}
log_putu(num);
errno = save_errno;
}
/*
@ -151,9 +101,7 @@ xmalloc(size_t size)
p = malloc(size);
if (p == NULL) {
log_puts("failed to allocate ");
log_putx(size);
log_puts(" bytes\n");
logx(0, "failed to allocate %zu bytes", size);
panic();
}
return p;
@ -167,7 +115,7 @@ xfree(void *p)
{
#ifdef DEBUG
if (p == NULL) {
log_puts("xfree with NULL arg\n");
logx(0, "xfree with NULL arg");
panic();
}
#endif

View File

@ -1,4 +1,4 @@
/* $OpenBSD: utils.h,v 1.7 2021/11/01 18:23:09 ratchov Exp $ */
/* $OpenBSD: utils.h,v 1.8 2024/12/20 07:35:56 ratchov Exp $ */
/*
* Copyright (c) 2003-2012 Alexandre Ratchov <alex@caoua.org>
*
@ -20,12 +20,15 @@
#include <stddef.h>
void log_puts(char *);
void log_putx(unsigned long);
void log_putu(unsigned long);
void log_puti(long);
#define logx(n, ...) \
do { \
if (log_level >= (n)) \
log_do(__VA_ARGS__); \
} while (0)
void panic(void);
void log_flush(void);
void log_do(const char *, ...) __attribute__((__format__ (printf, 1, 2)));
void *xmalloc(size_t);
char *xstrdup(char *);