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