printf: add infrastructure for pointer extensions hook

Does not do anything yet. On a sidenote, we can't just use
%p\001 or so to signal the extension because g-i complains
about an invalid ascii character then, so have to resort to
something more elaborate, such as %p\aA etc.

https://bugzilla.gnome.org/show_bug.cgi?id=613081
This commit is contained in:
Tim-Philipp Müller 2013-03-30 18:28:38 +00:00 committed by Tim-Philipp Müller
parent 7b19944280
commit fe7f7135e0
6 changed files with 118 additions and 60 deletions

View file

@ -127,6 +127,10 @@
/* our own printf implementation with custom extensions to %p for caps etc. */
#include "printf/printf.h"
#include "printf/printf-extension.h"
static char *gst_info_printf_pointer_extension_func (const char *format,
void *ptr);
#endif /* !GST_DISABLE_GST_DEBUG */
@ -223,13 +227,6 @@ dladdr (void *address, Dl_info * dl)
static void gst_debug_reset_threshold (gpointer category, gpointer unused);
static void gst_debug_reset_all_thresholds (void);
#if 0
static int _gst_info_printf_extension_ptr (FILE * stream,
const struct printf_info *info, const void *const *args);
static int _gst_info_printf_extension_segment (FILE * stream,
const struct printf_info *info, const void *const *args);
#endif
struct _GstDebugMessage
{
gchar *message;
@ -328,12 +325,8 @@ _priv_gst_debug_init (void)
/* get time we started for debugging messages */
_priv_gst_info_start_time = gst_util_get_timestamp ();
#if 0
register_printf_specifier (GST_PTR_FORMAT[0], _gst_info_printf_extension_ptr,
NULL);
register_printf_specifier (GST_SEGMENT_FORMAT[0],
_gst_info_printf_extension_segment, NULL);
#endif
__gst_printf_pointer_extension_set_func
(gst_info_printf_pointer_extension_func);
/* do NOT use a single debug function before this line has been run */
GST_CAT_DEFAULT = _gst_debug_category_new ("default",
@ -734,7 +727,6 @@ gst_debug_print_object (gpointer ptr)
return g_strdup_printf ("%p", ptr);
}
#if 0
static gchar *
gst_debug_print_segment (gpointer ptr)
{
@ -775,7 +767,31 @@ gst_debug_print_segment (gpointer ptr)
}
}
}
#endif
static char *
gst_info_printf_pointer_extension_func (const char *format, void *ptr)
{
char *s = NULL;
if (format[0] == 'p' && format[1] == '\a') {
switch (format[2]) {
case 'A': /* GST_PTR_FORMAT */
s = gst_debug_print_object (ptr);
break;
case 'B': /* GST_SEGMENT_FORMAT */
s = gst_debug_print_segment (ptr);
break;
default:
/* must have been compiled against a newer version with an extension
* we don't known about yet - just ignore and fallback to %p below */
break;
}
}
if (s == NULL)
s = g_strdup_printf ("%p", ptr);
return s;
}
/**
* gst_debug_construct_term_color:
@ -1775,48 +1791,6 @@ _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
g_mutex_unlock (&__dbg_functions_mutex);
}
/*** PRINTF EXTENSIONS ********************************************************/
#if 0
static int
_gst_info_printf_extension_ptr (FILE * stream, const struct printf_info *info,
const void *const *args)
{
char *buffer;
int len;
void *ptr;
buffer = NULL;
ptr = *(void **) args[0];
buffer = gst_debug_print_object (ptr);
len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
buffer);
g_free (buffer);
return len;
}
static int
_gst_info_printf_extension_segment (FILE * stream,
const struct printf_info *info, const void *const *args)
{
char *buffer;
int len;
void *ptr;
buffer = NULL;
ptr = *(void **) args[0];
buffer = gst_debug_print_segment (ptr);
len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
buffer);
g_free (buffer);
return len;
}
#endif
static void
gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
const guint8 * mem, gsize mem_offset, gsize mem_size)

View file

@ -235,7 +235,7 @@ struct _GstDebugCategory {
* printf format type used to debug GStreamer types.
* This can only be used on types whose size is >= sizeof(gpointer).
*/
#define GST_PTR_FORMAT "p" /* FIXME: add suffix for differentiation */
#define GST_PTR_FORMAT "p\aA"
/**
* GST_SEGMENT_FORMAT:
@ -243,7 +243,7 @@ struct _GstDebugCategory {
* printf format type used to debug GStreamer segments.
* This can only be used on pointers to GstSegment structures.
*/
#define GST_SEGMENT_FORMAT "p" /* FIXME: add suffix for differentiation */
#define GST_SEGMENT_FORMAT "p\aB"
typedef struct _GstDebugMessage GstDebugMessage;

View file

@ -13,6 +13,8 @@ libgstprintf_la_SOURCES = \
vasnprintf.h \
printf.c \
printf.h \
printf-extension.c \
printf-extension.h \
gst-printf.h
EXTRA_DIST = README

View file

@ -50,7 +50,8 @@ __gst_printf namespace for GStreamer. Also #define HAVE_SNPRINTF 0 has
been changed to #undef HAVE_SNPRINTF, and HAVE_ALLOCA has been changed to
GLIB_HAVE_ALLOCA_H
We will also add support for our custom printf format specifiers.
printf-extension.[ch] were added to provide support for custom pointer
arguments (e.g. caps, events, etc.)
Files have also been indented with gst-indent, so this is basically a
permanent fork and any patches will have to be merged manually.

View file

@ -0,0 +1,50 @@
/* GStreamer printf extension hooks
* Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "printf-extension.h"
#include "gst-printf.h"
#include <stdio.h>
#include <string.h>
static PrintfPointerExtensionFunc ptr_ext_func; /* NULL */
void
__gst_printf_pointer_extension_set_func (PrintfPointerExtensionFunc func)
{
/* since this is internal, we don't need to worry about thread-safety */
ptr_ext_func = func;
}
char *
__gst_printf_pointer_extension_serialize (const char *format, void *ptr)
{
char *buf;
if (ptr_ext_func == NULL) {
buf = malloc (32);
memset (buf, 0, 32);
sprintf (buf, "%p", ptr);
} else {
/* note: we map malloc/free to g_malloc/g_free in gst-printf.h, so the
* fact that gstinfo gives us a glib-allocated string and the printf
* routines free it with free() and not g_free() should not be a problem */
buf = ptr_ext_func (format, ptr);
}
return buf;
}

View file

@ -0,0 +1,31 @@
/* GStreamer printf extension hooks
* Copyright (C) 2013 Tim-Philipp Müller <tim centricular net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __GST_PRINTF_EXTENSION_H_INCLUDED__
#define __GST_PRINTF_EXTENSION_H_INCLUDED__
typedef char * (*PrintfPointerExtensionFunc) (const char * format, void * ptr);
/* we only need one global function, since it's only GstInfo registering extensions */
void __gst_printf_pointer_extension_set_func (PrintfPointerExtensionFunc func);
/* functions for internal printf implementation to handle the extensions */
char * __gst_printf_pointer_extension_serialize (const char * format, void * ptr);
#endif /* __GST_PRINTF_EXTENSION_H_INCLUDED__ */