spa: add context to debug functions

Add new spa_debugc_ funnctions that take a context. The user should also
redefine the spa_debugc macro to handle the context.

Use this to let some plugins log the pod and format to the log without
using the global logger.

Also use this to remove our custom pod logger function by reusing the
spa one with a custom context.
This commit is contained in:
Wim Taymans 2023-01-18 13:09:00 +01:00
parent f472fd736d
commit 3c67821c4a
14 changed files with 158 additions and 268 deletions

View file

@ -43,51 +43,51 @@ extern "C" {
#include <spa/debug/types.h>
#include <spa/buffer/type-info.h>
static inline int spa_debug_buffer(int indent, const struct spa_buffer *buffer)
static inline int spa_debugc_buffer(void *ctx, int indent, const struct spa_buffer *buffer)
{
uint32_t i;
spa_debug("%*s" "struct spa_buffer %p:", indent, "", buffer);
spa_debug("%*s" " n_metas: %u (at %p)", indent, "", buffer->n_metas, buffer->metas);
spa_debugc(ctx, "%*s" "struct spa_buffer %p:", indent, "", buffer);
spa_debugc(ctx, "%*s" " n_metas: %u (at %p)", indent, "", buffer->n_metas, buffer->metas);
for (i = 0; i < buffer->n_metas; i++) {
struct spa_meta *m = &buffer->metas[i];
const char *type_name;
type_name = spa_debug_type_find_name(spa_type_meta_type, m->type);
spa_debug("%*s" " meta %d: type %d (%s), data %p, size %d:", indent, "", i, m->type,
spa_debugc(ctx, "%*s" " meta %d: type %d (%s), data %p, size %d:", indent, "", i, m->type,
type_name, m->data, m->size);
switch (m->type) {
case SPA_META_Header:
{
struct spa_meta_header *h = (struct spa_meta_header*)m->data;
spa_debug("%*s" " struct spa_meta_header:", indent, "");
spa_debug("%*s" " flags: %08x", indent, "", h->flags);
spa_debug("%*s" " offset: %u", indent, "", h->offset);
spa_debug("%*s" " seq: %" PRIu64, indent, "", h->seq);
spa_debug("%*s" " pts: %" PRIi64, indent, "", h->pts);
spa_debug("%*s" " dts_offset: %" PRIi64, indent, "", h->dts_offset);
spa_debugc(ctx, "%*s" " struct spa_meta_header:", indent, "");
spa_debugc(ctx, "%*s" " flags: %08x", indent, "", h->flags);
spa_debugc(ctx, "%*s" " offset: %u", indent, "", h->offset);
spa_debugc(ctx, "%*s" " seq: %" PRIu64, indent, "", h->seq);
spa_debugc(ctx, "%*s" " pts: %" PRIi64, indent, "", h->pts);
spa_debugc(ctx, "%*s" " dts_offset: %" PRIi64, indent, "", h->dts_offset);
break;
}
case SPA_META_VideoCrop:
{
struct spa_meta_region *h = (struct spa_meta_region*)m->data;
spa_debug("%*s" " struct spa_meta_region:", indent, "");
spa_debug("%*s" " x: %d", indent, "", h->region.position.x);
spa_debug("%*s" " y: %d", indent, "", h->region.position.y);
spa_debug("%*s" " width: %d", indent, "", h->region.size.width);
spa_debug("%*s" " height: %d", indent, "", h->region.size.height);
spa_debugc(ctx, "%*s" " struct spa_meta_region:", indent, "");
spa_debugc(ctx, "%*s" " x: %d", indent, "", h->region.position.x);
spa_debugc(ctx, "%*s" " y: %d", indent, "", h->region.position.y);
spa_debugc(ctx, "%*s" " width: %d", indent, "", h->region.size.width);
spa_debugc(ctx, "%*s" " height: %d", indent, "", h->region.size.height);
break;
}
case SPA_META_VideoDamage:
{
struct spa_meta_region *h;
spa_meta_for_each(h, m) {
spa_debug("%*s" " struct spa_meta_region:", indent, "");
spa_debug("%*s" " x: %d", indent, "", h->region.position.x);
spa_debug("%*s" " y: %d", indent, "", h->region.position.y);
spa_debug("%*s" " width: %d", indent, "", h->region.size.width);
spa_debug("%*s" " height: %d", indent, "", h->region.size.height);
spa_debugc(ctx, "%*s" " struct spa_meta_region:", indent, "");
spa_debugc(ctx, "%*s" " x: %d", indent, "", h->region.position.x);
spa_debugc(ctx, "%*s" " y: %d", indent, "", h->region.position.y);
spa_debugc(ctx, "%*s" " width: %d", indent, "", h->region.size.width);
spa_debugc(ctx, "%*s" " height: %d", indent, "", h->region.size.height);
}
break;
}
@ -96,28 +96,32 @@ static inline int spa_debug_buffer(int indent, const struct spa_buffer *buffer)
case SPA_META_Cursor:
break;
default:
spa_debug("%*s" " Unknown:", indent, "");
spa_debug_mem(5, m->data, m->size);
spa_debugc(ctx, "%*s" " Unknown:", indent, "");
spa_debugc_mem(ctx, 5, m->data, m->size);
}
}
spa_debug("%*s" " n_datas: \t%u (at %p)", indent, "", buffer->n_datas, buffer->datas);
spa_debugc(ctx, "%*s" " n_datas: \t%u (at %p)", indent, "", buffer->n_datas, buffer->datas);
for (i = 0; i < buffer->n_datas; i++) {
struct spa_data *d = &buffer->datas[i];
spa_debug("%*s" " type: %d (%s)", indent, "", d->type,
spa_debugc(ctx, "%*s" " type: %d (%s)", indent, "", d->type,
spa_debug_type_find_name(spa_type_data_type, d->type));
spa_debug("%*s" " flags: %d", indent, "", d->flags);
spa_debug("%*s" " data: %p", indent, "", d->data);
spa_debug("%*s" " fd: %" PRIi64, indent, "", d->fd);
spa_debug("%*s" " offset: %d", indent, "", d->mapoffset);
spa_debug("%*s" " maxsize: %u", indent, "", d->maxsize);
spa_debug("%*s" " chunk: %p", indent, "", d->chunk);
spa_debug("%*s" " offset: %d", indent, "", d->chunk->offset);
spa_debug("%*s" " size: %u", indent, "", d->chunk->size);
spa_debug("%*s" " stride: %d", indent, "", d->chunk->stride);
spa_debugc(ctx, "%*s" " flags: %d", indent, "", d->flags);
spa_debugc(ctx, "%*s" " data: %p", indent, "", d->data);
spa_debugc(ctx, "%*s" " fd: %" PRIi64, indent, "", d->fd);
spa_debugc(ctx, "%*s" " offset: %d", indent, "", d->mapoffset);
spa_debugc(ctx, "%*s" " maxsize: %u", indent, "", d->maxsize);
spa_debugc(ctx, "%*s" " chunk: %p", indent, "", d->chunk);
spa_debugc(ctx, "%*s" " offset: %d", indent, "", d->chunk->offset);
spa_debugc(ctx, "%*s" " size: %u", indent, "", d->chunk->size);
spa_debugc(ctx, "%*s" " stride: %d", indent, "", d->chunk->stride);
}
return 0;
}
static inline int spa_debug_buffer(int indent, const struct spa_buffer *buffer)
{
return spa_debugc_buffer(NULL, indent, buffer);
}
/**
* \}
*/

View file

@ -37,16 +37,20 @@ extern "C" {
#include <spa/debug/log.h>
#include <spa/utils/dict.h>
static inline int spa_debug_dict(int indent, const struct spa_dict *dict)
static inline int spa_debugc_dict(void *ctx, int indent, const struct spa_dict *dict)
{
const struct spa_dict_item *item;
spa_debug("%*sflags:%08x n_items:%d", indent, "", dict->flags, dict->n_items);
spa_debugc(ctx, "%*sflags:%08x n_items:%d", indent, "", dict->flags, dict->n_items);
spa_dict_for_each(item, dict) {
spa_debug("%*s %s = \"%s\"", indent, "", item->key, item->value);
spa_debugc(ctx, "%*s %s = \"%s\"", indent, "", item->key, item->value);
}
return 0;
}
static inline int spa_debug_dict(int indent, const struct spa_dict *dict)
{
return spa_debugc_dict(NULL, indent, dict);
}
/**
* \}
*/

View file

@ -127,7 +127,7 @@ spa_debug_format_value(const struct spa_type_info *info,
return 0;
}
static inline int spa_debug_format(int indent,
static inline int spa_debugc_format(void *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *format)
{
const char *media_type;
@ -147,7 +147,7 @@ static inline int spa_debug_format(int indent,
media_type = spa_debug_type_find_name(spa_type_media_type, mtype);
media_subtype = spa_debug_type_find_name(spa_type_media_subtype, mstype);
spa_debug("%*s %s/%s", indent, "",
spa_debugc(ctx, "%*s %s/%s", indent, "",
media_type ? spa_debug_type_short_name(media_type) : "unknown",
media_subtype ? spa_debug_type_short_name(media_subtype) : "unknown");
@ -212,11 +212,16 @@ static inline int spa_debug_format(int indent,
}
spa_debug_buffer_append(&buf, "%s", esep);
}
spa_debug("%s", buffer);
spa_debugc(ctx, "%s", buffer);
}
return 0;
}
static inline int spa_debug_format(int indent,
const struct spa_type_info *info, const struct spa_pod *format)
{
return spa_debugc_format(NULL, indent, info, format);
}
/**
* \}
*/

View file

@ -38,11 +38,14 @@ extern "C" {
* \{
*/
#ifndef spa_debug
#define spa_debug(fmt,...) ({ printf((fmt"\n"), ## __VA_ARGS__); })
#endif
#ifndef spa_debugn
#define spa_debugn(fmt,...) ({ printf((fmt), ## __VA_ARGS__); })
#define spa_debugn(_fmt,...) printf((_fmt), ## __VA_ARGS__)
#endif
#ifndef spa_debug
#define spa_debug(_fmt,...) spa_debugn(_fmt"\n", ## __VA_ARGS__)
#endif
#ifndef spa_debugc
#define spa_debugc(_c,_fmt,...) spa_debug(_fmt, ## __VA_ARGS__)
#endif
struct spa_debug_buffer {

View file

@ -38,7 +38,7 @@ extern "C" {
#include <spa/debug/log.h>
static inline int spa_debug_mem(int indent, const void *data, size_t size)
static inline int spa_debugc_mem(void *ctx, int indent, const void *data, size_t size)
{
const uint8_t *t = (const uint8_t*)data;
char buffer[512];
@ -50,12 +50,16 @@ static inline int spa_debug_mem(int indent, const void *data, size_t size)
pos = sprintf(buffer, "%p: ", &t[i]);
pos += sprintf(buffer + pos, "%02x ", t[i]);
if (i % 16 == 15 || i == size - 1) {
spa_debug("%*s" "%s", indent, "", buffer);
spa_debugc(ctx, "%*s" "%s", indent, "", buffer);
}
}
return 0;
}
static inline int spa_debug_mem(int indent, const void *data, size_t size)
{
return spa_debugc_mem(NULL, indent, data, size);
}
/**
* \}
*/

View file

@ -38,19 +38,23 @@ extern "C" {
#include <spa/debug/log.h>
#include <spa/debug/dict.h>
static inline int spa_debug_port_info(int indent, const struct spa_port_info *info)
static inline int spa_debugc_port_info(void *ctx, int indent, const struct spa_port_info *info)
{
spa_debug("%*s" "struct spa_port_info %p:", indent, "", info);
spa_debug("%*s" " flags: \t%08" PRIx64, indent, "", info->flags);
spa_debug("%*s" " rate: \t%d/%d", indent, "", info->rate.num, info->rate.denom);
spa_debug("%*s" " props:", indent, "");
spa_debugc(ctx, "%*s" "struct spa_port_info %p:", indent, "", info);
spa_debugc(ctx, "%*s" " flags: \t%08" PRIx64, indent, "", info->flags);
spa_debugc(ctx, "%*s" " rate: \t%d/%d", indent, "", info->rate.num, info->rate.denom);
spa_debugc(ctx, "%*s" " props:", indent, "");
if (info->props)
spa_debug_dict(indent + 2, info->props);
spa_debugc_dict(ctx, indent + 2, info->props);
else
spa_debug("%*s" " none", indent, "");
spa_debugc(ctx, "%*s" " none", indent, "");
return 0;
}
static inline int spa_debug_port_info(int indent, const struct spa_port_info *info)
{
return spa_debugc_port_info(NULL, indent, info);
}
/**
* \}
*/

View file

@ -41,56 +41,56 @@ extern "C" {
#include <spa/pod/iter.h>
static inline int
spa_debug_pod_value(int indent, const struct spa_type_info *info,
spa_debugc_pod_value(void *ctx, int indent, const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
switch (type) {
case SPA_TYPE_Bool:
spa_debug("%*s" "Bool %s", indent, "", (*(int32_t *) body) ? "true" : "false");
spa_debugc(ctx, "%*s" "Bool %s", indent, "", (*(int32_t *) body) ? "true" : "false");
break;
case SPA_TYPE_Id:
spa_debug("%*s" "Id %-8d (%s)", indent, "", *(int32_t *) body,
spa_debugc(ctx, "%*s" "Id %-8d (%s)", indent, "", *(int32_t *) body,
spa_debug_type_find_name(info, *(int32_t *) body));
break;
case SPA_TYPE_Int:
spa_debug("%*s" "Int %d", indent, "", *(int32_t *) body);
spa_debugc(ctx, "%*s" "Int %d", indent, "", *(int32_t *) body);
break;
case SPA_TYPE_Long:
spa_debug("%*s" "Long %" PRIi64 "", indent, "", *(int64_t *) body);
spa_debugc(ctx, "%*s" "Long %" PRIi64 "", indent, "", *(int64_t *) body);
break;
case SPA_TYPE_Float:
spa_debug("%*s" "Float %f", indent, "", *(float *) body);
spa_debugc(ctx, "%*s" "Float %f", indent, "", *(float *) body);
break;
case SPA_TYPE_Double:
spa_debug("%*s" "Double %f", indent, "", *(double *) body);
spa_debugc(ctx, "%*s" "Double %f", indent, "", *(double *) body);
break;
case SPA_TYPE_String:
spa_debug("%*s" "String \"%s\"", indent, "", (char *) body);
spa_debugc(ctx, "%*s" "String \"%s\"", indent, "", (char *) body);
break;
case SPA_TYPE_Fd:
spa_debug("%*s" "Fd %d", indent, "", *(int *) body);
spa_debugc(ctx, "%*s" "Fd %d", indent, "", *(int *) body);
break;
case SPA_TYPE_Pointer:
{
struct spa_pod_pointer_body *b = (struct spa_pod_pointer_body *)body;
spa_debug("%*s" "Pointer %s %p", indent, "",
spa_debugc(ctx, "%*s" "Pointer %s %p", indent, "",
spa_debug_type_find_name(SPA_TYPE_ROOT, b->type), b->value);
break;
}
case SPA_TYPE_Rectangle:
{
struct spa_rectangle *r = (struct spa_rectangle *)body;
spa_debug("%*s" "Rectangle %dx%d", indent, "", r->width, r->height);
spa_debugc(ctx, "%*s" "Rectangle %dx%d", indent, "", r->width, r->height);
break;
}
case SPA_TYPE_Fraction:
{
struct spa_fraction *f = (struct spa_fraction *)body;
spa_debug("%*s" "Fraction %d/%d", indent, "", f->num, f->denom);
spa_debugc(ctx, "%*s" "Fraction %d/%d", indent, "", f->num, f->denom);
break;
}
case SPA_TYPE_Bitmap:
spa_debug("%*s" "Bitmap", indent, "");
spa_debugc(ctx, "%*s" "Bitmap", indent, "");
break;
case SPA_TYPE_Array:
{
@ -98,12 +98,12 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
void *p;
const struct spa_type_info *ti = spa_debug_type_find(SPA_TYPE_ROOT, b->child.type);
spa_debug("%*s" "Array: child.size %d, child.type %s", indent, "",
spa_debugc(ctx, "%*s" "Array: child.size %d, child.type %s", indent, "",
b->child.size, ti ? ti->name : "unknown");
info = info && info->values ? info->values : info;
SPA_POD_ARRAY_BODY_FOREACH(b, size, p)
spa_debug_pod_value(indent + 2, info, b->child.type, p, b->child.size);
spa_debugc_pod_value(ctx, indent + 2, info, b->child.type, p, b->child.size);
break;
}
case SPA_TYPE_Choice:
@ -112,19 +112,19 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
void *p;
const struct spa_type_info *ti = spa_debug_type_find(spa_type_choice, b->type);
spa_debug("%*s" "Choice: type %s, flags %08x %d %d", indent, "",
spa_debugc(ctx, "%*s" "Choice: type %s, flags %08x %d %d", indent, "",
ti ? ti->name : "unknown", b->flags, size, b->child.size);
SPA_POD_CHOICE_BODY_FOREACH(b, size, p)
spa_debug_pod_value(indent + 2, info, b->child.type, p, b->child.size);
spa_debugc_pod_value(ctx, indent + 2, info, b->child.type, p, b->child.size);
break;
}
case SPA_TYPE_Struct:
{
struct spa_pod *b = (struct spa_pod *)body, *p;
spa_debug("%*s" "Struct: size %d", indent, "", size);
spa_debugc(ctx, "%*s" "Struct: size %d", indent, "", size);
SPA_POD_FOREACH(b, size, p)
spa_debug_pod_value(indent + 2, info, p->type, SPA_POD_BODY(p), p->size);
spa_debugc_pod_value(ctx, indent + 2, info, p->type, SPA_POD_BODY(p), p->size);
break;
}
case SPA_TYPE_Object:
@ -137,7 +137,7 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
ii = ti ? spa_debug_type_find(ti->values, 0) : NULL;
ii = ii ? spa_debug_type_find(ii->values, b->id) : NULL;
spa_debug("%*s" "Object: size %d, type %s (%d), id %s (%d)", indent, "", size,
spa_debugc(ctx, "%*s" "Object: size %d, type %s (%d), id %s (%d)", indent, "", size,
ti ? ti->name : "unknown", b->type, ii ? ii->name : "unknown", b->id);
info = ti ? ti->values : info;
@ -145,10 +145,10 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
SPA_POD_OBJECT_BODY_FOREACH(b, size, p) {
ii = spa_debug_type_find(info, p->key);
spa_debug("%*s" "Prop: key %s (%d), flags %08x", indent+2, "",
spa_debugc(ctx, "%*s" "Prop: key %s (%d), flags %08x", indent+2, "",
ii ? ii->name : "unknown", p->key, p->flags);
spa_debug_pod_value(indent + 4, ii ? ii->values : NULL,
spa_debugc_pod_value(ctx, indent + 4, ii ? ii->values : NULL,
p->value.type,
SPA_POD_CONTENTS(struct spa_pod_prop, p),
p->value.size);
@ -163,16 +163,16 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
ti = spa_debug_type_find(info, b->unit);
spa_debug("%*s" "Sequence: size %d, unit %s", indent, "", size,
spa_debugc(ctx, "%*s" "Sequence: size %d, unit %s", indent, "", size,
ti ? ti->name : "unknown");
SPA_POD_SEQUENCE_BODY_FOREACH(b, size, c) {
ii = spa_debug_type_find(spa_type_control, c->type);
spa_debug("%*s" "Control: offset %d, type %s", indent+2, "",
spa_debugc(ctx, "%*s" "Control: offset %d, type %s", indent+2, "",
c->offset, ii ? ii->name : "unknown");
spa_debug_pod_value(indent + 4, ii ? ii->values : NULL,
spa_debugc_pod_value(ctx, indent + 4, ii ? ii->values : NULL,
c->value.type,
SPA_POD_CONTENTS(struct spa_pod_control, c),
c->value.size);
@ -180,29 +180,41 @@ spa_debug_pod_value(int indent, const struct spa_type_info *info,
break;
}
case SPA_TYPE_Bytes:
spa_debug("%*s" "Bytes", indent, "");
spa_debug_mem(indent + 2, body, size);
spa_debugc(ctx, "%*s" "Bytes", indent, "");
spa_debugc_mem(ctx, indent + 2, body, size);
break;
case SPA_TYPE_None:
spa_debug("%*s" "None", indent, "");
spa_debug_mem(indent + 2, body, size);
spa_debugc(ctx, "%*s" "None", indent, "");
spa_debugc_mem(ctx, indent + 2, body, size);
break;
default:
spa_debug("%*s" "unhandled POD type %d", indent, "", type);
spa_debugc(ctx, "%*s" "unhandled POD type %d", indent, "", type);
break;
}
return 0;
}
static inline int spa_debug_pod(int indent,
static inline int spa_debugc_pod(void *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *pod)
{
return spa_debug_pod_value(indent, info ? info : SPA_TYPE_ROOT,
return spa_debugc_pod_value(ctx, indent, info ? info : SPA_TYPE_ROOT,
SPA_POD_TYPE(pod),
SPA_POD_BODY(pod),
SPA_POD_BODY_SIZE(pod));
}
static inline int
spa_debug_pod_value(int indent, const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
return spa_debugc_pod_value(NULL, indent, info, type, body, size);
}
static inline int spa_debug_pod(int indent,
const struct spa_type_info *info, const struct spa_pod *pod)
{
return spa_debugc_pod(NULL, indent, info, pod);
}
/**
* \}
*/