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>
6 * gstinfo.c: debugging functions
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.
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.
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.
24 #include "gst_private.h"
28 #ifndef GST_DISABLE_GST_DEBUG
33 #ifdef HAVE_PRINTF_EXTENSION
37 #include <string.h> /* G_VA_COPY */
40 #include "gst_private.h"
41 #include "gstelement.h"
43 #include "gstscheduler.h"
44 #include "gst_private.h"
46 GST_DEBUG_CATEGORY_STATIC(GST_CAT_DEBUG);
50 #include <rld_interface.h>
51 typedef struct DL_INFO {
52 const char * dli_fname;
54 const char * dli_sname;
60 #define _RLD_DLADDR 14
61 int dladdr(void *address, Dl_info *dl);
63 int dladdr(void *address, Dl_info *dl)
66 v = _rld_new_interface(_RLD_DLADDR,address,dl);
72 extern gchar *_gst_progname;
74 static void gst_debug_reset_threshold (gpointer category,
76 static void gst_debug_reset_all_thresholds (void);
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,
85 struct _GstDebugMessage {
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;
99 /* list of all categories */
100 static GStaticMutex __cat_mutex = G_STATIC_MUTEX_INIT;
101 static GSList *__categories = NULL;
103 /* all registered debug handlers */
108 static GStaticMutex __log_func_mutex = G_STATIC_MUTEX_INIT;
109 static GSList *__log_functions = NULL;
111 static GstAtomicInt __default_level;
112 static GstAtomicInt __use_color;
113 gboolean __gst_debug_enabled = TRUE;
116 GstDebugCategory *GST_CAT_DEFAULT = NULL;
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;
151 * Initializes the debugging system.
152 * Normally you don't want to call this, because gst_init does it for you.
154 void _gst_debug_init (void)
156 gst_atomic_int_init (&__default_level, GST_LEVEL_DEFAULT);
157 gst_atomic_int_init (&__use_color, 1);
159 #ifdef HAVE_PRINTF_EXTENSION
160 register_printf_function (GST_PTR_FORMAT[0], _gst_info_printf_extension,
161 _gst_info_printf_extension_arginfo);
164 /* do NOT use a single debug function before this line has been run */
165 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
168 GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
169 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
170 "debugging subsystem");
172 gst_debug_add_log_function (gst_debug_log_default, NULL);
174 /* FIXME: add descriptions here */
175 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
176 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
178 GST_CAT_COTHREADS = _gst_debug_category_new ("GST_COTHREADS",
179 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
181 GST_CAT_COTHREAD_SWITCH = _gst_debug_category_new ("GST_COTHREAD_SWITCH",
182 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_GREEN,
184 GST_CAT_AUTOPLUG = _gst_debug_category_new ("GST_AUTOPLUG",
185 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
187 GST_CAT_AUTOPLUG_ATTEMPT = _gst_debug_category_new ("GST_AUTOPLUG_ATTEMPT",
188 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN | GST_DEBUG_BG_BLUE,
190 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
191 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
193 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
194 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
196 GST_CAT_PLANNING = _gst_debug_category_new ("GST_PLANNING",
197 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
199 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
200 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA,
202 GST_CAT_DATAFLOW = _gst_debug_category_new ("GST_DATAFLOW",
203 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
205 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
206 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN,
208 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
209 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
211 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
212 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW,
214 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
215 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
217 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
218 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
220 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
221 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
223 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
224 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
226 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
227 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN,
229 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
230 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE,
232 GST_CAT_THREAD = _gst_debug_category_new ("GST_THREAD",
233 GST_DEBUG_BOLD | GST_DEBUG_FG_RED,
235 GST_CAT_TYPES = _gst_debug_category_new ("GST_TYPES",
236 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
238 GST_CAT_XML = _gst_debug_category_new ("GST_XML",
239 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
241 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
242 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE,
244 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
245 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE | GST_DEBUG_BG_GREEN,
247 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
248 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE,
251 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
252 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED,
254 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
255 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW,
257 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
260 GST_CAT_SEEK = _gst_debug_category_new ("GST_SEEK",
262 "plugins reacting to seek events");
265 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
266 #define GST_CAT_DEFAULT GST_CAT_DEBUG
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
279 * Logs the given message using the currently registered debugging handlers.
281 void gst_debug_log (GstDebugCategory *category, GstDebugLevel level,
282 const gchar *file, const gchar *function, gint line,
283 GObject *object, const gchar *format, ...)
287 va_start (var_args, format);
288 gst_debug_log_valist (category, level, file, function, line, object, format, var_args);
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
302 * Logs the given message using the currently registered debugging handlers.
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)
308 GstDebugMessage message;
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);
317 message.message = NULL;
318 message.format = format;
319 G_VA_COPY (message.arguments, args);
321 handler = __log_functions;
323 entry = handler->data;
324 handler = g_slist_next (handler);
325 entry->func (category, level, file, function, line, object, &message, entry->user_data);
327 g_free (message.message);
328 va_end (message.arguments);
331 * gst_debug_message_get:
332 * @message: a debug message
334 * Gets the string representation of a GstDebugMessage. This function is used
335 * in debug handlers to extract the message.
338 gst_debug_message_get (GstDebugMessage *message)
340 if (message->message == NULL) {
341 message->message = g_strdup_vprintf (message->format, message->arguments);
343 return message->message;
348 gst_debug_print_object (gpointer ptr)
350 GObject *object = (GObject *)ptr;
353 /* This is a cute trick to detect unmapped memory, but is unportable,
354 * slow, screws around with madvise, and not actually that useful. */
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);
365 /* nicely printed object */
366 if (object == NULL) {
367 return g_strdup ("NULL");
369 if (*(GType *)ptr == GST_TYPE_CAPS) {
370 return gst_caps_to_string ((GstCaps *)ptr);
372 if (*(GType *)ptr == GST_TYPE_STRUCTURE) {
373 return gst_structure_to_string ((GstStructure *)ptr);
376 if (*(int *)ptr == 0xffffffff) {
377 return g_strdup_printf ("<poisoned@%p>", ptr);
380 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
381 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
383 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
384 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
386 if (G_IS_OBJECT (object)) {
387 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME(object), object);
390 return g_strdup_printf ("%p", ptr);
394 * gst_debug_construct_term_color:
395 * @colorinfo: the color info
397 * Constructs a string that can be used for getting the desired color in color
399 * You need to free the string after use.
401 * Returns: a string containing the color definition
404 gst_debug_construct_term_color (guint colorinfo)
409 color = g_string_new ("\033[00");
411 if (colorinfo & GST_DEBUG_BOLD) {
412 g_string_append (color, ";01");
414 if (colorinfo & GST_DEBUG_UNDERLINE) {
415 g_string_append (color, ";04");
417 if (colorinfo & GST_DEBUG_FG_MASK) {
418 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
420 if (colorinfo & GST_DEBUG_BG_MASK) {
421 g_string_append_printf (color, ";4%1d", (colorinfo & GST_DEBUG_BG_MASK) >> 4);
423 g_string_append (color, "m");
426 g_string_free (color, FALSE);
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.
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);
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)
458 if (level > gst_debug_category_get_threshold (category))
464 if (gst_debug_is_colored ()) {
465 color = gst_debug_construct_term_color (gst_debug_category_get_color (category));
467 pidcolor = g_strdup_printf ("\033[3%1dm", pid % 6 + 31);
469 color = g_strdup ("");
471 pidcolor = g_strdup ("");
474 obj = gst_debug_print_object (object);
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));
488 * gst_debug_level_get_name:
489 * @level: the level to get the name for
491 * Get the string trepresentation of a debugging level
496 gst_debug_level_get_name (GstDebugLevel 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 ";
506 g_warning ("invalid level specified for gst_debug_level_get_name");
511 * gst_debug_add_log_function:
512 * @func: the function to use
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.
519 gst_debug_add_log_function (GstLogFunction func, gpointer data)
524 g_return_if_fail (func != NULL);
526 entry = g_new (LogFuncEntry, 1);
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.
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);
540 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
544 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
546 gpointer entryfunc = ((LogFuncEntry *) entry)->func;
548 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
551 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
553 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
555 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
558 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
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);
570 g_free (found->data);
571 new = g_slist_delete_link (new, found);
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);
581 * gst_debug_remove_log_function:
582 * @func: the log function to remove
584 * Removes all registrered instances of the given logging functions.
586 * Returns: How many instances of the function were removed
589 gst_debug_remove_log_function (GstLogFunction func)
593 g_return_val_if_fail (func != NULL, 0);
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",
602 * gst_debug_remove_log_function_by_data:
603 * @data: user data of the log function to remove
605 * Removes all registrered instances of log functions with the given user data.
607 * Returns: How many instances of the function were removed
610 gst_debug_remove_log_function_by_data (gpointer data)
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",
621 * gst_debug_set_colored:
622 * @colored: Whether to use colored output or not
624 * Sets or unsets the use of coloured debugging output.
627 gst_debug_set_colored (gboolean colored)
629 gst_atomic_int_set (&__use_color, colored ? 1 : 0);
632 * gst_debug_is_colored:
634 * Checks if the debugging output should be colored.
636 * Returns: TRUE, if the debug output should be colored.
639 gst_debug_is_colored (void)
641 return gst_atomic_int_read (&__use_color) == 0 ? FALSE : TRUE;
644 * gst_debug_set_active:
645 * @active: Whether to use debugging output or not
647 * If activated, debugging messages are sent to the debugging
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>
654 gst_debug_set_active (gboolean active)
656 __gst_debug_enabled = active;
659 * gst_debug_is_active:
661 * Checks if debugging output is activated.
663 * Returns: TRUE, if debugging is activated
666 gst_debug_is_active (void)
668 return __gst_debug_enabled;
671 * gst_debug_set_default_threshold:
672 * @level: level to set
674 * Sets the default threshold to the given level and updates all categories to
675 * use this threshold.
678 gst_debug_set_default_threshold (GstDebugLevel level)
680 gst_atomic_int_set (&__default_level, level);
681 gst_debug_reset_all_thresholds ();
684 * gst_debug_get_default_threshold:
686 * Returns the default threshold that is used for new categories.
688 * Returns: the default threshold level
691 gst_debug_get_default_threshold (void)
693 return (GstDebugLevel) gst_atomic_int_read (&__default_level);
696 gst_debug_reset_threshold (gpointer category, gpointer unused)
698 GstDebugCategory *cat = (GstDebugCategory *) category;
701 g_static_mutex_lock (&__level_name_mutex);
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);
713 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
716 g_static_mutex_unlock (&__level_name_mutex);
719 gst_debug_reset_all_thresholds (void)
721 g_static_mutex_lock (&__cat_mutex);
722 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
723 g_static_mutex_unlock (&__cat_mutex);
726 for_each_threshold_by_entry (gpointer data, gpointer user_data)
728 GstDebugCategory *cat = (GstDebugCategory *) data;
729 LevelNameEntry *entry = (LevelNameEntry *) user_data;
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);
738 * gst_debug_set_threshold_for_name:
739 * @name: name of the categories to set
740 * @level: level to set them to
742 * Sets all categories which match the gven glob style pattern to the given
746 gst_debug_set_threshold_for_name (const gchar *name, GstDebugLevel level)
749 LevelNameEntry *entry;
751 g_return_if_fail (name != NULL);
753 pat = g_pattern_spec_new (name);
754 entry = g_new (LevelNameEntry, 1);
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);
765 * gst_debug_unset_threshold_for_name:
766 * @name: name of the categories to set
768 * Resets all categories with the given name back to the default level.
771 gst_debug_unset_threshold_for_name (const gchar *name)
776 g_return_if_fail (name != NULL);
778 pat = g_pattern_spec_new (name);
779 g_static_mutex_lock (&__level_name_mutex);
781 /* improve this if you want, it's mighty slow */
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);
788 g_slist_free_1 (walk);
792 g_static_mutex_unlock (&__level_name_mutex);
793 g_pattern_spec_free (pat);
794 gst_debug_reset_all_thresholds ();
797 _gst_debug_category_new (gchar *name, guint color, gchar *description)
799 GstDebugCategory *cat;
801 g_return_val_if_fail (name != NULL, NULL);
803 cat = g_new (GstDebugCategory, 1);
804 cat->name = g_strdup (name);
806 if (description != NULL) {
807 cat->description = g_strdup (description);
809 cat->description = g_strdup ("no description");
811 cat->threshold = g_new (GstAtomicInt, 1);
812 gst_atomic_int_init (cat->threshold, 0);
813 gst_debug_reset_threshold (cat, NULL);
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);
823 * gst_debug_category_free:
824 * @category: #GstDebugCategory to free.
826 * Removes and frees the category and all associated resources.
829 gst_debug_category_free (GstDebugCategory *category)
831 if (category == NULL) return;
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);
838 g_free ((gpointer) category->name);
839 g_free ((gpointer) category->description);
840 gst_atomic_int_destroy (category->threshold);
841 g_free (category->threshold);
846 * gst_debug_category_set_threshold:
847 * @category: a #GstDebugCategory to set threshold of.
848 * @level: the #GstDebugLevel threshold to set.
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
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).
860 gst_debug_category_set_threshold (GstDebugCategory *category, GstDebugLevel level)
862 g_return_if_fail (category != NULL);
864 gst_atomic_int_set (category->threshold, level);
868 * gst_debug_category_reset_threshold:
869 * @category: a #GstDebugCategory to reset threshold of.
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
874 * Use this function to set the threshold back to where it was after using
875 * gst_debug_category_set_threshold().
878 gst_debug_category_reset_threshold (GstDebugCategory *category)
880 gst_debug_reset_threshold (category, NULL);
884 * gst_debug_category_get_threshold:
885 * @category: a #GstDebugCategory to get threshold of.
887 * Returns the threshold of a #GstCategory.
889 * Returns: the #GstDebugLevel that is used as threshold.
892 gst_debug_category_get_threshold (GstDebugCategory *category)
894 return gst_atomic_int_read (category->threshold);
898 * gst_debug_category_get_name:
899 * @category: a #GstDebugCategory to get name of.
901 * Returns the name of a debug category.
903 * Returns: the name of the category.
906 gst_debug_category_get_name (GstDebugCategory *category)
908 return category->name;
912 * gst_debug_category_get_color:
913 * @category: a #GstDebugCategory to get the color of.
915 * Returns the color of a debug category used when printing output in this
918 * Returns: the color of the category.
921 gst_debug_category_get_color (GstDebugCategory *category)
923 return category->color;
927 * gst_debug_category_get_description:
928 * @category: a #GstDebugCategory to get the description of.
930 * Returns the description of a debug category.
932 * Returns: the description of the category.
935 gst_debug_category_get_description (GstDebugCategory *category)
937 return category->description;
940 * gst_debug_get_all_categories:
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>
948 * Returns: the list of categories
951 gst_debug_get_all_categories (void)
955 g_static_mutex_lock (&__cat_mutex);
956 ret = g_slist_copy (__categories);
957 g_static_mutex_unlock (&__cat_mutex);
962 /*** FUNCTION POINTERS ********************************************************/
964 GHashTable *__gst_function_pointers = NULL;
965 const gchar *_gst_debug_nameof_funcptr (void *ptr) G_GNUC_NO_INSTRUMENT;
967 /* This function MUST NOT return NULL */
969 _gst_debug_nameof_funcptr (void *ptr)
976 if (__gst_function_pointers && (ptrname = g_hash_table_lookup(__gst_function_pointers,ptr))) {
979 /* we need to create an entry in the hash table for this one so we don't leak
982 if (dladdr(ptr,&dlinfo) && dlinfo.dli_sname) {
983 gchar *name = g_strdup (dlinfo.dli_sname);
984 _gst_debug_register_funcptr (ptr, name);
989 gchar *name = g_strdup_printf ("%p", ptr);
990 _gst_debug_register_funcptr (ptr, name);
996 _gst_debug_register_funcptr (void *ptr, gchar *ptrname)
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);
1006 #ifdef HAVE_PRINTF_EXTENSION
1008 _gst_info_printf_extension (FILE *stream, const struct printf_info *info,
1009 const void *const *args)
1016 ptr = *(void **)args[0];
1018 buffer = gst_debug_print_object (ptr);
1019 len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
1027 _gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
1031 argtypes[0] = PA_POINTER;
1034 #endif /* HAVE_PRINTF_EXTENSION */
1036 #endif /* GST_DISABLE_GST_DEBUG */
1038 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
1039 /* FIXME make this thread specific */
1040 static GSList *stack_trace = NULL;
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)
1045 gchar *name = _gst_debug_nameof_funcptr (this_fn);
1046 gchar *site = _gst_debug_nameof_funcptr (call_site);
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));
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)
1058 gchar *name = _gst_debug_nameof_funcptr (this_fn);
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);
1068 gst_debug_print_stack_trace (void)
1070 GSList *walk = stack_trace;
1074 walk = g_slist_next (walk);
1077 gchar *name = (gchar *) walk->data;
1079 g_print ("#%-2d %s\n", count++, name);
1081 walk = g_slist_next (walk);
1086 gst_debug_print_stack_trace (void)
1088 /* nothing because it's compiled out */
1091 #endif /* GST_ENABLE_FUNC_INTSTRUMENTATION */