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