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 * Copyright (C) 2008-2009 Tim-Philipp Müller <tim centricular net>
7 * gstinfo.c: debugging functions
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * @short_description: Debugging and logging facilities
28 * @see_also: #gstreamer-gstconfig, #gstreamer-Gst for command line parameters
29 * and environment variables that affect the debugging output.
31 * GStreamer's debugging subsystem is an easy way to get information about what
32 * the application is doing. It is not meant for programming errors. Use GLib
33 * methods (g_warning and friends) for that.
35 * The debugging subsystem works only after GStreamer has been initialized
36 * - for example by calling gst_init().
38 * The debugging subsystem is used to log informational messages while the
39 * application runs. Each messages has some properties attached to it. Among
40 * these properties are the debugging category, the severity (called "level"
41 * here) and an optional #GObject it belongs to. Each of these messages is sent
42 * to all registered debugging handlers, which then handle the messages.
43 * GStreamer attaches a default handler on startup, which outputs requested
46 * Messages are output by using shortcut macros like #GST_DEBUG,
47 * #GST_CAT_ERROR_OBJECT or similar. These all expand to calling gst_debug_log()
48 * with the right parameters.
49 * The only thing a developer will probably want to do is define his own
50 * categories. This is easily done with 3 lines. At the top of your code,
52 * the variables and set the default category.
54 * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically)
55 * #define GST_CAT_DEFAULT my_category // set as default
57 * After that you only need to initialize the category.
59 * GST_DEBUG_CATEGORY_INIT (my_category, "my category",
60 * 0, "This is my very own");
62 * Initialization must be done before the category is used first.
64 * in their plugin_init function, libraries and applications should do that
65 * during their initialization.
67 * The whole debugging subsystem can be disabled at build time with passing the
68 * --disable-gst-debug switch to configure. If this is done, every function,
69 * macro and even structs described in this file evaluate to default values or
71 * So don't take addresses of these functions or use other tricks.
72 * If you must do that for some reason, there is still an option.
74 * subsystem was compiled out, #GST_DISABLE_GST_DEBUG is defined in
76 * so you can check that before doing your trick.
77 * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
78 * speed increase and will reduce the size of your compiled code. The GStreamer
79 * library itself becomes around 10% smaller.
81 * Please note that there are naming conventions for the names of debugging
82 * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
86 #include "gst_private.h"
89 #undef gst_debug_remove_log_function
90 #undef gst_debug_add_log_function
92 #ifndef GST_DISABLE_GST_DEBUG
97 #include <stdio.h> /* fprintf */
98 #include <glib/gstdio.h>
101 # include <unistd.h> /* getpid on UNIX */
103 #ifdef HAVE_PROCESS_H
104 # include <process.h> /* getpid on win32 */
106 #include <string.h> /* G_VA_COPY */
108 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
109 # include <windows.h> /* GetStdHandle, windows console */
112 #include "gst_private.h"
113 #include "gstutils.h"
114 #include "gstquark.h"
115 #include "gstsegment.h"
116 #include "gstvalue.h"
117 #include "gstcapsfeatures.h"
119 #ifdef HAVE_VALGRIND_VALGRIND_H
120 # include <valgrind/valgrind.h>
122 #include <glib/gprintf.h> /* g_sprintf */
124 /* our own printf implementation with custom extensions to %p for caps etc. */
125 #include "printf/printf.h"
126 #include "printf/printf-extension.h"
128 static char *gst_info_printf_pointer_extension_func (const char *format,
131 #endif /* !GST_DISABLE_GST_DEBUG */
133 extern gboolean gst_is_initialized (void);
135 /* we want these symbols exported even if debug is disabled, to maintain
136 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
137 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
139 /* disabled by default, as soon as some threshold is set > NONE,
140 * it becomes enabled. */
141 gboolean _gst_debug_enabled = FALSE;
142 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
144 GstDebugCategory *GST_CAT_DEFAULT = NULL;
146 GstDebugCategory *GST_CAT_GST_INIT = NULL;
147 GstDebugCategory *GST_CAT_MEMORY = NULL;
148 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
149 GstDebugCategory *GST_CAT_STATES = NULL;
150 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
152 GstDebugCategory *GST_CAT_BUFFER = NULL;
153 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
154 GstDebugCategory *GST_CAT_BUS = NULL;
155 GstDebugCategory *GST_CAT_CAPS = NULL;
156 GstDebugCategory *GST_CAT_CLOCK = NULL;
157 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
158 GstDebugCategory *GST_CAT_PADS = NULL;
159 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
160 GstDebugCategory *GST_CAT_PIPELINE = NULL;
161 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
162 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
163 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
164 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
165 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
166 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
167 GstDebugCategory *GST_CAT_EVENT = NULL;
168 GstDebugCategory *GST_CAT_MESSAGE = NULL;
169 GstDebugCategory *GST_CAT_PARAMS = NULL;
170 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
171 GstDebugCategory *GST_CAT_SIGNAL = NULL;
172 GstDebugCategory *GST_CAT_PROBE = NULL;
173 GstDebugCategory *GST_CAT_REGISTRY = NULL;
174 GstDebugCategory *GST_CAT_QOS = NULL;
175 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
176 GstDebugCategory *GST_CAT_META = NULL;
177 GstDebugCategory *GST_CAT_LOCKING = NULL;
178 GstDebugCategory *GST_CAT_CONTEXT = NULL;
181 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
183 #ifndef GST_DISABLE_GST_DEBUG
185 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
186 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
188 /* time of initialization, so we get useful debugging output times
189 * FIXME: we use this in gstdebugutils.c, what about a function + macro to
190 * get the running time: GST_DEBUG_RUNNING_TIME
192 GstClockTime _priv_gst_info_start_time;
196 #include <rld_interface.h>
197 typedef struct DL_INFO
199 const char *dli_fname;
201 const char *dli_sname;
205 long dli_reserved[4];
209 #define _RLD_DLADDR 14
210 int dladdr (void *address, Dl_info * dl);
213 dladdr (void *address, Dl_info * dl)
217 v = _rld_new_interface (_RLD_DLADDR, address, dl);
223 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
224 static void gst_debug_reset_all_thresholds (void);
226 struct _GstDebugMessage
233 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
234 static GMutex __level_name_mutex;
235 static GSList *__level_name = NULL;
243 /* list of all categories */
244 static GMutex __cat_mutex;
245 static GSList *__categories = NULL;
247 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
250 /* all registered debug handlers */
255 GDestroyNotify notify;
258 static GMutex __log_func_mutex;
259 static GSList *__log_functions = NULL;
261 #define PRETTY_TAGS_DEFAULT TRUE
262 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
264 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
265 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
267 static FILE *log_file;
269 /* FIXME: export this? */
271 _priv_gst_in_valgrind (void)
279 in_valgrind = GST_VG_UNCHECKED;
281 if (in_valgrind == GST_VG_UNCHECKED) {
282 #ifdef HAVE_VALGRIND_VALGRIND_H
283 if (RUNNING_ON_VALGRIND) {
284 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
285 printf ("GStreamer has detected that it is running inside valgrind.\n");
286 printf ("It might now take different code paths to ease debugging.\n");
287 printf ("Of course, this may also lead to different bugs.\n");
288 in_valgrind = GST_VG_INSIDE;
290 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
291 in_valgrind = GST_VG_NO_VALGRIND;
294 in_valgrind = GST_VG_NO_VALGRIND;
296 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
297 in_valgrind == GST_VG_INSIDE);
299 return (in_valgrind == GST_VG_INSIDE);
302 /* Initialize the debugging system */
304 _priv_gst_debug_init (void)
308 env = g_getenv ("GST_DEBUG_FILE");
309 if (env != NULL && *env != '\0') {
310 if (strcmp (env, "-") == 0) {
313 log_file = g_fopen (env, "w");
314 if (log_file == NULL) {
315 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
324 /* get time we started for debugging messages */
325 _priv_gst_info_start_time = gst_util_get_timestamp ();
327 __gst_printf_pointer_extension_set_func
328 (gst_info_printf_pointer_extension_func);
330 /* do NOT use a single debug function before this line has been run */
331 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
332 GST_DEBUG_UNDERLINE, NULL);
333 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
334 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
336 gst_debug_add_log_function (gst_debug_log_default, NULL, NULL);
338 /* FIXME: add descriptions here */
339 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
340 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
341 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
342 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
343 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
344 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
345 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
346 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
347 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
348 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
349 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
350 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
351 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
352 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
353 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
354 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
355 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
356 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
357 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
358 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
359 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
360 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
361 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
362 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
363 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
364 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
365 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
366 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
367 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
368 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
369 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
370 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
371 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
372 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
373 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
374 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
375 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
376 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
377 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
379 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
380 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
381 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
382 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
383 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
384 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
385 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
386 GST_DEBUG_BOLD, NULL);
387 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
388 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
389 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
391 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
392 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
393 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
394 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
395 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
396 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
398 /* print out the valgrind message if we're in valgrind */
399 _priv_gst_in_valgrind ();
401 env = g_getenv ("GST_DEBUG_OPTIONS");
403 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
405 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
409 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
410 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
411 env = g_getenv ("GST_DEBUG_COLOR_MODE");
413 gst_debug_set_color_mode_from_string (env);
415 env = g_getenv ("GST_DEBUG");
417 gst_debug_set_threshold_from_string (env, FALSE);
421 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
422 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
426 * @category: category to log
427 * @level: level of the message is in
428 * @file: the file that emitted the message, usually the __FILE__ identifier
429 * @function: the function that emitted the message
430 * @line: the line from that the message was emitted, usually __LINE__
431 * @object: (transfer none) (allow-none): the object this message relates to,
433 * @format: a printf style format string
434 * @...: optional arguments for the format
436 * Logs the given message using the currently registered debugging handlers.
439 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
440 const gchar * file, const gchar * function, gint line,
441 GObject * object, const gchar * format, ...)
445 va_start (var_args, format);
446 gst_debug_log_valist (category, level, file, function, line, object, format,
452 /* based on g_basename(), which we can't use because it was deprecated */
453 static inline const gchar *
454 gst_path_basename (const gchar * file_name)
456 register const gchar *base;
458 base = strrchr (file_name, G_DIR_SEPARATOR);
461 const gchar *q = strrchr (file_name, '/');
462 if (base == NULL || (q != NULL && q > base))
469 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
470 return file_name + 2;
477 * gst_debug_log_valist:
478 * @category: category to log
479 * @level: level of the message is in
480 * @file: the file that emitted the message, usually the __FILE__ identifier
481 * @function: the function that emitted the message
482 * @line: the line from that the message was emitted, usually __LINE__
483 * @object: (transfer none) (allow-none): the object this message relates to,
485 * @format: a printf style format string
486 * @args: optional arguments for the format
488 * Logs the given message using the currently registered debugging handlers.
491 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
492 const gchar * file, const gchar * function, gint line,
493 GObject * object, const gchar * format, va_list args)
495 GstDebugMessage message;
499 g_return_if_fail (category != NULL);
500 g_return_if_fail (file != NULL);
501 g_return_if_fail (function != NULL);
502 g_return_if_fail (format != NULL);
504 /* The predefined macro __FILE__ is always the exact path given to the
505 * compiler with MSVC, which may or may not be the basename. We work
506 * around it at runtime to improve the readability. */
508 file = gst_path_basename (file);
511 message.message = NULL;
512 message.format = format;
513 G_VA_COPY (message.arguments, args);
515 handler = __log_functions;
517 entry = handler->data;
518 handler = g_slist_next (handler);
519 entry->func (category, level, file, function, line, object, &message,
522 g_free (message.message);
523 va_end (message.arguments);
527 * gst_debug_message_get:
528 * @message: a debug message
530 * Gets the string representation of a #GstDebugMessage. This function is used
531 * in debug handlers to extract the message.
533 * Returns: the string representation of a #GstDebugMessage.
536 gst_debug_message_get (GstDebugMessage * message)
538 if (message->message == NULL) {
541 len = __gst_vasprintf (&message->message, message->format,
545 message->message = NULL;
547 return message->message;
550 #define MAX_BUFFER_DUMP_STRING_LEN 100
552 /* structure_to_pretty_string:
553 * @str: a serialized #GstStructure
555 * If the serialized structure contains large buffers such as images the hex
556 * representation of those buffers will be shortened so that the string remains
559 * Returns: the filtered string
562 prettify_structure_string (gchar * str)
564 gchar *pos = str, *end;
566 while ((pos = strstr (pos, "(buffer)"))) {
569 pos += strlen ("(buffer)");
570 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
572 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
573 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
574 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
575 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
576 strlen (pos + count) + 1);
577 pos += MAX_BUFFER_DUMP_STRING_LEN;
584 static inline gchar *
585 gst_info_structure_to_string (const GstStructure * s)
588 gchar *str = gst_structure_to_string (s);;
589 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
590 return prettify_structure_string (str);
597 static inline gchar *
598 gst_info_describe_buffer (GstBuffer * buffer)
600 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
601 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
602 ", offset %" G_GUINT64_FORMAT ", offset_end %" G_GUINT64_FORMAT
603 ", flags 0x%x", buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
604 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
605 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
606 gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer),
607 GST_BUFFER_OFFSET_END (buffer), GST_BUFFER_FLAGS (buffer));
610 static inline gchar *
611 gst_info_describe_event (GstEvent * event)
615 s = gst_info_structure_to_string (gst_event_get_structure (event));
616 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
617 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
618 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
624 static inline gchar *
625 gst_info_describe_message (GstMessage * message)
629 s = gst_info_structure_to_string (gst_message_get_structure (message));
630 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
631 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
632 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
633 GST_MESSAGE_SEQNUM (message),
634 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
640 static inline gchar *
641 gst_info_describe_query (GstQuery * query)
645 s = gst_info_structure_to_string (gst_query_get_structure (query));
646 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
647 query, (s ? s : "(NULL)"));
653 gst_debug_print_object (gpointer ptr)
655 GObject *object = (GObject *) ptr;
658 /* This is a cute trick to detect unmapped memory, but is unportable,
659 * slow, screws around with madvise, and not actually that useful. */
663 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
664 if (ret == -1 && errno == ENOMEM) {
665 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
670 /* nicely printed object */
671 if (object == NULL) {
672 return g_strdup ("(NULL)");
674 if (GST_IS_CAPS (ptr)) {
675 return gst_caps_to_string ((const GstCaps *) ptr);
677 if (GST_IS_STRUCTURE (ptr)) {
678 return gst_info_structure_to_string ((const GstStructure *) ptr);
680 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
681 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
683 if (GST_IS_TAG_LIST (ptr)) {
684 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
685 if (G_UNLIKELY (pretty_tags))
686 return prettify_structure_string (str);
691 /* FIXME, datetime does not have a type as first field */
692 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
693 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
696 if (GST_IS_BUFFER (ptr)) {
697 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
700 if (*(guint32 *) ptr == 0xffffffff) {
701 return g_strdup_printf ("<poisoned@%p>", ptr);
704 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
705 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
707 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
708 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
710 if (G_IS_OBJECT (object)) {
711 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
713 if (GST_IS_MESSAGE (object)) {
714 return gst_info_describe_message (GST_MESSAGE_CAST (object));
716 if (GST_IS_QUERY (object)) {
717 return gst_info_describe_query (GST_QUERY_CAST (object));
719 if (GST_IS_EVENT (object)) {
720 return gst_info_describe_event (GST_EVENT_CAST (object));
722 if (GST_IS_CONTEXT (object)) {
723 GstContext *context = GST_CONTEXT_CAST (object);
726 const GstStructure *structure;
728 type = gst_context_get_context_type (context);
729 structure = gst_context_get_structure (context);
731 s = gst_info_structure_to_string (structure);
733 ret = g_strdup_printf ("context '%s'='%s'", type, s);
738 return g_strdup_printf ("%p", ptr);
742 gst_debug_print_segment (gpointer ptr)
744 GstSegment *segment = (GstSegment *) ptr;
746 /* nicely printed segment */
747 if (segment == NULL) {
748 return g_strdup ("(NULL)");
751 switch (segment->format) {
752 case GST_FORMAT_UNDEFINED:{
753 return g_strdup_printf ("UNDEFINED segment");
755 case GST_FORMAT_TIME:{
756 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
757 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
758 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
759 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
760 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
761 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
762 segment->rate, segment->applied_rate, (guint) segment->flags,
763 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
764 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
767 const gchar *format_name;
769 format_name = gst_format_get_name (segment->format);
770 if (G_UNLIKELY (format_name == NULL))
771 format_name = "(UNKNOWN FORMAT)";
772 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
773 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
774 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
775 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
776 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
777 segment->offset, segment->stop, segment->rate, segment->applied_rate,
778 (guint) segment->flags, segment->time, segment->base,
779 segment->position, segment->duration);
785 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
789 if (format[0] == 'p' && format[1] == '\a') {
791 case 'A': /* GST_PTR_FORMAT */
792 s = gst_debug_print_object (ptr);
794 case 'B': /* GST_SEGMENT_FORMAT */
795 s = gst_debug_print_segment (ptr);
798 /* must have been compiled against a newer version with an extension
799 * we don't known about yet - just ignore and fallback to %p below */
804 s = g_strdup_printf ("%p", ptr);
810 * gst_debug_construct_term_color:
811 * @colorinfo: the color info
813 * Constructs a string that can be used for getting the desired color in color
815 * You need to free the string after use.
817 * Returns: (transfer full) (type gchar*): a string containing the color
821 gst_debug_construct_term_color (guint colorinfo)
825 color = g_string_new ("\033[00");
827 if (colorinfo & GST_DEBUG_BOLD) {
828 g_string_append_len (color, ";01", 3);
830 if (colorinfo & GST_DEBUG_UNDERLINE) {
831 g_string_append_len (color, ";04", 3);
833 if (colorinfo & GST_DEBUG_FG_MASK) {
834 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
836 if (colorinfo & GST_DEBUG_BG_MASK) {
837 g_string_append_printf (color, ";4%1d",
838 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
840 g_string_append_c (color, 'm');
842 return g_string_free (color, FALSE);
846 * gst_debug_construct_win_color:
847 * @colorinfo: the color info
849 * Constructs an integer that can be used for getting the desired color in
850 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
851 * ignore this attribute.
853 * This function returns 0 on non-windows machines.
855 * Returns: an integer containing the color definition
858 gst_debug_construct_win_color (guint colorinfo)
862 static const guchar ansi_to_win_fg[8] = {
864 FOREGROUND_RED, /* red */
865 FOREGROUND_GREEN, /* green */
866 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
867 FOREGROUND_BLUE, /* blue */
868 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
869 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
870 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
872 static const guchar ansi_to_win_bg[8] = {
876 BACKGROUND_RED | BACKGROUND_GREEN,
878 BACKGROUND_RED | BACKGROUND_BLUE,
879 BACKGROUND_GREEN | FOREGROUND_BLUE,
880 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
883 /* we draw black as white, as cmd.exe can only have black bg */
884 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
885 color = ansi_to_win_fg[7];
887 if (colorinfo & GST_DEBUG_UNDERLINE) {
888 color |= BACKGROUND_INTENSITY;
890 if (colorinfo & GST_DEBUG_BOLD) {
891 color |= FOREGROUND_INTENSITY;
893 if (colorinfo & GST_DEBUG_FG_MASK) {
894 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
896 if (colorinfo & GST_DEBUG_BG_MASK) {
897 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
903 /* width of %p varies depending on actual value of pointer, which can make
904 * output unevenly aligned if multiple threads are involved, hence the %14p
905 * (should really be %18p, but %14p seems a good compromise between too many
906 * white spaces and likely unalignment on my system) */
907 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
908 #define PTR_FMT "%14p"
910 #define PTR_FMT "%10p"
912 #define PID_FMT "%5d"
913 #define CAT_FMT "%20s %s:%d:%s:%s"
916 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
918 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
919 /* GST_LEVEL_ERROR */
920 FOREGROUND_RED | FOREGROUND_INTENSITY,
921 /* GST_LEVEL_WARNING */
922 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
924 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
925 /* GST_LEVEL_DEBUG */
926 FOREGROUND_GREEN | FOREGROUND_BLUE,
928 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
929 /* GST_LEVEL_FIXME */
930 FOREGROUND_RED | FOREGROUND_GREEN,
931 /* GST_LEVEL_TRACE */
932 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
933 /* placeholder for log level 8 */
935 /* GST_LEVEL_MEMDUMP */
936 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
939 static const guchar available_colors[] = {
940 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
941 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
942 FOREGROUND_GREEN | FOREGROUND_BLUE,
944 #endif /* G_OS_WIN32 */
945 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
946 "\033[37m", /* GST_LEVEL_NONE */
947 "\033[31;01m", /* GST_LEVEL_ERROR */
948 "\033[33;01m", /* GST_LEVEL_WARNING */
949 "\033[32;01m", /* GST_LEVEL_INFO */
950 "\033[36m", /* GST_LEVEL_DEBUG */
951 "\033[37m", /* GST_LEVEL_LOG */
952 "\033[33;01m", /* GST_LEVEL_FIXME */
953 "\033[37m", /* GST_LEVEL_TRACE */
954 "\033[37m", /* placeholder for log level 8 */
955 "\033[37m" /* GST_LEVEL_MEMDUMP */
959 * gst_debug_log_default:
960 * @category: category to log
961 * @level: level of the message
962 * @file: the file that emitted the message, usually the __FILE__ identifier
963 * @function: the function that emitted the message
964 * @line: the line from that the message was emitted, usually __LINE__
965 * @message: the actual message
966 * @object: (transfer none) (allow-none): the object this message relates to,
968 * @unused: an unused variable, reserved for some user_data.
970 * The default logging handler used by GStreamer. Logging functions get called
971 * whenever a macro like GST_DEBUG or similar is used. This function outputs the
972 * message and additional info to stderr (or the log file specified via the
973 * GST_DEBUG_FILE environment variable).
975 * You can add other handlers by using gst_debug_add_log_function().
976 * And you can remove this handler by calling
977 * gst_debug_remove_log_function(gst_debug_log_default);
980 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
981 const gchar * file, const gchar * function, gint line,
982 GObject * object, GstDebugMessage * message, gpointer unused)
985 GstClockTime elapsed;
987 GstDebugColorMode color_mode;
989 if (level > gst_debug_category_get_threshold (category))
993 color_mode = gst_debug_get_color_mode ();
996 obj = gst_debug_print_object (object);
1001 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
1002 gst_util_get_timestamp ());
1004 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1006 /* We take a lock to keep colors and content together.
1007 * Maybe there is a better way but for now this will do the right
1009 static GMutex win_print_mutex;
1010 g_mutex_lock (&win_print_mutex);
1011 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1013 /* colors, non-windows */
1014 gchar *color = NULL;
1017 const gchar *levelcolor;
1019 color = gst_debug_construct_term_color (gst_debug_category_get_color
1022 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1023 levelcolor = levelcolormap[level];
1025 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1026 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1027 pidcolor, pid, clear, g_thread_self (), levelcolor,
1028 gst_debug_level_get_name (level), clear, color,
1029 gst_debug_category_get_name (category), file, line, function, obj,
1030 clear, gst_debug_message_get (message));
1036 /* colors, windows. */
1037 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1038 #define SET_COLOR(c) G_STMT_START { \
1039 if (log_file == stderr) \
1040 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1043 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1046 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1047 fprintf (log_file, PID_FMT, pid);
1051 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1054 SET_COLOR (levelcolormap_w32[level]);
1055 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1058 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1060 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1061 file, line, function, obj);
1065 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1068 g_mutex_unlock (&win_print_mutex);
1071 /* no color, all platforms */
1072 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1073 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1074 pid, g_thread_self (), gst_debug_level_get_name (level),
1075 gst_debug_category_get_name (category), file, line, function, obj,
1076 gst_debug_message_get (message));
1085 * gst_debug_level_get_name:
1086 * @level: the level to get the name for
1088 * Get the string representation of a debugging level
1093 gst_debug_level_get_name (GstDebugLevel level)
1096 case GST_LEVEL_NONE:
1098 case GST_LEVEL_ERROR:
1100 case GST_LEVEL_WARNING:
1102 case GST_LEVEL_INFO:
1104 case GST_LEVEL_DEBUG:
1108 case GST_LEVEL_FIXME:
1110 case GST_LEVEL_TRACE:
1112 case GST_LEVEL_MEMDUMP:
1115 g_warning ("invalid level specified for gst_debug_level_get_name");
1121 * gst_debug_add_log_function:
1122 * @func: the function to use
1123 * @user_data: user data
1124 * @notify: called when @user_data is not used anymore
1126 * Adds the logging function to the list of logging functions.
1127 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1130 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1131 GDestroyNotify notify)
1133 LogFuncEntry *entry;
1137 func = gst_debug_log_default;
1139 entry = g_slice_new (LogFuncEntry);
1141 entry->user_data = user_data;
1142 entry->notify = notify;
1143 /* FIXME: we leak the old list here - other threads might access it right now
1144 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1145 * but that is waaay costly.
1146 * It'd probably be clever to use some kind of RCU here, but I don't know
1147 * anything about that.
1149 g_mutex_lock (&__log_func_mutex);
1150 list = g_slist_copy (__log_functions);
1151 __log_functions = g_slist_prepend (list, entry);
1152 g_mutex_unlock (&__log_func_mutex);
1154 if (gst_is_initialized ())
1155 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1160 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1162 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1164 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1168 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1170 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1172 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1176 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1179 GSList *new, *cleanup = NULL;
1182 g_mutex_lock (&__log_func_mutex);
1183 new = __log_functions;
1185 while ((found = g_slist_find_custom (new, data, func))) {
1186 if (new == __log_functions) {
1187 /* make a copy when we have the first hit, so that we modify the copy and
1188 * make that the new list later */
1189 new = g_slist_copy (new);
1192 cleanup = g_slist_prepend (cleanup, found->data);
1193 new = g_slist_delete_link (new, found);
1196 /* FIXME: We leak the old list here. See _add_log_function for why. */
1197 __log_functions = new;
1198 g_mutex_unlock (&__log_func_mutex);
1201 LogFuncEntry *entry = cleanup->data;
1204 entry->notify (entry->user_data);
1206 g_slice_free (LogFuncEntry, entry);
1207 cleanup = g_slist_delete_link (cleanup, cleanup);
1213 * gst_debug_remove_log_function:
1214 * @func: (scope call): the log function to remove
1216 * Removes all registered instances of the given logging functions.
1218 * Returns: How many instances of the function were removed
1221 gst_debug_remove_log_function (GstLogFunction func)
1226 func = gst_debug_log_default;
1229 gst_debug_remove_with_compare_func
1230 (gst_debug_compare_log_function_by_func, (gpointer) func);
1231 if (gst_is_initialized ())
1232 GST_DEBUG ("removed log function %p %d times from log function list", func,
1239 * gst_debug_remove_log_function_by_data:
1240 * @data: user data of the log function to remove
1242 * Removes all registered instances of log functions with the given user data.
1244 * Returns: How many instances of the function were removed
1247 gst_debug_remove_log_function_by_data (gpointer data)
1252 gst_debug_remove_with_compare_func
1253 (gst_debug_compare_log_function_by_data, data);
1255 if (gst_is_initialized ())
1257 ("removed %d log functions with user data %p from log function list",
1264 * gst_debug_set_colored:
1265 * @colored: Whether to use colored output or not
1267 * Sets or unsets the use of coloured debugging output.
1268 * Same as gst_debug_set_color_mode () with the argument being
1269 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1271 * This function may be called before gst_init().
1274 gst_debug_set_colored (gboolean colored)
1276 GstDebugColorMode new_mode;
1277 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1278 g_atomic_int_set (&__use_color, (gint) new_mode);
1282 * gst_debug_set_color_mode:
1283 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1285 * Changes the coloring mode for debug output.
1287 * This function may be called before gst_init().
1292 gst_debug_set_color_mode (GstDebugColorMode mode)
1294 g_atomic_int_set (&__use_color, mode);
1298 * gst_debug_set_color_mode_from_string:
1299 * @mode: The coloring mode for debug output. One of the following:
1300 * "on", "auto", "off", "disable", "unix".
1302 * Changes the coloring mode for debug output.
1304 * This function may be called before gst_init().
1309 gst_debug_set_color_mode_from_string (const gchar * mode)
1311 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1312 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1313 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1314 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1315 else if (strcmp (mode, "unix") == 0)
1316 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1320 * gst_debug_is_colored:
1322 * Checks if the debugging output should be colored.
1324 * Returns: %TRUE, if the debug output should be colored.
1327 gst_debug_is_colored (void)
1329 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1330 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1334 * gst_debug_get_color_mode:
1336 * Changes the coloring mode for debug output.
1338 * Returns: see @GstDebugColorMode for possible values.
1343 gst_debug_get_color_mode (void)
1345 return g_atomic_int_get (&__use_color);
1349 * gst_debug_set_active:
1350 * @active: Whether to use debugging output or not
1352 * If activated, debugging messages are sent to the debugging
1354 * It makes sense to deactivate it for speed issues.
1355 * <note><para>This function is not threadsafe. It makes sense to only call it
1356 * during initialization.</para></note>
1359 gst_debug_set_active (gboolean active)
1361 _gst_debug_enabled = active;
1363 _gst_debug_min = GST_LEVEL_COUNT;
1365 _gst_debug_min = GST_LEVEL_NONE;
1369 * gst_debug_is_active:
1371 * Checks if debugging output is activated.
1373 * Returns: %TRUE, if debugging is activated
1376 gst_debug_is_active (void)
1378 return _gst_debug_enabled;
1382 * gst_debug_set_default_threshold:
1383 * @level: level to set
1385 * Sets the default threshold to the given level and updates all categories to
1386 * use this threshold.
1388 * This function may be called before gst_init().
1391 gst_debug_set_default_threshold (GstDebugLevel level)
1393 g_atomic_int_set (&__default_level, level);
1394 gst_debug_reset_all_thresholds ();
1398 * gst_debug_get_default_threshold:
1400 * Returns the default threshold that is used for new categories.
1402 * Returns: the default threshold level
1405 gst_debug_get_default_threshold (void)
1407 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1411 gst_debug_reset_threshold (gpointer category, gpointer unused)
1413 GstDebugCategory *cat = (GstDebugCategory *) category;
1416 g_mutex_lock (&__level_name_mutex);
1417 walk = __level_name;
1419 LevelNameEntry *entry = walk->data;
1421 walk = g_slist_next (walk);
1422 if (g_pattern_match_string (entry->pat, cat->name)) {
1423 if (gst_is_initialized ())
1424 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1425 cat->name, entry->pat, entry->level);
1426 gst_debug_category_set_threshold (cat, entry->level);
1430 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1433 g_mutex_unlock (&__level_name_mutex);
1437 gst_debug_reset_all_thresholds (void)
1439 g_mutex_lock (&__cat_mutex);
1440 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1441 g_mutex_unlock (&__cat_mutex);
1445 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1447 GstDebugCategory *cat = (GstDebugCategory *) data;
1448 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1450 if (g_pattern_match_string (entry->pat, cat->name)) {
1451 if (gst_is_initialized ())
1452 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1453 cat->name, entry->pat, entry->level);
1454 gst_debug_category_set_threshold (cat, entry->level);
1459 * gst_debug_set_threshold_for_name:
1460 * @name: name of the categories to set
1461 * @level: level to set them to
1463 * Sets all categories which match the given glob style pattern to the given
1467 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1470 LevelNameEntry *entry;
1472 g_return_if_fail (name != NULL);
1474 pat = g_pattern_spec_new (name);
1475 entry = g_slice_new (LevelNameEntry);
1477 entry->level = level;
1478 g_mutex_lock (&__level_name_mutex);
1479 __level_name = g_slist_prepend (__level_name, entry);
1480 g_mutex_unlock (&__level_name_mutex);
1481 g_mutex_lock (&__cat_mutex);
1482 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1483 g_mutex_unlock (&__cat_mutex);
1487 * gst_debug_unset_threshold_for_name:
1488 * @name: name of the categories to set
1490 * Resets all categories with the given name back to the default level.
1493 gst_debug_unset_threshold_for_name (const gchar * name)
1498 g_return_if_fail (name != NULL);
1500 pat = g_pattern_spec_new (name);
1501 g_mutex_lock (&__level_name_mutex);
1502 walk = __level_name;
1503 /* improve this if you want, it's mighty slow */
1505 LevelNameEntry *entry = walk->data;
1507 if (g_pattern_spec_equal (entry->pat, pat)) {
1508 __level_name = g_slist_remove_link (__level_name, walk);
1509 g_pattern_spec_free (entry->pat);
1510 g_slice_free (LevelNameEntry, entry);
1511 g_slist_free_1 (walk);
1512 walk = __level_name;
1515 g_mutex_unlock (&__level_name_mutex);
1516 g_pattern_spec_free (pat);
1517 gst_debug_reset_all_thresholds ();
1521 _gst_debug_category_new (const gchar * name, guint color,
1522 const gchar * description)
1524 GstDebugCategory *cat, *catfound;
1526 g_return_val_if_fail (name != NULL, NULL);
1528 cat = g_slice_new (GstDebugCategory);
1529 cat->name = g_strdup (name);
1531 if (description != NULL) {
1532 cat->description = g_strdup (description);
1534 cat->description = g_strdup ("no description");
1536 g_atomic_int_set (&cat->threshold, 0);
1537 gst_debug_reset_threshold (cat, NULL);
1539 /* add to category list */
1540 g_mutex_lock (&__cat_mutex);
1541 catfound = _gst_debug_get_category_locked (name);
1543 g_free ((gpointer) cat->name);
1544 g_free ((gpointer) cat->description);
1545 g_slice_free (GstDebugCategory, cat);
1548 __categories = g_slist_prepend (__categories, cat);
1550 g_mutex_unlock (&__cat_mutex);
1556 * gst_debug_category_free:
1557 * @category: #GstDebugCategory to free.
1559 * Removes and frees the category and all associated resources.
1562 gst_debug_category_free (GstDebugCategory * category)
1564 if (category == NULL)
1567 /* remove from category list */
1568 g_mutex_lock (&__cat_mutex);
1569 __categories = g_slist_remove (__categories, category);
1570 g_mutex_unlock (&__cat_mutex);
1572 g_free ((gpointer) category->name);
1573 g_free ((gpointer) category->description);
1574 g_slice_free (GstDebugCategory, category);
1578 * gst_debug_category_set_threshold:
1579 * @category: a #GstDebugCategory to set threshold of.
1580 * @level: the #GstDebugLevel threshold to set.
1582 * Sets the threshold of the category to the given level. Debug information will
1583 * only be output if the threshold is lower or equal to the level of the
1584 * debugging message.
1586 * Do not use this function in production code, because other functions may
1587 * change the threshold of categories as side effect. It is however a nice
1588 * function to use when debugging (even from gdb).
1592 gst_debug_category_set_threshold (GstDebugCategory * category,
1593 GstDebugLevel level)
1595 g_return_if_fail (category != NULL);
1597 if (level > _gst_debug_min) {
1598 _gst_debug_enabled = TRUE;
1599 _gst_debug_min = level;
1602 g_atomic_int_set (&category->threshold, level);
1606 * gst_debug_category_reset_threshold:
1607 * @category: a #GstDebugCategory to reset threshold of.
1609 * Resets the threshold of the category to the default level. Debug information
1610 * will only be output if the threshold is lower or equal to the level of the
1611 * debugging message.
1612 * Use this function to set the threshold back to where it was after using
1613 * gst_debug_category_set_threshold().
1616 gst_debug_category_reset_threshold (GstDebugCategory * category)
1618 gst_debug_reset_threshold (category, NULL);
1622 * gst_debug_category_get_threshold:
1623 * @category: a #GstDebugCategory to get threshold of.
1625 * Returns the threshold of a #GstDebugCategory.
1627 * Returns: the #GstDebugLevel that is used as threshold.
1630 gst_debug_category_get_threshold (GstDebugCategory * category)
1632 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1636 * gst_debug_category_get_name:
1637 * @category: a #GstDebugCategory to get name of.
1639 * Returns the name of a debug category.
1641 * Returns: the name of the category.
1644 gst_debug_category_get_name (GstDebugCategory * category)
1646 return category->name;
1650 * gst_debug_category_get_color:
1651 * @category: a #GstDebugCategory to get the color of.
1653 * Returns the color of a debug category used when printing output in this
1656 * Returns: the color of the category.
1659 gst_debug_category_get_color (GstDebugCategory * category)
1661 return category->color;
1665 * gst_debug_category_get_description:
1666 * @category: a #GstDebugCategory to get the description of.
1668 * Returns the description of a debug category.
1670 * Returns: the description of the category.
1673 gst_debug_category_get_description (GstDebugCategory * category)
1675 return category->description;
1679 * gst_debug_get_all_categories:
1681 * Returns a snapshot of a all categories that are currently in use . This list
1682 * may change anytime.
1683 * The caller has to free the list after use.
1685 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1689 gst_debug_get_all_categories (void)
1693 g_mutex_lock (&__cat_mutex);
1694 ret = g_slist_copy (__categories);
1695 g_mutex_unlock (&__cat_mutex);
1700 static GstDebugCategory *
1701 _gst_debug_get_category_locked (const gchar * name)
1703 GstDebugCategory *ret = NULL;
1706 for (node = __categories; node; node = g_slist_next (node)) {
1707 ret = (GstDebugCategory *) node->data;
1708 if (!strcmp (name, ret->name)) {
1716 _gst_debug_get_category (const gchar * name)
1718 GstDebugCategory *ret;
1720 g_mutex_lock (&__cat_mutex);
1721 ret = _gst_debug_get_category_locked (name);
1722 g_mutex_unlock (&__cat_mutex);
1728 parse_debug_category (gchar * str, const gchar ** category)
1733 /* works in place */
1736 if (str[0] != '\0') {
1745 parse_debug_level (gchar * str, GstDebugLevel * level)
1750 /* works in place */
1753 if (g_ascii_isdigit (str[0])) {
1756 l = strtoul (str, &endptr, 10);
1757 if (endptr > str && endptr[0] == 0) {
1758 *level = (GstDebugLevel) l;
1762 } else if (strcmp (str, "ERROR") == 0) {
1763 *level = GST_LEVEL_ERROR;
1764 } else if (strncmp (str, "WARN", 4) == 0) {
1765 *level = GST_LEVEL_WARNING;
1766 } else if (strcmp (str, "FIXME") == 0) {
1767 *level = GST_LEVEL_FIXME;
1768 } else if (strcmp (str, "INFO") == 0) {
1769 *level = GST_LEVEL_INFO;
1770 } else if (strcmp (str, "DEBUG") == 0) {
1771 *level = GST_LEVEL_DEBUG;
1772 } else if (strcmp (str, "LOG") == 0) {
1773 *level = GST_LEVEL_LOG;
1774 } else if (strcmp (str, "TRACE") == 0) {
1775 *level = GST_LEVEL_TRACE;
1776 } else if (strcmp (str, "MEMDUMP") == 0) {
1777 *level = GST_LEVEL_MEMDUMP;
1785 * gst_debug_set_threshold_from_string:
1786 * @list: comma-separated list of "category:level" pairs to be used
1787 * as debug logging levels
1788 * @reset: %TRUE to clear all previously-set debug levels before setting
1790 * %FALSE if adding the threshold described by @list to the one already set.
1792 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1793 * environment variable. You can use wildcards such as '*', but note that
1794 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1795 * everything to log level 2.
1800 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1808 gst_debug_set_default_threshold (0);
1810 split = g_strsplit (list, ",", 0);
1812 for (walk = split; *walk; walk++) {
1813 if (strchr (*walk, ':')) {
1814 gchar **values = g_strsplit (*walk, ":", 2);
1816 if (values[0] && values[1]) {
1817 GstDebugLevel level;
1818 const gchar *category;
1820 if (parse_debug_category (values[0], &category)
1821 && parse_debug_level (values[1], &level))
1822 gst_debug_set_threshold_for_name (category, level);
1825 g_strfreev (values);
1827 GstDebugLevel level;
1829 if (parse_debug_level (*walk, &level))
1830 gst_debug_set_default_threshold (level);
1837 /*** FUNCTION POINTERS ********************************************************/
1839 static GHashTable *__gst_function_pointers; /* NULL */
1840 static GMutex __dbg_functions_mutex;
1842 /* This function MUST NOT return NULL */
1844 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1852 if (G_UNLIKELY (func == NULL))
1855 g_mutex_lock (&__dbg_functions_mutex);
1856 if (G_LIKELY (__gst_function_pointers)) {
1857 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1858 g_mutex_unlock (&__dbg_functions_mutex);
1859 if (G_LIKELY (ptrname))
1862 g_mutex_unlock (&__dbg_functions_mutex);
1864 /* we need to create an entry in the hash table for this one so we don't leak
1867 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1868 gchar *name = g_strdup (dl_info.dli_sname);
1870 _gst_debug_register_funcptr (func, name);
1875 gchar *name = g_strdup_printf ("%p", (gpointer) func);
1877 _gst_debug_register_funcptr (func, name);
1883 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1885 gpointer ptr = (gpointer) func;
1887 g_mutex_lock (&__dbg_functions_mutex);
1889 if (!__gst_function_pointers)
1890 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1891 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1892 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1894 g_mutex_unlock (&__dbg_functions_mutex);
1898 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1899 const guint8 * mem, gsize mem_offset, gsize mem_size)
1901 gchar hexstr[50], ascstr[18], digitstr[4];
1913 while (i < mem_size) {
1914 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1915 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1916 g_strlcat (hexstr, digitstr, sizeof (hexstr));
1922 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1923 (guint) mem_offset, hexstr, ascstr);
1927 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1928 const gchar * func, gint line, GObject * obj, const gchar * msg,
1929 const guint8 * data, guint length)
1933 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1934 "-------------------------------------------------------------------");
1936 if (msg != NULL && *msg != '\0') {
1937 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1940 while (off < length) {
1943 /* gst_info_dump_mem_line will process 16 bytes at most */
1944 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1945 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
1949 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1950 "-------------------------------------------------------------------");
1953 #else /* !GST_DISABLE_GST_DEBUG */
1954 #ifndef GST_REMOVE_DISABLED
1957 _gst_debug_category_new (const gchar * name, guint color,
1958 const gchar * description)
1964 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1968 /* This function MUST NOT return NULL */
1970 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1976 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
1977 const gchar * file, const gchar * function, gint line,
1978 GObject * object, const gchar * format, ...)
1983 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
1984 const gchar * file, const gchar * function, gint line,
1985 GObject * object, const gchar * format, va_list args)
1990 gst_debug_message_get (GstDebugMessage * message)
1996 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1997 const gchar * file, const gchar * function, gint line,
1998 GObject * object, GstDebugMessage * message, gpointer unused)
2003 gst_debug_level_get_name (GstDebugLevel level)
2009 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2010 GDestroyNotify notify)
2015 gst_debug_remove_log_function (GstLogFunction func)
2021 gst_debug_remove_log_function_by_data (gpointer data)
2027 gst_debug_set_active (gboolean active)
2032 gst_debug_is_active (void)
2038 gst_debug_set_colored (gboolean colored)
2043 gst_debug_set_color_mode (GstDebugColorMode mode)
2048 gst_debug_set_color_mode_from_string (const gchar * str)
2053 gst_debug_is_colored (void)
2059 gst_debug_get_color_mode (void)
2061 return GST_DEBUG_COLOR_MODE_OFF;
2065 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2070 gst_debug_set_default_threshold (GstDebugLevel level)
2075 gst_debug_get_default_threshold (void)
2077 return GST_LEVEL_NONE;
2081 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2086 gst_debug_unset_threshold_for_name (const gchar * name)
2091 gst_debug_category_free (GstDebugCategory * category)
2096 gst_debug_category_set_threshold (GstDebugCategory * category,
2097 GstDebugLevel level)
2102 gst_debug_category_reset_threshold (GstDebugCategory * category)
2107 gst_debug_category_get_threshold (GstDebugCategory * category)
2109 return GST_LEVEL_NONE;
2113 gst_debug_category_get_name (GstDebugCategory * category)
2119 gst_debug_category_get_color (GstDebugCategory * category)
2125 gst_debug_category_get_description (GstDebugCategory * category)
2131 gst_debug_get_all_categories (void)
2137 _gst_debug_get_category (const gchar * name)
2143 gst_debug_construct_term_color (guint colorinfo)
2145 return g_strdup ("00");
2149 gst_debug_construct_win_color (guint colorinfo)
2155 _priv_gst_in_valgrind (void)
2161 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2162 const gchar * func, gint line, GObject * obj, const gchar * msg,
2163 const guint8 * data, guint length)
2166 #endif /* GST_REMOVE_DISABLED */
2167 #endif /* GST_DISABLE_GST_DEBUG */
2169 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2170 * fallback function that cleans up the format string and replaces all pointer
2171 * extension formats with plain %p. */
2172 #ifdef GST_DISABLE_GST_DEBUG
2173 #include <glib/gprintf.h>
2175 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2177 gchar *clean_format, *c;
2183 clean_format = g_strdup (format);
2185 while ((c = strstr (c, "%p\a"))) {
2186 if (c[3] < 'A' || c[3] > 'Z') {
2190 len = strlen (c + 4);
2191 memmove (c + 2, c + 4, len + 1);
2194 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2196 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2199 len = g_vasprintf (result, clean_format, args);
2201 g_free (clean_format);
2203 if (*result == NULL)
2210 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2211 /* FIXME make this thread specific */
2212 static GSList *stack_trace = NULL;
2215 __cyg_profile_func_enter (void *this_fn, void *call_site)
2216 G_GNUC_NO_INSTRUMENT;
2217 void __cyg_profile_func_enter (void *this_fn, void *call_site)
2219 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2220 gchar *site = _gst_debug_nameof_funcptr (call_site);
2222 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2225 g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2226 this_fn, name, call_site, site));
2233 __cyg_profile_func_exit (void *this_fn, void *call_site)
2234 G_GNUC_NO_INSTRUMENT;
2235 void __cyg_profile_func_exit (void *this_fn, void *call_site)
2237 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2239 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2240 g_free (stack_trace->data);
2241 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2247 * gst_debug_print_stack_trace:
2249 * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2250 * gstreamer code, which can be printed with this function.
2253 gst_debug_print_stack_trace (void)
2255 GSList *walk = stack_trace;
2259 walk = g_slist_next (walk);
2262 gchar *name = (gchar *) walk->data;
2264 g_print ("#%-2d %s\n", count++, name);
2266 walk = g_slist_next (walk);
2271 gst_debug_print_stack_trace (void)
2273 /* nothing because it's compiled out */
2276 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */