tracer: add a GFlag for the tracer scope
[platform/upstream/gstreamer.git] / gst / gsttracerrecord.c
1 /* GStreamer
2  * Copyright (C) 2016 Stefan Sauer <ensonic@users.sf.net>
3  *
4  * gsttracerrecord.c: tracer log record 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:gsttracerrecord
24  * @short_description: Trace log entry class
25  *
26  * Tracing modules will create instances of this class to announce the data they
27  * will log and create a log formatter.
28  *
29  * Since: 1.8
30  */
31
32 #include "gst_private.h"
33 #include "gstinfo.h"
34 #include "gststructure.h"
35 #include "gsttracerrecord.h"
36 #include "gstvalue.h"
37
38 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
39 #define GST_CAT_DEFAULT tracer_debug
40
41
42 enum
43 {
44   PROP_0,
45   PROP_SPEC,
46   PROP_LAST
47 };
48
49 static GParamSpec *properties[PROP_LAST];
50
51 struct _GstTracerRecord
52 {
53   GstObject parent;
54
55   GstStructure *spec;
56   gchar *format;
57 };
58
59 struct _GstTracerRecordClass
60 {
61   GstObjectClass parent_class;
62 };
63
64 #define gst_tracer_record_parent_class parent_class
65 G_DEFINE_TYPE (GstTracerRecord, gst_tracer_record, GST_TYPE_OBJECT);
66
67 static gboolean
68 build_field_template (GQuark field_id, const GValue * value, gpointer user_data)
69 {
70   GString *s = (GString *) user_data;
71   const GstStructure *sub;
72   GValue template_value = { 0, };
73   GType type;
74   gboolean res;
75
76   g_return_val_if_fail (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE, FALSE);
77
78   sub = gst_value_get_structure (value);
79   type = g_value_get_gtype (gst_structure_get_value (sub, "type"));
80   g_value_init (&template_value, type);
81
82   res = priv__gst_structure_append_template_to_gstring (field_id,
83       &template_value, s);
84   g_value_unset (&template_value);
85   return res;
86 }
87
88 static void
89 gst_tracer_record_build_format (GstTracerRecord * self)
90 {
91   GstStructure *structure = self->spec;
92   GString *s;
93   gchar *name = (gchar *) g_quark_to_string (structure->name);
94   gchar *p;
95
96   g_return_if_fail (g_str_has_suffix (name, ".class"));
97
98   GST_TRACE ("%" GST_PTR_FORMAT, structure);
99
100   /* cut off '.class' suffix */
101   name = g_strdup (name);
102   p = strrchr (name, '.');
103   *p = '\0';
104
105   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
106   g_string_append (s, name);
107   gst_structure_foreach (structure, build_field_template, s);
108   g_string_append_c (s, ';');
109
110   self->format = g_string_free (s, FALSE);
111   GST_INFO ("new format string: %s", self->format);
112   g_free (name);
113 }
114
115 static void
116 gst_tracer_record_dispose (GObject * object)
117 {
118   GstTracerRecord *self = GST_TRACER_RECORD (object);
119
120   gst_structure_free (self->spec);
121   g_free (self->format);
122 }
123
124 static void
125 gst_tracer_record_set_property (GObject * object, guint prop_id,
126     const GValue * value, GParamSpec * pspec)
127 {
128   GstTracerRecord *self = GST_TRACER_RECORD_CAST (object);
129
130   switch (prop_id) {
131     case PROP_SPEC:
132       self->spec = g_value_get_boxed (value);
133       gst_tracer_record_build_format (self);
134       break;
135     default:
136       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
137       break;
138   }
139 }
140
141 static void
142 gst_tracer_record_get_property (GObject * object, guint prop_id,
143     GValue * value, GParamSpec * pspec)
144 {
145   GstTracerRecord *self = GST_TRACER_RECORD_CAST (object);
146
147   switch (prop_id) {
148     case PROP_SPEC:
149       // TODO(ensonic): copy?
150       g_value_set_boxed (value, self->spec);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155   }
156 }
157
158 static void
159 gst_tracer_record_class_init (GstTracerRecordClass * klass)
160 {
161   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
162
163   gobject_class->set_property = gst_tracer_record_set_property;
164   gobject_class->get_property = gst_tracer_record_get_property;
165   gobject_class->dispose = gst_tracer_record_dispose;
166
167   properties[PROP_SPEC] =
168       g_param_spec_boxed ("spec", "Spec", "Log record specification",
169       GST_TYPE_STRUCTURE,
170       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
171
172   g_object_class_install_properties (gobject_class, PROP_LAST, properties);
173 }
174
175 static void
176 gst_tracer_record_init (GstTracerRecord * self)
177 {
178 }
179
180 /**
181  * gst_tracer_record_new:
182  * @spec: the record specification
183  *
184  * Create a new tracer record. The record instance can be used to efficiently
185  * log entries using gst_tracer_record_log().
186  *
187  * The name of the @spec #GstStructure must end on '.class'. This name without
188  * the suffix will be used for the log records. The @spec must have a field for
189  * each value that gets logged where the field name is the value name. The field
190  * must be a nested structure describing the value. The sub structure must
191  * contain a field called 'type' of %G_TYPE_GTYPE that contains the GType of the
192  * value.
193  *
194  * The way to deal with optional values is to log an additional boolean before
195  * the optional field, that if %TRUE signals that the optional field is valid
196  * and %FALSE signals that the optional field should be ignored. One must still
197  * log a placeholder value for the optional field though. Please also note, that
198  * pointer type values must not be NULL - the underlying serialisation can not
199  * handle that right now.
200  *
201  * <note><para>
202  *   Please note that this is still under discussion and subject to change.
203  * </para></note>
204  *
205  * Returns: a new #GstTracerRecord
206  */
207 GstTracerRecord *
208 gst_tracer_record_new (GstStructure * spec)
209 {
210   return g_object_new (GST_TYPE_TRACER_RECORD, "spec", spec, NULL);
211 }
212
213 /**
214  * gst_tracer_record_log:
215  * @self: the tracer-record
216  * @...: the args as described in the spec-
217  *
218  * Serialzes the trace event into the log.
219  *
220  * Right now this is using the gstreamer debug log with the level TRACE (7) and
221  * the category "GST_TRACER".
222  * <note><para>
223  *   Please note that this is still under discussion and subject to change.
224  * </para></note>
225  */
226 void
227 gst_tracer_record_log (GstTracerRecord * self, ...)
228 {
229   va_list var_args;
230
231   /*
232    * does it make sense to use the {file, line, func} from the tracer hook?
233    * a)
234    * - we'd need to pass them in the macros to gst_tracer_dispatch()
235    * - and each tracer needs to grab them from the va_list and pass them here
236    * b)
237    * - we create a context in dispatch, pass that to the tracer
238    * - and the tracer will pass that here
239    * ideally we also use *our* ts instead of the one that
240    * gst_debug_log_default() will pick
241    */
242
243   va_start (var_args, self);
244   if (G_LIKELY (GST_LEVEL_TRACE <= _gst_debug_min)) {
245     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_TRACE, "", "", 0, NULL,
246         self->format, var_args);
247   }
248   va_end (var_args);
249 }