Blame gst/gsttracerrecord.c

Packit f546b1
/* GStreamer
Packit f546b1
 * Copyright (C) 2016 Stefan Sauer <ensonic@users.sf.net>
Packit f546b1
 *
Packit f546b1
 * gsttracerrecord.c: tracer log record class
Packit f546b1
 *
Packit f546b1
 * This library is free software; you can redistribute it and/or
Packit f546b1
 * modify it under the terms of the GNU Library General Public
Packit f546b1
 * License as published by the Free Software Foundation; either
Packit f546b1
 * version 2 of the License, or (at your option) any later version.
Packit f546b1
 *
Packit f546b1
 * This library is distributed in the hope that it will be useful,
Packit f546b1
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit f546b1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit f546b1
 * Library General Public License for more details.
Packit f546b1
 *
Packit f546b1
 * You should have received a copy of the GNU Library General Public
Packit f546b1
 * License along with this library; if not, write to the
Packit f546b1
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit f546b1
 * Boston, MA 02110-1301, USA.
Packit f546b1
 */
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * SECTION:gsttracerrecord
Packit f546b1
 * @title: GstTracerRecord
Packit f546b1
 * @short_description: Trace log entry class
Packit f546b1
 *
Packit f546b1
 * Tracing modules will create instances of this class to announce the data they
Packit f546b1
 * will log and create a log formatter.
Packit f546b1
 *
Packit f546b1
 * Since: 1.8
Packit f546b1
 */
Packit f546b1
Packit f546b1
#define GST_USE_UNSTABLE_API
Packit f546b1
Packit f546b1
#include "gst_private.h"
Packit f546b1
#include "gstenumtypes.h"
Packit f546b1
#include "gstinfo.h"
Packit f546b1
#include "gststructure.h"
Packit f546b1
#include "gsttracerrecord.h"
Packit f546b1
#include "gstvalue.h"
Packit f546b1
#include <gobject/gvaluecollector.h>
Packit f546b1
Packit f546b1
GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
Packit f546b1
#define GST_CAT_DEFAULT tracer_debug
Packit f546b1
Packit f546b1
struct _GstTracerRecord
Packit f546b1
{
Packit f546b1
  GstObject parent;
Packit f546b1
Packit f546b1
  GstStructure *spec;
Packit f546b1
  gchar *format;
Packit f546b1
};
Packit f546b1
Packit f546b1
struct _GstTracerRecordClass
Packit f546b1
{
Packit f546b1
  GstObjectClass parent_class;
Packit f546b1
};
Packit f546b1
Packit f546b1
#define gst_tracer_record_parent_class parent_class
Packit f546b1
G_DEFINE_TYPE (GstTracerRecord, gst_tracer_record, GST_TYPE_OBJECT);
Packit f546b1
Packit f546b1
static gboolean
Packit f546b1
build_field_template (GQuark field_id, const GValue * value, gpointer user_data)
Packit f546b1
{
Packit f546b1
  GString *s = (GString *) user_data;
Packit f546b1
  const GstStructure *sub;
Packit f546b1
  GValue template_value = { 0, };
Packit f546b1
  GType type = G_TYPE_INVALID;
Packit f546b1
  GstTracerValueFlags flags = GST_TRACER_VALUE_FLAGS_NONE;
Packit f546b1
  gboolean res;
Packit f546b1
Packit f546b1
  if (G_VALUE_TYPE (value) != GST_TYPE_STRUCTURE) {
Packit f546b1
    GST_WARNING ("expected field of type GstStructure, but %s is %s",
Packit f546b1
        g_quark_to_string (field_id), G_VALUE_TYPE_NAME (value));
Packit f546b1
    return FALSE;
Packit f546b1
  }
Packit f546b1
Packit f546b1
  sub = gst_value_get_structure (value);
Packit f546b1
  gst_structure_get (sub, "type", G_TYPE_GTYPE, &type, "flags",
Packit f546b1
      GST_TYPE_TRACER_VALUE_FLAGS, &flags, NULL);
Packit f546b1
Packit f546b1
  if (flags & GST_TRACER_VALUE_FLAGS_OPTIONAL) {
Packit f546b1
    gchar *opt_name = g_strconcat ("have-", g_quark_to_string (field_id), NULL);
Packit f546b1
Packit f546b1
    /* add a boolean field, that indicates the presence of the next field */
Packit f546b1
    g_value_init (&template_value, G_TYPE_BOOLEAN);
Packit f546b1
    priv__gst_structure_append_template_to_gstring (g_quark_from_string
Packit f546b1
        (opt_name), &template_value, s);
Packit f546b1
    g_value_unset (&template_value);
Packit f546b1
    g_free (opt_name);
Packit f546b1
  }
Packit f546b1
Packit f546b1
  g_value_init (&template_value, type);
Packit f546b1
  res = priv__gst_structure_append_template_to_gstring (field_id,
Packit f546b1
      &template_value, s);
Packit f546b1
  g_value_unset (&template_value);
Packit f546b1
  return res;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_tracer_record_build_format (GstTracerRecord * self)
Packit f546b1
{
Packit f546b1
  GstStructure *structure = self->spec;
Packit f546b1
  GString *s;
Packit f546b1
  gchar *name = (gchar *) g_quark_to_string (structure->name);
Packit f546b1
  gchar *p;
Packit f546b1
Packit f546b1
  g_return_if_fail (g_str_has_suffix (name, ".class"));
Packit f546b1
Packit f546b1
  /* announce the format */
Packit f546b1
  GST_TRACE ("%" GST_PTR_FORMAT, structure);
Packit f546b1
Packit f546b1
  /* cut off '.class' suffix */
Packit f546b1
  name = g_strdup (name);
Packit f546b1
  p = strrchr (name, '.');
Packit f546b1
  g_assert (p != NULL);
Packit f546b1
  *p = '\0';
Packit f546b1
Packit f546b1
  s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
Packit f546b1
  g_string_append (s, name);
Packit f546b1
  gst_structure_foreach (structure, build_field_template, s);
Packit f546b1
  g_string_append_c (s, ';');
Packit f546b1
Packit f546b1
  self->format = g_string_free (s, FALSE);
Packit f546b1
  GST_DEBUG ("new format string: %s", self->format);
Packit f546b1
  g_free (name);
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_tracer_record_dispose (GObject * object)
Packit f546b1
{
Packit f546b1
  GstTracerRecord *self = GST_TRACER_RECORD (object);
Packit f546b1
Packit f546b1
  if (self->spec) {
Packit f546b1
    gst_structure_free (self->spec);
Packit f546b1
    self->spec = NULL;
Packit f546b1
  }
Packit f546b1
  g_free (self->format);
Packit f546b1
  self->format = NULL;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_tracer_record_class_init (GstTracerRecordClass * klass)
Packit f546b1
{
Packit f546b1
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit f546b1
Packit f546b1
  gobject_class->dispose = gst_tracer_record_dispose;
Packit f546b1
}
Packit f546b1
Packit f546b1
static void
Packit f546b1
gst_tracer_record_init (GstTracerRecord * self)
Packit f546b1
{
Packit f546b1
}
Packit f546b1
Packit f546b1
/**
Packit f546b1
 * gst_tracer_record_new:
Packit f546b1
 * @name: name of new record, must end on ".class".
Packit f546b1
 * @firstfield: name of first field to set
Packit f546b1
 * @...: additional arguments
Packit f546b1
Packit f546b1
 *
Packit f546b1
 * Create a new tracer record. The record instance can be used to efficiently
Packit f546b1
 * log entries using gst_tracer_record_log().
Packit f546b1
 *
Packit f546b1
 * The @name without the ".class" suffix will be used for the log records.
Packit f546b1
 * There must be fields for each value that gets logged where the field name is
Packit f546b1
 * the value name. The field must be a #GstStructure describing the value. The
Packit f546b1
 * sub structure must contain a field called 'type' of %G_TYPE_GTYPE that
Packit f546b1
 * contains the GType of the value. The resulting #GstTracerRecord will take
Packit f546b1
 * ownership of the field structures.
Packit f546b1
 *
Packit f546b1
 * The way to deal with optional values is to log an additional boolean before
Packit f546b1
 * the optional field, that if %TRUE signals that the optional field is valid
Packit f546b1
 * and %FALSE signals that the optional field should be ignored. One must still
Packit f546b1
 * log a placeholder value for the optional field though. Please also note, that
Packit f546b1
 * pointer type values must not be NULL - the underlying serialisation can not
Packit f546b1
 * handle that right now.
Packit f546b1
 *
Packit f546b1
 * > Please note that this is still under discussion and subject to change.
Packit f546b1
 *
Packit f546b1
 * Returns: (transfer full): a new #GstTracerRecord
Packit f546b1
 */
Packit f546b1
GstTracerRecord *
Packit f546b1
gst_tracer_record_new (const gchar * name, const gchar * firstfield, ...)
Packit f546b1
{
Packit f546b1
  GstTracerRecord *self;
Packit f546b1
  GstStructure *structure;
Packit f546b1
  va_list varargs;
Packit f546b1
  gchar *err = NULL;
Packit f546b1
  GType type;
Packit f546b1
  GQuark id;
Packit f546b1
Packit f546b1
  va_start (varargs, firstfield);
Packit f546b1
  structure = gst_structure_new_empty (name);
Packit f546b1
Packit f546b1
  while (firstfield) {
Packit f546b1
    GValue val = { 0, };
Packit f546b1
Packit f546b1
    id = g_quark_from_string (firstfield);
Packit f546b1
    type = va_arg (varargs, GType);
Packit f546b1
Packit f546b1
    /* all fields passed here must be GstStructures which we take over */
Packit f546b1
    if (type != GST_TYPE_STRUCTURE) {
Packit f546b1
      GST_WARNING ("expected field of type GstStructure, but %s is %s",
Packit f546b1
          firstfield, g_type_name (type));
Packit f546b1
    }
Packit f546b1
Packit f546b1
    G_VALUE_COLLECT_INIT (&val, type, varargs, G_VALUE_NOCOPY_CONTENTS, &err;;
Packit f546b1
    if (G_UNLIKELY (err)) {
Packit f546b1
      g_critical ("%s", err);
Packit f546b1
      break;
Packit f546b1
    }
Packit f546b1
    /* see boxed_proxy_collect_value */
Packit f546b1
    val.data[1].v_uint &= ~G_VALUE_NOCOPY_CONTENTS;
Packit f546b1
    gst_structure_id_take_value (structure, id, &val;;
Packit f546b1
Packit f546b1
    firstfield = va_arg (varargs, gchar *);
Packit f546b1
  }
Packit f546b1
  va_end (varargs);
Packit f546b1
Packit f546b1
  self = g_object_new (GST_TYPE_TRACER_RECORD, NULL);
Packit f546b1
Packit f546b1
  /* Clear floating flag */
Packit f546b1
  gst_object_ref_sink (self);
Packit f546b1
Packit f546b1
  self->spec = structure;
Packit f546b1
  gst_tracer_record_build_format (self);
Packit f546b1
Packit f546b1
  return self;
Packit f546b1
}
Packit f546b1
Packit f546b1
#ifndef GST_DISABLE_GST_DEBUG
Packit f546b1
/**
Packit f546b1
 * gst_tracer_record_log:
Packit f546b1
 * @self: the tracer-record
Packit f546b1
 * @...: the args as described in the spec-
Packit f546b1
 *
Packit f546b1
 * Serialzes the trace event into the log.
Packit f546b1
 *
Packit f546b1
 * Right now this is using the gstreamer debug log with the level TRACE (7) and
Packit f546b1
 * the category "GST_TRACER".
Packit f546b1
 *
Packit f546b1
 * > Please note that this is still under discussion and subject to change.
Packit f546b1
 */
Packit f546b1
void
Packit f546b1
gst_tracer_record_log (GstTracerRecord * self, ...)
Packit f546b1
{
Packit f546b1
  va_list var_args;
Packit f546b1
Packit f546b1
  /*
Packit f546b1
   * does it make sense to use the {file, line, func} from the tracer hook?
Packit f546b1
   * a)
Packit f546b1
   * - we'd need to pass them in the macros to gst_tracer_dispatch()
Packit f546b1
   * - and each tracer needs to grab them from the va_list and pass them here
Packit f546b1
   * b)
Packit f546b1
   * - we create a context in dispatch, pass that to the tracer
Packit f546b1
   * - and the tracer will pass that here
Packit f546b1
   * ideally we also use *our* ts instead of the one that
Packit f546b1
   * gst_debug_log_default() will pick
Packit f546b1
   */
Packit f546b1
Packit f546b1
  va_start (var_args, self);
Packit f546b1
  if (G_LIKELY (GST_LEVEL_TRACE <= _gst_debug_min)) {
Packit f546b1
    gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_TRACE, "", "", 0, NULL,
Packit f546b1
        self->format, var_args);
Packit f546b1
  }
Packit f546b1
  va_end (var_args);
Packit f546b1
}
Packit f546b1
#endif