Merging gst-devtools
[platform/upstream/gstreamer.git] / validate / gst / validate / gst-validate-reporter.c
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  * @title: GstValidateReporter
25  * @short_description: A #GInterface that allows #GObject to be used as originator of
26  * issues in the GstValidate reporting system
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <math.h>
35 #include "gst-validate-internal.h"
36 #include "gst-validate-reporter.h"
37 #include "gst-validate-report.h"
38
39 #define REPORTER_PRIVATE "gst-validate-reporter-private"
40
41 typedef struct _GstValidateReporterPrivate
42 {
43   GWeakRef runner;
44   GHashTable *reports;
45   char *name;
46   guint log_handler_id;
47   GMutex reports_lock;
48 } GstValidateReporterPrivate;
49
50 static GstValidateReporterPrivate *g_log_handler = NULL;
51
52 G_DEFINE_INTERFACE (GstValidateReporter, gst_validate_reporter, G_TYPE_OBJECT);
53
54 static void
55 gst_validate_reporter_default_init (GstValidateReporterInterface * iface)
56 {
57   g_object_interface_install_property (iface,
58       g_param_spec_object ("validate-runner", "Validate Runner",
59           "The Validate runner to report errors to",
60           GST_TYPE_VALIDATE_RUNNER,
61           G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
62 }
63
64 static void
65 _free_priv (GstValidateReporterPrivate * priv)
66 {
67   if (g_log_handler == priv) {
68     g_log_set_default_handler (g_log_default_handler, NULL);
69     g_log_handler = NULL;
70   }
71
72   g_hash_table_unref (priv->reports);
73   g_free (priv->name);
74   g_mutex_clear (&priv->reports_lock);
75   g_weak_ref_clear (&priv->runner);
76   g_slice_free (GstValidateReporterPrivate, priv);
77 }
78
79 static GstValidateReporterPrivate *
80 gst_validate_reporter_get_priv (GstValidateReporter * reporter)
81 {
82   GstValidateReporterPrivate *priv;
83
84   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
85
86   if (priv == NULL) {
87     priv = g_slice_new0 (GstValidateReporterPrivate);
88     priv->reports = g_hash_table_new_full (g_direct_hash,
89         g_direct_equal, NULL, (GDestroyNotify) gst_validate_report_unref);
90
91     g_mutex_init (&priv->reports_lock);
92     g_object_set_data_full (G_OBJECT (reporter), REPORTER_PRIVATE, priv,
93         (GDestroyNotify) _free_priv);
94   }
95
96   return priv;
97 }
98
99 #define GST_VALIDATE_REPORTER_REPORTS_LOCK(r)                   \
100   G_STMT_START {                                        \
101   (g_mutex_lock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock));                \
102   } G_STMT_END
103
104 #define GST_VALIDATE_REPORTER_REPORTS_UNLOCK(r)                 \
105   G_STMT_START {                                        \
106   (g_mutex_unlock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock));              \
107   } G_STMT_END
108
109 static GstValidateInterceptionReturn
110 gst_validate_reporter_intercept_report (GstValidateReporter * reporter,
111     GstValidateReport * report)
112 {
113   GstValidateInterceptionReturn ret = GST_VALIDATE_REPORTER_REPORT;
114   GstValidateReporterInterface *iface =
115       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
116
117   if (iface->intercept_report) {
118     ret = iface->intercept_report (reporter, report);
119   }
120
121   return ret;
122 }
123
124 GstValidateReportingDetails
125 gst_validate_reporter_get_reporting_level (GstValidateReporter * reporter)
126 {
127   GstValidateReportingDetails ret = GST_VALIDATE_SHOW_UNKNOWN;
128   GstValidateReporterInterface *iface =
129       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
130
131   if (iface->get_reporting_level) {
132     ret = iface->get_reporting_level (reporter);
133   }
134
135   return ret;
136 }
137
138 /**
139  * gst_validate_reporter_get_pipeline:
140  * @reporter: The reporter to get the pipeline from
141  *
142  * Returns: (transfer full) (allow-none): The #GstPipeline
143  */
144 GstPipeline *
145 gst_validate_reporter_get_pipeline (GstValidateReporter * reporter)
146 {
147   GstValidateReporterInterface *iface =
148       GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
149
150   if (iface->get_pipeline)
151     return iface->get_pipeline (reporter);
152
153   return NULL;
154 }
155
156 GstValidateReport *
157 gst_validate_reporter_get_report (GstValidateReporter * reporter,
158     GstValidateIssueId issue_id)
159 {
160   GstValidateReport *report;
161   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
162
163   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
164   report = g_hash_table_lookup (priv->reports, (gconstpointer) issue_id);
165   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
166
167   return report;
168 }
169
170 void
171 gst_validate_report_valist (GstValidateReporter * reporter,
172     GstValidateIssueId issue_id, const gchar * format, va_list var_args)
173 {
174   GstValidateReport *report, *prev_report;
175   gchar *message, *combo;
176   va_list vacopy;
177   GstValidateIssue *issue;
178   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
179   GstValidateInterceptionReturn int_ret;
180   GstValidateRunner *runner = NULL;
181
182   issue = gst_validate_issue_from_id (issue_id);
183
184   g_return_if_fail (issue != NULL);
185   g_return_if_fail (GST_IS_VALIDATE_REPORTER (reporter));
186
187   G_VA_COPY (vacopy, var_args);
188   message = gst_info_strdup_vprintf (format, vacopy);
189   report = gst_validate_report_new (issue, reporter, message);
190
191 #ifndef GST_DISABLE_GST_DEBUG
192   combo =
193       g_strdup_printf ("<%s> %" GST_VALIDATE_ISSUE_FORMAT " : %s", priv->name,
194       GST_VALIDATE_ISSUE_ARGS (issue), format);
195   G_VA_COPY (vacopy, var_args);
196   if (report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
197     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_ERROR, __FILE__,
198         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
199   } else if (report->level == GST_VALIDATE_REPORT_LEVEL_WARNING)
200     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_WARNING, __FILE__,
201         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
202   else if (report->level == GST_VALIDATE_REPORT_LEVEL_ISSUE)
203     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_LOG, __FILE__,
204         GST_FUNCTION, __LINE__, (GObject *) NULL, combo, vacopy);
205   else
206     gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_DEBUG, __FILE__,
207         GST_FUNCTION, __LINE__, NULL, combo, vacopy);
208   g_free (combo);
209 #endif
210   va_end (vacopy);
211
212   int_ret = gst_validate_reporter_intercept_report (reporter, report);
213
214   if (int_ret == GST_VALIDATE_REPORTER_DROP) {
215     gst_validate_report_unref (report);
216     goto done;
217   }
218
219   prev_report = g_hash_table_lookup (priv->reports, (gconstpointer) issue_id);
220
221   runner = gst_validate_reporter_get_runner (reporter);
222   if (prev_report && prev_report->level != GST_VALIDATE_REPORT_LEVEL_EXPECTED) {
223     GstValidateReportingDetails reporter_level =
224         gst_validate_reporter_get_reporting_level (reporter);
225     GstValidateReportingDetails runner_level = GST_VALIDATE_SHOW_UNKNOWN;
226
227     if (runner)
228       runner_level = gst_validate_runner_get_default_reporting_level (runner);
229
230     if ((reporter_level == GST_VALIDATE_SHOW_ALL ||
231             (runner_level == GST_VALIDATE_SHOW_ALL &&
232                 reporter_level == GST_VALIDATE_SHOW_UNKNOWN)) ||
233         (issue->flags & GST_VALIDATE_ISSUE_FLAGS_FULL_DETAILS)) {
234
235       gst_validate_report_add_repeated_report (prev_report, report);
236     }
237
238     gst_validate_report_unref (report);
239     goto done;
240   }
241
242   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
243   g_hash_table_insert (priv->reports, (gpointer) issue_id, report);
244   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
245
246   if (runner && int_ret == GST_VALIDATE_REPORTER_REPORT) {
247     gst_validate_runner_add_report (runner, report);
248   }
249
250   if (gst_validate_report_check_abort (report)) {
251     if (runner)
252       gst_validate_runner_printf (runner);
253
254     gst_validate_abort ("Fatal report received: %"
255         GST_VALIDATE_ERROR_REPORT_PRINT_FORMAT,
256         GST_VALIDATE_REPORT_PRINT_ARGS (report));
257   }
258
259 done:
260   if (runner)
261     gst_object_unref (runner);
262
263   g_free (message);
264 }
265
266 static void
267 gst_validate_default_log_hanlder (const gchar * log_domain,
268     GLogLevelFlags log_level, const gchar * message, gpointer user_data)
269 {
270   gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
271
272   if (trace) {
273     gst_validate_printf (NULL, "\nStack trace:\n%s\n", trace);
274     g_free (trace);
275   }
276
277   g_log_default_handler (log_domain, log_level, message, user_data);
278 }
279
280 static void
281 gst_validate_reporter_destroyed (gpointer udata, GObject * freed_reporter)
282 {
283   g_log_set_handler ("GStreamer",
284       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
285   g_log_set_handler ("GLib",
286       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
287   g_log_set_handler ("GLib-GObject",
288       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_default_log_hanlder, NULL);
289 }
290
291 static void
292 gst_validate_reporter_g_log_func (const gchar * log_domain,
293     GLogLevelFlags log_level, const gchar * message,
294     GstValidateReporter * reporter)
295 {
296   if (log_level & G_LOG_LEVEL_ERROR)
297     gst_validate_default_log_hanlder (log_domain, log_level, message, reporter);
298   else if (log_level & G_LOG_LEVEL_CRITICAL)
299     GST_VALIDATE_REPORT (reporter, G_LOG_CRITICAL, "%s", message);
300   else if (log_level & G_LOG_LEVEL_WARNING)
301     GST_VALIDATE_REPORT (reporter, G_LOG_WARNING, "%s", message);
302 }
303
304 /**
305  * gst_validate_report:
306  * @reporter: The source of the new report
307  * @issue_id: The #GstValidateIssueId of the issue
308  * @format: The format of the message describing the issue in a printf
309  *       format followed by the parameters.
310  * @...: Substitution arguments for @format
311  *
312  * Reports a new issue in the GstValidate reporting system.
313  *
314  * You can also use #GST_VALIDATE_REPORT instead.
315  */
316 void
317 gst_validate_report (GstValidateReporter * reporter,
318     GstValidateIssueId issue_id, const gchar * format, ...)
319 {
320   va_list var_args;
321
322   va_start (var_args, format);
323   gst_validate_report_valist (reporter, issue_id, format, var_args);
324   va_end (var_args);
325 }
326
327 /**
328  * gst_validate_report_action:
329  * @reporter: The source of the new report
330  * @action: The action reporting the issue
331  * @issue_id: The #GstValidateIssueId of the issue
332  * @format: The format of the message describing the issue in a printf
333  *       format followed by the parameters.
334  * @...: Substitution arguments for @format
335  *
336  * Reports a new issue in the GstValidate reporting system specifying @action
337  * as failling action .
338  *
339  * You can also use #GST_VALIDATE_REPORT instead.
340  */
341 void
342 gst_validate_report_action (GstValidateReporter * reporter,
343     GstValidateAction * action, GstValidateIssueId issue_id,
344     const gchar * format, ...)
345 {
346   va_list var_args, var_copy;
347   GString *f;
348
349   if (!action) {
350     f = g_string_new (format);
351     goto done;
352   }
353
354   f = g_string_new (NULL);
355   g_string_append_printf (f, "\n> %s:%d", GST_VALIDATE_ACTION_FILENAME (action),
356       GST_VALIDATE_ACTION_LINENO (action));
357
358   if (GST_VALIDATE_ACTION_N_REPEATS (action))
359     g_string_append_printf (f, " (repeat: %d/%d)",
360         action->repeat, GST_VALIDATE_ACTION_N_REPEATS (action));
361
362   g_string_append_printf (f, "\n%s", GST_VALIDATE_ACTION_DEBUG (action));
363   if (gst_validate_action_get_level (action)) {
364     gchar *subaction_str = gst_structure_to_string (action->structure);
365
366     g_string_append_printf (f, "\n       |-> %s", subaction_str);
367     g_free (subaction_str);
368   }
369
370   g_string_append_printf (f, "\n       >\n       > %s", format);
371
372 done:
373   va_start (var_args, format);
374   G_VA_COPY (var_copy, var_args);
375   gst_validate_report_valist (reporter, issue_id, f->str, var_args);
376   if (action) {
377     gint i, indent = gst_validate_action_get_level (action) * 2;
378     gchar *message, **lines, *color = NULL;
379     const gchar *endcolor = "";
380
381     if (g_log_writer_supports_color (fileno (stderr))) {
382       color = gst_debug_construct_term_color (GST_DEBUG_FG_RED);
383       endcolor = "\033[0m";
384     }
385     gst_validate_printf (NULL, "%*s%s> Error%s:\n", indent, "",
386         color ? color : "", endcolor);
387
388     message = gst_info_strdup_vprintf (f->str, var_copy);
389     lines = g_strsplit (message, "\n", -1);
390     for (i = 1; lines[i]; i++)
391       gst_validate_printf (NULL, "%*s%s>%s %s\n", indent, "", color, endcolor,
392           lines[i]);
393     g_strfreev (lines);
394     g_free (message);
395     g_free (color);
396   }
397   va_end (var_args);
398   va_end (var_copy);
399
400   g_string_free (f, TRUE);
401 }
402
403 void
404 gst_validate_reporter_report_simple (GstValidateReporter * reporter,
405     GstValidateIssueId issue_id, const gchar * message)
406 {
407   gst_validate_report (reporter, issue_id, "%s", message);
408 }
409
410 /**
411  * gst_validate_reporter_set_name:
412  * @reporter: The reporter to set the name on
413  * @name: (transfer full): The name of the reporter
414  *
415  * Sets @ name on @reporter
416  */
417 void
418 gst_validate_reporter_set_name (GstValidateReporter * reporter, gchar * name)
419 {
420   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
421
422   g_free (priv->name);
423
424   priv->name = name;
425 }
426
427 const gchar *
428 gst_validate_reporter_get_name (GstValidateReporter * reporter)
429 {
430   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
431
432   return priv->name;
433 }
434
435 /**
436  * gst_validate_reporter_get_runner:
437  * @reporter: The reporter to get the runner from
438  *
439  * Returns: (transfer full): The runner
440  */
441 GstValidateRunner *
442 gst_validate_reporter_get_runner (GstValidateReporter * reporter)
443 {
444   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
445
446   return g_weak_ref_get (&priv->runner);
447 }
448
449 void
450 gst_validate_reporter_set_runner (GstValidateReporter * reporter,
451     GstValidateRunner * runner)
452 {
453   GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
454
455   g_weak_ref_set (&priv->runner, runner);
456
457   g_object_notify (G_OBJECT (reporter), "validate-runner");
458 }
459
460 /**
461  * gst_validate_reporter_set_handle_g_logs:
462  * @reporter: The #GstValidateReporter to set has the handler for g_log
463  *
464  * Set @reporter has the 'source' of any g_log happening during the
465  * execution. Usually the monitor of the first #GstPipeline is used
466  * to handle g_logs.
467  *
468  * Basically this function is used in order to start tracking any
469  * issue reported with g_log in the process into GstValidate report
470  * in the GstValidate reporting system.
471  */
472 void
473 gst_validate_reporter_set_handle_g_logs (GstValidateReporter * reporter)
474 {
475   g_log_set_default_handler ((GLogFunc) gst_validate_reporter_g_log_func,
476       reporter);
477
478   g_log_set_handler ("GStreamer",
479       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
480
481   g_log_set_handler ("GLib",
482       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
483
484
485   g_log_set_handler ("GLib-GObject",
486       G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
487
488   g_log_handler = gst_validate_reporter_get_priv (reporter);
489   g_object_weak_ref (G_OBJECT (reporter), gst_validate_reporter_destroyed,
490       NULL);
491
492 }
493
494 /**
495  * gst_validate_reporter_get_reports:
496  * @reporter: a #GstValidateReporter
497  *
498  * Get the list of reports present in the reporter.
499  *
500  * Returns: (transfer full) (element-type GstValidateReport): the list of
501  * #GstValidateReport present in the reporter.
502  * The caller should unref each report once it is done with them.
503  */
504 GList *
505 gst_validate_reporter_get_reports (GstValidateReporter * reporter)
506 {
507   GstValidateReporterPrivate *priv;
508   GList *reports, *tmp;
509   GList *ret = NULL;
510
511   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
512
513   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
514   reports = g_hash_table_get_values (priv->reports);
515   for (tmp = reports; tmp; tmp = tmp->next) {
516     ret =
517         g_list_append (ret,
518         gst_validate_report_ref ((GstValidateReport *) (tmp->data)));
519   }
520   g_list_free (reports);
521   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
522
523   return ret;
524 }
525
526 /**
527  * gst_validate_reporter_get_reports_count:
528  * @reporter: a #GstValidateReporter
529  *
530  * Get the number of reports present in the reporter.
531  *
532  * Returns: the number of reports currently present in @reporter.
533  */
534 gint
535 gst_validate_reporter_get_reports_count (GstValidateReporter * reporter)
536 {
537   GstValidateReporterPrivate *priv;
538   gint ret;
539
540   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
541
542   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
543   ret = g_hash_table_size (priv->reports);
544   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
545
546   return ret;
547 }
548
549 /**
550  * gst_validate_reporter_purge_reports:
551  * @reporter: a #GstValidateReporter
552  *
553  * Remove all the #GstValidateReport from @reporter. This should be called
554  * before unreffing the reporter to break cyclic references.
555  */
556 void
557 gst_validate_reporter_purge_reports (GstValidateReporter * reporter)
558 {
559   GstValidateReporterPrivate *priv;
560
561   priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
562
563   GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
564   g_hash_table_remove_all (priv->reports);
565   GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
566 }