tracerrecord: Remove useless NULL check and add assertion for making assumptions...
[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 #include <gobject/gvaluecollector.h>
41
42 GST_DEBUG_CATEGORY_EXTERN (tracer_debug);
43 #define GST_CAT_DEFAULT tracer_debug
44
45 struct _GstTracerRecord
46 {
47   GstObject parent;
48
49   GstStructure *spec;
50   gchar *format;
51 };
52
53 struct _GstTracerRecordClass
54 {
55   GstObjectClass parent_class;
56 };
57
58 #define gst_tracer_record_parent_class parent_class
59 G_DEFINE_TYPE (GstTracerRecord, gst_tracer_record, GST_TYPE_OBJECT);
60
61 static gboolean
62 build_field_template (GQuark field_id, const GValue * value, gpointer user_data)
63 {
64   GString *s = (GString *) user_data;
65   const GstStructure *sub;
66   GValue template_value = { 0, };
67   GType type = G_TYPE_INVALID;
68   GstTracerValueFlags flags = GST_TRACER_VALUE_FLAGS_NONE;
69   gboolean res;
70
71   if (G_VALUE_TYPE (value) != GST_TYPE_STRUCTURE) {
72     GST_WARNING ("expected field of type GstStructure, but %s is %s",
73         g_quark_to_string (field_id), G_VALUE_TYPE_NAME (value));
74     return FALSE;
75   }
76
77   sub = gst_value_get_structure (value);
78   gst_structure_get (sub, "type", G_TYPE_GTYPE, &type, "flags",
79       GST_TYPE_TRACER_VALUE_FLAGS, &flags, NULL);
80
81   if (flags & GST_TRACER_VALUE_FLAGS_OPTIONAL) {
82     gchar *opt_name = g_strconcat ("have-", g_quark_to_string (field_id), NULL);
83
84     /* add a boolean field, that indicates the presence of the next field */
85     g_value_init (&template_value, G_TYPE_BOOLEAN);
86     priv__gst_structure_append_template_to_gstring (g_quark_from_string
87         (opt_name), &template_value, s);
88     g_value_unset (&template_value);
89     g_free (opt_name);
90   }
91
92   g_value_init (&template_value, type);
93   res = priv__gst_structure_append_template_to_gstring (field_id,
94       &template_value, s);
95   g_value_unset (&template_value);
96   return res;
97 }
98
99 static void
100 gst_tracer_record_build_format (GstTracerRecord * self)
101 {
102   GstStructure *structure = self->spec;
103   GString *s;
104   gchar *name = (gchar *) g_quark_to_string (structure->name);
105   gchar *p;
106
107   g_return_if_fail (g_str_has_suffix (name, ".class"));
108
109   /* announce the format */
110   GST_TRACE ("%" GST_PTR_FORMAT, structure);
111
112   /* cut off '.class' suffix */
113   name = g_strdup (name);
114   p = strrchr (name, '.');
115   g_assert (p != NULL);
116   *p = '\0';
117
118   s = g_string_sized_new (STRUCTURE_ESTIMATED_STRING_LEN (structure));
119   g_string_append (s, name);
120   gst_structure_foreach (structure, build_field_template, s);
121   g_string_append_c (s, ';');
122
123   self->format = g_string_free (s, FALSE);
124   GST_DEBUG ("new format string: %s", self->format);
125   g_free (name);
126 }
127
128 static void
129 gst_tracer_record_dispose (GObject * object)
130 {
131   GstTracerRecord *self = GST_TRACER_RECORD (object);
132
133   if (self->spec) {
134     gst_structure_free (self->spec);
135     self->spec = NULL;
136   }
137   g_free (self->format);
138   self->format = NULL;
139 }
140
141 static void
142 gst_tracer_record_class_init (GstTracerRecordClass * klass)
143 {
144   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
145
146   gobject_class->dispose = gst_tracer_record_dispose;
147 }
148
149 static void
150 gst_tracer_record_init (GstTracerRecord * self)
151 {
152 }
153
154 /**
155  * gst_tracer_record_new:
156  * @name: name of new record, must end on ".class".
157  * @firstfield: name of first field to set
158  * @...: additional arguments
159
160  *
161  * Create a new tracer record. The record instance can be used to efficiently
162  * log entries using gst_tracer_record_log().
163  *
164  * The @name without the ".class" suffix will be used for the log records.
165  * There must be fields for each value that gets logged where the field name is
166  * the value name. The field must be a #GstStructure describing the value. The
167  * sub structure must contain a field called 'type' of %G_TYPE_GTYPE that
168  * contains the GType of the value. The resulting #GstTracerRecord will take
169  * ownership of the field structures.
170  *
171  * The way to deal with optional values is to log an additional boolean before
172  * the optional field, that if %TRUE signals that the optional field is valid
173  * and %FALSE signals that the optional field should be ignored. One must still
174  * log a placeholder value for the optional field though. Please also note, that
175  * pointer type values must not be NULL - the underlying serialisation can not
176  * handle that right now.
177  *
178  * <note><para>
179  *   Please note that this is still under discussion and subject to change.
180  * </para></note>
181  *
182  * Returns: a new #GstTracerRecord
183  */
184 GstTracerRecord *
185 gst_tracer_record_new (const gchar * name, const gchar * firstfield, ...)
186 {
187   GstTracerRecord *self;
188   GstStructure *structure;
189   va_list varargs;
190   gchar *err = NULL;
191   GType type;
192   GQuark id;
193
194   va_start (varargs, firstfield);
195   structure = gst_structure_new_empty (name);
196
197   while (firstfield) {
198     GValue val = { 0, };
199
200     id = g_quark_from_string (firstfield);
201     type = va_arg (varargs, GType);
202
203     /* all fields passed here must be GstStructures which we take over */
204     if (type != GST_TYPE_STRUCTURE) {
205       GST_WARNING ("expected field of type GstStructure, but %s is %s",
206           firstfield, g_type_name (type));
207     }
208
209     G_VALUE_COLLECT_INIT (&val, type, varargs, G_VALUE_NOCOPY_CONTENTS, &err);
210     if (G_UNLIKELY (err)) {
211       g_critical ("%s", err);
212       break;
213     }
214     /* see boxed_proxy_collect_value */
215     val.data[1].v_uint &= ~G_VALUE_NOCOPY_CONTENTS;
216     gst_structure_id_take_value (structure, id, &val);
217
218     firstfield = va_arg (varargs, gchar *);
219   }
220   va_end (varargs);
221
222   self = g_object_newv (GST_TYPE_TRACER_RECORD, 0, NULL);
223   self->spec = structure;
224   gst_tracer_record_build_format (self);
225
226   return self;
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