validate-runner: report-level initial work.
[platform/upstream/gstreamer.git] / validate / gst / validate / gst-validate-runner.c
1 /* GStreamer
2  *
3  * Copyright (C) 2013 Collabora Ltd.
4  *  Author: Thiago Sousa Santos <thiago.sousa.santos@collabora.com>
5  *
6  * gst-validate-runner.c - Validate Runner class
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "gst-validate-internal.h"
29 #include "gst-validate-report.h"
30 #include "gst-validate-monitor-factory.h"
31 #include "gst-validate-override-registry.h"
32 #include "gst-validate-runner.h"
33
34 /**
35  * SECTION:gst-validate-runner
36  * @short_description: Class that runs Gst Validate tests for a pipeline
37  *
38  * Allows you to test a pipeline within GstValidate. It is the object where
39  * all issue reporting is done.
40  *
41  * In the tools using GstValidate the only minimal code to be able to monitor
42  * your pipelines is:
43  *
44  * |[
45  *  GstPipeline *pipeline = gst_pipeline_new ("monitored-pipeline");
46  *  GstValidateRunner *runner = gst_validate_runner_new ();
47  *  GstValidateMonitor *monitor = gst_validate_monitor_factory_create (
48  *          GST_OBJECT (pipeline), runner, NULL);
49  *
50  *  // Run the pipeline and do whatever you want with it
51  *
52  *  // In that same order
53  *  gst_object_unref (pipeline);
54  *  gst_object_unref (runner);
55  *  gst_object_unref (monitor);
56  * ]|
57  */
58
59 struct _GstValidateRunnerPrivate
60 {
61   GMutex mutex;
62   GList *reports;
63   GstValidateReportingLevel default_level;
64 };
65
66 #define GST_VALIDATE_RUNNER_LOCK(r)                     \
67   G_STMT_START {                                        \
68   GST_LOG_OBJECT (r, "About to lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
69   (g_mutex_lock (&GST_VALIDATE_RUNNER_CAST(r)->priv->mutex));           \
70   GST_LOG_OBJECT (r, "Acquired lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
71   } G_STMT_END
72
73 #define GST_VALIDATE_RUNNER_UNLOCK(r)                   \
74   G_STMT_START {                                        \
75   GST_LOG_OBJECT (r, "About to unlock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
76   (g_mutex_unlock (&GST_VALIDATE_RUNNER_CAST(r)->priv->mutex));         \
77   GST_LOG_OBJECT (r, "Released lock %p", &GST_VALIDATE_RUNNER_CAST(r)->priv->mutex); \
78   } G_STMT_END
79
80 #define gst_validate_runner_parent_class parent_class
81 G_DEFINE_TYPE (GstValidateRunner, gst_validate_runner, G_TYPE_OBJECT);
82
83 /* signals */
84 enum
85 {
86   REPORT_ADDED_SIGNAL,
87   /* add more above */
88   LAST_SIGNAL
89 };
90
91 static guint _signals[LAST_SIGNAL] = { 0 };
92
93 static void
94 _set_report_levels_from_string (GstValidateRunner * self, const gchar * list)
95 {
96   GST_DEBUG_OBJECT (self, "setting report levels from string [%s]", list);
97 }
98
99 static void
100 _init_report_levels (GstValidateRunner * self)
101 {
102   const gchar *env;
103
104   env = g_getenv ("GST_VALIDATE_REPORT_LEVEL");
105   if (env)
106     _set_report_levels_from_string (self, env);
107 }
108
109 static void
110 gst_validate_runner_dispose (GObject * object)
111 {
112   GstValidateRunner *runner = GST_VALIDATE_RUNNER_CAST (object);
113
114   g_list_free_full (runner->priv->reports,
115       (GDestroyNotify) gst_validate_report_unref);
116
117   g_mutex_clear (&runner->priv->mutex);
118
119   G_OBJECT_CLASS (parent_class)->dispose (object);
120 }
121
122 static void
123 gst_validate_runner_class_init (GstValidateRunnerClass * klass)
124 {
125   GObjectClass *gobject_class;
126
127   gobject_class = G_OBJECT_CLASS (klass);
128
129   gobject_class->dispose = gst_validate_runner_dispose;
130
131   g_type_class_add_private (klass, sizeof (GstValidateRunnerPrivate));
132
133   _signals[REPORT_ADDED_SIGNAL] =
134       g_signal_new ("report-added", G_TYPE_FROM_CLASS (klass),
135       G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1,
136       GST_TYPE_VALIDATE_REPORT);
137 }
138
139 static void
140 gst_validate_runner_init (GstValidateRunner * runner)
141 {
142   runner->priv = G_TYPE_INSTANCE_GET_PRIVATE (runner, GST_TYPE_VALIDATE_RUNNER,
143       GstValidateRunnerPrivate);
144   g_mutex_init (&runner->priv->mutex);
145
146   runner->priv->default_level = GST_VALIDATE_REPORTING_LEVEL_DEFAULT;
147   _init_report_levels (runner);
148 }
149
150 /**
151  * gst_validate_runner_new:
152  *
153  * Create a new #GstValidateRunner
154  *
155  * Returns: A newly created #GstValidateRunner
156  */
157 GstValidateRunner *
158 gst_validate_runner_new (void)
159 {
160   return g_object_new (GST_TYPE_VALIDATE_RUNNER, NULL);
161 }
162
163 GstValidateReportingLevel
164 gst_validate_runner_get_default_reporting_level (GstValidateRunner * runner)
165 {
166   return runner->priv->default_level;
167 }
168
169 void
170 gst_validate_runner_add_report (GstValidateRunner * runner,
171     GstValidateReport * report)
172 {
173   GST_VALIDATE_RUNNER_LOCK (runner);
174   runner->priv->reports = g_list_append (runner->priv->reports, report);
175   GST_VALIDATE_RUNNER_UNLOCK (runner);
176
177   g_signal_emit (runner, _signals[REPORT_ADDED_SIGNAL], 0, report);
178 }
179
180 /**
181  * gst_validate_runner_get_reports_count:
182  * @runner: The $GstValidateRunner to get the number of report from
183  *
184  * Get the number of reports present in the runner:
185  *
186  * Returns: The number of report present in the runner.
187  */
188 guint
189 gst_validate_runner_get_reports_count (GstValidateRunner * runner)
190 {
191   guint l;
192
193   g_return_val_if_fail (runner != NULL, 0);
194
195   GST_VALIDATE_RUNNER_LOCK (runner);
196   l = g_list_length (runner->priv->reports);
197   GST_VALIDATE_RUNNER_UNLOCK (runner);
198
199   return l;
200 }
201
202 GList *
203 gst_validate_runner_get_reports (GstValidateRunner * runner)
204 {
205   GList *ret;
206
207   GST_VALIDATE_RUNNER_LOCK (runner);
208   ret =
209       g_list_copy_deep (runner->priv->reports,
210       (GCopyFunc) gst_validate_report_ref, NULL);
211   GST_VALIDATE_RUNNER_UNLOCK (runner);
212
213   return ret;
214 }
215
216 /**
217  * gst_validate_runner_printf:
218  * @runner: The #GstValidateRunner to print all the reports for
219  *
220  * Prints all the report on the terminal or on wherever set
221  * in the #GST_VALIDATE_FILE env variable.
222  *
223  * Returns: 0 if no critical error has been found and 18 if a critical
224  * error has been detected. That return value is usually to be used as
225  * exit code of the application.
226  * */
227 int
228 gst_validate_runner_printf (GstValidateRunner * runner)
229 {
230   GList *reports, *tmp;
231   guint count = 0;
232   int ret = 0;
233   GList *criticals = NULL;
234
235   reports = gst_validate_runner_get_reports (runner);
236   for (tmp = reports; tmp; tmp = tmp->next) {
237     GstValidateReport *report = tmp->data;
238
239     if (gst_validate_report_should_print (report))
240       gst_validate_report_printf (report);
241
242     if (ret == 0 && report->level == GST_VALIDATE_REPORT_LEVEL_CRITICAL) {
243       criticals = g_list_append (criticals, tmp->data);
244       ret = 18;
245     }
246     count++;
247   }
248
249   if (criticals) {
250     GList *iter;
251
252     g_printerr ("\n\n==== Got criticals, Return value set to 18 ====\n");
253
254     for (iter = criticals; iter; iter = iter->next) {
255       g_printerr ("     Critical error %s\n",
256           ((GstValidateReport *) (iter->data))->message);
257     }
258     g_printerr ("\n");
259   }
260
261   g_list_free_full (reports, (GDestroyNotify) gst_validate_report_unref);
262   gst_validate_printf (NULL, "Issues found: %u\n", count);
263   g_list_free (criticals);
264   return ret;
265 }