/* gcc $(pkg-config --cflags --libs glib-2.0)  -O2 -g gstdebug_printfextension_success.c  -o gstdebug_printfextension_success */


#include <string.h>
#include <stdio.h>
#include <printf.h>
#include <glib.h>
#include <glib/gstdio.h>

void gst_debug_log_valist (const gchar * format, va_list args);
void gst_debug_log (const gchar * format, ...);

FILE *log_file;



static int
_gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
    int *argtypes, int *size)
{
  if (n > 0) {
    argtypes[0] = PA_POINTER;
    *size = sizeof (void *);
  }
  return 1;
}
static int
_bob_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 = (char *)ptr;
  len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
      buffer);

  return len;
}

static int
_bob_printf_extension_a (FILE * stream,
    const struct printf_info *info, const void *const *args)
{
  char *buffer;
  int len;
  void *ptr;

  buffer = NULL;
  ptr = *(void **) args[0];

  buffer = *(char **)ptr;
  len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
      buffer);

  return len;
}

void
gst_debug_log_valist (const gchar * format, va_list args)
{
  va_list arguments;
  G_VA_COPY (arguments, args);


fprintf (log_file,"%s\n", g_strdup_vprintf(format, args));
  va_end (arguments);
}


void
gst_debug_log (const gchar * format, ...)
{
  va_list var_args;

  va_start (var_args, format);
  gst_debug_log_valist (format,
      var_args);
  va_end (var_args);
}



gpointer tmain(gpointer data) {
  const char * a = "lea";
  char * b = "bob";
  char **c = &b;
  gst_debug_log("sending segment event %hQ to pad %P", a, c);
  return 0; 
}


int main () {
  GThread* t1;
  const char * a = "lea";
  char * b = "bob";
  char **c = &b;

  register_printf_specifier ('P', _bob_printf_extension_a,
      _gst_info_printf_extension_arginfo);
  register_printf_specifier ('Q', _bob_printf_extension_segment,
      _gst_info_printf_extension_arginfo);

  register_printf_modifier (L"Q");

  log_file = stderr;

  t1 = g_thread_new( "Thread 1", tmain, NULL );

  gst_debug_log("sending segment event %hQ to pad %P", a, c);

  g_thread_join( t1 );

  return 0;
}
