fix gst_debug_remove_with_compare_func and make testsuite/debug run again
[platform/upstream/gstreamer.git] / gst / gstinfo.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5  *
6  * gstinfo.c: debugging functions
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 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 #include "gst_private.h"
25
26 #include "gstinfo.h"
27
28 #ifndef GST_DISABLE_GST_DEBUG
29
30 #ifdef HAVE_DLFCN_H
31 #include <dlfcn.h>
32 #endif
33 #include <unistd.h>
34 #include "gstinfo.h"
35 #include "gstlog.h"
36 #include "gst_private.h"
37 #include "gstelement.h"
38 #include "gstpad.h"
39 #include "gstscheduler.h"
40 #include "gst_private.h"
41
42 GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEBUG);
43
44 #if 0
45 #if defined __sgi__
46 #include <rld_interface.h>
47 typedef struct DL_INFO {
48   const char * dli_fname;
49   void       * dli_fbase;
50   const char * dli_sname;
51   void       * dli_saddr;
52   int          dli_version;
53   int          dli_reserved1;
54   long         dli_reserved[4];
55 } Dl_info;
56 #define _RLD_DLADDR             14
57 int dladdr(void *address, Dl_info *dl);
58
59 int dladdr(void *address, Dl_info *dl)
60 {
61   void *v;
62   v = _rld_new_interface(_RLD_DLADDR,address,dl);
63   return (int)v;
64 }
65 #endif /* __sgi__ */
66 #endif
67
68 extern gchar *_gst_progname;
69
70 static void     gst_debug_reset_threshold       (gpointer category,
71                                                  gpointer unused);
72 static void     gst_debug_reset_all_thresholds  (void);
73
74 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
75 static GStaticMutex __level_name_mutex = G_STATIC_MUTEX_INIT;
76 static GSList *__level_name = NULL;
77 typedef struct {
78   GPatternSpec *        pat;
79   GstDebugLevel         level;
80 } LevelNameEntry;
81
82 /* list of all categories */
83 static GStaticMutex __cat_mutex = G_STATIC_MUTEX_INIT;
84 static GSList *__categories = NULL;
85
86 /* all registered debug handlers */
87 typedef struct {
88   GstLogFunction        func;
89   gpointer              user_data;
90 } LogFuncEntry;
91 static GStaticMutex __log_func_mutex = G_STATIC_MUTEX_INIT;
92 static GSList *__log_functions = NULL;
93
94 static GstAtomicInt __default_level;
95 static GstAtomicInt __use_color;
96 gboolean __gst_debug_enabled = TRUE;
97
98
99 GstDebugCategory *GST_CAT_DEFAULT = NULL;
100
101 GstDebugCategory *GST_CAT_GST_INIT = NULL;
102 GstDebugCategory *GST_CAT_COTHREADS = NULL;
103 GstDebugCategory *GST_CAT_COTHREAD_SWITCH = NULL;
104 GstDebugCategory *GST_CAT_AUTOPLUG = NULL;
105 GstDebugCategory *GST_CAT_AUTOPLUG_ATTEMPT = NULL;
106 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
107 GstDebugCategory *GST_CAT_STATES = NULL;
108 GstDebugCategory *GST_CAT_PLANNING = NULL;
109 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
110 GstDebugCategory *GST_CAT_DATAFLOW = NULL;
111 GstDebugCategory *GST_CAT_BUFFER = NULL;
112 GstDebugCategory *GST_CAT_CAPS = NULL;
113 GstDebugCategory *GST_CAT_CLOCK = NULL;
114 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
115 GstDebugCategory *GST_CAT_ELEMENT_FACTORY = NULL;
116 GstDebugCategory *GST_CAT_PADS = NULL;
117 GstDebugCategory *GST_CAT_PIPELINE = NULL;
118 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
119 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
120 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
121 GstDebugCategory *GST_CAT_THREAD = NULL;
122 GstDebugCategory *GST_CAT_TYPES = NULL;
123 GstDebugCategory *GST_CAT_XML = NULL;
124 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
125 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
126 GstDebugCategory *GST_CAT_EVENT = NULL;
127 GstDebugCategory *GST_CAT_PARAMS = NULL;
128 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
129 GstDebugCategory *GST_CAT_SEEK = NULL;
130
131 /**
132  * _gst_debug_init:
133  * 
134  * Initializes the debugging system.
135  * Normally you don't want to call this, because gst_init does it for you.
136  */
137 void _gst_debug_init (void)
138 {
139   gst_atomic_int_init (&__default_level, GST_LEVEL_DEFAULT);
140   gst_atomic_int_init (&__use_color, 1);
141
142   /* do NOT use a single debug function before this line has been run */
143   GST_CAT_DEFAULT       = _gst_debug_category_new ("default", 
144                                 GST_DEBUG_UNDERLINE,
145                                 NULL);
146   GST_CAT_DEBUG         = _gst_debug_category_new ("GST_DEBUG",
147                                 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
148                                 "debugging subsystem");
149
150   gst_debug_add_log_function (gst_debug_log_default, NULL);
151
152   /* FIXME: add descriptions here */
153   GST_CAT_GST_INIT      = _gst_debug_category_new ("GST_INIT",
154                                 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
155                                 NULL);
156   GST_CAT_COTHREADS     = _gst_debug_category_new ("GST_COTHREADS",
157                                 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
158                                 NULL);
159   GST_CAT_COTHREAD_SWITCH = _gst_debug_category_new ("GST_COTHREAD_SWITCH",
160                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_GREEN,
161                                 NULL);
162   GST_CAT_AUTOPLUG      = _gst_debug_category_new ("GST_AUTOPLUG",
163                                 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
164                                 NULL);
165   GST_CAT_AUTOPLUG_ATTEMPT = _gst_debug_category_new ("GST_AUTOPLUG_ATTEMPT",
166                                 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN | GST_DEBUG_BG_BLUE,
167                                 NULL);
168   GST_CAT_PARENTAGE     = _gst_debug_category_new ("GST_PARENTAGE",
169                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
170                                 NULL);
171   GST_CAT_STATES        = _gst_debug_category_new ("GST_STATES",
172                                 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
173                                 NULL);
174   GST_CAT_PLANNING      = _gst_debug_category_new ("GST_PLANNING",
175                                 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
176                                 NULL);
177   GST_CAT_SCHEDULING    = _gst_debug_category_new ("GST_SCHEDULING",
178                                 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
179                                 NULL);
180   GST_CAT_DATAFLOW      = _gst_debug_category_new ("GST_DATAFLOW",
181                                 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
182                                 NULL);
183   GST_CAT_BUFFER        = _gst_debug_category_new ("GST_BUFFER",
184                                 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
185                                 NULL);
186   GST_CAT_CAPS          = _gst_debug_category_new ("GST_CAPS",
187                                 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
188                                 NULL);
189   GST_CAT_CLOCK         = _gst_debug_category_new ("GST_CLOCK",
190                                 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
191                                 NULL);
192   GST_CAT_ELEMENT_PADS  = _gst_debug_category_new ("GST_ELEMENT_PADS",
193                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
194                                 NULL);
195   GST_CAT_ELEMENT_FACTORY = _gst_debug_category_new ("GST_ELEMENT_FACTORY",
196                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
197                                 NULL);
198   GST_CAT_PADS          = _gst_debug_category_new ("GST_PADS",
199                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
200                                 NULL);
201   GST_CAT_PIPELINE      = _gst_debug_category_new ("GST_PIPELINE",
202                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
203                                 NULL);
204   GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
205                                 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
206                                 NULL);
207   GST_CAT_PLUGIN_INFO   = _gst_debug_category_new ("GST_PLUGIN_INFO",
208                                 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
209                                 NULL);
210   GST_CAT_PROPERTIES    = _gst_debug_category_new ("GST_PROPERTIES",
211                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE,
212                                 NULL);
213   GST_CAT_THREAD        = _gst_debug_category_new ("GST_THREAD",
214                                 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
215                                 NULL);
216   GST_CAT_TYPES         = _gst_debug_category_new ("GST_TYPES",
217                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
218                                 NULL);
219   GST_CAT_XML           = _gst_debug_category_new ("GST_XML",
220                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
221                                 NULL);
222   GST_CAT_NEGOTIATION   = _gst_debug_category_new ("GST_NEGOTIATION",
223                                 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
224                                 NULL);
225   GST_CAT_REFCOUNTING   = _gst_debug_category_new ("GST_REFCOUNTING",
226                                 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE | GST_DEBUG_BG_GREEN,
227                                 NULL);
228   GST_CAT_EVENT         = _gst_debug_category_new ("GST_EVENT",
229                                 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
230                                 NULL);
231   GST_CAT_PARAMS        = _gst_debug_category_new ("GST_PARAMS",
232                                 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW,
233                                 NULL);
234   GST_CAT_CALL_TRACE    = _gst_debug_category_new ("GST_CALL_TRACE",
235                                 GST_DEBUG_BOLD,
236                                 NULL);
237   GST_CAT_SEEK          = _gst_debug_category_new ("GST_SEEK",
238                                 0,
239                                 "plugins reacting to seek events");
240 }
241
242 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
243 #define GST_CAT_DEFAULT GST_CAT_DEBUG
244
245 /**
246  * gst_debug_log:
247  * @category: category to log
248  * @level: level of the message is in
249  * @file: the file that emitted the message, usually the __FILE__ identifier
250  * @function: the function that emitted the message
251  * @line: the line from that the message was emitted, usually __LINE__
252  * @object: the object this message relates to or NULL if none
253  * @format: a printf style format string
254  * @...: optional arguments for the format
255  * 
256  * Logs the given message using the currently registered debugging handlers.
257  */
258 void gst_debug_log (GstDebugCategory *category, GstDebugLevel level,
259                     const gchar *file, const gchar *function, gint line,
260                     GObject *object, gchar *format, ...)
261 {
262   va_list var_args;
263   
264   va_start (var_args, format);
265   gst_debug_logv (category, level, file, function, line, object, format, var_args);
266   va_end (var_args);
267 }
268 /**
269  * gst_debug_logv:
270  * @category: category to log
271  * @level: level of the message is in
272  * @file: the file that emitted the message, usually the __FILE__ identifier
273  * @function: the function that emitted the message
274  * @line: the line from that the message was emitted, usually __LINE__
275  * @object: the object this message relates to or NULL if none
276  * @format: a printf style format string
277  * @args: optional arguments for the format
278  * 
279  * Logs the given message using the currently registered debugging handlers.
280  */
281 void gst_debug_logv (GstDebugCategory *category, GstDebugLevel level,
282                      const gchar *file, const gchar *function, gint line,
283                      GObject *object, gchar *format, va_list args)
284 {
285   gchar *message;
286   LogFuncEntry *entry;
287   GSList *handler;
288
289   g_return_if_fail (category != NULL);
290   g_return_if_fail (file != NULL);
291   g_return_if_fail (function != NULL);
292   g_return_if_fail (format != NULL);
293
294   message = g_strdup_vprintf (format, args);
295   handler = __log_functions;
296   while (handler) {
297     entry = handler->data;
298     handler = g_slist_next (handler);
299     entry->func (category, level, file, function, line, object, message, entry->user_data);
300   }
301   g_free (message);
302 }
303 /**
304  * gst_debug_construct_term_color:
305  * @colorinfo: the color info
306  * 
307  * Constructs a string that can be used for getting the desired color in color
308  * terminals.
309  * You need to free the string after use.
310  * 
311  * Returns: a string containing the color definition
312  */
313 gchar *
314 gst_debug_construct_term_color (guint colorinfo)
315 {
316   GString *color;
317   gchar *ret;
318
319   color = g_string_new ("\033[00");
320
321   if (colorinfo & GST_DEBUG_BOLD) {
322     g_string_append (color, ";01");
323   }
324   if (colorinfo & GST_DEBUG_UNDERLINE) {
325     g_string_append (color, ";04");
326   }
327   if (colorinfo & GST_DEBUG_FG_MASK) {
328     g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
329   }
330   if (colorinfo & GST_DEBUG_BG_MASK) {
331     g_string_append_printf (color, ";4%1d", (colorinfo & GST_DEBUG_BG_MASK) >> 4);
332   }
333   g_string_append (color, "m");
334
335   ret = color->str;
336   g_string_free (color, FALSE);
337   return ret;
338 }
339 /**
340  * gst_debug_log_default:
341  * @category: category to log
342  * @level: level of the message
343  * @file: the file that emitted the message, usually the __FILE__ identifier
344  * @function: the function that emitted the message
345  * @line: the line from that the message was emitted, usually __LINE__
346  * @message: the actual message
347  * @object: the object this message relates to or NULL if none
348  * @unused: an unused variable, reserved for some user_data.
349  * 
350  * The default logging handler used by GStreamer. Logging functions get called
351  * whenever a macro like GST_DEBUG or similar is used. This function outputs the
352  * message and additional info using the glib error handler.
353  * You can add other handlers by using #gst_debug_add_log_function. 
354  * And you can remove this handler by calling
355  * gst_debug_remove_log_function (gst_debug_log_default);
356  */
357 void
358 gst_debug_log_default (GstDebugCategory *category, GstDebugLevel level,
359                        const gchar *file, const gchar *function, gint line,
360                        GObject *object, gchar *message, gpointer unused)
361 {
362   gchar *color;
363   gchar *clear;
364   gchar *obj;
365   gchar *pidcolor;
366   gint pid;
367   
368   if (level > gst_debug_category_get_threshold (category))
369     return;
370   
371   pid = getpid();
372
373   /* color info */
374   if (gst_debug_is_colored ()) {
375     color = gst_debug_construct_term_color (gst_debug_category_get_color (category));
376     clear = "\033[00m";
377     pidcolor = g_strdup_printf ("\033[3%1dm", pid % 6 + 31);
378   } else {
379     color = g_strdup ("");
380     clear = "";
381     pidcolor = g_strdup ("");
382   }
383   /* nicely printed object */
384   if (object == NULL) {
385     obj = g_strdup ("");
386   } else if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
387     obj = g_strdup_printf (" [%s:%s]", GST_DEBUG_PAD_NAME (object));
388   } else if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
389     obj = g_strdup_printf (" [%s]", GST_OBJECT_NAME (object));
390   } else {
391     obj = g_strdup_printf (" [%s@%p]", G_OBJECT_TYPE_NAME(object), object);  
392   }
393
394   g_printerr ("%s %s%15s%s(%s%5d%s) %s%s(%d):%s:%s%s %s\n", 
395               gst_debug_level_get_name (level),
396               color, gst_debug_category_get_name (category), clear,
397               pidcolor, pid, clear,
398               color, file, line, function, obj, clear,
399               message);
400
401   g_free (color);
402   g_free (pidcolor);
403   g_free (obj);
404 }
405 /**
406  * gst_debug_level_get_name:
407  * @level: the level to get the name for
408  * 
409  * Get the string trepresentation of a debugging level
410  * 
411  * Returns: the name
412  */
413 const gchar *
414 gst_debug_level_get_name (GstDebugLevel level)
415 {
416   switch (level) {
417     case GST_LEVEL_NONE:        return "";
418     case GST_LEVEL_ERROR:       return "ERROR";
419     case GST_LEVEL_WARNING:     return "WARN ";
420     case GST_LEVEL_INFO:        return "INFO ";
421     case GST_LEVEL_DEBUG:       return "DEBUG";
422     case GST_LEVEL_LOG:         return "LOG  ";
423     default:
424       g_warning ("invalid level specified for gst_debug_level_get_name");
425       return "";
426   }
427 }
428 /**
429  * gst_debug_add_log_function:
430  * @func: the function to use
431  * @data: user data
432  * 
433  * Adds the logging function to the list of logging functions.
434  * Be sure to use G_GNUC_NO_INSTRUMENT on that function, it is needed.
435  */
436 void
437 gst_debug_add_log_function (GstLogFunction func, gpointer data)
438 {
439   LogFuncEntry *entry;
440   GSList *list;
441
442   g_return_if_fail (func != NULL);
443
444   entry = g_new (LogFuncEntry, 1);
445   entry->func = func;
446   entry->user_data = data;
447   /* FIXME: we leak the old list here - other threads might access it right now
448    * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
449    * but that is waaay costly.
450    * It'd probably be clever to use some kind of RCU here, but I don't know 
451    * anything about that.
452    */
453   g_static_mutex_lock (&__log_func_mutex);
454   list = g_slist_copy (__log_functions);
455   __log_functions = g_slist_prepend (list, entry);
456   g_static_mutex_unlock (&__log_func_mutex);
457
458   GST_DEBUG ("prepended log function %p (user data %p) to log functions",
459              func, data);
460 }
461 static gint
462 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
463 {
464   gpointer entryfunc = ((LogFuncEntry *) entry)->func;
465   
466   return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
467 }
468 static gint
469 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
470 {
471   gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
472   
473   return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
474 }
475 static guint
476 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
477 {
478   GSList *found;
479   GSList *new;
480   guint removals = 0;
481   g_static_mutex_lock (&__log_func_mutex);
482   new = __log_functions;
483   while ((found = g_slist_find_custom (new, data, func))) {
484     if (new == __log_functions) {
485       new = g_slist_copy (new);
486       continue;
487     }
488     g_free (found->data);
489     new = g_slist_remove_link (new, found);
490     g_slist_free_1 (found);
491     removals++;
492   }
493   /* FIXME: We leak the old list here. See _add_log_function for why. */
494   __log_functions = new;
495   g_static_mutex_unlock (&__log_func_mutex);
496
497   return removals;
498 }
499 /**
500  * gst_debug_remove_log_function:
501  * @func: the log function to remove
502  * 
503  * Removes all registrered instances of the given logging functions.
504  * 
505  * Returns: How many instances of the function were removed
506  */
507 guint
508 gst_debug_remove_log_function (GstLogFunction func)
509 {
510   guint removals;
511   
512   g_return_val_if_fail (func != NULL, 0);
513
514   removals = gst_debug_remove_with_compare_func (gst_debug_compare_log_function_by_func, func);
515   GST_DEBUG ("removed log function %p %d times from log function list",
516              func, removals);
517
518   return removals;
519 }
520 /**
521  * gst_debug_remove_log_function_by_data:
522  * @data: user data of the log function to remove
523  * 
524  * Removes all registrered instances of log functions with the given user data.
525  * 
526  * Returns: How many instances of the function were removed
527  */
528 guint
529 gst_debug_remove_log_function_by_data (gpointer data)
530 {
531   guint removals;
532
533   removals = gst_debug_remove_with_compare_func (gst_debug_compare_log_function_by_data, data);
534   GST_DEBUG ("removed %d log functions with user data %p from log function list",
535              removals, data);
536
537   return removals;
538 }
539 /**
540  * gst_debug_set_colored:
541  * @colored: Whether to use colored output or not
542  * 
543  * Sets or unsets the use of coloured debugging output.
544  */
545 void
546 gst_debug_set_colored (gboolean colored)
547 {
548   gst_atomic_int_set (&__use_color, colored ? 1 : 0);
549 }
550 /**
551  * gst_debug_is_colored:
552  * 
553  * Checks if the debugging output should be colored.
554  * 
555  * Returns: TRUE, if the debug output should be colored.
556  */
557 gboolean
558 gst_debug_is_colored (void)
559 {
560   return gst_atomic_int_read (&__use_color) == 0 ? FALSE : TRUE;
561 }
562 /**
563  * gst_debug_set_active:
564  * @active: Whether to use debugging output or not
565  * 
566  * If activated, debugging messages are sent to the debugging
567  * handlers.
568  * It makes sense to deactivate it for speed issues.
569  * <note><para>This function is not threadsafe. It makes sense to only call it
570  * during initialization.</para></note>
571  */
572 void
573 gst_debug_set_active (gboolean active)
574 {
575   __gst_debug_enabled = active;
576 }
577 /**
578  * gst_debug_is_active:
579  * 
580  * Checks if debugging output is activated.
581  * 
582  * Returns: TRUE, if debugging is activated
583  */
584 gboolean
585 gst_debug_is_active (void)
586 {
587   return __gst_debug_enabled;
588 }
589 /**
590  * gst_debug_set_default_threshold:
591  * @level: level to set
592  * 
593  * Sets the default threshold to the given level and updates all categories to
594  * use this threshold.
595  */
596 void
597 gst_debug_set_default_threshold (GstDebugLevel level)
598 {
599   gst_atomic_int_set (&__default_level, level);
600   gst_debug_reset_all_thresholds ();
601 }
602 /**
603  * gst_debug_get_default_threshold:
604  * 
605  * Returns the default threshold that is used for new categories.
606  * 
607  * Returns: the default threshold level
608  */
609 GstDebugLevel
610 gst_debug_get_default_threshold (void)
611 {       
612   return (GstDebugLevel) gst_atomic_int_read (&__default_level);
613 }
614 static void
615 gst_debug_reset_threshold (gpointer category, gpointer unused)
616 {
617   GstDebugCategory *cat = (GstDebugCategory *) category;
618   GSList *walk;
619   
620   g_static_mutex_lock (&__level_name_mutex);
621   walk = __level_name;
622   while (walk) {
623     LevelNameEntry *entry = walk->data;
624     walk = g_slist_next (walk);
625     if (g_pattern_match_string (entry->pat, cat->name)) {
626       GST_LOG ("category %s matches pattern %p - gets set to level %d",
627                cat->name, entry->pat, entry->level);
628       gst_debug_category_set_threshold (cat, entry->level);
629       goto exit;
630     }
631   }
632   gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
633
634 exit:
635   g_static_mutex_unlock (&__level_name_mutex);
636 }
637 static void
638 gst_debug_reset_all_thresholds (void)
639 {
640   g_static_mutex_lock (&__cat_mutex);
641   g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
642   g_static_mutex_unlock (&__cat_mutex);
643 }
644 static void
645 for_each_threshold_by_entry (gpointer data, gpointer user_data)
646 {
647   GstDebugCategory *cat = (GstDebugCategory *) data;
648   LevelNameEntry *entry = (LevelNameEntry *) user_data;
649
650   if (g_pattern_match_string (entry->pat, cat->name)) {
651     GST_LOG ("category %s matches pattern %p - gets set to level %d",
652              cat->name, entry->pat, entry->level);
653     gst_debug_category_set_threshold (cat, entry->level);
654   }
655 }
656 /**
657  * gst_debug_set_threshold_for_name:
658  * @name: name of the categories to set
659  * @level: level to set them to
660  * 
661  * Sets all categories which match the gven glob style pattern to the given 
662  * level.
663  */
664 void
665 gst_debug_set_threshold_for_name (const gchar *name, GstDebugLevel level)
666 {
667   GPatternSpec *pat;
668   LevelNameEntry *entry;
669         
670   g_return_if_fail (name != NULL);
671
672   pat = g_pattern_spec_new (name);
673   entry = g_new (LevelNameEntry, 1);
674   entry->pat = pat;
675   entry->level = level;
676   g_static_mutex_lock (&__level_name_mutex);
677   __level_name = g_slist_prepend (__level_name, entry);
678   g_static_mutex_unlock (&__level_name_mutex);
679   g_static_mutex_lock (&__cat_mutex);
680   g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
681   g_static_mutex_unlock (&__cat_mutex);
682 }
683 /**
684  * gst_debug_unset_threshold_for_name:
685  * @name: name of the categories to set
686  * 
687  * Resets all categories with the given name back to the default level.
688  */
689 void
690 gst_debug_unset_threshold_for_name (const gchar *name)
691 {
692   GSList *walk;
693   GPatternSpec *pat;
694         
695   g_return_if_fail (name != NULL);
696
697   pat = g_pattern_spec_new (name);
698   g_static_mutex_lock (&__level_name_mutex);
699   walk = __level_name;
700   /* improve this if you want, it's mighty slow */
701   while (walk) {
702     LevelNameEntry *entry = walk->data;
703     if (g_pattern_spec_equal (entry->pat, pat)) {
704       __level_name = g_slist_remove_link (__level_name, walk);
705       g_pattern_spec_free (entry->pat);
706       g_free (entry);
707       g_slist_free_1 (walk);
708       walk = __level_name;
709     }
710   }
711   g_static_mutex_unlock (&__level_name_mutex);
712   g_pattern_spec_free (pat);
713   gst_debug_reset_all_thresholds ();
714 }
715 GstDebugCategory *
716 _gst_debug_category_new (gchar *name, guint color, gchar *description)
717 {
718   GstDebugCategory *cat;
719   
720   g_return_val_if_fail (name != NULL, NULL);
721
722   cat = g_new (GstDebugCategory, 1);
723   cat->name = g_strdup (name);
724   cat->color = color;
725   if (description != NULL) {
726     cat->description = g_strdup (description);
727   } else {
728     cat->description = g_strdup ("no description");
729   }
730   cat->threshold = g_new (GstAtomicInt, 1);
731   gst_atomic_int_init (cat->threshold, 0);
732   gst_debug_reset_threshold (cat, NULL);
733
734   /* add to category list */
735   g_static_mutex_lock (&__cat_mutex);
736   __categories = g_slist_prepend (__categories, cat);
737   g_static_mutex_unlock (&__cat_mutex);
738
739   return cat;
740 }
741 /**
742  * gst_debug_category_free:
743  * @category: category to remove
744  * 
745  * Removes and frees the category and all associated ressources.
746  */
747 void
748 gst_debug_category_free (GstDebugCategory *category)
749 {
750   if (category == NULL) return;
751
752   /* remove from category list */
753   g_static_mutex_lock (&__cat_mutex);
754   __categories = g_slist_remove (__categories, category);
755   g_static_mutex_unlock (&__cat_mutex);
756
757   g_free ((gpointer) category->name); 
758   g_free ((gpointer) category->description); 
759   gst_atomic_int_destroy (category->threshold);
760   g_free (category->threshold);
761   g_free (category);
762 }
763 /**
764  * gst_debug_category_set_threshold:
765  * @category: category to set threshold for
766  * @level: the threshold to set
767  * 
768  * Sets the threshold of the category to the given level. Debug information will
769  * only be output if the threshold is lower or equal to the level of the 
770  * debugging message.
771  * <note><para>
772  * Do not use this function in production code, because other functions may 
773  * change the threshold of categories as side effect. It is however a nice 
774  * function to use when debugging (even from gdb).
775  * </para></note>
776  */
777 void
778 gst_debug_category_set_threshold (GstDebugCategory *category, GstDebugLevel level)
779 {
780   g_return_if_fail (category != NULL);
781
782   gst_atomic_int_set (category->threshold, level);
783 }
784 /**
785  * gst_debug_category_reset_threshold:
786  * @category: category to set threshold for
787  * 
788  * Resets the threshold of the category to the default level. Debug information 
789  * will only be output if the threshold is lower or equal to the level of the 
790  * debugging message.
791  * Use this function to set the threshold back to where it was after using 
792  * gst_debug_category_set_threshold().
793  */
794 void
795 gst_debug_category_reset_threshold (GstDebugCategory *category)
796 {
797   gst_debug_reset_threshold (category, NULL);
798 }
799 /**
800  * gst_debug_category_get_threshold:
801  * @category: category to get threshold for
802  * 
803  * Returns the threshold of a #GstCategory.
804  * 
805  * Returns: the level that is used as threshold
806  */
807 GstDebugLevel
808 gst_debug_category_get_threshold (GstDebugCategory *category)
809 {
810   return gst_atomic_int_read (category->threshold);
811 }
812 /**
813  * gst_debug_category_get_name:
814  * @category: category to get name for
815  * 
816  * Returns the name of a #GstCategory.
817  * 
818  * Returns: the name of the category
819  */
820 const gchar *
821 gst_debug_category_get_name (GstDebugCategory *category)
822 {
823   return category->name;
824 }
825 /**
826  * gst_debug_category_get_color:
827  * @category: category to get color for
828  * 
829  * Returns the color of a #GstCategory to use when outputting this.
830  * 
831  * Returns: the color of the category
832  */
833 guint
834 gst_debug_category_get_color (GstDebugCategory *category)
835 {
836   return category->color;
837 }
838 /**
839  * gst_debug_category_get_description:
840  * @category: category to get description for
841  * 
842  * Returns the description of a #GstCategory
843  * 
844  * Returns: the description of the category
845  */
846 const gchar *
847 gst_debug_category_get_description (GstDebugCategory *category)
848 {
849   return category->description;
850 }
851 /**
852  * gst_debug_get_all_categories:
853  *
854  * Returns a snapshot of a all categories that are currently in use . This list
855  * may change anytime.
856  * The caller has to free the list after use.
857  * <emphasis>This function is not threadsafe, so only use it while only the 
858  * main thread is running.</emphasis>
859  * 
860  * Returns: the list of categories
861  */
862 GSList *
863 gst_debug_get_all_categories (void)
864 {
865   GSList *ret;
866
867   g_static_mutex_lock (&__cat_mutex);
868   ret = g_slist_copy (__categories);
869   g_static_mutex_unlock (&__cat_mutex);
870
871   return ret; 
872 }
873
874 /*** FUNCTION POINTERS ********************************************************/
875
876 GHashTable *__gst_function_pointers = NULL;
877 const gchar *_gst_debug_nameof_funcptr (void *ptr) G_GNUC_NO_INSTRUMENT;
878
879 /* This function MUST NOT return NULL */
880 const gchar *
881 _gst_debug_nameof_funcptr (void *ptr)
882 {
883   gchar *ptrname;
884 #ifdef HAVE_DLADDR
885   Dl_info dlinfo;
886 #endif
887
888   if (__gst_function_pointers && (ptrname = g_hash_table_lookup(__gst_function_pointers,ptr))) {
889     return ptrname;
890   }
891   /* we need to create an entry in the hash table for this one so we don't leak
892    * the name */
893 #ifdef HAVE_DLADDR
894   if (dladdr(ptr,&dlinfo) && dlinfo.dli_sname) {
895     gchar *name = g_strdup (dlinfo.dli_sname);
896     _gst_debug_register_funcptr (ptr, name);
897     return name;
898   } else
899 #endif
900   {
901     gchar *name = g_strdup_printf ("%p", ptr);
902     _gst_debug_register_funcptr (ptr, name);
903     return name;
904   }
905 }
906
907 void *
908 _gst_debug_register_funcptr (void *ptr, gchar *ptrname)
909 {
910   if (!__gst_function_pointers)
911     __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
912   if (!g_hash_table_lookup (__gst_function_pointers, ptr))
913     g_hash_table_insert (__gst_function_pointers, ptr, ptrname);
914
915   return ptr;
916 }
917
918 #endif /* GST_DISABLE_GST_DEBUG */
919
920 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
921 /* FIXME make this thread specific */
922 static GSList *stack_trace = NULL;
923
924 void __cyg_profile_func_enter(void *this_fn,void *call_site) G_GNUC_NO_INSTRUMENT;
925 void __cyg_profile_func_enter(void *this_fn,void *call_site) 
926 {
927   gchar *name = _gst_debug_nameof_funcptr (this_fn);
928   gchar *site = _gst_debug_nameof_funcptr (call_site);
929         
930   GST_CAT_DEBUG(GST_CAT_CALL_TRACE, "entering function %s from %s", name, site);
931   stack_trace = g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)", this_fn, name, call_site, site));
932
933   g_free (name);
934   g_free (site);
935 }
936
937 void __cyg_profile_func_exit(void *this_fn,void *call_site) G_GNUC_NO_INSTRUMENT;
938 void __cyg_profile_func_exit(void *this_fn,void *call_site) 
939 {
940   gchar *name = _gst_debug_nameof_funcptr (this_fn);
941
942   GST_CAT_DEBUG(GST_CAT_CALL_TRACE, "leaving function %s", name);
943   g_free (stack_trace->data);
944   stack_trace = g_slist_delete_link (stack_trace, stack_trace);
945
946   g_free (name);
947 }
948
949 void 
950 gst_debug_print_stack_trace (void)
951 {
952   GSList *walk = stack_trace;
953   gint count = 0;
954
955   if (walk)
956     walk = g_slist_next (walk);
957
958   while (walk) {
959     gchar *name = (gchar *) walk->data;
960
961     g_print ("#%-2d %s\n", count++, name);
962
963     walk = g_slist_next (walk);
964   }
965 }
966 #else
967 void 
968 gst_debug_print_stack_trace (void)
969 {
970   /* nothing because it's compiled out */
971 }
972
973 #endif /* GST_ENABLE_FUNC_INTSTRUMENTATION */