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