3 * Copyright (C) 2013 Thibault Saunier <thibault.saunier@collabora.com>
5 * gst-validate-reporter.c
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.
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.
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.
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
35 #include "gst-validate-internal.h"
36 #include "gst-validate-reporter.h"
37 #include "gst-validate-report.h"
39 #define REPORTER_PRIVATE "gst-validate-reporter-private"
41 typedef struct _GstValidateReporterPrivate
48 } GstValidateReporterPrivate;
50 static GstValidateReporterPrivate *g_log_handler = NULL;
52 G_DEFINE_INTERFACE (GstValidateReporter, gst_validate_reporter, G_TYPE_OBJECT);
55 gst_validate_reporter_default_init (GstValidateReporterInterface * iface)
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));
65 _free_priv (GstValidateReporterPrivate * priv)
67 if (g_log_handler == priv) {
68 g_log_set_default_handler (g_log_default_handler, NULL);
72 g_hash_table_unref (priv->reports);
74 g_mutex_clear (&priv->reports_lock);
75 g_weak_ref_clear (&priv->runner);
76 g_slice_free (GstValidateReporterPrivate, priv);
79 static GstValidateReporterPrivate *
80 gst_validate_reporter_get_priv (GstValidateReporter * reporter)
82 GstValidateReporterPrivate *priv;
84 priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
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);
91 g_mutex_init (&priv->reports_lock);
92 g_object_set_data_full (G_OBJECT (reporter), REPORTER_PRIVATE, priv,
93 (GDestroyNotify) _free_priv);
99 #define GST_VALIDATE_REPORTER_REPORTS_LOCK(r) \
101 (g_mutex_lock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock)); \
104 #define GST_VALIDATE_REPORTER_REPORTS_UNLOCK(r) \
106 (g_mutex_unlock (&gst_validate_reporter_get_priv(GST_VALIDATE_REPORTER_CAST(r))->reports_lock)); \
109 static GstValidateInterceptionReturn
110 gst_validate_reporter_intercept_report (GstValidateReporter * reporter,
111 GstValidateReport * report)
113 GstValidateInterceptionReturn ret = GST_VALIDATE_REPORTER_REPORT;
114 GstValidateReporterInterface *iface =
115 GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
117 if (iface->intercept_report) {
118 ret = iface->intercept_report (reporter, report);
124 GstValidateReportingDetails
125 gst_validate_reporter_get_reporting_level (GstValidateReporter * reporter)
127 GstValidateReportingDetails ret = GST_VALIDATE_SHOW_UNKNOWN;
128 GstValidateReporterInterface *iface =
129 GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
131 if (iface->get_reporting_level) {
132 ret = iface->get_reporting_level (reporter);
139 * gst_validate_reporter_get_pipeline:
140 * @reporter: The reporter to get the pipeline from
142 * Returns: (transfer full) (allow-none): The #GstPipeline
145 gst_validate_reporter_get_pipeline (GstValidateReporter * reporter)
147 GstValidateReporterInterface *iface =
148 GST_VALIDATE_REPORTER_GET_INTERFACE (reporter);
150 if (iface->get_pipeline)
151 return iface->get_pipeline (reporter);
157 gst_validate_reporter_get_report (GstValidateReporter * reporter,
158 GstValidateIssueId issue_id)
160 GstValidateReport *report;
161 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
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);
171 gst_validate_report_valist (GstValidateReporter * reporter,
172 GstValidateIssueId issue_id, const gchar * format, va_list var_args)
174 GstValidateReport *report, *prev_report;
175 gchar *message, *combo;
177 GstValidateIssue *issue;
178 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
179 GstValidateInterceptionReturn int_ret;
180 GstValidateRunner *runner = NULL;
182 issue = gst_validate_issue_from_id (issue_id);
184 g_return_if_fail (issue != NULL);
185 g_return_if_fail (GST_IS_VALIDATE_REPORTER (reporter));
187 G_VA_COPY (vacopy, var_args);
188 message = gst_info_strdup_vprintf (format, vacopy);
189 report = gst_validate_report_new (issue, reporter, message);
191 #ifndef GST_DISABLE_GST_DEBUG
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);
206 gst_debug_log_valist (GST_CAT_DEFAULT, GST_LEVEL_DEBUG, __FILE__,
207 GST_FUNCTION, __LINE__, NULL, combo, vacopy);
212 int_ret = gst_validate_reporter_intercept_report (reporter, report);
214 if (int_ret == GST_VALIDATE_REPORTER_DROP) {
215 gst_validate_report_unref (report);
219 prev_report = g_hash_table_lookup (priv->reports, (gconstpointer) issue_id);
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;
228 runner_level = gst_validate_runner_get_default_reporting_level (runner);
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)) {
235 gst_validate_report_add_repeated_report (prev_report, report);
238 gst_validate_report_unref (report);
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);
246 if (runner && int_ret == GST_VALIDATE_REPORTER_REPORT) {
247 gst_validate_runner_add_report (runner, report);
250 if (gst_validate_report_check_abort (report)) {
252 gst_validate_runner_printf (runner);
254 gst_validate_abort ("Fatal report received: %"
255 GST_VALIDATE_ERROR_REPORT_PRINT_FORMAT,
256 GST_VALIDATE_REPORT_PRINT_ARGS (report));
261 gst_object_unref (runner);
267 gst_validate_default_log_hanlder (const gchar * log_domain,
268 GLogLevelFlags log_level, const gchar * message, gpointer user_data)
270 gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
273 gst_validate_printf (NULL, "\nStack trace:\n%s\n", trace);
277 g_log_default_handler (log_domain, log_level, message, user_data);
281 gst_validate_reporter_destroyed (gpointer udata, GObject * freed_reporter)
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);
292 gst_validate_reporter_g_log_func (const gchar * log_domain,
293 GLogLevelFlags log_level, const gchar * message,
294 GstValidateReporter * reporter)
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);
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
312 * Reports a new issue in the GstValidate reporting system.
314 * You can also use #GST_VALIDATE_REPORT instead.
317 gst_validate_report (GstValidateReporter * reporter,
318 GstValidateIssueId issue_id, const gchar * format, ...)
322 va_start (var_args, format);
323 gst_validate_report_valist (reporter, issue_id, format, var_args);
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
336 * Reports a new issue in the GstValidate reporting system specifying @action
337 * as failling action .
339 * You can also use #GST_VALIDATE_REPORT instead.
342 gst_validate_report_action (GstValidateReporter * reporter,
343 GstValidateAction * action, GstValidateIssueId issue_id,
344 const gchar * format, ...)
346 va_list var_args, var_copy;
350 f = g_string_new (format);
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));
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));
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);
366 g_string_append_printf (f, "\n |-> %s", subaction_str);
367 g_free (subaction_str);
370 g_string_append_printf (f, "\n >\n > %s", format);
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);
377 gint i, indent = gst_validate_action_get_level (action) * 2;
378 gchar *message, **lines, *color = NULL;
379 const gchar *endcolor = "";
381 if (g_log_writer_supports_color (fileno (stderr))) {
382 color = gst_debug_construct_term_color (GST_DEBUG_FG_RED);
383 endcolor = "\033[0m";
385 gst_validate_printf (NULL, "%*s%s> Error%s:\n", indent, "",
386 color ? color : "", endcolor);
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,
400 g_string_free (f, TRUE);
404 gst_validate_reporter_report_simple (GstValidateReporter * reporter,
405 GstValidateIssueId issue_id, const gchar * message)
407 gst_validate_report (reporter, issue_id, "%s", message);
411 * gst_validate_reporter_set_name:
412 * @reporter: The reporter to set the name on
413 * @name: (transfer full): The name of the reporter
415 * Sets @ name on @reporter
418 gst_validate_reporter_set_name (GstValidateReporter * reporter, gchar * name)
420 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
428 gst_validate_reporter_get_name (GstValidateReporter * reporter)
430 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
436 * gst_validate_reporter_get_runner:
437 * @reporter: The reporter to get the runner from
439 * Returns: (transfer full): The runner
442 gst_validate_reporter_get_runner (GstValidateReporter * reporter)
444 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
446 return g_weak_ref_get (&priv->runner);
450 gst_validate_reporter_set_runner (GstValidateReporter * reporter,
451 GstValidateRunner * runner)
453 GstValidateReporterPrivate *priv = gst_validate_reporter_get_priv (reporter);
455 g_weak_ref_set (&priv->runner, runner);
457 g_object_notify (G_OBJECT (reporter), "validate-runner");
461 * gst_validate_reporter_set_handle_g_logs:
462 * @reporter: The #GstValidateReporter to set has the handler for g_log
464 * Set @reporter has the 'source' of any g_log happening during the
465 * execution. Usually the monitor of the first #GstPipeline is used
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.
473 gst_validate_reporter_set_handle_g_logs (GstValidateReporter * reporter)
475 g_log_set_default_handler ((GLogFunc) gst_validate_reporter_g_log_func,
478 g_log_set_handler ("GStreamer",
479 G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
481 g_log_set_handler ("GLib",
482 G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
485 g_log_set_handler ("GLib-GObject",
486 G_LOG_LEVEL_MASK, (GLogFunc) gst_validate_reporter_g_log_func, reporter);
488 g_log_handler = gst_validate_reporter_get_priv (reporter);
489 g_object_weak_ref (G_OBJECT (reporter), gst_validate_reporter_destroyed,
495 * gst_validate_reporter_get_reports:
496 * @reporter: a #GstValidateReporter
498 * Get the list of reports present in the reporter.
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.
505 gst_validate_reporter_get_reports (GstValidateReporter * reporter)
507 GstValidateReporterPrivate *priv;
508 GList *reports, *tmp;
511 priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
513 GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
514 reports = g_hash_table_get_values (priv->reports);
515 for (tmp = reports; tmp; tmp = tmp->next) {
518 gst_validate_report_ref ((GstValidateReport *) (tmp->data)));
520 g_list_free (reports);
521 GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
527 * gst_validate_reporter_get_reports_count:
528 * @reporter: a #GstValidateReporter
530 * Get the number of reports present in the reporter.
532 * Returns: the number of reports currently present in @reporter.
535 gst_validate_reporter_get_reports_count (GstValidateReporter * reporter)
537 GstValidateReporterPrivate *priv;
540 priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
542 GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
543 ret = g_hash_table_size (priv->reports);
544 GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);
550 * gst_validate_reporter_purge_reports:
551 * @reporter: a #GstValidateReporter
553 * Remove all the #GstValidateReport from @reporter. This should be called
554 * before unreffing the reporter to break cyclic references.
557 gst_validate_reporter_purge_reports (GstValidateReporter * reporter)
559 GstValidateReporterPrivate *priv;
561 priv = g_object_get_data (G_OBJECT (reporter), REPORTER_PRIVATE);
563 GST_VALIDATE_REPORTER_REPORTS_LOCK (reporter);
564 g_hash_table_remove_all (priv->reports);
565 GST_VALIDATE_REPORTER_REPORTS_UNLOCK (reporter);