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: #gst-running 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;
179 GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL;
182 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
184 #ifndef GST_DISABLE_GST_DEBUG
186 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
187 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
191 #include <rld_interface.h>
192 typedef struct DL_INFO
194 const char *dli_fname;
196 const char *dli_sname;
200 long dli_reserved[4];
204 #define _RLD_DLADDR 14
205 int dladdr (void *address, Dl_info * dl);
208 dladdr (void *address, Dl_info * dl)
212 v = _rld_new_interface (_RLD_DLADDR, address, dl);
218 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
219 static void gst_debug_reset_all_thresholds (void);
221 struct _GstDebugMessage
228 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
229 static GMutex __level_name_mutex;
230 static GSList *__level_name = NULL;
238 /* list of all categories */
239 static GMutex __cat_mutex;
240 static GSList *__categories = NULL;
242 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
245 /* all registered debug handlers */
250 GDestroyNotify notify;
253 static GMutex __log_func_mutex;
254 static GSList *__log_functions = NULL;
256 #define PRETTY_TAGS_DEFAULT TRUE
257 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
259 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
260 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
262 /* FIXME: export this? */
264 _priv_gst_in_valgrind (void)
272 in_valgrind = GST_VG_UNCHECKED;
274 if (in_valgrind == GST_VG_UNCHECKED) {
275 #ifdef HAVE_VALGRIND_VALGRIND_H
276 if (RUNNING_ON_VALGRIND) {
277 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
278 in_valgrind = GST_VG_INSIDE;
280 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
281 in_valgrind = GST_VG_NO_VALGRIND;
284 in_valgrind = GST_VG_NO_VALGRIND;
286 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
287 in_valgrind == GST_VG_INSIDE);
289 return (in_valgrind == GST_VG_INSIDE);
292 /* Initialize the debugging system */
294 _priv_gst_debug_init (void)
299 env = g_getenv ("GST_DEBUG_FILE");
300 if (env != NULL && *env != '\0') {
301 if (strcmp (env, "-") == 0) {
304 log_file = g_fopen (env, "w");
305 if (log_file == NULL) {
306 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
315 __gst_printf_pointer_extension_set_func
316 (gst_info_printf_pointer_extension_func);
318 /* do NOT use a single debug function before this line has been run */
319 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
320 GST_DEBUG_UNDERLINE, NULL);
321 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
322 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
324 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
326 /* FIXME: add descriptions here */
327 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
328 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
329 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
330 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
331 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
332 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
333 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
334 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
335 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
336 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
337 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
338 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
339 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
340 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
341 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
342 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
343 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
344 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
345 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
346 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
347 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
348 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
349 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
350 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
351 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
352 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
353 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
354 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
355 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
356 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
357 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
358 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
359 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
360 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
361 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
362 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
363 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
364 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
365 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
367 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
368 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
369 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
370 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
371 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
372 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
373 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
374 GST_DEBUG_BOLD, NULL);
375 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
376 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
377 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
378 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
379 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
380 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
381 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
382 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
383 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
384 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
385 _priv_GST_CAT_PROTECTION =
386 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
388 /* print out the valgrind message if we're in valgrind */
389 _priv_gst_in_valgrind ();
391 env = g_getenv ("GST_DEBUG_OPTIONS");
393 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
395 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
399 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
400 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
401 env = g_getenv ("GST_DEBUG_COLOR_MODE");
403 gst_debug_set_color_mode_from_string (env);
405 env = g_getenv ("GST_DEBUG");
407 gst_debug_set_threshold_from_string (env, FALSE);
411 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
412 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
416 * @category: category to log
417 * @level: level of the message is in
418 * @file: the file that emitted the message, usually the __FILE__ identifier
419 * @function: the function that emitted the message
420 * @line: the line from that the message was emitted, usually __LINE__
421 * @object: (transfer none) (allow-none): the object this message relates to,
423 * @format: a printf style format string
424 * @...: optional arguments for the format
426 * Logs the given message using the currently registered debugging handlers.
429 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
430 const gchar * file, const gchar * function, gint line,
431 GObject * object, const gchar * format, ...)
435 va_start (var_args, format);
436 gst_debug_log_valist (category, level, file, function, line, object, format,
441 /* based on g_basename(), which we can't use because it was deprecated */
442 static inline const gchar *
443 gst_path_basename (const gchar * file_name)
445 register const gchar *base;
447 base = strrchr (file_name, G_DIR_SEPARATOR);
450 const gchar *q = strrchr (file_name, '/');
451 if (base == NULL || (q != NULL && q > base))
458 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
459 return file_name + 2;
465 * gst_debug_log_valist:
466 * @category: category to log
467 * @level: level of the message is in
468 * @file: the file that emitted the message, usually the __FILE__ identifier
469 * @function: the function that emitted the message
470 * @line: the line from that the message was emitted, usually __LINE__
471 * @object: (transfer none) (allow-none): the object this message relates to,
473 * @format: a printf style format string
474 * @args: optional arguments for the format
476 * Logs the given message using the currently registered debugging handlers.
479 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
480 const gchar * file, const gchar * function, gint line,
481 GObject * object, const gchar * format, va_list args)
483 GstDebugMessage message;
487 g_return_if_fail (category != NULL);
489 if (level > gst_debug_category_get_threshold (category))
492 g_return_if_fail (file != NULL);
493 g_return_if_fail (function != NULL);
494 g_return_if_fail (format != NULL);
496 message.message = NULL;
497 message.format = format;
498 G_VA_COPY (message.arguments, args);
500 handler = __log_functions;
502 entry = handler->data;
503 handler = g_slist_next (handler);
504 entry->func (category, level, file, function, line, object, &message,
507 g_free (message.message);
508 va_end (message.arguments);
512 * gst_debug_message_get:
513 * @message: a debug message
515 * Gets the string representation of a #GstDebugMessage. This function is used
516 * in debug handlers to extract the message.
518 * Returns: the string representation of a #GstDebugMessage.
521 gst_debug_message_get (GstDebugMessage * message)
523 if (message->message == NULL) {
526 len = __gst_vasprintf (&message->message, message->format,
530 message->message = NULL;
532 return message->message;
535 #define MAX_BUFFER_DUMP_STRING_LEN 100
537 /* structure_to_pretty_string:
538 * @str: a serialized #GstStructure
540 * If the serialized structure contains large buffers such as images the hex
541 * representation of those buffers will be shortened so that the string remains
544 * Returns: the filtered string
547 prettify_structure_string (gchar * str)
549 gchar *pos = str, *end;
551 while ((pos = strstr (pos, "(buffer)"))) {
554 pos += strlen ("(buffer)");
555 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
557 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
558 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
559 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
560 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
561 strlen (pos + count) + 1);
562 pos += MAX_BUFFER_DUMP_STRING_LEN;
569 static inline gchar *
570 gst_info_structure_to_string (const GstStructure * s)
573 gchar *str = gst_structure_to_string (s);
574 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
575 return prettify_structure_string (str);
582 static inline gchar *
583 gst_info_describe_buffer (GstBuffer * buffer)
585 const gchar *offset_str = "none";
586 const gchar *offset_end_str = "none";
587 gchar offset_buf[32], offset_end_buf[32];
589 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
590 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
591 GST_BUFFER_OFFSET (buffer));
592 offset_str = offset_buf;
594 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
595 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
596 GST_BUFFER_OFFSET_END (buffer));
597 offset_end_str = offset_end_buf;
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 %s, offset_end %s, flags 0x%x", buffer,
603 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), offset_str, offset_end_str,
607 GST_BUFFER_FLAGS (buffer));
610 static inline gchar *
611 gst_info_describe_buffer_list (GstBufferList * list)
613 GstClockTime pts = GST_CLOCK_TIME_NONE;
614 GstClockTime dts = GST_CLOCK_TIME_NONE;
615 gsize total_size = 0;
618 n = gst_buffer_list_length (list);
619 for (i = 0; i < n; ++i) {
620 GstBuffer *buf = gst_buffer_list_get (list, i);
623 pts = GST_BUFFER_PTS (buf);
624 dts = GST_BUFFER_DTS (buf);
627 total_size += gst_buffer_get_size (buf);
630 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
631 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
632 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
635 static inline gchar *
636 gst_info_describe_event (GstEvent * event)
640 s = gst_info_structure_to_string (gst_event_get_structure (event));
641 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
642 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
643 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
649 static inline gchar *
650 gst_info_describe_message (GstMessage * message)
654 s = gst_info_structure_to_string (gst_message_get_structure (message));
655 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
656 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
657 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
658 GST_MESSAGE_SEQNUM (message),
659 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
665 static inline gchar *
666 gst_info_describe_query (GstQuery * query)
670 s = gst_info_structure_to_string (gst_query_get_structure (query));
671 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
672 query, (s ? s : "(NULL)"));
678 gst_debug_print_object (gpointer ptr)
680 GObject *object = (GObject *) ptr;
683 /* This is a cute trick to detect unmapped memory, but is unportable,
684 * slow, screws around with madvise, and not actually that useful. */
688 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
689 if (ret == -1 && errno == ENOMEM) {
690 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
695 /* nicely printed object */
696 if (object == NULL) {
697 return g_strdup ("(NULL)");
699 if (GST_IS_CAPS (ptr)) {
700 return gst_caps_to_string ((const GstCaps *) ptr);
702 if (GST_IS_STRUCTURE (ptr)) {
703 return gst_info_structure_to_string ((const GstStructure *) ptr);
705 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
706 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
708 if (GST_IS_TAG_LIST (ptr)) {
709 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
710 if (G_UNLIKELY (pretty_tags))
711 return prettify_structure_string (str);
715 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
716 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
718 if (GST_IS_BUFFER (ptr)) {
719 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
721 if (GST_IS_BUFFER_LIST (ptr)) {
722 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
725 if (*(guint32 *) ptr == 0xffffffff) {
726 return g_strdup_printf ("<poisoned@%p>", ptr);
729 if (GST_IS_MESSAGE (object)) {
730 return gst_info_describe_message (GST_MESSAGE_CAST (object));
732 if (GST_IS_QUERY (object)) {
733 return gst_info_describe_query (GST_QUERY_CAST (object));
735 if (GST_IS_EVENT (object)) {
736 return gst_info_describe_event (GST_EVENT_CAST (object));
738 if (GST_IS_CONTEXT (object)) {
739 GstContext *context = GST_CONTEXT_CAST (object);
742 const GstStructure *structure;
744 type = gst_context_get_context_type (context);
745 structure = gst_context_get_structure (context);
747 s = gst_info_structure_to_string (structure);
749 ret = g_strdup_printf ("context '%s'='%s'", type, s);
753 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
754 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
756 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
757 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
759 if (G_IS_OBJECT (object)) {
760 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
763 return g_strdup_printf ("%p", ptr);
767 gst_debug_print_segment (gpointer ptr)
769 GstSegment *segment = (GstSegment *) ptr;
771 /* nicely printed segment */
772 if (segment == NULL) {
773 return g_strdup ("(NULL)");
776 switch (segment->format) {
777 case GST_FORMAT_UNDEFINED:{
778 return g_strdup_printf ("UNDEFINED segment");
780 case GST_FORMAT_TIME:{
781 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
782 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
783 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
784 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
785 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
786 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
787 segment->rate, segment->applied_rate, (guint) segment->flags,
788 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
789 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
792 const gchar *format_name;
794 format_name = gst_format_get_name (segment->format);
795 if (G_UNLIKELY (format_name == NULL))
796 format_name = "(UNKNOWN FORMAT)";
797 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
798 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
799 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
800 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
801 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
802 segment->offset, segment->stop, segment->rate, segment->applied_rate,
803 (guint) segment->flags, segment->time, segment->base,
804 segment->position, segment->duration);
810 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
814 if (format[0] == 'p' && format[1] == '\a') {
816 case 'A': /* GST_PTR_FORMAT */
817 s = gst_debug_print_object (ptr);
819 case 'B': /* GST_SEGMENT_FORMAT */
820 s = gst_debug_print_segment (ptr);
823 /* must have been compiled against a newer version with an extension
824 * we don't known about yet - just ignore and fallback to %p below */
829 s = g_strdup_printf ("%p", ptr);
835 * gst_debug_construct_term_color:
836 * @colorinfo: the color info
838 * Constructs a string that can be used for getting the desired color in color
840 * You need to free the string after use.
842 * Returns: (transfer full) (type gchar*): a string containing the color
846 gst_debug_construct_term_color (guint colorinfo)
850 color = g_string_new ("\033[00");
852 if (colorinfo & GST_DEBUG_BOLD) {
853 g_string_append_len (color, ";01", 3);
855 if (colorinfo & GST_DEBUG_UNDERLINE) {
856 g_string_append_len (color, ";04", 3);
858 if (colorinfo & GST_DEBUG_FG_MASK) {
859 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
861 if (colorinfo & GST_DEBUG_BG_MASK) {
862 g_string_append_printf (color, ";4%1d",
863 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
865 g_string_append_c (color, 'm');
867 return g_string_free (color, FALSE);
871 * gst_debug_construct_win_color:
872 * @colorinfo: the color info
874 * Constructs an integer that can be used for getting the desired color in
875 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
876 * ignore this attribute.
878 * This function returns 0 on non-windows machines.
880 * Returns: an integer containing the color definition
883 gst_debug_construct_win_color (guint colorinfo)
887 static const guchar ansi_to_win_fg[8] = {
889 FOREGROUND_RED, /* red */
890 FOREGROUND_GREEN, /* green */
891 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
892 FOREGROUND_BLUE, /* blue */
893 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
894 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
895 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
897 static const guchar ansi_to_win_bg[8] = {
901 BACKGROUND_RED | BACKGROUND_GREEN,
903 BACKGROUND_RED | BACKGROUND_BLUE,
904 BACKGROUND_GREEN | FOREGROUND_BLUE,
905 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
908 /* we draw black as white, as cmd.exe can only have black bg */
909 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
910 color = ansi_to_win_fg[7];
912 if (colorinfo & GST_DEBUG_UNDERLINE) {
913 color |= BACKGROUND_INTENSITY;
915 if (colorinfo & GST_DEBUG_BOLD) {
916 color |= FOREGROUND_INTENSITY;
918 if (colorinfo & GST_DEBUG_FG_MASK) {
919 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
921 if (colorinfo & GST_DEBUG_BG_MASK) {
922 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
928 /* width of %p varies depending on actual value of pointer, which can make
929 * output unevenly aligned if multiple threads are involved, hence the %14p
930 * (should really be %18p, but %14p seems a good compromise between too many
931 * white spaces and likely unalignment on my system) */
932 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
933 #define PTR_FMT "%14p"
935 #define PTR_FMT "%10p"
937 #define PID_FMT "%5d"
938 #define CAT_FMT "%20s %s:%d:%s:%s"
941 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
943 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
944 /* GST_LEVEL_ERROR */
945 FOREGROUND_RED | FOREGROUND_INTENSITY,
946 /* GST_LEVEL_WARNING */
947 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
949 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
950 /* GST_LEVEL_DEBUG */
951 FOREGROUND_GREEN | FOREGROUND_BLUE,
953 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
954 /* GST_LEVEL_FIXME */
955 FOREGROUND_RED | FOREGROUND_GREEN,
956 /* GST_LEVEL_TRACE */
957 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
958 /* placeholder for log level 8 */
960 /* GST_LEVEL_MEMDUMP */
961 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
964 static const guchar available_colors[] = {
965 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
966 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
967 FOREGROUND_GREEN | FOREGROUND_BLUE,
969 #endif /* G_OS_WIN32 */
970 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
971 "\033[37m", /* GST_LEVEL_NONE */
972 "\033[31;01m", /* GST_LEVEL_ERROR */
973 "\033[33;01m", /* GST_LEVEL_WARNING */
974 "\033[32;01m", /* GST_LEVEL_INFO */
975 "\033[36m", /* GST_LEVEL_DEBUG */
976 "\033[37m", /* GST_LEVEL_LOG */
977 "\033[33;01m", /* GST_LEVEL_FIXME */
978 "\033[37m", /* GST_LEVEL_TRACE */
979 "\033[37m", /* placeholder for log level 8 */
980 "\033[37m" /* GST_LEVEL_MEMDUMP */
984 * gst_debug_log_default:
985 * @category: category to log
986 * @level: level of the message
987 * @file: the file that emitted the message, usually the __FILE__ identifier
988 * @function: the function that emitted the message
989 * @line: the line from that the message was emitted, usually __LINE__
990 * @message: the actual message
991 * @object: (transfer none) (allow-none): the object this message relates to,
993 * @user_data: the FILE* to log to
995 * The default logging handler used by GStreamer. Logging functions get called
996 * whenever a macro like GST_DEBUG or similar is used. By default this function
997 * is setup to output the message and additional info to stderr (or the log file
998 * specified via the GST_DEBUG_FILE environment variable) as received via
1001 * You can add other handlers by using gst_debug_add_log_function().
1002 * And you can remove this handler by calling
1003 * gst_debug_remove_log_function(gst_debug_log_default);
1006 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1007 const gchar * file, const gchar * function, gint line,
1008 GObject * object, GstDebugMessage * message, gpointer user_data)
1011 GstClockTime elapsed;
1013 GstDebugColorMode color_mode;
1014 FILE *log_file = user_data ? user_data : stderr;
1017 /* __FILE__ might be a file name or an absolute path or a
1018 * relative path, irrespective of the exact compiler used,
1019 * in which case we want to shorten it to the filename for
1022 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1023 file = gst_path_basename (file);
1027 color_mode = gst_debug_get_color_mode ();
1030 obj = gst_debug_print_object (object);
1035 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1037 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1039 /* We take a lock to keep colors and content together.
1040 * Maybe there is a better way but for now this will do the right
1042 static GMutex win_print_mutex;
1043 g_mutex_lock (&win_print_mutex);
1044 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1046 /* colors, non-windows */
1047 gchar *color = NULL;
1050 const gchar *levelcolor;
1052 color = gst_debug_construct_term_color (gst_debug_category_get_color
1055 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1056 levelcolor = levelcolormap[level];
1058 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1059 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1060 pidcolor, pid, clear, g_thread_self (), levelcolor,
1061 gst_debug_level_get_name (level), clear, color,
1062 gst_debug_category_get_name (category), file, line, function, obj,
1063 clear, gst_debug_message_get (message));
1069 /* colors, windows. */
1070 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1071 #define SET_COLOR(c) G_STMT_START { \
1072 if (log_file == stderr) \
1073 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1076 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1079 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1080 fprintf (log_file, PID_FMT, pid);
1084 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1087 SET_COLOR (levelcolormap_w32[level]);
1088 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1091 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1093 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1094 file, line, function, obj);
1098 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1101 g_mutex_unlock (&win_print_mutex);
1104 /* no color, all platforms */
1105 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1106 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1107 pid, g_thread_self (), gst_debug_level_get_name (level),
1108 gst_debug_category_get_name (category), file, line, function, obj,
1109 gst_debug_message_get (message));
1119 * gst_debug_level_get_name:
1120 * @level: the level to get the name for
1122 * Get the string representation of a debugging level
1127 gst_debug_level_get_name (GstDebugLevel level)
1130 case GST_LEVEL_NONE:
1132 case GST_LEVEL_ERROR:
1134 case GST_LEVEL_WARNING:
1136 case GST_LEVEL_INFO:
1138 case GST_LEVEL_DEBUG:
1142 case GST_LEVEL_FIXME:
1144 case GST_LEVEL_TRACE:
1146 case GST_LEVEL_MEMDUMP:
1149 g_warning ("invalid level specified for gst_debug_level_get_name");
1155 * gst_debug_add_log_function:
1156 * @func: the function to use
1157 * @user_data: user data
1158 * @notify: called when @user_data is not used anymore
1160 * Adds the logging function to the list of logging functions.
1161 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1164 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1165 GDestroyNotify notify)
1167 LogFuncEntry *entry;
1171 func = gst_debug_log_default;
1173 entry = g_slice_new (LogFuncEntry);
1175 entry->user_data = user_data;
1176 entry->notify = notify;
1177 /* FIXME: we leak the old list here - other threads might access it right now
1178 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1179 * but that is waaay costly.
1180 * It'd probably be clever to use some kind of RCU here, but I don't know
1181 * anything about that.
1183 g_mutex_lock (&__log_func_mutex);
1184 list = g_slist_copy (__log_functions);
1185 __log_functions = g_slist_prepend (list, entry);
1186 g_mutex_unlock (&__log_func_mutex);
1188 if (gst_is_initialized ())
1189 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1194 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1196 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1198 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1202 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1204 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1206 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1210 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1213 GSList *new, *cleanup = NULL;
1216 g_mutex_lock (&__log_func_mutex);
1217 new = __log_functions;
1219 while ((found = g_slist_find_custom (new, data, func))) {
1220 if (new == __log_functions) {
1221 /* make a copy when we have the first hit, so that we modify the copy and
1222 * make that the new list later */
1223 new = g_slist_copy (new);
1226 cleanup = g_slist_prepend (cleanup, found->data);
1227 new = g_slist_delete_link (new, found);
1230 /* FIXME: We leak the old list here. See _add_log_function for why. */
1231 __log_functions = new;
1232 g_mutex_unlock (&__log_func_mutex);
1235 LogFuncEntry *entry = cleanup->data;
1238 entry->notify (entry->user_data);
1240 g_slice_free (LogFuncEntry, entry);
1241 cleanup = g_slist_delete_link (cleanup, cleanup);
1247 * gst_debug_remove_log_function:
1248 * @func: (scope call): the log function to remove
1250 * Removes all registered instances of the given logging functions.
1252 * Returns: How many instances of the function were removed
1255 gst_debug_remove_log_function (GstLogFunction func)
1260 func = gst_debug_log_default;
1263 gst_debug_remove_with_compare_func
1264 (gst_debug_compare_log_function_by_func, (gpointer) func);
1265 if (gst_is_initialized ())
1266 GST_DEBUG ("removed log function %p %d times from log function list", func,
1273 * gst_debug_remove_log_function_by_data:
1274 * @data: user data of the log function to remove
1276 * Removes all registered instances of log functions with the given user data.
1278 * Returns: How many instances of the function were removed
1281 gst_debug_remove_log_function_by_data (gpointer data)
1286 gst_debug_remove_with_compare_func
1287 (gst_debug_compare_log_function_by_data, data);
1289 if (gst_is_initialized ())
1291 ("removed %d log functions with user data %p from log function list",
1298 * gst_debug_set_colored:
1299 * @colored: Whether to use colored output or not
1301 * Sets or unsets the use of coloured debugging output.
1302 * Same as gst_debug_set_color_mode () with the argument being
1303 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1305 * This function may be called before gst_init().
1308 gst_debug_set_colored (gboolean colored)
1310 GstDebugColorMode new_mode;
1311 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1312 g_atomic_int_set (&__use_color, (gint) new_mode);
1316 * gst_debug_set_color_mode:
1317 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1319 * Changes the coloring mode for debug output.
1321 * This function may be called before gst_init().
1326 gst_debug_set_color_mode (GstDebugColorMode mode)
1328 g_atomic_int_set (&__use_color, mode);
1332 * gst_debug_set_color_mode_from_string:
1333 * @mode: The coloring mode for debug output. One of the following:
1334 * "on", "auto", "off", "disable", "unix".
1336 * Changes the coloring mode for debug output.
1338 * This function may be called before gst_init().
1343 gst_debug_set_color_mode_from_string (const gchar * mode)
1345 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1346 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1347 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1348 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1349 else if (strcmp (mode, "unix") == 0)
1350 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1354 * gst_debug_is_colored:
1356 * Checks if the debugging output should be colored.
1358 * Returns: %TRUE, if the debug output should be colored.
1361 gst_debug_is_colored (void)
1363 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1364 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1368 * gst_debug_get_color_mode:
1370 * Changes the coloring mode for debug output.
1372 * Returns: see @GstDebugColorMode for possible values.
1377 gst_debug_get_color_mode (void)
1379 return g_atomic_int_get (&__use_color);
1383 * gst_debug_set_active:
1384 * @active: Whether to use debugging output or not
1386 * If activated, debugging messages are sent to the debugging
1388 * It makes sense to deactivate it for speed issues.
1389 * <note><para>This function is not threadsafe. It makes sense to only call it
1390 * during initialization.</para></note>
1393 gst_debug_set_active (gboolean active)
1395 _gst_debug_enabled = active;
1397 _gst_debug_min = GST_LEVEL_COUNT;
1399 _gst_debug_min = GST_LEVEL_NONE;
1403 * gst_debug_is_active:
1405 * Checks if debugging output is activated.
1407 * Returns: %TRUE, if debugging is activated
1410 gst_debug_is_active (void)
1412 return _gst_debug_enabled;
1416 * gst_debug_set_default_threshold:
1417 * @level: level to set
1419 * Sets the default threshold to the given level and updates all categories to
1420 * use this threshold.
1422 * This function may be called before gst_init().
1425 gst_debug_set_default_threshold (GstDebugLevel level)
1427 g_atomic_int_set (&__default_level, level);
1428 gst_debug_reset_all_thresholds ();
1432 * gst_debug_get_default_threshold:
1434 * Returns the default threshold that is used for new categories.
1436 * Returns: the default threshold level
1439 gst_debug_get_default_threshold (void)
1441 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1445 gst_debug_reset_threshold (gpointer category, gpointer unused)
1447 GstDebugCategory *cat = (GstDebugCategory *) category;
1450 g_mutex_lock (&__level_name_mutex);
1451 walk = __level_name;
1453 LevelNameEntry *entry = walk->data;
1455 walk = g_slist_next (walk);
1456 if (g_pattern_match_string (entry->pat, cat->name)) {
1457 if (gst_is_initialized ())
1458 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1459 cat->name, entry->pat, entry->level);
1460 gst_debug_category_set_threshold (cat, entry->level);
1464 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1467 g_mutex_unlock (&__level_name_mutex);
1471 gst_debug_reset_all_thresholds (void)
1473 g_mutex_lock (&__cat_mutex);
1474 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1475 g_mutex_unlock (&__cat_mutex);
1479 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1481 GstDebugCategory *cat = (GstDebugCategory *) data;
1482 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1484 if (g_pattern_match_string (entry->pat, cat->name)) {
1485 if (gst_is_initialized ())
1486 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1487 cat->name, entry->pat, entry->level);
1488 gst_debug_category_set_threshold (cat, entry->level);
1493 * gst_debug_set_threshold_for_name:
1494 * @name: name of the categories to set
1495 * @level: level to set them to
1497 * Sets all categories which match the given glob style pattern to the given
1501 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1504 LevelNameEntry *entry;
1506 g_return_if_fail (name != NULL);
1508 pat = g_pattern_spec_new (name);
1509 entry = g_slice_new (LevelNameEntry);
1511 entry->level = level;
1512 g_mutex_lock (&__level_name_mutex);
1513 __level_name = g_slist_prepend (__level_name, entry);
1514 g_mutex_unlock (&__level_name_mutex);
1515 g_mutex_lock (&__cat_mutex);
1516 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1517 g_mutex_unlock (&__cat_mutex);
1521 * gst_debug_unset_threshold_for_name:
1522 * @name: name of the categories to set
1524 * Resets all categories with the given name back to the default level.
1527 gst_debug_unset_threshold_for_name (const gchar * name)
1532 g_return_if_fail (name != NULL);
1534 pat = g_pattern_spec_new (name);
1535 g_mutex_lock (&__level_name_mutex);
1536 walk = __level_name;
1537 /* improve this if you want, it's mighty slow */
1539 LevelNameEntry *entry = walk->data;
1541 if (g_pattern_spec_equal (entry->pat, pat)) {
1542 __level_name = g_slist_remove_link (__level_name, walk);
1543 g_pattern_spec_free (entry->pat);
1544 g_slice_free (LevelNameEntry, entry);
1545 g_slist_free_1 (walk);
1546 walk = __level_name;
1548 walk = g_slist_next (walk);
1551 g_mutex_unlock (&__level_name_mutex);
1552 g_pattern_spec_free (pat);
1553 gst_debug_reset_all_thresholds ();
1557 _gst_debug_category_new (const gchar * name, guint color,
1558 const gchar * description)
1560 GstDebugCategory *cat, *catfound;
1562 g_return_val_if_fail (name != NULL, NULL);
1564 cat = g_slice_new (GstDebugCategory);
1565 cat->name = g_strdup (name);
1567 if (description != NULL) {
1568 cat->description = g_strdup (description);
1570 cat->description = g_strdup ("no description");
1572 g_atomic_int_set (&cat->threshold, 0);
1573 gst_debug_reset_threshold (cat, NULL);
1575 /* add to category list */
1576 g_mutex_lock (&__cat_mutex);
1577 catfound = _gst_debug_get_category_locked (name);
1579 g_free ((gpointer) cat->name);
1580 g_free ((gpointer) cat->description);
1581 g_slice_free (GstDebugCategory, cat);
1584 __categories = g_slist_prepend (__categories, cat);
1586 g_mutex_unlock (&__cat_mutex);
1592 * gst_debug_category_free:
1593 * @category: #GstDebugCategory to free.
1595 * Removes and frees the category and all associated resources.
1598 gst_debug_category_free (GstDebugCategory * category)
1600 if (category == NULL)
1603 /* remove from category list */
1604 g_mutex_lock (&__cat_mutex);
1605 __categories = g_slist_remove (__categories, category);
1606 g_mutex_unlock (&__cat_mutex);
1608 g_free ((gpointer) category->name);
1609 g_free ((gpointer) category->description);
1610 g_slice_free (GstDebugCategory, category);
1614 * gst_debug_category_set_threshold:
1615 * @category: a #GstDebugCategory to set threshold of.
1616 * @level: the #GstDebugLevel threshold to set.
1618 * Sets the threshold of the category to the given level. Debug information will
1619 * only be output if the threshold is lower or equal to the level of the
1620 * debugging message.
1622 * Do not use this function in production code, because other functions may
1623 * change the threshold of categories as side effect. It is however a nice
1624 * function to use when debugging (even from gdb).
1628 gst_debug_category_set_threshold (GstDebugCategory * category,
1629 GstDebugLevel level)
1631 g_return_if_fail (category != NULL);
1633 if (level > _gst_debug_min) {
1634 _gst_debug_enabled = TRUE;
1635 _gst_debug_min = level;
1638 g_atomic_int_set (&category->threshold, level);
1642 * gst_debug_category_reset_threshold:
1643 * @category: a #GstDebugCategory to reset threshold of.
1645 * Resets the threshold of the category to the default level. Debug information
1646 * will only be output if the threshold is lower or equal to the level of the
1647 * debugging message.
1648 * Use this function to set the threshold back to where it was after using
1649 * gst_debug_category_set_threshold().
1652 gst_debug_category_reset_threshold (GstDebugCategory * category)
1654 gst_debug_reset_threshold (category, NULL);
1658 * gst_debug_category_get_threshold:
1659 * @category: a #GstDebugCategory to get threshold of.
1661 * Returns the threshold of a #GstDebugCategory.
1663 * Returns: the #GstDebugLevel that is used as threshold.
1666 gst_debug_category_get_threshold (GstDebugCategory * category)
1668 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1672 * gst_debug_category_get_name:
1673 * @category: a #GstDebugCategory to get name of.
1675 * Returns the name of a debug category.
1677 * Returns: the name of the category.
1680 gst_debug_category_get_name (GstDebugCategory * category)
1682 return category->name;
1686 * gst_debug_category_get_color:
1687 * @category: a #GstDebugCategory to get the color of.
1689 * Returns the color of a debug category used when printing output in this
1692 * Returns: the color of the category.
1695 gst_debug_category_get_color (GstDebugCategory * category)
1697 return category->color;
1701 * gst_debug_category_get_description:
1702 * @category: a #GstDebugCategory to get the description of.
1704 * Returns the description of a debug category.
1706 * Returns: the description of the category.
1709 gst_debug_category_get_description (GstDebugCategory * category)
1711 return category->description;
1715 * gst_debug_get_all_categories:
1717 * Returns a snapshot of a all categories that are currently in use . This list
1718 * may change anytime.
1719 * The caller has to free the list after use.
1721 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1725 gst_debug_get_all_categories (void)
1729 g_mutex_lock (&__cat_mutex);
1730 ret = g_slist_copy (__categories);
1731 g_mutex_unlock (&__cat_mutex);
1736 static GstDebugCategory *
1737 _gst_debug_get_category_locked (const gchar * name)
1739 GstDebugCategory *ret = NULL;
1742 for (node = __categories; node; node = g_slist_next (node)) {
1743 ret = (GstDebugCategory *) node->data;
1744 if (!strcmp (name, ret->name)) {
1752 _gst_debug_get_category (const gchar * name)
1754 GstDebugCategory *ret;
1756 g_mutex_lock (&__cat_mutex);
1757 ret = _gst_debug_get_category_locked (name);
1758 g_mutex_unlock (&__cat_mutex);
1764 parse_debug_category (gchar * str, const gchar ** category)
1769 /* works in place */
1772 if (str[0] != '\0') {
1781 parse_debug_level (gchar * str, GstDebugLevel * level)
1786 /* works in place */
1789 if (g_ascii_isdigit (str[0])) {
1792 l = strtoul (str, &endptr, 10);
1793 if (endptr > str && endptr[0] == 0) {
1794 *level = (GstDebugLevel) l;
1798 } else if (strcmp (str, "ERROR") == 0) {
1799 *level = GST_LEVEL_ERROR;
1800 } else if (strncmp (str, "WARN", 4) == 0) {
1801 *level = GST_LEVEL_WARNING;
1802 } else if (strcmp (str, "FIXME") == 0) {
1803 *level = GST_LEVEL_FIXME;
1804 } else if (strcmp (str, "INFO") == 0) {
1805 *level = GST_LEVEL_INFO;
1806 } else if (strcmp (str, "DEBUG") == 0) {
1807 *level = GST_LEVEL_DEBUG;
1808 } else if (strcmp (str, "LOG") == 0) {
1809 *level = GST_LEVEL_LOG;
1810 } else if (strcmp (str, "TRACE") == 0) {
1811 *level = GST_LEVEL_TRACE;
1812 } else if (strcmp (str, "MEMDUMP") == 0) {
1813 *level = GST_LEVEL_MEMDUMP;
1821 * gst_debug_set_threshold_from_string:
1822 * @list: comma-separated list of "category:level" pairs to be used
1823 * as debug logging levels
1824 * @reset: %TRUE to clear all previously-set debug levels before setting
1826 * %FALSE if adding the threshold described by @list to the one already set.
1828 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1829 * environment variable. You can use wildcards such as '*', but note that
1830 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1831 * everything to log level 2.
1836 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1844 gst_debug_set_default_threshold (0);
1846 split = g_strsplit (list, ",", 0);
1848 for (walk = split; *walk; walk++) {
1849 if (strchr (*walk, ':')) {
1850 gchar **values = g_strsplit (*walk, ":", 2);
1852 if (values[0] && values[1]) {
1853 GstDebugLevel level;
1854 const gchar *category;
1856 if (parse_debug_category (values[0], &category)
1857 && parse_debug_level (values[1], &level))
1858 gst_debug_set_threshold_for_name (category, level);
1861 g_strfreev (values);
1863 GstDebugLevel level;
1865 if (parse_debug_level (*walk, &level))
1866 gst_debug_set_default_threshold (level);
1873 /*** FUNCTION POINTERS ********************************************************/
1875 static GHashTable *__gst_function_pointers; /* NULL */
1876 static GMutex __dbg_functions_mutex;
1878 /* This function MUST NOT return NULL */
1880 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1888 if (G_UNLIKELY (func == NULL))
1891 g_mutex_lock (&__dbg_functions_mutex);
1892 if (G_LIKELY (__gst_function_pointers)) {
1893 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1894 g_mutex_unlock (&__dbg_functions_mutex);
1895 if (G_LIKELY (ptrname))
1898 g_mutex_unlock (&__dbg_functions_mutex);
1900 /* we need to create an entry in the hash table for this one so we don't leak
1903 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1904 gchar *name = g_strdup (dl_info.dli_sname);
1906 _gst_debug_register_funcptr (func, name);
1911 gchar *name = g_strdup_printf ("%p", (gpointer) func);
1913 _gst_debug_register_funcptr (func, name);
1919 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1921 gpointer ptr = (gpointer) func;
1923 g_mutex_lock (&__dbg_functions_mutex);
1925 if (!__gst_function_pointers)
1926 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1927 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1928 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1930 g_mutex_unlock (&__dbg_functions_mutex);
1934 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1935 const guint8 * mem, gsize mem_offset, gsize mem_size)
1937 gchar hexstr[50], ascstr[18], digitstr[4];
1949 while (i < mem_size) {
1950 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1951 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1952 g_strlcat (hexstr, digitstr, sizeof (hexstr));
1958 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1959 (guint) mem_offset, hexstr, ascstr);
1963 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1964 const gchar * func, gint line, GObject * obj, const gchar * msg,
1965 const guint8 * data, guint length)
1969 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1970 "-------------------------------------------------------------------");
1972 if (msg != NULL && *msg != '\0') {
1973 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1976 while (off < length) {
1979 /* gst_info_dump_mem_line will process 16 bytes at most */
1980 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1981 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
1985 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1986 "-------------------------------------------------------------------");
1989 #else /* !GST_DISABLE_GST_DEBUG */
1990 #ifndef GST_REMOVE_DISABLED
1993 _gst_debug_category_new (const gchar * name, guint color,
1994 const gchar * description)
2000 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2004 /* This function MUST NOT return NULL */
2006 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2012 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2013 const gchar * file, const gchar * function, gint line,
2014 GObject * object, const gchar * format, ...)
2019 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2020 const gchar * file, const gchar * function, gint line,
2021 GObject * object, const gchar * format, va_list args)
2026 gst_debug_message_get (GstDebugMessage * message)
2032 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2033 const gchar * file, const gchar * function, gint line,
2034 GObject * object, GstDebugMessage * message, gpointer unused)
2039 gst_debug_level_get_name (GstDebugLevel level)
2045 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2046 GDestroyNotify notify)
2051 gst_debug_remove_log_function (GstLogFunction func)
2057 gst_debug_remove_log_function_by_data (gpointer data)
2063 gst_debug_set_active (gboolean active)
2068 gst_debug_is_active (void)
2074 gst_debug_set_colored (gboolean colored)
2079 gst_debug_set_color_mode (GstDebugColorMode mode)
2084 gst_debug_set_color_mode_from_string (const gchar * str)
2089 gst_debug_is_colored (void)
2095 gst_debug_get_color_mode (void)
2097 return GST_DEBUG_COLOR_MODE_OFF;
2101 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2106 gst_debug_set_default_threshold (GstDebugLevel level)
2111 gst_debug_get_default_threshold (void)
2113 return GST_LEVEL_NONE;
2117 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2122 gst_debug_unset_threshold_for_name (const gchar * name)
2127 gst_debug_category_free (GstDebugCategory * category)
2132 gst_debug_category_set_threshold (GstDebugCategory * category,
2133 GstDebugLevel level)
2138 gst_debug_category_reset_threshold (GstDebugCategory * category)
2143 gst_debug_category_get_threshold (GstDebugCategory * category)
2145 return GST_LEVEL_NONE;
2149 gst_debug_category_get_name (GstDebugCategory * category)
2155 gst_debug_category_get_color (GstDebugCategory * category)
2161 gst_debug_category_get_description (GstDebugCategory * category)
2167 gst_debug_get_all_categories (void)
2173 _gst_debug_get_category (const gchar * name)
2179 gst_debug_construct_term_color (guint colorinfo)
2181 return g_strdup ("00");
2185 gst_debug_construct_win_color (guint colorinfo)
2191 _priv_gst_in_valgrind (void)
2197 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2198 const gchar * func, gint line, GObject * obj, const gchar * msg,
2199 const guint8 * data, guint length)
2202 #endif /* GST_REMOVE_DISABLED */
2203 #endif /* GST_DISABLE_GST_DEBUG */
2205 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2206 * fallback function that cleans up the format string and replaces all pointer
2207 * extension formats with plain %p. */
2208 #ifdef GST_DISABLE_GST_DEBUG
2209 #include <glib/gprintf.h>
2211 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2213 gchar *clean_format, *c;
2219 clean_format = g_strdup (format);
2221 while ((c = strstr (c, "%p\a"))) {
2222 if (c[3] < 'A' || c[3] > 'Z') {
2226 len = strlen (c + 4);
2227 memmove (c + 2, c + 4, len + 1);
2230 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2232 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2235 len = g_vasprintf (result, clean_format, args);
2237 g_free (clean_format);
2239 if (*result == NULL)
2246 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2247 /* FIXME make this thread specific */
2248 static GSList *stack_trace = NULL;
2251 __cyg_profile_func_enter (void *this_fn, void *call_site)
2252 G_GNUC_NO_INSTRUMENT;
2253 void __cyg_profile_func_enter (void *this_fn, void *call_site)
2255 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2256 gchar *site = _gst_debug_nameof_funcptr (call_site);
2258 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2261 g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2262 this_fn, name, call_site, site));
2269 __cyg_profile_func_exit (void *this_fn, void *call_site)
2270 G_GNUC_NO_INSTRUMENT;
2271 void __cyg_profile_func_exit (void *this_fn, void *call_site)
2273 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2275 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2276 g_free (stack_trace->data);
2277 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2283 * gst_debug_print_stack_trace:
2285 * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2286 * gstreamer code, which can be printed with this function.
2289 gst_debug_print_stack_trace (void)
2291 GSList *walk = stack_trace;
2295 walk = g_slist_next (walk);
2298 gchar *name = (gchar *) walk->data;
2300 g_print ("#%-2d %s\n", count++, name);
2302 walk = g_slist_next (walk);
2307 gst_debug_print_stack_trace (void)
2309 /* nothing because it's compiled out */
2312 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */