tracer: split into tracer and tracerutils
[platform/upstream/gstreamer.git] / gst / gsttracerutils.c
1 /* GStreamer
2  * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracerutils.c: tracing subsystem
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:gsttracerutils
24  * @short_description: Tracing subsystem
25  *
26  * The tracing subsystem provides hooks in the core library and API for modules
27  * to attach to them.
28  *
29  * The user can activate tracers by setting the environment variable GST_TRACE
30  * to a ';' separated list of tracers.
31  */
32
33 #define GST_USE_UNSTABLE_API
34
35 #include "gst_private.h"
36 #include "gsttracer.h"
37 #include "gsttracerfactory.h"
38 #include "gsttracerutils.h"
39
40 #ifndef GST_DISABLE_GST_DEBUG
41
42 /* tracing helpers */
43
44 gboolean _priv_tracer_enabled = FALSE;
45 /* TODO(ensonic): use array of GPtrArray* ? */
46 GList *_priv_tracers[GST_TRACER_HOOK_ID_LAST] = { NULL, };
47
48 /* Initialize the tracing system */
49 void
50 _priv_gst_tracer_init (void)
51 {
52   const gchar *env = g_getenv ("GST_TRACE");
53
54   if (env != NULL && *env != '\0') {
55     GstRegistry *registry = gst_registry_get ();
56     GstPluginFeature *feature;
57     GstTracerFactory *factory;
58     GstTracerHook mask;
59     GstObject *tracer;
60     gchar **t = g_strsplit_set (env, ";", 0);
61     gint i = 0, j;
62     gchar *params;
63
64     GST_INFO ("enabling tracers: '%s'", env);
65
66     while (t[i]) {
67       // check t[i] for params
68       if ((params = strchr (t[i], '('))) {
69         gchar *end = strchr (&params[1], ')');
70         *params = '\0';
71         params++;
72         if (end)
73           *end = '\0';
74       } else {
75         params = NULL;
76       }
77
78       GST_INFO ("checking tracer: '%s'", t[i]);
79
80       if ((feature = gst_registry_lookup_feature (registry, t[i]))) {
81         factory = GST_TRACER_FACTORY (gst_plugin_feature_load (feature));
82         if (factory) {
83           GST_INFO_OBJECT (factory, "creating tracer: type-id=%u",
84               (guint) factory->type);
85
86           tracer = g_object_new (factory->type, "params", params, NULL);
87           g_object_get (tracer, "mask", &mask, NULL);
88
89           if (mask) {
90             /* add to lists according to mask */
91             j = 0;
92             while (mask && (j < GST_TRACER_HOOK_ID_LAST)) {
93               if (mask & 1) {
94                 _priv_tracers[j] = g_list_prepend (_priv_tracers[j],
95                     gst_object_ref (tracer));
96                 GST_WARNING_OBJECT (tracer, "added tracer to hook %d", j);
97               }
98               mask >>= 1;
99               j++;
100             }
101
102             _priv_tracer_enabled = TRUE;
103           } else {
104             GST_WARNING_OBJECT (tracer,
105                 "tracer with zero mask won't have any effect");
106           }
107           gst_object_unref (tracer);
108         } else {
109           GST_WARNING_OBJECT (feature,
110               "loading plugin containing feature %s failed!", t[i]);
111         }
112       } else {
113         GST_WARNING ("no tracer named '%s'", t[i]);
114       }
115       i++;
116     }
117     g_strfreev (t);
118   }
119 }
120
121 void
122 _priv_gst_tracer_deinit (void)
123 {
124   gint i;
125   GList *node;
126
127   /* shutdown tracers for final reports */
128   for (i = 0; i < GST_TRACER_HOOK_ID_LAST; i++) {
129     for (node = _priv_tracers[i]; node; node = g_list_next (node)) {
130       gst_object_unref (node->data);
131     }
132     g_list_free (_priv_tracers[i]);
133     _priv_tracers[i] = NULL;
134   }
135   _priv_tracer_enabled = FALSE;
136 }
137
138 void
139 gst_tracer_dispatch (GstTracerHookId hid, GstTracerMessageId mid, ...)
140 {
141   va_list var_args;
142   GList *node;
143
144   for (node = _priv_tracers[hid]; node; node = g_list_next (node)) {
145     va_start (var_args, mid);
146     gst_tracer_invoke (node->data, hid, mid, var_args);
147     va_end (var_args);
148   }
149 }
150
151 #endif /* GST_DISABLE_GST_DEBUG */