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.
53 * |[<!-- language="C" -->
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.
58 * |[<!-- language="C" -->
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 /* whether to add the default log function in gst_init() */
257 static gboolean add_default_log_func = TRUE;
259 #define PRETTY_TAGS_DEFAULT TRUE
260 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
262 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
263 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
265 /* FIXME: export this? */
267 _priv_gst_in_valgrind (void)
275 in_valgrind = GST_VG_UNCHECKED;
277 if (in_valgrind == GST_VG_UNCHECKED) {
278 #ifdef HAVE_VALGRIND_VALGRIND_H
279 if (RUNNING_ON_VALGRIND) {
280 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
281 in_valgrind = GST_VG_INSIDE;
283 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
284 in_valgrind = GST_VG_NO_VALGRIND;
287 in_valgrind = GST_VG_NO_VALGRIND;
289 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
290 in_valgrind == GST_VG_INSIDE);
292 return (in_valgrind == GST_VG_INSIDE);
295 /* Initialize the debugging system */
297 _priv_gst_debug_init (void)
302 if (add_default_log_func) {
303 env = g_getenv ("GST_DEBUG_FILE");
304 if (env != NULL && *env != '\0') {
305 if (strcmp (env, "-") == 0) {
308 log_file = g_fopen (env, "w");
309 if (log_file == NULL) {
310 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
319 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
322 __gst_printf_pointer_extension_set_func
323 (gst_info_printf_pointer_extension_func);
325 /* do NOT use a single debug function before this line has been run */
326 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
327 GST_DEBUG_UNDERLINE, NULL);
328 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
329 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
331 /* FIXME: add descriptions here */
332 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
333 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
334 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
335 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
336 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
337 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
338 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
339 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
340 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
341 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
342 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
343 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
344 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
345 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
346 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
347 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
348 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
349 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
350 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
351 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
352 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
353 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
354 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
355 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
356 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
357 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
358 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
359 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
360 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
361 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
362 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
363 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
364 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
365 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
366 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
367 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
368 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
369 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
370 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
372 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
373 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
374 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
375 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
376 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
377 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
378 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
379 GST_DEBUG_BOLD, NULL);
380 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
381 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
382 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
383 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
384 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
385 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
386 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
387 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
388 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
389 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
390 _priv_GST_CAT_PROTECTION =
391 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
393 /* print out the valgrind message if we're in valgrind */
394 _priv_gst_in_valgrind ();
396 env = g_getenv ("GST_DEBUG_OPTIONS");
398 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
400 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
404 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
405 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
406 env = g_getenv ("GST_DEBUG_COLOR_MODE");
408 gst_debug_set_color_mode_from_string (env);
410 env = g_getenv ("GST_DEBUG");
412 gst_debug_set_threshold_from_string (env, FALSE);
416 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
417 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
421 * @category: category to log
422 * @level: level of the message is in
423 * @file: the file that emitted the message, usually the __FILE__ identifier
424 * @function: the function that emitted the message
425 * @line: the line from that the message was emitted, usually __LINE__
426 * @object: (transfer none) (allow-none): the object this message relates to,
428 * @format: a printf style format string
429 * @...: optional arguments for the format
431 * Logs the given message using the currently registered debugging handlers.
434 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
435 const gchar * file, const gchar * function, gint line,
436 GObject * object, const gchar * format, ...)
440 va_start (var_args, format);
441 gst_debug_log_valist (category, level, file, function, line, object, format,
446 /* based on g_basename(), which we can't use because it was deprecated */
447 static inline const gchar *
448 gst_path_basename (const gchar * file_name)
450 register const gchar *base;
452 base = strrchr (file_name, G_DIR_SEPARATOR);
455 const gchar *q = strrchr (file_name, '/');
456 if (base == NULL || (q != NULL && q > base))
463 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
464 return file_name + 2;
470 * gst_debug_log_valist:
471 * @category: category to log
472 * @level: level of the message is in
473 * @file: the file that emitted the message, usually the __FILE__ identifier
474 * @function: the function that emitted the message
475 * @line: the line from that the message was emitted, usually __LINE__
476 * @object: (transfer none) (allow-none): the object this message relates to,
478 * @format: a printf style format string
479 * @args: optional arguments for the format
481 * Logs the given message using the currently registered debugging handlers.
484 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
485 const gchar * file, const gchar * function, gint line,
486 GObject * object, const gchar * format, va_list args)
488 GstDebugMessage message;
492 g_return_if_fail (category != NULL);
494 if (level > gst_debug_category_get_threshold (category))
497 g_return_if_fail (file != NULL);
498 g_return_if_fail (function != NULL);
499 g_return_if_fail (format != NULL);
501 message.message = NULL;
502 message.format = format;
503 G_VA_COPY (message.arguments, args);
505 handler = __log_functions;
507 entry = handler->data;
508 handler = g_slist_next (handler);
509 entry->func (category, level, file, function, line, object, &message,
512 g_free (message.message);
513 va_end (message.arguments);
517 * gst_debug_message_get:
518 * @message: a debug message
520 * Gets the string representation of a #GstDebugMessage. This function is used
521 * in debug handlers to extract the message.
523 * Returns: the string representation of a #GstDebugMessage.
526 gst_debug_message_get (GstDebugMessage * message)
528 if (message->message == NULL) {
531 len = __gst_vasprintf (&message->message, message->format,
535 message->message = NULL;
537 return message->message;
540 #define MAX_BUFFER_DUMP_STRING_LEN 100
542 /* structure_to_pretty_string:
543 * @str: a serialized #GstStructure
545 * If the serialized structure contains large buffers such as images the hex
546 * representation of those buffers will be shortened so that the string remains
549 * Returns: the filtered string
552 prettify_structure_string (gchar * str)
554 gchar *pos = str, *end;
556 while ((pos = strstr (pos, "(buffer)"))) {
559 pos += strlen ("(buffer)");
560 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
562 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
563 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
564 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
565 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
566 strlen (pos + count) + 1);
567 pos += MAX_BUFFER_DUMP_STRING_LEN;
574 static inline gchar *
575 gst_info_structure_to_string (const GstStructure * s)
578 gchar *str = gst_structure_to_string (s);
579 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
580 return prettify_structure_string (str);
587 static inline gchar *
588 gst_info_describe_buffer (GstBuffer * buffer)
590 const gchar *offset_str = "none";
591 const gchar *offset_end_str = "none";
592 gchar offset_buf[32], offset_end_buf[32];
594 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
595 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
596 GST_BUFFER_OFFSET (buffer));
597 offset_str = offset_buf;
599 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
600 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
601 GST_BUFFER_OFFSET_END (buffer));
602 offset_end_str = offset_end_buf;
605 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
606 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
607 ", offset %s, offset_end %s, flags 0x%x", buffer,
608 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
609 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
610 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
611 gst_buffer_get_size (buffer), offset_str, offset_end_str,
612 GST_BUFFER_FLAGS (buffer));
615 static inline gchar *
616 gst_info_describe_buffer_list (GstBufferList * list)
618 GstClockTime pts = GST_CLOCK_TIME_NONE;
619 GstClockTime dts = GST_CLOCK_TIME_NONE;
620 gsize total_size = 0;
623 n = gst_buffer_list_length (list);
624 for (i = 0; i < n; ++i) {
625 GstBuffer *buf = gst_buffer_list_get (list, i);
628 pts = GST_BUFFER_PTS (buf);
629 dts = GST_BUFFER_DTS (buf);
632 total_size += gst_buffer_get_size (buf);
635 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
636 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
637 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
640 static inline gchar *
641 gst_info_describe_event (GstEvent * event)
645 s = gst_info_structure_to_string (gst_event_get_structure (event));
646 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
647 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
648 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
654 static inline gchar *
655 gst_info_describe_message (GstMessage * message)
659 s = gst_info_structure_to_string (gst_message_get_structure (message));
660 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
661 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
662 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
663 GST_MESSAGE_SEQNUM (message),
664 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
670 static inline gchar *
671 gst_info_describe_query (GstQuery * query)
675 s = gst_info_structure_to_string (gst_query_get_structure (query));
676 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
677 query, (s ? s : "(NULL)"));
683 gst_debug_print_object (gpointer ptr)
685 GObject *object = (GObject *) ptr;
688 /* This is a cute trick to detect unmapped memory, but is unportable,
689 * slow, screws around with madvise, and not actually that useful. */
693 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
694 if (ret == -1 && errno == ENOMEM) {
695 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
700 /* nicely printed object */
701 if (object == NULL) {
702 return g_strdup ("(NULL)");
704 if (GST_IS_CAPS (ptr)) {
705 return gst_caps_to_string ((const GstCaps *) ptr);
707 if (GST_IS_STRUCTURE (ptr)) {
708 return gst_info_structure_to_string ((const GstStructure *) ptr);
710 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
711 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
713 if (GST_IS_TAG_LIST (ptr)) {
714 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
715 if (G_UNLIKELY (pretty_tags))
716 return prettify_structure_string (str);
720 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
721 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
723 if (GST_IS_BUFFER (ptr)) {
724 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
726 if (GST_IS_BUFFER_LIST (ptr)) {
727 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
730 if (*(guint32 *) ptr == 0xffffffff) {
731 return g_strdup_printf ("<poisoned@%p>", ptr);
734 if (GST_IS_MESSAGE (object)) {
735 return gst_info_describe_message (GST_MESSAGE_CAST (object));
737 if (GST_IS_QUERY (object)) {
738 return gst_info_describe_query (GST_QUERY_CAST (object));
740 if (GST_IS_EVENT (object)) {
741 return gst_info_describe_event (GST_EVENT_CAST (object));
743 if (GST_IS_CONTEXT (object)) {
744 GstContext *context = GST_CONTEXT_CAST (object);
747 const GstStructure *structure;
749 type = gst_context_get_context_type (context);
750 structure = gst_context_get_structure (context);
752 s = gst_info_structure_to_string (structure);
754 ret = g_strdup_printf ("context '%s'='%s'", type, s);
758 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
759 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
761 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
762 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
764 if (G_IS_OBJECT (object)) {
765 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
768 return g_strdup_printf ("%p", ptr);
772 gst_debug_print_segment (gpointer ptr)
774 GstSegment *segment = (GstSegment *) ptr;
776 /* nicely printed segment */
777 if (segment == NULL) {
778 return g_strdup ("(NULL)");
781 switch (segment->format) {
782 case GST_FORMAT_UNDEFINED:{
783 return g_strdup_printf ("UNDEFINED segment");
785 case GST_FORMAT_TIME:{
786 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
787 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
788 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
789 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
790 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
791 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
792 segment->rate, segment->applied_rate, (guint) segment->flags,
793 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
794 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
797 const gchar *format_name;
799 format_name = gst_format_get_name (segment->format);
800 if (G_UNLIKELY (format_name == NULL))
801 format_name = "(UNKNOWN FORMAT)";
802 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
803 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
804 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
805 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
806 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
807 segment->offset, segment->stop, segment->rate, segment->applied_rate,
808 (guint) segment->flags, segment->time, segment->base,
809 segment->position, segment->duration);
815 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
819 if (format[0] == 'p' && format[1] == '\a') {
821 case 'A': /* GST_PTR_FORMAT */
822 s = gst_debug_print_object (ptr);
824 case 'B': /* GST_SEGMENT_FORMAT */
825 s = gst_debug_print_segment (ptr);
827 case 'a': /* GST_WRAPPED_PTR_FORMAT */
828 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
831 /* must have been compiled against a newer version with an extension
832 * we don't known about yet - just ignore and fallback to %p below */
837 s = g_strdup_printf ("%p", ptr);
843 * gst_debug_construct_term_color:
844 * @colorinfo: the color info
846 * Constructs a string that can be used for getting the desired color in color
848 * You need to free the string after use.
850 * Returns: (transfer full) (type gchar*): a string containing the color
854 gst_debug_construct_term_color (guint colorinfo)
858 color = g_string_new ("\033[00");
860 if (colorinfo & GST_DEBUG_BOLD) {
861 g_string_append_len (color, ";01", 3);
863 if (colorinfo & GST_DEBUG_UNDERLINE) {
864 g_string_append_len (color, ";04", 3);
866 if (colorinfo & GST_DEBUG_FG_MASK) {
867 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
869 if (colorinfo & GST_DEBUG_BG_MASK) {
870 g_string_append_printf (color, ";4%1d",
871 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
873 g_string_append_c (color, 'm');
875 return g_string_free (color, FALSE);
879 * gst_debug_construct_win_color:
880 * @colorinfo: the color info
882 * Constructs an integer that can be used for getting the desired color in
883 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
884 * ignore this attribute.
886 * This function returns 0 on non-windows machines.
888 * Returns: an integer containing the color definition
891 gst_debug_construct_win_color (guint colorinfo)
895 static const guchar ansi_to_win_fg[8] = {
897 FOREGROUND_RED, /* red */
898 FOREGROUND_GREEN, /* green */
899 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
900 FOREGROUND_BLUE, /* blue */
901 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
902 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
903 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
905 static const guchar ansi_to_win_bg[8] = {
909 BACKGROUND_RED | BACKGROUND_GREEN,
911 BACKGROUND_RED | BACKGROUND_BLUE,
912 BACKGROUND_GREEN | FOREGROUND_BLUE,
913 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
916 /* we draw black as white, as cmd.exe can only have black bg */
917 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
918 color = ansi_to_win_fg[7];
920 if (colorinfo & GST_DEBUG_UNDERLINE) {
921 color |= BACKGROUND_INTENSITY;
923 if (colorinfo & GST_DEBUG_BOLD) {
924 color |= FOREGROUND_INTENSITY;
926 if (colorinfo & GST_DEBUG_FG_MASK) {
927 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
929 if (colorinfo & GST_DEBUG_BG_MASK) {
930 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
936 /* width of %p varies depending on actual value of pointer, which can make
937 * output unevenly aligned if multiple threads are involved, hence the %14p
938 * (should really be %18p, but %14p seems a good compromise between too many
939 * white spaces and likely unalignment on my system) */
940 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
941 #define PTR_FMT "%14p"
943 #define PTR_FMT "%10p"
945 #define PID_FMT "%5d"
946 #define CAT_FMT "%20s %s:%d:%s:%s"
949 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
951 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
952 /* GST_LEVEL_ERROR */
953 FOREGROUND_RED | FOREGROUND_INTENSITY,
954 /* GST_LEVEL_WARNING */
955 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
957 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
958 /* GST_LEVEL_DEBUG */
959 FOREGROUND_GREEN | FOREGROUND_BLUE,
961 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
962 /* GST_LEVEL_FIXME */
963 FOREGROUND_RED | FOREGROUND_GREEN,
964 /* GST_LEVEL_TRACE */
965 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
966 /* placeholder for log level 8 */
968 /* GST_LEVEL_MEMDUMP */
969 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
972 static const guchar available_colors[] = {
973 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
974 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
975 FOREGROUND_GREEN | FOREGROUND_BLUE,
977 #endif /* G_OS_WIN32 */
978 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
979 "\033[37m", /* GST_LEVEL_NONE */
980 "\033[31;01m", /* GST_LEVEL_ERROR */
981 "\033[33;01m", /* GST_LEVEL_WARNING */
982 "\033[32;01m", /* GST_LEVEL_INFO */
983 "\033[36m", /* GST_LEVEL_DEBUG */
984 "\033[37m", /* GST_LEVEL_LOG */
985 "\033[33;01m", /* GST_LEVEL_FIXME */
986 "\033[37m", /* GST_LEVEL_TRACE */
987 "\033[37m", /* placeholder for log level 8 */
988 "\033[37m" /* GST_LEVEL_MEMDUMP */
992 * gst_debug_log_default:
993 * @category: category to log
994 * @level: level of the message
995 * @file: the file that emitted the message, usually the __FILE__ identifier
996 * @function: the function that emitted the message
997 * @line: the line from that the message was emitted, usually __LINE__
998 * @message: the actual message
999 * @object: (transfer none) (allow-none): the object this message relates to,
1001 * @user_data: the FILE* to log to
1003 * The default logging handler used by GStreamer. Logging functions get called
1004 * whenever a macro like GST_DEBUG or similar is used. By default this function
1005 * is setup to output the message and additional info to stderr (or the log file
1006 * specified via the GST_DEBUG_FILE environment variable) as received via
1009 * You can add other handlers by using gst_debug_add_log_function().
1010 * And you can remove this handler by calling
1011 * gst_debug_remove_log_function(gst_debug_log_default);
1014 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1015 const gchar * file, const gchar * function, gint line,
1016 GObject * object, GstDebugMessage * message, gpointer user_data)
1019 GstClockTime elapsed;
1021 GstDebugColorMode color_mode;
1022 FILE *log_file = user_data ? user_data : stderr;
1025 /* __FILE__ might be a file name or an absolute path or a
1026 * relative path, irrespective of the exact compiler used,
1027 * in which case we want to shorten it to the filename for
1030 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1031 file = gst_path_basename (file);
1035 color_mode = gst_debug_get_color_mode ();
1038 obj = gst_debug_print_object (object);
1043 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1045 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1047 /* We take a lock to keep colors and content together.
1048 * Maybe there is a better way but for now this will do the right
1050 static GMutex win_print_mutex;
1051 g_mutex_lock (&win_print_mutex);
1052 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1054 /* colors, non-windows */
1055 gchar *color = NULL;
1058 const gchar *levelcolor;
1060 color = gst_debug_construct_term_color (gst_debug_category_get_color
1063 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1064 levelcolor = levelcolormap[level];
1066 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1067 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1068 pidcolor, pid, clear, g_thread_self (), levelcolor,
1069 gst_debug_level_get_name (level), clear, color,
1070 gst_debug_category_get_name (category), file, line, function, obj,
1071 clear, gst_debug_message_get (message));
1077 /* colors, windows. */
1078 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1079 #define SET_COLOR(c) G_STMT_START { \
1080 if (log_file == stderr) \
1081 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1084 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1087 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1088 fprintf (log_file, PID_FMT, pid);
1092 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1095 SET_COLOR (levelcolormap_w32[level]);
1096 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1099 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1101 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1102 file, line, function, obj);
1106 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1109 g_mutex_unlock (&win_print_mutex);
1112 /* no color, all platforms */
1113 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1114 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1115 pid, g_thread_self (), gst_debug_level_get_name (level),
1116 gst_debug_category_get_name (category), file, line, function, obj,
1117 gst_debug_message_get (message));
1127 * gst_debug_level_get_name:
1128 * @level: the level to get the name for
1130 * Get the string representation of a debugging level
1135 gst_debug_level_get_name (GstDebugLevel level)
1138 case GST_LEVEL_NONE:
1140 case GST_LEVEL_ERROR:
1142 case GST_LEVEL_WARNING:
1144 case GST_LEVEL_INFO:
1146 case GST_LEVEL_DEBUG:
1150 case GST_LEVEL_FIXME:
1152 case GST_LEVEL_TRACE:
1154 case GST_LEVEL_MEMDUMP:
1157 g_warning ("invalid level specified for gst_debug_level_get_name");
1163 * gst_debug_add_log_function:
1164 * @func: the function to use
1165 * @user_data: user data
1166 * @notify: called when @user_data is not used anymore
1168 * Adds the logging function to the list of logging functions.
1169 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1172 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1173 GDestroyNotify notify)
1175 LogFuncEntry *entry;
1179 func = gst_debug_log_default;
1181 entry = g_slice_new (LogFuncEntry);
1183 entry->user_data = user_data;
1184 entry->notify = notify;
1185 /* FIXME: we leak the old list here - other threads might access it right now
1186 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1187 * but that is waaay costly.
1188 * It'd probably be clever to use some kind of RCU here, but I don't know
1189 * anything about that.
1191 g_mutex_lock (&__log_func_mutex);
1192 list = g_slist_copy (__log_functions);
1193 __log_functions = g_slist_prepend (list, entry);
1194 g_mutex_unlock (&__log_func_mutex);
1196 if (gst_is_initialized ())
1197 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1202 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1204 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1206 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1210 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1212 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1214 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1218 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1221 GSList *new, *cleanup = NULL;
1224 g_mutex_lock (&__log_func_mutex);
1225 new = __log_functions;
1227 while ((found = g_slist_find_custom (new, data, func))) {
1228 if (new == __log_functions) {
1229 /* make a copy when we have the first hit, so that we modify the copy and
1230 * make that the new list later */
1231 new = g_slist_copy (new);
1234 cleanup = g_slist_prepend (cleanup, found->data);
1235 new = g_slist_delete_link (new, found);
1238 /* FIXME: We leak the old list here. See _add_log_function for why. */
1239 __log_functions = new;
1240 g_mutex_unlock (&__log_func_mutex);
1243 LogFuncEntry *entry = cleanup->data;
1246 entry->notify (entry->user_data);
1248 g_slice_free (LogFuncEntry, entry);
1249 cleanup = g_slist_delete_link (cleanup, cleanup);
1255 * gst_debug_remove_log_function:
1256 * @func: (scope call): the log function to remove
1258 * Removes all registered instances of the given logging functions.
1260 * Returns: How many instances of the function were removed
1263 gst_debug_remove_log_function (GstLogFunction func)
1268 func = gst_debug_log_default;
1271 gst_debug_remove_with_compare_func
1272 (gst_debug_compare_log_function_by_func, (gpointer) func);
1274 if (gst_is_initialized ()) {
1275 GST_DEBUG ("removed log function %p %d times from log function list", func,
1278 /* If the default log function is removed before gst_init() was called,
1279 * set a flag so we don't add it in gst_init() later */
1280 if (func == gst_debug_log_default) {
1281 add_default_log_func = FALSE;
1290 * gst_debug_remove_log_function_by_data:
1291 * @data: user data of the log function to remove
1293 * Removes all registered instances of log functions with the given user data.
1295 * Returns: How many instances of the function were removed
1298 gst_debug_remove_log_function_by_data (gpointer data)
1303 gst_debug_remove_with_compare_func
1304 (gst_debug_compare_log_function_by_data, data);
1306 if (gst_is_initialized ())
1308 ("removed %d log functions with user data %p from log function list",
1315 * gst_debug_set_colored:
1316 * @colored: Whether to use colored output or not
1318 * Sets or unsets the use of coloured debugging output.
1319 * Same as gst_debug_set_color_mode () with the argument being
1320 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1322 * This function may be called before gst_init().
1325 gst_debug_set_colored (gboolean colored)
1327 GstDebugColorMode new_mode;
1328 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1329 g_atomic_int_set (&__use_color, (gint) new_mode);
1333 * gst_debug_set_color_mode:
1334 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1336 * Changes the coloring mode for debug output.
1338 * This function may be called before gst_init().
1343 gst_debug_set_color_mode (GstDebugColorMode mode)
1345 g_atomic_int_set (&__use_color, mode);
1349 * gst_debug_set_color_mode_from_string:
1350 * @mode: The coloring mode for debug output. One of the following:
1351 * "on", "auto", "off", "disable", "unix".
1353 * Changes the coloring mode for debug output.
1355 * This function may be called before gst_init().
1360 gst_debug_set_color_mode_from_string (const gchar * mode)
1362 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1363 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1364 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1365 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1366 else if (strcmp (mode, "unix") == 0)
1367 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1371 * gst_debug_is_colored:
1373 * Checks if the debugging output should be colored.
1375 * Returns: %TRUE, if the debug output should be colored.
1378 gst_debug_is_colored (void)
1380 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1381 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1385 * gst_debug_get_color_mode:
1387 * Changes the coloring mode for debug output.
1389 * Returns: see @GstDebugColorMode for possible values.
1394 gst_debug_get_color_mode (void)
1396 return g_atomic_int_get (&__use_color);
1400 * gst_debug_set_active:
1401 * @active: Whether to use debugging output or not
1403 * If activated, debugging messages are sent to the debugging
1405 * It makes sense to deactivate it for speed issues.
1406 * <note><para>This function is not threadsafe. It makes sense to only call it
1407 * during initialization.</para></note>
1410 gst_debug_set_active (gboolean active)
1412 _gst_debug_enabled = active;
1414 _gst_debug_min = GST_LEVEL_COUNT;
1416 _gst_debug_min = GST_LEVEL_NONE;
1420 * gst_debug_is_active:
1422 * Checks if debugging output is activated.
1424 * Returns: %TRUE, if debugging is activated
1427 gst_debug_is_active (void)
1429 return _gst_debug_enabled;
1433 * gst_debug_set_default_threshold:
1434 * @level: level to set
1436 * Sets the default threshold to the given level and updates all categories to
1437 * use this threshold.
1439 * This function may be called before gst_init().
1442 gst_debug_set_default_threshold (GstDebugLevel level)
1444 g_atomic_int_set (&__default_level, level);
1445 gst_debug_reset_all_thresholds ();
1449 * gst_debug_get_default_threshold:
1451 * Returns the default threshold that is used for new categories.
1453 * Returns: the default threshold level
1456 gst_debug_get_default_threshold (void)
1458 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1462 gst_debug_reset_threshold (gpointer category, gpointer unused)
1464 GstDebugCategory *cat = (GstDebugCategory *) category;
1467 g_mutex_lock (&__level_name_mutex);
1468 walk = __level_name;
1470 LevelNameEntry *entry = walk->data;
1472 walk = g_slist_next (walk);
1473 if (g_pattern_match_string (entry->pat, cat->name)) {
1474 if (gst_is_initialized ())
1475 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1476 cat->name, entry->pat, entry->level);
1477 gst_debug_category_set_threshold (cat, entry->level);
1481 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1484 g_mutex_unlock (&__level_name_mutex);
1488 gst_debug_reset_all_thresholds (void)
1490 g_mutex_lock (&__cat_mutex);
1491 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1492 g_mutex_unlock (&__cat_mutex);
1496 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1498 GstDebugCategory *cat = (GstDebugCategory *) data;
1499 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1501 if (g_pattern_match_string (entry->pat, cat->name)) {
1502 if (gst_is_initialized ())
1503 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1504 cat->name, entry->pat, entry->level);
1505 gst_debug_category_set_threshold (cat, entry->level);
1510 * gst_debug_set_threshold_for_name:
1511 * @name: name of the categories to set
1512 * @level: level to set them to
1514 * Sets all categories which match the given glob style pattern to the given
1518 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1521 LevelNameEntry *entry;
1523 g_return_if_fail (name != NULL);
1525 pat = g_pattern_spec_new (name);
1526 entry = g_slice_new (LevelNameEntry);
1528 entry->level = level;
1529 g_mutex_lock (&__level_name_mutex);
1530 __level_name = g_slist_prepend (__level_name, entry);
1531 g_mutex_unlock (&__level_name_mutex);
1532 g_mutex_lock (&__cat_mutex);
1533 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1534 g_mutex_unlock (&__cat_mutex);
1538 * gst_debug_unset_threshold_for_name:
1539 * @name: name of the categories to set
1541 * Resets all categories with the given name back to the default level.
1544 gst_debug_unset_threshold_for_name (const gchar * name)
1549 g_return_if_fail (name != NULL);
1551 pat = g_pattern_spec_new (name);
1552 g_mutex_lock (&__level_name_mutex);
1553 walk = __level_name;
1554 /* improve this if you want, it's mighty slow */
1556 LevelNameEntry *entry = walk->data;
1558 if (g_pattern_spec_equal (entry->pat, pat)) {
1559 __level_name = g_slist_remove_link (__level_name, walk);
1560 g_pattern_spec_free (entry->pat);
1561 g_slice_free (LevelNameEntry, entry);
1562 g_slist_free_1 (walk);
1563 walk = __level_name;
1565 walk = g_slist_next (walk);
1568 g_mutex_unlock (&__level_name_mutex);
1569 g_pattern_spec_free (pat);
1570 gst_debug_reset_all_thresholds ();
1574 _gst_debug_category_new (const gchar * name, guint color,
1575 const gchar * description)
1577 GstDebugCategory *cat, *catfound;
1579 g_return_val_if_fail (name != NULL, NULL);
1581 cat = g_slice_new (GstDebugCategory);
1582 cat->name = g_strdup (name);
1584 if (description != NULL) {
1585 cat->description = g_strdup (description);
1587 cat->description = g_strdup ("no description");
1589 g_atomic_int_set (&cat->threshold, 0);
1590 gst_debug_reset_threshold (cat, NULL);
1592 /* add to category list */
1593 g_mutex_lock (&__cat_mutex);
1594 catfound = _gst_debug_get_category_locked (name);
1596 g_free ((gpointer) cat->name);
1597 g_free ((gpointer) cat->description);
1598 g_slice_free (GstDebugCategory, cat);
1601 __categories = g_slist_prepend (__categories, cat);
1603 g_mutex_unlock (&__cat_mutex);
1609 * gst_debug_category_free:
1610 * @category: #GstDebugCategory to free.
1612 * Removes and frees the category and all associated resources.
1615 gst_debug_category_free (GstDebugCategory * category)
1617 if (category == NULL)
1620 /* remove from category list */
1621 g_mutex_lock (&__cat_mutex);
1622 __categories = g_slist_remove (__categories, category);
1623 g_mutex_unlock (&__cat_mutex);
1625 g_free ((gpointer) category->name);
1626 g_free ((gpointer) category->description);
1627 g_slice_free (GstDebugCategory, category);
1631 * gst_debug_category_set_threshold:
1632 * @category: a #GstDebugCategory to set threshold of.
1633 * @level: the #GstDebugLevel threshold to set.
1635 * Sets the threshold of the category to the given level. Debug information will
1636 * only be output if the threshold is lower or equal to the level of the
1637 * debugging message.
1639 * Do not use this function in production code, because other functions may
1640 * change the threshold of categories as side effect. It is however a nice
1641 * function to use when debugging (even from gdb).
1645 gst_debug_category_set_threshold (GstDebugCategory * category,
1646 GstDebugLevel level)
1648 g_return_if_fail (category != NULL);
1650 if (level > _gst_debug_min) {
1651 _gst_debug_enabled = TRUE;
1652 _gst_debug_min = level;
1655 g_atomic_int_set (&category->threshold, level);
1659 * gst_debug_category_reset_threshold:
1660 * @category: a #GstDebugCategory to reset threshold of.
1662 * Resets the threshold of the category to the default level. Debug information
1663 * will only be output if the threshold is lower or equal to the level of the
1664 * debugging message.
1665 * Use this function to set the threshold back to where it was after using
1666 * gst_debug_category_set_threshold().
1669 gst_debug_category_reset_threshold (GstDebugCategory * category)
1671 gst_debug_reset_threshold (category, NULL);
1675 * gst_debug_category_get_threshold:
1676 * @category: a #GstDebugCategory to get threshold of.
1678 * Returns the threshold of a #GstDebugCategory.
1680 * Returns: the #GstDebugLevel that is used as threshold.
1683 gst_debug_category_get_threshold (GstDebugCategory * category)
1685 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1689 * gst_debug_category_get_name:
1690 * @category: a #GstDebugCategory to get name of.
1692 * Returns the name of a debug category.
1694 * Returns: the name of the category.
1697 gst_debug_category_get_name (GstDebugCategory * category)
1699 return category->name;
1703 * gst_debug_category_get_color:
1704 * @category: a #GstDebugCategory to get the color of.
1706 * Returns the color of a debug category used when printing output in this
1709 * Returns: the color of the category.
1712 gst_debug_category_get_color (GstDebugCategory * category)
1714 return category->color;
1718 * gst_debug_category_get_description:
1719 * @category: a #GstDebugCategory to get the description of.
1721 * Returns the description of a debug category.
1723 * Returns: the description of the category.
1726 gst_debug_category_get_description (GstDebugCategory * category)
1728 return category->description;
1732 * gst_debug_get_all_categories:
1734 * Returns a snapshot of a all categories that are currently in use . This list
1735 * may change anytime.
1736 * The caller has to free the list after use.
1738 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1742 gst_debug_get_all_categories (void)
1746 g_mutex_lock (&__cat_mutex);
1747 ret = g_slist_copy (__categories);
1748 g_mutex_unlock (&__cat_mutex);
1753 static GstDebugCategory *
1754 _gst_debug_get_category_locked (const gchar * name)
1756 GstDebugCategory *ret = NULL;
1759 for (node = __categories; node; node = g_slist_next (node)) {
1760 ret = (GstDebugCategory *) node->data;
1761 if (!strcmp (name, ret->name)) {
1769 _gst_debug_get_category (const gchar * name)
1771 GstDebugCategory *ret;
1773 g_mutex_lock (&__cat_mutex);
1774 ret = _gst_debug_get_category_locked (name);
1775 g_mutex_unlock (&__cat_mutex);
1781 parse_debug_category (gchar * str, const gchar ** category)
1786 /* works in place */
1789 if (str[0] != '\0') {
1798 parse_debug_level (gchar * str, GstDebugLevel * level)
1803 /* works in place */
1806 if (g_ascii_isdigit (str[0])) {
1809 l = strtoul (str, &endptr, 10);
1810 if (endptr > str && endptr[0] == 0) {
1811 *level = (GstDebugLevel) l;
1815 } else if (strcmp (str, "ERROR") == 0) {
1816 *level = GST_LEVEL_ERROR;
1817 } else if (strncmp (str, "WARN", 4) == 0) {
1818 *level = GST_LEVEL_WARNING;
1819 } else if (strcmp (str, "FIXME") == 0) {
1820 *level = GST_LEVEL_FIXME;
1821 } else if (strcmp (str, "INFO") == 0) {
1822 *level = GST_LEVEL_INFO;
1823 } else if (strcmp (str, "DEBUG") == 0) {
1824 *level = GST_LEVEL_DEBUG;
1825 } else if (strcmp (str, "LOG") == 0) {
1826 *level = GST_LEVEL_LOG;
1827 } else if (strcmp (str, "TRACE") == 0) {
1828 *level = GST_LEVEL_TRACE;
1829 } else if (strcmp (str, "MEMDUMP") == 0) {
1830 *level = GST_LEVEL_MEMDUMP;
1838 * gst_debug_set_threshold_from_string:
1839 * @list: comma-separated list of "category:level" pairs to be used
1840 * as debug logging levels
1841 * @reset: %TRUE to clear all previously-set debug levels before setting
1843 * %FALSE if adding the threshold described by @list to the one already set.
1845 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1846 * environment variable. You can use wildcards such as '*', but note that
1847 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1848 * everything to log level 2.
1853 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1861 gst_debug_set_default_threshold (0);
1863 split = g_strsplit (list, ",", 0);
1865 for (walk = split; *walk; walk++) {
1866 if (strchr (*walk, ':')) {
1867 gchar **values = g_strsplit (*walk, ":", 2);
1869 if (values[0] && values[1]) {
1870 GstDebugLevel level;
1871 const gchar *category;
1873 if (parse_debug_category (values[0], &category)
1874 && parse_debug_level (values[1], &level))
1875 gst_debug_set_threshold_for_name (category, level);
1878 g_strfreev (values);
1880 GstDebugLevel level;
1882 if (parse_debug_level (*walk, &level))
1883 gst_debug_set_default_threshold (level);
1890 /*** FUNCTION POINTERS ********************************************************/
1892 static GHashTable *__gst_function_pointers; /* NULL */
1893 static GMutex __dbg_functions_mutex;
1895 /* This function MUST NOT return NULL */
1897 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1905 if (G_UNLIKELY (func == NULL))
1908 g_mutex_lock (&__dbg_functions_mutex);
1909 if (G_LIKELY (__gst_function_pointers)) {
1910 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1911 g_mutex_unlock (&__dbg_functions_mutex);
1912 if (G_LIKELY (ptrname))
1915 g_mutex_unlock (&__dbg_functions_mutex);
1917 /* we need to create an entry in the hash table for this one so we don't leak
1920 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1921 gchar *name = g_strdup (dl_info.dli_sname);
1923 _gst_debug_register_funcptr (func, name);
1928 gchar *name = g_strdup_printf ("%p", (gpointer) func);
1930 _gst_debug_register_funcptr (func, name);
1936 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1938 gpointer ptr = (gpointer) func;
1940 g_mutex_lock (&__dbg_functions_mutex);
1942 if (!__gst_function_pointers)
1943 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1944 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1945 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1947 g_mutex_unlock (&__dbg_functions_mutex);
1951 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1952 const guint8 * mem, gsize mem_offset, gsize mem_size)
1954 gchar hexstr[50], ascstr[18], digitstr[4];
1966 while (i < mem_size) {
1967 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1968 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1969 g_strlcat (hexstr, digitstr, sizeof (hexstr));
1975 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1976 (guint) mem_offset, hexstr, ascstr);
1980 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1981 const gchar * func, gint line, GObject * obj, const gchar * msg,
1982 const guint8 * data, guint length)
1986 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1987 "-------------------------------------------------------------------");
1989 if (msg != NULL && *msg != '\0') {
1990 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1993 while (off < length) {
1996 /* gst_info_dump_mem_line will process 16 bytes at most */
1997 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1998 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2002 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2003 "-------------------------------------------------------------------");
2006 #else /* !GST_DISABLE_GST_DEBUG */
2007 #ifndef GST_REMOVE_DISABLED
2010 _gst_debug_category_new (const gchar * name, guint color,
2011 const gchar * description)
2017 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2021 /* This function MUST NOT return NULL */
2023 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2029 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2030 const gchar * file, const gchar * function, gint line,
2031 GObject * object, const gchar * format, ...)
2036 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2037 const gchar * file, const gchar * function, gint line,
2038 GObject * object, const gchar * format, va_list args)
2043 gst_debug_message_get (GstDebugMessage * message)
2049 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2050 const gchar * file, const gchar * function, gint line,
2051 GObject * object, GstDebugMessage * message, gpointer unused)
2056 gst_debug_level_get_name (GstDebugLevel level)
2062 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2063 GDestroyNotify notify)
2068 gst_debug_remove_log_function (GstLogFunction func)
2074 gst_debug_remove_log_function_by_data (gpointer data)
2080 gst_debug_set_active (gboolean active)
2085 gst_debug_is_active (void)
2091 gst_debug_set_colored (gboolean colored)
2096 gst_debug_set_color_mode (GstDebugColorMode mode)
2101 gst_debug_set_color_mode_from_string (const gchar * str)
2106 gst_debug_is_colored (void)
2112 gst_debug_get_color_mode (void)
2114 return GST_DEBUG_COLOR_MODE_OFF;
2118 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2123 gst_debug_set_default_threshold (GstDebugLevel level)
2128 gst_debug_get_default_threshold (void)
2130 return GST_LEVEL_NONE;
2134 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2139 gst_debug_unset_threshold_for_name (const gchar * name)
2144 gst_debug_category_free (GstDebugCategory * category)
2149 gst_debug_category_set_threshold (GstDebugCategory * category,
2150 GstDebugLevel level)
2155 gst_debug_category_reset_threshold (GstDebugCategory * category)
2160 gst_debug_category_get_threshold (GstDebugCategory * category)
2162 return GST_LEVEL_NONE;
2166 gst_debug_category_get_name (GstDebugCategory * category)
2172 gst_debug_category_get_color (GstDebugCategory * category)
2178 gst_debug_category_get_description (GstDebugCategory * category)
2184 gst_debug_get_all_categories (void)
2190 _gst_debug_get_category (const gchar * name)
2196 gst_debug_construct_term_color (guint colorinfo)
2198 return g_strdup ("00");
2202 gst_debug_construct_win_color (guint colorinfo)
2208 _priv_gst_in_valgrind (void)
2214 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2215 const gchar * func, gint line, GObject * obj, const gchar * msg,
2216 const guint8 * data, guint length)
2219 #endif /* GST_REMOVE_DISABLED */
2220 #endif /* GST_DISABLE_GST_DEBUG */
2222 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2223 * fallback function that cleans up the format string and replaces all pointer
2224 * extension formats with plain %p. */
2225 #ifdef GST_DISABLE_GST_DEBUG
2226 #include <glib/gprintf.h>
2228 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2230 gchar *clean_format, *c;
2236 clean_format = g_strdup (format);
2238 while ((c = strstr (c, "%p\a"))) {
2239 if (c[3] < 'A' || c[3] > 'Z') {
2243 len = strlen (c + 4);
2244 memmove (c + 2, c + 4, len + 1);
2247 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2249 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2252 len = g_vasprintf (result, clean_format, args);
2254 g_free (clean_format);
2256 if (*result == NULL)
2264 * gst_info_vasprintf:
2265 * @result: (out): the resulting string
2266 * @format: a printf style format string
2267 * @args: the va_list of printf arguments for @format
2269 * Allocates and fills a string large enough (including the terminating null
2270 * byte) to hold the specified printf style @format and @args.
2272 * This function deals with the GStreamer specific printf specifiers
2273 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2274 * in your @format string, you do not need to use this function and can use
2275 * alternatives such as g_vasprintf().
2277 * Free @result with g_free().
2279 * Returns: the length of the string allocated into @result or -1 on any error
2284 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2286 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2287 * gst_private.h if the debug system is disabled which will remove the gst
2288 * specific printf format specifiers */
2289 return __gst_vasprintf (result, format, args);
2293 * gst_info_strdup_vprintf:
2294 * @format: a printf style format string
2295 * @args: the va_list of printf arguments for @format
2297 * Allocates, fills and returns a null terminated string from the printf style
2298 * @format string and @args.
2300 * See gst_info_vasprintf() for when this function is required.
2302 * Free with g_free().
2304 * Returns: a newly allocated null terminated string or %NULL on any error
2309 gst_info_strdup_vprintf (const gchar * format, va_list args)
2313 if (gst_info_vasprintf (&ret, format, args) < 0)
2320 * gst_info_strdup_printf:
2321 * @format: a printf style format string
2322 * @...: the printf arguments for @format
2324 * Allocates, fills and returns a null terminated string from the printf style
2325 * @format string and corresponding arguments.
2327 * See gst_info_vasprintf() for when this function is required.
2329 * Free with g_free().
2331 * Returns: a newly allocated null terminated string or %NULL on any error
2336 gst_info_strdup_printf (const gchar * format, ...)
2341 va_start (args, format);
2342 ret = gst_info_strdup_vprintf (format, args);
2348 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2349 /* FIXME make this thread specific */
2350 static GSList *stack_trace = NULL;
2353 __cyg_profile_func_enter (void *this_fn, void *call_site)
2354 G_GNUC_NO_INSTRUMENT;
2355 void __cyg_profile_func_enter (void *this_fn, void *call_site)
2357 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2358 gchar *site = _gst_debug_nameof_funcptr (call_site);
2360 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2363 g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2364 this_fn, name, call_site, site));
2371 __cyg_profile_func_exit (void *this_fn, void *call_site)
2372 G_GNUC_NO_INSTRUMENT;
2373 void __cyg_profile_func_exit (void *this_fn, void *call_site)
2375 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2377 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2378 g_free (stack_trace->data);
2379 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2385 * gst_debug_print_stack_trace:
2387 * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2388 * gstreamer code, which can be printed with this function.
2391 gst_debug_print_stack_trace (void)
2393 GSList *walk = stack_trace;
2397 walk = g_slist_next (walk);
2400 gchar *name = (gchar *) walk->data;
2402 g_print ("#%-2d %s\n", count++, name);
2404 walk = g_slist_next (walk);
2409 gst_debug_print_stack_trace (void)
2411 /* nothing because it's compiled out */
2414 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */