tracers: Only link against libgstprintf.la if the debugging system is enabled
[platform/upstream/gstreamer.git] / gst / gsttracer.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracer.c: tracer base class
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gsttracer
24  * @short_description: Tracing base class
25  *
26  * Tracing modules will subclass #GstTracer and register through
27  * gst_tracer_register(). Modules can attach to various hook-types - see
28  * #GstTracerHook. When invoked they receive hook specific contextual data, 
29  * which they must not modify.
30  */
31
32 #define GST_USE_UNSTABLE_API
33
34 #include "gst_private.h"
35 #include "gstenumtypes.h"
36 #include "gsttracer.h"
37 #include "gsttracerfactory.h"
38
39 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
40 #define GST_CAT_DEFAULT tracer_debug
41
42 /* tracing plugins base class */
43
44 enum
45 {
46   PROP_0,
47   PROP_PARAMS,
48   PROP_LAST
49 };
50
51 static GParamSpec *properties[PROP_LAST];
52
53 static void gst_tracer_set_property (GObject * object, guint prop_id,
54     const GValue * value, GParamSpec * pspec);
55 static void gst_tracer_get_property (GObject * object, guint prop_id,
56     GValue * value, GParamSpec * pspec);
57
58 struct _GstTracerPrivate
59 {
60   gchar *params;
61 };
62
63 #define gst_tracer_parent_class parent_class
64 G_DEFINE_ABSTRACT_TYPE (GstTracer, gst_tracer, GST_TYPE_OBJECT);
65
66 static void
67 gst_tracer_dispose (GObject * object)
68 {
69   GstTracer *tracer = GST_TRACER (object);
70   g_free (tracer->priv->params);
71 }
72
73 static void
74 gst_tracer_class_init (GstTracerClass * klass)
75 {
76   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
77
78   gobject_class->set_property = gst_tracer_set_property;
79   gobject_class->get_property = gst_tracer_get_property;
80   gobject_class->dispose = gst_tracer_dispose;
81
82   properties[PROP_PARAMS] =
83       g_param_spec_string ("params", "Params", "Extra configuration parameters",
84       NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
85
86   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
87   g_type_class_add_private (klass, sizeof (GstTracerPrivate));
88 }
89
90 static void
91 gst_tracer_init (GstTracer * tracer)
92 {
93   tracer->priv = G_TYPE_INSTANCE_GET_PRIVATE (tracer, GST_TYPE_TRACER,
94       GstTracerPrivate);
95 }
96
97 static void
98 gst_tracer_set_property (GObject * object, guint prop_id,
99     const GValue * value, GParamSpec * pspec)
100 {
101   GstTracer *self = GST_TRACER_CAST (object);
102
103   switch (prop_id) {
104     case PROP_PARAMS:
105       self->priv->params = g_value_dup_string (value);
106       break;
107     default:
108       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109       break;
110   }
111 }
112
113 static void
114 gst_tracer_get_property (GObject * object, guint prop_id,
115     GValue * value, GParamSpec * pspec)
116 {
117   GstTracer *self = GST_TRACER_CAST (object);
118
119   switch (prop_id) {
120     case PROP_PARAMS:
121       g_value_set_string (value, self->priv->params);
122       break;
123     default:
124       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125       break;
126   }
127 }
128
129 /* tracing modules */
130
131 gboolean
132 gst_tracer_register (GstPlugin * plugin, const gchar * name, GType type)
133 {
134   GstPluginFeature *existing_feature;
135   GstRegistry *registry;
136   GstTracerFactory *factory;
137
138   g_return_val_if_fail (name != NULL, FALSE);
139   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_TRACER), FALSE);
140
141   registry = gst_registry_get ();
142   /* check if feature already exists, if it exists there is no need to update it
143    * when the registry is getting updated, outdated plugins and all their
144    * features are removed and readded.
145    */
146   existing_feature = gst_registry_lookup_feature (registry, name);
147   if (existing_feature) {
148     GST_DEBUG_OBJECT (registry, "update existing feature %p (%s)",
149         existing_feature, name);
150     factory = GST_TRACER_FACTORY_CAST (existing_feature);
151     factory->type = type;
152     existing_feature->loaded = TRUE;
153     gst_object_unref (existing_feature);
154     return TRUE;
155   }
156
157   factory = g_object_newv (GST_TYPE_TRACER_FACTORY, 0, NULL);
158   GST_DEBUG_OBJECT (factory, "new tracer factory for %s", name);
159
160   gst_plugin_feature_set_name (GST_PLUGIN_FEATURE_CAST (factory), name);
161   gst_plugin_feature_set_rank (GST_PLUGIN_FEATURE_CAST (factory),
162       GST_RANK_NONE);
163
164   factory->type = type;
165   GST_DEBUG_OBJECT (factory, "tracer factory for %u:%s",
166       (guint) type, g_type_name (type));
167
168   if (plugin && plugin->desc.name) {
169     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = plugin->desc.name; /* interned string */
170     GST_PLUGIN_FEATURE_CAST (factory)->plugin = plugin;
171     g_object_add_weak_pointer ((GObject *) plugin,
172         (gpointer *) & GST_PLUGIN_FEATURE_CAST (factory)->plugin);
173   } else {
174     GST_PLUGIN_FEATURE_CAST (factory)->plugin_name = "NULL";
175     GST_PLUGIN_FEATURE_CAST (factory)->plugin = NULL;
176   }
177   GST_PLUGIN_FEATURE_CAST (factory)->loaded = TRUE;
178
179   gst_registry_add_feature (gst_registry_get (),
180       GST_PLUGIN_FEATURE_CAST (factory));
181
182   return TRUE;
183 }
184
185 /* tracing module helpers */
186
187 void
188 gst_tracer_log_trace (GstStructure * s)
189 {
190   GST_TRACE ("%" GST_PTR_FORMAT, s);
191   /* expands to:
192      gst_debug_log_valist (
193      GST_CAT_DEFAULT, GST_LEVEL_TRACE,
194      file, func, line, object
195      "%" GST_PTR_FORMAT, s);
196      // does it make sense to use the {file, line, func} from the tracer hook?
197      // a)
198      // - we'd need to pass them in the macros to gst_tracer_dispatch()
199      // - and each tracer needs to grab them from the va_list and pass them here
200      // b)
201      // - we create a content in dispatch, pass that to the tracer
202      // - and the tracer will pass that here
203      // ideally we also use *our* ts instead of the one that
204      // gst_debug_log_default() will pick
205    */
206   gst_structure_free (s);
207 }