1 /* GStreamer
2  *
3  * Copyright (C) 2013 Thibault Saunier <thibault.saunier@collabora.com>
4  *
5  * gst-validate-reporter.c
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 /**
23  * SECTION:gst-validate-reporter
24  * @short_description: A #GInterface that allows #GObject to be used as originator of
25  * issues in the GstValidate reporting system
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31 
32 #include "gst-validate-internal.h"
33 #include "gst-validate-reporter.h"
34 #include "gst-validate-report.h"
35 
36 #define REPORTER_PRIVATE "gst-validate-reporter-private"
37 
38 typedef struct _GstValidateReporterPrivate
39 {
40   GWeakRef runner;
41   GHashTable *reports;
42   char *name;
43   guint log_handler_id;
44   GMutex reports_lock;
45 } GstValidateReporterPrivate;
46 
47 static GstValidateReporterPrivate *g_log_handler = NULL;
48 
49 G_DEFINE_INTERFACE (GstValidateReporter, gst_validate_reporter, G_TYPE_OBJECT);
50 
51 static void
gst_validate_reporter_default_init(GstValidateReporterInterface * iface)52 gst_validate_reporter_default_init (GstValidateReporterInterface * iface)
53 {
54   g_object_interface_install_property (iface,
55       g_param_spec_object ("validate-runner", "Validate Runner",
56           "The Validate runner to report errors to",
57           GST_TYPE_VALIDATE_RUNNER,
58           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
59 }
60 
61 static void
_free_priv(GstValidateReporterPrivate * priv)62 _free_priv (GstValidateReporterPrivate * priv)
63 {
64   if (g_log_handler == priv) {
65     g_log_set_default_handler (g_log_default_handler, NULL);
66     g_log_handler = NULL;
67   }
68 
69   g_hash_table_unref (priv->reports);
70   g_free (priv->name);
71   g_mutex_clear (&priv->reports_lock);
72   g_weak_ref_clear (&priv->runner);
73   g_slice_free (GstValidateReporterPrivate, priv);
74 }
75 
76 static GstValidateReporterPrivate *
gst_validate_reporter_get_priv(GstValidateReporter * reporter)77 gst_validate_reporter_get_priv (GstValidateReporter * reporter)
78 {
79   GstValidateReporterPrivate *priv;
80 
81   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
82 
83   if (priv == NULL) {
84     priv = g_slice_new0 (GstValidateReporterPrivate);
85     priv->reports = g_hash_table_new_full (g_direct_hash,
86         g_direct_equal, NULL, (GDestroyNotify) gst_validate_report_unref);
87 
88     g_mutex_init (&priv->reports_lock);
89     g_object_set_data_full (G_OBJECT (reporter), REPORTER_PRIVATE, priv,
90         (GDestroyNotify) _free_priv);
91   }
92 
93   return priv;
94 }
95 
96 #define GST_VALIDATE_REPORTER_REPORTS_LOCK(r)			\
97   G_STMT_START {					\
98   (g_mutex_lock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock));		\
99   } G_STMT_END
100 
101 #define GST_VALIDATE_REPORTER_REPORTS_UNLOCK(r)			\
102   G_STMT_START {					\
103   (g_mutex_unlock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock));		\
104   } G_STMT_END
105 
106 static GstValidateInterceptionReturn
gst_validate_reporter_intercept_report(GstValidateReporter * reporter,GstValidateReport * report)107 gst_validate_reporter_intercept_report (GstValidateReporter * reporter,
108     GstValidateReport * report)
109 {
110   GstValidateInterceptionReturn ret = GST_VALIDATE_REPORTER_REPORT;
111   GstValidateReporterInterface *iface =
112       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
113 
114   if (iface->intercept_report) {
115     ret = iface->intercept_report (reporter, report);
116   }
117 
118   return ret;
119 }
120 
121 GstValidateReportingDetails
gst_validate_reporter_get_reporting_level(GstValidateReporter * reporter)122 gst_validate_reporter_get_reporting_level (GstValidateReporter * reporter)
123 {
124   GstValidateReportingDetails ret = GST_VALIDATE_SHOW_UNKNOWN;
125   GstValidateReporterInterface *iface =
126       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
127 
128   if (iface->get_reporting_level) {
129     ret = iface->get_reporting_level (reporter);
130   }
131 
132   return ret;
133 }
134 
135 /**
136  * gst_validate_reporter_get_pipeline:
137  * @reporter: The reporter to get the pipeline from
138  *
139  * Returns: (transfer full) (allow-none): The #GstPipeline
140  */
141 GstPipeline *
gst_validate_reporter_get_pipeline(GstValidateReporter * reporter)142 gst_validate_reporter_get_pipeline (GstValidateReporter * reporter)
143 {
144   GstValidateReporterInterface *iface =
145       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
146 
147   if (iface->get_pipeline)
148     return iface->get_pipeline (reporter);
149 
150   return NULL;
151 }
152 
153 GstValidateReport *
gst_validate_reporter_get_report(GstValidateReporter * reporter,GstValidateIssueId issue_id)154 gst_validate_reporter_get_report (GstValidateReporter * reporter,
155     GstValidateIssueId issue_id)
156 {
157   GstValidateReport *report;
158   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
159 
160   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
161   report = g_hash_table_lookup (priv->reports, (gconstpointer) issue_id);
162   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
163 
164   return report;
165 }
166 
167 void
gst_validate_report_valist(GstValidateReporter * reporter,GstValidateIssueId issue_id,const gchar * format,va_list var_args)168 gst_validate_report_valist (GstValidateReporter * reporter,
169     GstValidateIssueId issue_id, const gchar * format, va_list var_args)
170 {
171   GstValidateReport *report, *prev_report;
172   gchar *message, *combo;
173   va_list vacopy;
174   GstValidateIssue *issue;
175   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
176   GstValidateInterceptionReturn int_ret;
177   GstValidateRunner *runner = NULL;
178 
179   issue = gst_validate_issue_from_id (issue_id);
180 
181   g_return_if_fail (issue != NULL);
182   g_return_if_fail (GST_IS_VALIDATE_REPORTER (reporter));
183 
184   G_VA_COPY (vacopy, var_args);
185   message = g_strdup_vprintf (format, vacopy);
186   report = gst_validate_report_new (issue, reporter, message);
187 
188 #ifndef GST_DISABLE_GST_DEBUG
189   combo =
190       g_strdup_printf ("<%s> %" GST_VALIDATE_ISSUE_FORMAT " : %s", priv->name,
191       GST_VALIDATE_ISSUE_ARGS (issue), format);
192   G_VA_COPY (vacopy, var_args);
193   if (report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
194     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_ERROR, __FILE__,
195         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
196   } else if (report->level == GST_VALIDATE_REPORT_LEVEL_WARNING)
197     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_WARNING, __FILE__,
198         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
199   else if (report->level == GST_VALIDATE_REPORT_LEVEL_ISSUE)
200     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_LOG, __FILE__,
201         GST_FUNCTION, __LINE__, (GObject *) NULL, combo, vacopy);
202   else
203     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_DEBUG, __FILE__,
204         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
205   g_free (combo);
206 #endif
207   va_end (vacopy);
208 
209   int_ret = gst_validate_reporter_intercept_report (reporter, report);
210 
211   if (int_ret == GST_VALIDATE_REPORTER_DROP) {
212     gst_validate_report_unref (report);
213     goto done;
214   }
215 
216   prev_report = g_hash_table_lookup (priv->reports, (gconstpointer) issue_id);
217 
218   runner = gst_validate_reporter_get_runner (reporter);
219   if (prev_report) {
220     GstValidateReportingDetails reporter_level =
221         gst_validate_reporter_get_reporting_level (reporter);
222     GstValidateReportingDetails runner_level = GST_VALIDATE_SHOW_UNKNOWN;
223 
224     if (runner)
225       runner_level = gst_validate_runner_get_default_reporting_level (runner);
226 
227     if (reporter_level == GST_VALIDATE_SHOW_ALL ||
228         (runner_level == GST_VALIDATE_SHOW_ALL &&
229             reporter_level == GST_VALIDATE_SHOW_UNKNOWN))
230       gst_validate_report_add_repeated_report (prev_report, report);
231 
232     gst_validate_report_unref (report);
233     goto done;
234   }
235 
236   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
237   g_hash_table_insert (priv->reports, (gpointer) issue_id, report);
238   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
239 
240   if (runner && int_ret == GST_VALIDATE_REPORTER_REPORT) {
241     gst_validate_runner_add_report (runner, report);
242   }
243 
244   if (gst_validate_report_check_abort (report)) {
245     if (runner)
246       gst_validate_runner_printf (runner);
247 
248     g_error ("Fatal report received: %" GST_VALIDATE_ERROR_REPORT_PRINT_FORMAT,
249         GST_VALIDATE_REPORT_PRINT_ARGS (report));
250   }
251 
252 done:
253   if (runner)
254     gst_object_unref (runner);
255 
256   g_free (message);
257 }
258 
259 static void
gst_validate_default_log_hanlder(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,gpointer user_data)260 gst_validate_default_log_hanlder (const gchar * log_domain,
261     GLogLevelFlags log_level, const gchar * message, gpointer user_data)
262 {
263   gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
264 
265   if (trace) {
266     g_print ("\nStack trace:\n%s\n", trace);
267     g_free (trace);
268   }
269 
270   g_log_default_handler (log_domain, log_level, message, user_data);
271 }
272 
273 static void
gst_validate_reporter_destroyed(gpointer udata,GObject * freed_reporter)274 gst_validate_reporter_destroyed (gpointer udata, GObject * freed_reporter)
275 {
276   g_log_set_handler ("GStreamer",
277       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
278   g_log_set_handler ("GLib",
279       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
280   g_log_set_handler ("GLib-GObject",
281       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
282 }
283 
284 static void
gst_validate_reporter_g_log_func(const gchar * log_domain,GLogLevelFlags log_level,const gchar * message,GstValidateReporter * reporter)285 gst_validate_reporter_g_log_func (const gchar * log_domain,
286     GLogLevelFlags log_level, const gchar * message,
287     GstValidateReporter * reporter)
288 {
289   if (log_level & G_LOG_LEVEL_ERROR)
290     gst_validate_default_log_hanlder (log_domain, log_level, message, reporter);
291   else if (log_level & G_LOG_LEVEL_CRITICAL)
292     GST_VALIDATE_REPORT (reporter, G_LOG_CRITICAL, "%s", message);
293   else if (log_level & G_LOG_LEVEL_WARNING)
294     GST_VALIDATE_REPORT (reporter, G_LOG_WARNING, "%s", message);
295   else
296     GST_VALIDATE_REPORT (reporter, G_LOG_ISSUE, "%s", message);
297 }
298 
299 /**
300  * gst_validate_report:
301  * @reporter: The source of the new report
302  * @issue_id: The #GstValidateIssueId of the issue
303  * @format: The format of the message describing the issue in a printf
304  *       format followed by the parameters.
305  * @...: Substitution arguments for @format
306  *
307  * Reports a new issue in the GstValidate reporting system with @m
308  * as the source of that issue.
309  *
310  * You can also use #GST_VALIDATE_REPORT instead.
311  */
312 void
gst_validate_report(GstValidateReporter * reporter,GstValidateIssueId issue_id,const gchar * format,...)313 gst_validate_report (GstValidateReporter * reporter,
314     GstValidateIssueId issue_id, const gchar * format, ...)
315 {
316   va_list var_args;
317 
318   va_start (var_args, format);
319   gst_validate_report_valist (reporter, issue_id, format, var_args);
320   va_end (var_args);
321 }
322 
323 void
gst_validate_reporter_report_simple(GstValidateReporter * reporter,GstValidateIssueId issue_id,const gchar * message)324 gst_validate_reporter_report_simple (GstValidateReporter * reporter,
325     GstValidateIssueId issue_id, const gchar * message)
326 {
327   gst_validate_report (reporter, issue_id, "%s", message);
328 }
329 
330 /**
331  * gst_validate_reporter_set_name:
332  * @reporter: The reporter to set the name on
333  * @name: (transfer full): The name of the reporter
334  *
335  * Sets @ name on @reporter
336  */
337 void
gst_validate_reporter_set_name(GstValidateReporter * reporter,gchar * name)338 gst_validate_reporter_set_name (GstValidateReporter * reporter, gchar * name)
339 {
340   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
341 
342   g_free (priv->name);
343 
344   priv->name = name;
345 }
346 
347 const gchar *
gst_validate_reporter_get_name(GstValidateReporter * reporter)348 gst_validate_reporter_get_name (GstValidateReporter * reporter)
349 {
350   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
351 
352   return priv->name;
353 }
354 
355 /**
356  * gst_validate_reporter_get_runner:
357  * @reporter: The reporter to get the runner from
358  *
359  * Returns: (transfer full): The runner
360  */
361 GstValidateRunner *
gst_validate_reporter_get_runner(GstValidateReporter * reporter)362 gst_validate_reporter_get_runner (GstValidateReporter * reporter)
363 {
364   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
365 
366   return g_weak_ref_get (&priv->runner);
367 }
368 
369 void
gst_validate_reporter_set_runner(GstValidateReporter * reporter,GstValidateRunner * runner)370 gst_validate_reporter_set_runner (GstValidateReporter * reporter,
371     GstValidateRunner * runner)
372 {
373   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
374 
375   g_weak_ref_set (&priv->runner, runner);
376 
377   g_object_notify (G_OBJECT (reporter), "validate-runner");
378 }
379 
380 /**
381  * gst_validate_reporter_set_handle_g_logs:
382  * @reporter: The #GstValidateReporter to set has the handler for g_log
383  *
384  * Set @reporter has the 'source' of any g_log happening during the
385  * execution. Usually the monitor of the first #GstPipeline is used
386  * to handle g_logs.
387  *
388  * Basically this function is used in order to start tracking any
389  * issue reported with g_log in the process into GstValidate report
390  * in the GstValidate reporting system.
391  */
392 void
gst_validate_reporter_set_handle_g_logs(GstValidateReporter * reporter)393 gst_validate_reporter_set_handle_g_logs (GstValidateReporter * reporter)
394 {
395   g_log_set_default_handler ((GLogFunc) gst_validate_reporter_g_log_func,
396       reporter);
397 
398   g_log_set_handler ("GStreamer",
399       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
400 
401   g_log_set_handler ("GLib",
402       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
403 
404 
405   g_log_set_handler ("GLib-GObject",
406       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
407 
408   g_log_handler = gst_validate_reporter_get_priv (reporter);
409   g_object_weak_ref (G_OBJECT (reporter), gst_validate_reporter_destroyed,
410       NULL);
411 
412 }
413 
414 /**
415  * gst_validate_reporter_get_reports:
416  * @reporter: a #GstValidateReporter
417  *
418  * Get the list of reports present in the reporter.
419  *
420  * Returns: (transfer full) (element-type GstValidateReport): the list of
421  * #GstValidateReport present in the reporter.
422  * The caller should unref each report once it is done with them.
423  */
424 GList *
gst_validate_reporter_get_reports(GstValidateReporter * reporter)425 gst_validate_reporter_get_reports (GstValidateReporter * reporter)
426 {
427   GstValidateReporterPrivate *priv;
428   GList *reports, *tmp;
429   GList *ret = NULL;
430 
431   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
432 
433   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
434   reports = g_hash_table_get_values (priv->reports);
435   for (tmp = reports; tmp; tmp = tmp->next) {
436     ret =
437         g_list_append (ret,
438         gst_validate_report_ref ((GstValidateReport *) (tmp->data)));
439   }
440   g_list_free (reports);
441   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
442 
443   return ret;
444 }
445 
446 /**
447  * gst_validate_reporter_get_reports_count:
448  * @reporter: a #GstValidateReporter
449  *
450  * Get the number of reports present in the reporter.
451  *
452  * Returns: the number of reports currently present in @reporter.
453  */
454 gint
gst_validate_reporter_get_reports_count(GstValidateReporter * reporter)455 gst_validate_reporter_get_reports_count (GstValidateReporter * reporter)
456 {
457   GstValidateReporterPrivate *priv;
458   gint ret;
459 
460   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
461 
462   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
463   ret = g_hash_table_size (priv->reports);
464   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
465 
466   return ret;
467 }
468 
469 /**
470  * gst_validate_reporter_purge_reports:
471  * @reporter: a #GstValidateReporter
472  *
473  * Remove all the #GstValidateReport from @reporter. This should be called
474  * before unreffing the reporter to break cyclic references.
475  */
476 void
gst_validate_reporter_purge_reports(GstValidateReporter * reporter)477 gst_validate_reporter_purge_reports (GstValidateReporter * reporter)
478 {
479   GstValidateReporterPrivate *priv;
480 
481   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
482 
483   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
484   g_hash_table_remove_all (priv->reports);
485   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
486 }
487