spa: add debug log context

Make a real debug context with a log function and move it to a new file.
This way we don't need to redefine a macro.
Make a new context for debugging to a log file. Make new functions to
debug to a log file.
Move the stringbuffer to string utils.
Integrate file/line/func and topics into the debug log.
We can remove some more things from the pipewire log_object function and
also add support for topics.
This commit is contained in:
Wim Taymans 2023-01-18 17:41:16 +01:00
parent 3c67821c4a
commit 6207d98ff1
21 changed files with 231 additions and 164 deletions

View file

@ -38,12 +38,12 @@ extern "C" {
* \{
*/
#include <spa/debug/log.h>
#include <spa/debug/context.h>
#include <spa/debug/mem.h>
#include <spa/debug/types.h>
#include <spa/buffer/type-info.h>
static inline int spa_debugc_buffer(void *ctx, int indent, const struct spa_buffer *buffer)
static inline int spa_debugc_buffer(struct spa_debug_context *ctx, int indent, const struct spa_buffer *buffer)
{
uint32_t i;

View file

@ -0,0 +1,62 @@
/* Simple Plugin API
*
* Copyright © 2023 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_DEBUG_CONTEXT_H
#define SPA_DEBUG_CONTEXT_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
#include <spa/utils/defs.h>
/**
* \addtogroup spa_debug
* \{
*/
#ifndef spa_debugn
#define spa_debugn(_fmt,...) printf((_fmt), ## __VA_ARGS__)
#endif
#ifndef spa_debug
#define spa_debug(_fmt,...) spa_debugn(_fmt"\n", ## __VA_ARGS__)
#endif
struct spa_debug_context {
void (*log) (struct spa_debug_context *ctx, const char *fmt, ...) SPA_PRINTF_FUNC(2, 3);
};
#define spa_debugc(_c,_fmt,...) (_c)?((_c)->log((_c),_fmt, ## __VA_ARGS__)):(void)spa_debug(_fmt, ## __VA_ARGS__)
/**
* \}
*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_DEBUG_CONTEXT_H */

View file

@ -34,10 +34,10 @@ extern "C" {
* \{
*/
#include <spa/debug/log.h>
#include <spa/debug/context.h>
#include <spa/utils/dict.h>
static inline int spa_debugc_dict(void *ctx, int indent, const struct spa_dict *dict)
static inline int spa_debugc_dict(struct spa_debug_context *ctx, int indent, const struct spa_dict *dict)
{
const struct spa_dict_item *item;
spa_debugc(ctx, "%*sflags:%08x n_items:%d", indent, "", dict->flags, dict->n_items);

View file

@ -35,19 +35,20 @@ extern "C" {
*/
#include <spa/pod/parser.h>
#include <spa/debug/log.h>
#include <spa/utils/string.h>
#include <spa/debug/context.h>
#include <spa/debug/types.h>
#include <spa/param/type-info.h>
#include <spa/param/format-utils.h>
static inline int
spa_debug_buffer_format_value(struct spa_debug_buffer *buffer, const struct spa_type_info *info,
spa_debug_strbuf_format_value(struct spa_strbuf *buffer, const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
switch (type) {
case SPA_TYPE_Bool:
spa_debug_buffer_append(buffer, "%s", *(int32_t *) body ? "true" : "false");
spa_strbuf_append(buffer, "%s", *(int32_t *) body ? "true" : "false");
break;
case SPA_TYPE_Id:
{
@ -57,41 +58,41 @@ spa_debug_buffer_format_value(struct spa_debug_buffer *buffer, const struct spa_
snprintf(tmp, sizeof(tmp), "%d", *(int32_t*)body);
str = tmp;
}
spa_debug_buffer_append(buffer, "%s", str);
spa_strbuf_append(buffer, "%s", str);
break;
}
case SPA_TYPE_Int:
spa_debug_buffer_append(buffer, "%d", *(int32_t *) body);
spa_strbuf_append(buffer, "%d", *(int32_t *) body);
break;
case SPA_TYPE_Long:
spa_debug_buffer_append(buffer, "%" PRIi64, *(int64_t *) body);
spa_strbuf_append(buffer, "%" PRIi64, *(int64_t *) body);
break;
case SPA_TYPE_Float:
spa_debug_buffer_append(buffer, "%f", *(float *) body);
spa_strbuf_append(buffer, "%f", *(float *) body);
break;
case SPA_TYPE_Double:
spa_debug_buffer_append(buffer, "%f", *(double *) body);
spa_strbuf_append(buffer, "%f", *(double *) body);
break;
case SPA_TYPE_String:
spa_debug_buffer_append(buffer, "%s", (char *) body);
spa_strbuf_append(buffer, "%s", (char *) body);
break;
case SPA_TYPE_Rectangle:
{
struct spa_rectangle *r = (struct spa_rectangle *)body;
spa_debug_buffer_append(buffer, "%" PRIu32 "x%" PRIu32, r->width, r->height);
spa_strbuf_append(buffer, "%" PRIu32 "x%" PRIu32, r->width, r->height);
break;
}
case SPA_TYPE_Fraction:
{
struct spa_fraction *f = (struct spa_fraction *)body;
spa_debug_buffer_append(buffer, "%" PRIu32 "/%" PRIu32, f->num, f->denom);
spa_strbuf_append(buffer, "%" PRIu32 "/%" PRIu32, f->num, f->denom);
break;
}
case SPA_TYPE_Bitmap:
spa_debug_buffer_append(buffer, "Bitmap");
spa_strbuf_append(buffer, "Bitmap");
break;
case SPA_TYPE_Bytes:
spa_debug_buffer_append(buffer, "Bytes");
spa_strbuf_append(buffer, "Bytes");
break;
case SPA_TYPE_Array:
{
@ -99,17 +100,17 @@ spa_debug_buffer_format_value(struct spa_debug_buffer *buffer, const struct spa_
struct spa_pod_array_body *b = (struct spa_pod_array_body *)body;
int i = 0;
info = info && info->values ? info->values : info;
spa_debug_buffer_append(buffer, "< ");
spa_strbuf_append(buffer, "< ");
SPA_POD_ARRAY_BODY_FOREACH(b, size, p) {
if (i++ > 0)
spa_debug_buffer_append(buffer, ", ");
spa_debug_buffer_format_value(buffer, info, b->child.type, p, b->child.size);
spa_strbuf_append(buffer, ", ");
spa_debug_strbuf_format_value(buffer, info, b->child.type, p, b->child.size);
}
spa_debug_buffer_append(buffer, " >");
spa_strbuf_append(buffer, " >");
break;
}
default:
spa_debug_buffer_append(buffer, "INVALID type %d", type);
spa_strbuf_append(buffer, "INVALID type %d", type);
break;
}
return 0;
@ -120,14 +121,14 @@ spa_debug_format_value(const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
char buffer[1024];
struct spa_debug_buffer buf;
spa_debug_buffer_init(&buf, buffer, sizeof(buffer));
spa_debug_buffer_format_value(&buf, info, type, body, size);
struct spa_strbuf buf;
spa_strbuf_init(&buf, buffer, sizeof(buffer));
spa_debug_strbuf_format_value(&buf, info, type, body, size);
spa_debugn("%s", buffer);
return 0;
}
static inline int spa_debugc_format(void *ctx, int indent,
static inline int spa_debugc_format(struct spa_debug_context *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *format)
{
const char *media_type;
@ -158,7 +159,7 @@ static inline int spa_debugc_format(void *ctx, int indent,
const struct spa_pod *val;
void *vals;
char buffer[1024];
struct spa_debug_buffer buf;
struct spa_strbuf buf;
if (prop->key == SPA_FORMAT_mediaType ||
prop->key == SPA_FORMAT_mediaSubtype)
@ -176,13 +177,13 @@ static inline int spa_debugc_format(void *ctx, int indent,
ti = spa_debug_type_find(info, prop->key);
key = ti ? ti->name : NULL;
spa_debug_buffer_init(&buf, buffer, sizeof(buffer));
spa_debug_buffer_append(&buf, "%*s %16s : (%s) ", indent, "",
spa_strbuf_init(&buf, buffer, sizeof(buffer));
spa_strbuf_append(&buf, "%*s %16s : (%s) ", indent, "",
key ? spa_debug_type_short_name(key) : "unknown",
spa_debug_type_short_name(spa_types[type].name));
if (choice == SPA_CHOICE_None) {
spa_debug_buffer_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
} else {
const char *ssep, *esep, *sep;
@ -202,15 +203,15 @@ static inline int spa_debugc_format(void *ctx, int indent,
break;
}
spa_debug_buffer_append(&buf, "%s", ssep);
spa_strbuf_append(&buf, "%s", ssep);
for (i = 1; i < n_vals; i++) {
vals = SPA_PTROFF(vals, size, void);
if (i > 1)
spa_debug_buffer_append(&buf, "%s", sep);
spa_debug_buffer_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
spa_strbuf_append(&buf, "%s", sep);
spa_debug_strbuf_format_value(&buf, ti ? ti->values : NULL, type, vals, size);
}
spa_debug_buffer_append(&buf, "%s", esep);
spa_strbuf_append(&buf, "%s", esep);
}
spa_debugc(ctx, "%s", buffer);
}

View file

@ -33,48 +33,65 @@ extern "C" {
#include <stdarg.h>
#include <spa/utils/defs.h>
#include <spa/support/log.h>
#include <spa/debug/context.h>
/**
* \addtogroup spa_debug
* \{
*/
#ifndef spa_debugn
#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 {
char *buffer;
size_t maxsize;
size_t pos;
struct spa_debug_log_ctx {
struct spa_debug_context ctx;
struct spa_log *log;
enum spa_log_level level;
const struct spa_log_topic *topic;
const char *file;
int line;
const char *func;
};
static inline void spa_debug_buffer_init(struct spa_debug_buffer *buf, char *buffer, size_t maxsize)
SPA_PRINTF_FUNC(2,3)
static inline void spa_debug_log_log(struct spa_debug_context *ctx, const char *fmt, ...)
{
buf->buffer = buffer;
buf->maxsize = maxsize;
buf->pos = 0;
}
SPA_PRINTF_FUNC(2, 3)
static inline int spa_debug_buffer_append(struct spa_debug_buffer *buf, const char *fmt, ...)
{
size_t remain = buf->maxsize - buf->pos;
ssize_t written;
struct spa_debug_log_ctx *c = (struct spa_debug_log_ctx*)ctx;
va_list args;
va_start(args, fmt);
written = vsnprintf(&buf->buffer[buf->pos], remain, fmt, args);
spa_log_logtv(c->log, c->level, c->topic, c->file, c->line, c->func, fmt, args);
va_end(args);
if (written > 0)
buf->pos += SPA_MIN(remain, (size_t)written);
return written;
}
#define SPA_LOGF_DEBUG_INIT(_l,_lev,_t,_file,_line,_func) \
(struct spa_debug_log_ctx){ { spa_debug_log_log }, _l, _lev, _t, \
_file, _line, _func }
#define SPA_LOGT_DEBUG_INIT(_l,_lev,_t) \
SPA_LOGF_DEBUG_INIT(_l,_lev,_t,__FILE__,__LINE__,__func__)
#define SPA_LOG_DEBUG_INIT(l,lev) \
SPA_LOGT_DEBUG_INIT(l,lev,SPA_LOG_TOPIC_DEFAULT)
#define spa_debug_log_pod(l,lev,indent,info,pod) \
({ \
struct spa_debug_log_ctx c = SPA_LOG_DEBUG_INIT(l,lev); \
if (SPA_UNLIKELY(spa_log_level_topic_enabled(c.log, c.topic, c.level))) \
spa_debugc_pod(&c.ctx, indent, info, pod); \
})
#define spa_debug_log_format(l,lev,indent,info,format) \
({ \
struct spa_debug_log_ctx c = SPA_LOG_DEBUG_INIT(l,lev); \
if (SPA_UNLIKELY(spa_log_level_topic_enabled(c.log, c.topic, c.level))) \
spa_debugc_format(&c.ctx, indent, info, format); \
})
#define spa_debug_log_mem(l,lev,indent,data,len) \
({ \
struct spa_debug_log_ctx c = SPA_LOG_DEBUG_INIT(l,lev); \
if (SPA_UNLIKELY(spa_log_level_topic_enabled(c.log, c.topic, c.level))) \
spa_debugc_mem(&c.ctx, indent, data, len); \
})
/**
* \}
*/
@ -83,4 +100,4 @@ static inline int spa_debug_buffer_append(struct spa_debug_buffer *buf, const ch
} /* extern "C" */
#endif
#endif /* SPA_DEBUG_LOGH */
#endif /* SPA_DEBUG_LOG_H */

View file

@ -36,9 +36,9 @@ extern "C" {
* \{
*/
#include <spa/debug/log.h>
#include <spa/debug/context.h>
static inline int spa_debugc_mem(void *ctx, int indent, const void *data, size_t size)
static inline int spa_debugc_mem(struct spa_debug_context *ctx, int indent, const void *data, size_t size)
{
const uint8_t *t = (const uint8_t*)data;
char buffer[512];

View file

@ -35,10 +35,10 @@ extern "C" {
*/
#include <spa/node/node.h>
#include <spa/debug/log.h>
#include <spa/debug/context.h>
#include <spa/debug/dict.h>
static inline int spa_debugc_port_info(void *ctx, int indent, const struct spa_port_info *info)
static inline int spa_debugc_port_info(struct spa_debug_context *ctx, int indent, const struct spa_port_info *info)
{
spa_debugc(ctx, "%*s" "struct spa_port_info %p:", indent, "", info);
spa_debugc(ctx, "%*s" " flags: \t%08" PRIx64, indent, "", info->flags);

View file

@ -34,14 +34,14 @@ extern "C" {
* \{
*/
#include <spa/debug/log.h>
#include <spa/debug/context.h>
#include <spa/debug/mem.h>
#include <spa/debug/types.h>
#include <spa/pod/pod.h>
#include <spa/pod/iter.h>
static inline int
spa_debugc_pod_value(void *ctx, int indent, const struct spa_type_info *info,
spa_debugc_pod_value(struct spa_debug_context *ctx, int indent, const struct spa_type_info *info,
uint32_t type, void *body, uint32_t size)
{
switch (type) {
@ -194,7 +194,7 @@ spa_debugc_pod_value(void *ctx, int indent, const struct spa_type_info *info,
return 0;
}
static inline int spa_debugc_pod(void *ctx, int indent,
static inline int spa_debugc_pod(struct spa_debug_context *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *pod)
{
return spa_debugc_pod_value(ctx, indent, info ? info : SPA_TYPE_ROOT,