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