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.
28 * @short_description: Debugging and logging facilities
29 * @see_also: #gst-running for command line parameters
30 * and environment variables that affect the debugging output.
32 * GStreamer's debugging subsystem is an easy way to get information about what
33 * the application is doing. It is not meant for programming errors. Use GLib
34 * methods (g_warning and friends) for that.
36 * The debugging subsystem works only after GStreamer has been initialized
37 * - for example by calling gst_init().
39 * The debugging subsystem is used to log informational messages while the
40 * application runs. Each messages has some properties attached to it. Among
41 * these properties are the debugging category, the severity (called "level"
42 * here) and an optional #GObject it belongs to. Each of these messages is sent
43 * to all registered debugging handlers, which then handle the messages.
44 * GStreamer attaches a default handler on startup, which outputs requested
47 * Messages are output by using shortcut macros like #GST_DEBUG,
48 * #GST_CAT_ERROR_OBJECT or similar. These all expand to calling gst_debug_log()
49 * with the right parameters.
50 * The only thing a developer will probably want to do is define his own
51 * categories. This is easily done with 3 lines. At the top of your code,
53 * the variables and set the default category.
54 * |[<!-- language="C" -->
55 * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically)
56 * #define GST_CAT_DEFAULT my_category // set as default
58 * After that you only need to initialize the category.
59 * |[<!-- language="C" -->
60 * GST_DEBUG_CATEGORY_INIT (my_category, "my category",
61 * 0, "This is my very own");
63 * Initialization must be done before the category is used first.
65 * in their plugin_init function, libraries and applications should do that
66 * during their initialization.
68 * The whole debugging subsystem can be disabled at build time with passing the
69 * --disable-gst-debug switch to configure. If this is done, every function,
70 * macro and even structs described in this file evaluate to default values or
72 * So don't take addresses of these functions or use other tricks.
73 * If you must do that for some reason, there is still an option.
75 * subsystem was compiled out, #GST_DISABLE_GST_DEBUG is defined in
77 * so you can check that before doing your trick.
78 * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
79 * speed increase and will reduce the size of your compiled code. The GStreamer
80 * library itself becomes around 10% smaller.
82 * Please note that there are naming conventions for the names of debugging
83 * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
87 #include "gst_private.h"
90 #undef gst_debug_remove_log_function
91 #undef gst_debug_add_log_function
93 #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,
130 #else /* GST_DISABLE_GST_DEBUG */
132 #include <glib/gprintf.h>
133 #endif /* !GST_DISABLE_GST_DEBUG */
136 /* No need for remote debugging so turn on the 'local only' optimizations in
138 #define UNW_LOCAL_ONLY
140 #include <libunwind.h>
149 #include <elfutils/libdwfl.h>
151 #endif /* HAVE_UNWIND */
153 #ifdef HAVE_BACKTRACE
154 #include <execinfo.h>
155 #define BT_BUF_SIZE 100
156 #endif /* HAVE_BACKTRACE */
161 #include <tlhelp32.h>
162 #endif /* HAVE_DBGHELP */
165 /* We take a lock in order to
166 * 1) keep colors and content together for a single line
167 * 2) serialise gst_print*() and gst_printerr*() with each other and the debug
168 * log to keep the debug log colouring from interfering with those and
169 * to prevent broken output on the windows terminal.
170 * Maybe there is a better way but for now this will do the right
172 G_LOCK_DEFINE_STATIC (win_print_mutex);
175 extern gboolean gst_is_initialized (void);
177 /* we want these symbols exported even if debug is disabled, to maintain
178 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
179 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
181 /* disabled by default, as soon as some threshold is set > NONE,
182 * it becomes enabled. */
183 gboolean _gst_debug_enabled = FALSE;
184 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
186 GstDebugCategory *GST_CAT_DEFAULT = NULL;
188 GstDebugCategory *GST_CAT_GST_INIT = NULL;
189 GstDebugCategory *GST_CAT_MEMORY = NULL;
190 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
191 GstDebugCategory *GST_CAT_STATES = NULL;
192 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
194 GstDebugCategory *GST_CAT_BUFFER = NULL;
195 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
196 GstDebugCategory *GST_CAT_BUS = NULL;
197 GstDebugCategory *GST_CAT_CAPS = NULL;
198 GstDebugCategory *GST_CAT_CLOCK = NULL;
199 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
200 GstDebugCategory *GST_CAT_PADS = NULL;
201 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
202 GstDebugCategory *GST_CAT_PIPELINE = NULL;
203 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
204 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
205 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
206 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
207 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
208 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
209 GstDebugCategory *GST_CAT_EVENT = NULL;
210 GstDebugCategory *GST_CAT_MESSAGE = NULL;
211 GstDebugCategory *GST_CAT_PARAMS = NULL;
212 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
213 GstDebugCategory *GST_CAT_SIGNAL = NULL;
214 GstDebugCategory *GST_CAT_PROBE = NULL;
215 GstDebugCategory *GST_CAT_REGISTRY = NULL;
216 GstDebugCategory *GST_CAT_QOS = NULL;
217 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
218 GstDebugCategory *GST_CAT_META = NULL;
219 GstDebugCategory *GST_CAT_LOCKING = NULL;
220 GstDebugCategory *GST_CAT_CONTEXT = NULL;
221 GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL;
224 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
226 #ifndef GST_DISABLE_GST_DEBUG
228 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
229 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
233 #include <rld_interface.h>
234 typedef struct DL_INFO
236 const char *dli_fname;
238 const char *dli_sname;
242 long dli_reserved[4];
246 #define _RLD_DLADDR 14
247 int dladdr (void *address, Dl_info * dl);
250 dladdr (void *address, Dl_info * dl)
254 v = _rld_new_interface (_RLD_DLADDR, address, dl);
260 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
261 static void gst_debug_reset_all_thresholds (void);
263 struct _GstDebugMessage
270 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
271 static GMutex __level_name_mutex;
272 static GSList *__level_name = NULL;
280 /* list of all categories */
281 static GMutex __cat_mutex;
282 static GSList *__categories = NULL;
284 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
287 /* all registered debug handlers */
292 GDestroyNotify notify;
295 static GMutex __log_func_mutex;
296 static GSList *__log_functions = NULL;
298 /* whether to add the default log function in gst_init() */
299 static gboolean add_default_log_func = TRUE;
301 #define PRETTY_TAGS_DEFAULT TRUE
302 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
304 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
305 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
307 /* FIXME: export this? */
309 _priv_gst_in_valgrind (void)
317 in_valgrind = GST_VG_UNCHECKED;
319 if (in_valgrind == GST_VG_UNCHECKED) {
320 #ifdef HAVE_VALGRIND_VALGRIND_H
321 if (RUNNING_ON_VALGRIND) {
322 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
323 in_valgrind = GST_VG_INSIDE;
325 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
326 in_valgrind = GST_VG_NO_VALGRIND;
329 in_valgrind = GST_VG_NO_VALGRIND;
331 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
332 in_valgrind == GST_VG_INSIDE);
334 return (in_valgrind == GST_VG_INSIDE);
338 _replace_pattern_in_gst_debug_file_name (gchar * name, const char *token,
342 if ((token_start = strstr (name, token))) {
343 gsize token_len = strlen (token);
344 gchar *name_prefix = name;
345 gchar *name_suffix = token_start + token_len;
346 token_start[0] = '\0';
347 name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
348 g_free (name_prefix);
354 _priv_gst_debug_file_name (const gchar * env)
358 name = g_strdup (env);
359 name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
360 name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
365 /* Initialize the debugging system */
367 _priv_gst_debug_init (void)
372 if (add_default_log_func) {
373 env = g_getenv ("GST_DEBUG_FILE");
374 if (env != NULL && *env != '\0') {
375 if (strcmp (env, "-") == 0) {
378 gchar *name = _priv_gst_debug_file_name (env);
379 log_file = g_fopen (name, "w");
381 if (log_file == NULL) {
382 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
391 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
394 __gst_printf_pointer_extension_set_func
395 (gst_info_printf_pointer_extension_func);
397 /* do NOT use a single debug function before this line has been run */
398 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
399 GST_DEBUG_UNDERLINE, NULL);
400 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
401 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
403 /* FIXME: add descriptions here */
404 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
405 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
406 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
407 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
408 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
410 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
412 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
413 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
414 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
415 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
416 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
417 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
418 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
419 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
420 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
421 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
422 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
423 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
424 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
425 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
426 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
427 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
428 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
429 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
430 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
431 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
432 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
433 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
434 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
435 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
436 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
437 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
438 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
439 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
440 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
441 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
442 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
444 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
445 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
446 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
447 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
448 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
449 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
450 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
451 GST_DEBUG_BOLD, NULL);
452 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
453 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
454 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
455 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
456 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
457 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
458 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
459 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
460 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
461 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
462 _priv_GST_CAT_PROTECTION =
463 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
465 /* print out the valgrind message if we're in valgrind */
466 _priv_gst_in_valgrind ();
468 env = g_getenv ("GST_DEBUG_OPTIONS");
470 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
472 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
476 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
477 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
478 env = g_getenv ("GST_DEBUG_COLOR_MODE");
480 gst_debug_set_color_mode_from_string (env);
482 env = g_getenv ("GST_DEBUG");
484 gst_debug_set_threshold_from_string (env, FALSE);
487 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
488 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
492 * @category: category to log
493 * @level: level of the message is in
494 * @file: the file that emitted the message, usually the __FILE__ identifier
495 * @function: the function that emitted the message
496 * @line: the line from that the message was emitted, usually __LINE__
497 * @object: (transfer none) (allow-none): the object this message relates to,
499 * @format: a printf style format string
500 * @...: optional arguments for the format
502 * Logs the given message using the currently registered debugging handlers.
505 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
506 const gchar * file, const gchar * function, gint line,
507 GObject * object, const gchar * format, ...)
511 va_start (var_args, format);
512 gst_debug_log_valist (category, level, file, function, line, object, format,
517 /* based on g_basename(), which we can't use because it was deprecated */
518 static inline const gchar *
519 gst_path_basename (const gchar * file_name)
521 register const gchar *base;
523 base = strrchr (file_name, G_DIR_SEPARATOR);
526 const gchar *q = strrchr (file_name, '/');
527 if (base == NULL || (q != NULL && q > base))
534 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
535 return file_name + 2;
541 * gst_debug_log_valist:
542 * @category: category to log
543 * @level: level of the message is in
544 * @file: the file that emitted the message, usually the __FILE__ identifier
545 * @function: the function that emitted the message
546 * @line: the line from that the message was emitted, usually __LINE__
547 * @object: (transfer none) (allow-none): the object this message relates to,
549 * @format: a printf style format string
550 * @args: optional arguments for the format
552 * Logs the given message using the currently registered debugging handlers.
555 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
556 const gchar * file, const gchar * function, gint line,
557 GObject * object, const gchar * format, va_list args)
559 GstDebugMessage message;
563 g_return_if_fail (category != NULL);
565 if (level > gst_debug_category_get_threshold (category))
568 g_return_if_fail (file != NULL);
569 g_return_if_fail (function != NULL);
570 g_return_if_fail (format != NULL);
572 message.message = NULL;
573 message.format = format;
574 G_VA_COPY (message.arguments, args);
576 handler = __log_functions;
578 entry = handler->data;
579 handler = g_slist_next (handler);
580 entry->func (category, level, file, function, line, object, &message,
583 g_free (message.message);
584 va_end (message.arguments);
588 * gst_debug_message_get:
589 * @message: a debug message
591 * Gets the string representation of a #GstDebugMessage. This function is used
592 * in debug handlers to extract the message.
594 * Returns: (nullable): the string representation of a #GstDebugMessage.
597 gst_debug_message_get (GstDebugMessage * message)
599 if (message->message == NULL) {
602 len = __gst_vasprintf (&message->message, message->format,
606 message->message = NULL;
608 return message->message;
611 #define MAX_BUFFER_DUMP_STRING_LEN 100
613 /* structure_to_pretty_string:
614 * @str: a serialized #GstStructure
616 * If the serialized structure contains large buffers such as images the hex
617 * representation of those buffers will be shortened so that the string remains
620 * Returns: the filtered string
623 prettify_structure_string (gchar * str)
625 gchar *pos = str, *end;
627 while ((pos = strstr (pos, "(buffer)"))) {
630 pos += strlen ("(buffer)");
631 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
633 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
634 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
635 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
636 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
637 strlen (pos + count) + 1);
638 pos += MAX_BUFFER_DUMP_STRING_LEN;
645 static inline gchar *
646 gst_info_structure_to_string (const GstStructure * s)
649 gchar *str = gst_structure_to_string (s);
650 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
651 return prettify_structure_string (str);
658 static inline gchar *
659 gst_info_describe_buffer (GstBuffer * buffer)
661 const gchar *offset_str = "none";
662 const gchar *offset_end_str = "none";
663 gchar offset_buf[32], offset_end_buf[32];
665 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
666 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
667 GST_BUFFER_OFFSET (buffer));
668 offset_str = offset_buf;
670 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
671 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
672 GST_BUFFER_OFFSET_END (buffer));
673 offset_end_str = offset_end_buf;
676 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
677 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
678 ", offset %s, offset_end %s, flags 0x%x", buffer,
679 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
680 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
681 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
682 gst_buffer_get_size (buffer), offset_str, offset_end_str,
683 GST_BUFFER_FLAGS (buffer));
686 static inline gchar *
687 gst_info_describe_buffer_list (GstBufferList * list)
689 GstClockTime pts = GST_CLOCK_TIME_NONE;
690 GstClockTime dts = GST_CLOCK_TIME_NONE;
691 gsize total_size = 0;
694 n = gst_buffer_list_length (list);
695 for (i = 0; i < n; ++i) {
696 GstBuffer *buf = gst_buffer_list_get (list, i);
699 pts = GST_BUFFER_PTS (buf);
700 dts = GST_BUFFER_DTS (buf);
703 total_size += gst_buffer_get_size (buf);
706 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
707 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
708 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
711 static inline gchar *
712 gst_info_describe_event (GstEvent * event)
716 s = gst_info_structure_to_string (gst_event_get_structure (event));
717 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
718 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
719 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
725 static inline gchar *
726 gst_info_describe_message (GstMessage * message)
730 s = gst_info_structure_to_string (gst_message_get_structure (message));
731 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
732 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
733 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
734 GST_MESSAGE_SEQNUM (message),
735 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
741 static inline gchar *
742 gst_info_describe_query (GstQuery * query)
746 s = gst_info_structure_to_string (gst_query_get_structure (query));
747 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
748 query, (s ? s : "(NULL)"));
753 static inline gchar *
754 gst_info_describe_stream (GstStream * stream)
756 gchar *ret, *caps_str = NULL, *tags_str = NULL;
760 caps = gst_stream_get_caps (stream);
762 caps_str = gst_caps_to_string (caps);
763 gst_caps_unref (caps);
766 tags = gst_stream_get_tags (stream);
768 tags_str = gst_tag_list_to_string (tags);
769 gst_tag_list_unref (tags);
773 g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]",
774 gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream,
775 gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream),
776 caps_str ? caps_str : "", tags_str ? tags_str : "");
784 static inline gchar *
785 gst_info_describe_stream_collection (GstStreamCollection * collection)
788 GString *streams_str;
791 streams_str = g_string_new ("<");
792 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
793 GstStream *stream = gst_stream_collection_get_stream (collection, i);
796 s = gst_info_describe_stream (stream);
797 g_string_append_printf (streams_str, " %s,", s);
800 g_string_append (streams_str, " >");
802 ret = g_strdup_printf ("collection %p (%d streams) %s", collection,
803 gst_stream_collection_get_size (collection), streams_str->str);
805 g_string_free (streams_str, TRUE);
810 gst_debug_print_object (gpointer ptr)
812 GObject *object = (GObject *) ptr;
815 /* This is a cute trick to detect unmapped memory, but is unportable,
816 * slow, screws around with madvise, and not actually that useful. */
820 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
821 if (ret == -1 && errno == ENOMEM) {
822 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
827 /* nicely printed object */
828 if (object == NULL) {
829 return g_strdup ("(NULL)");
831 if (GST_IS_CAPS (ptr)) {
832 return gst_caps_to_string ((const GstCaps *) ptr);
834 if (GST_IS_STRUCTURE (ptr)) {
835 return gst_info_structure_to_string ((const GstStructure *) ptr);
837 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
838 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
840 if (GST_IS_TAG_LIST (ptr)) {
841 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
842 if (G_UNLIKELY (pretty_tags))
843 return prettify_structure_string (str);
847 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
848 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
850 if (GST_IS_BUFFER (ptr)) {
851 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
853 if (GST_IS_BUFFER_LIST (ptr)) {
854 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
857 if (*(guint32 *) ptr == 0xffffffff) {
858 return g_strdup_printf ("<poisoned@%p>", ptr);
861 if (GST_IS_MESSAGE (object)) {
862 return gst_info_describe_message (GST_MESSAGE_CAST (object));
864 if (GST_IS_QUERY (object)) {
865 return gst_info_describe_query (GST_QUERY_CAST (object));
867 if (GST_IS_EVENT (object)) {
868 return gst_info_describe_event (GST_EVENT_CAST (object));
870 if (GST_IS_CONTEXT (object)) {
871 GstContext *context = GST_CONTEXT_CAST (object);
874 const GstStructure *structure;
876 type = gst_context_get_context_type (context);
877 structure = gst_context_get_structure (context);
879 s = gst_info_structure_to_string (structure);
881 ret = g_strdup_printf ("context '%s'='%s'", type, s);
885 if (GST_IS_STREAM (object)) {
886 return gst_info_describe_stream (GST_STREAM_CAST (object));
888 if (GST_IS_STREAM_COLLECTION (object)) {
890 gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST
893 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
894 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
896 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
897 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
899 if (G_IS_OBJECT (object)) {
900 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
903 return g_strdup_printf ("%p", ptr);
907 gst_debug_print_segment (gpointer ptr)
909 GstSegment *segment = (GstSegment *) ptr;
911 /* nicely printed segment */
912 if (segment == NULL) {
913 return g_strdup ("(NULL)");
916 switch (segment->format) {
917 case GST_FORMAT_UNDEFINED:{
918 return g_strdup_printf ("UNDEFINED segment");
920 case GST_FORMAT_TIME:{
921 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
922 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
923 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
924 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
925 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
926 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
927 segment->rate, segment->applied_rate, (guint) segment->flags,
928 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
929 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
932 const gchar *format_name;
934 format_name = gst_format_get_name (segment->format);
935 if (G_UNLIKELY (format_name == NULL))
936 format_name = "(UNKNOWN FORMAT)";
937 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
938 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
939 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
940 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
941 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
942 segment->offset, segment->stop, segment->rate, segment->applied_rate,
943 (guint) segment->flags, segment->time, segment->base,
944 segment->position, segment->duration);
950 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
954 if (format[0] == 'p' && format[1] == '\a') {
956 case 'A': /* GST_PTR_FORMAT */
957 s = gst_debug_print_object (ptr);
959 case 'B': /* GST_SEGMENT_FORMAT */
960 s = gst_debug_print_segment (ptr);
962 case 'a': /* GST_WRAPPED_PTR_FORMAT */
963 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
966 /* must have been compiled against a newer version with an extension
967 * we don't known about yet - just ignore and fallback to %p below */
972 s = g_strdup_printf ("%p", ptr);
978 * gst_debug_construct_term_color:
979 * @colorinfo: the color info
981 * Constructs a string that can be used for getting the desired color in color
983 * You need to free the string after use.
985 * Returns: (transfer full) (type gchar*): a string containing the color
989 gst_debug_construct_term_color (guint colorinfo)
993 color = g_string_new ("\033[00");
995 if (colorinfo & GST_DEBUG_BOLD) {
996 g_string_append_len (color, ";01", 3);
998 if (colorinfo & GST_DEBUG_UNDERLINE) {
999 g_string_append_len (color, ";04", 3);
1001 if (colorinfo & GST_DEBUG_FG_MASK) {
1002 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
1004 if (colorinfo & GST_DEBUG_BG_MASK) {
1005 g_string_append_printf (color, ";4%1d",
1006 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
1008 g_string_append_c (color, 'm');
1010 return g_string_free (color, FALSE);
1014 * gst_debug_construct_win_color:
1015 * @colorinfo: the color info
1017 * Constructs an integer that can be used for getting the desired color in
1018 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
1019 * ignore this attribute.
1021 * This function returns 0 on non-windows machines.
1023 * Returns: an integer containing the color definition
1026 gst_debug_construct_win_color (guint colorinfo)
1030 static const guchar ansi_to_win_fg[8] = {
1032 FOREGROUND_RED, /* red */
1033 FOREGROUND_GREEN, /* green */
1034 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
1035 FOREGROUND_BLUE, /* blue */
1036 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
1037 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
1038 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
1040 static const guchar ansi_to_win_bg[8] = {
1044 BACKGROUND_RED | BACKGROUND_GREEN,
1046 BACKGROUND_RED | BACKGROUND_BLUE,
1047 BACKGROUND_GREEN | FOREGROUND_BLUE,
1048 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
1051 /* we draw black as white, as cmd.exe can only have black bg */
1052 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
1053 color = ansi_to_win_fg[7];
1055 if (colorinfo & GST_DEBUG_UNDERLINE) {
1056 color |= BACKGROUND_INTENSITY;
1058 if (colorinfo & GST_DEBUG_BOLD) {
1059 color |= FOREGROUND_INTENSITY;
1061 if (colorinfo & GST_DEBUG_FG_MASK) {
1062 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
1064 if (colorinfo & GST_DEBUG_BG_MASK) {
1065 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
1071 /* width of %p varies depending on actual value of pointer, which can make
1072 * output unevenly aligned if multiple threads are involved, hence the %14p
1073 * (should really be %18p, but %14p seems a good compromise between too many
1074 * white spaces and likely unalignment on my system) */
1075 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
1076 #define PTR_FMT "%14p"
1078 #define PTR_FMT "%10p"
1080 #define PID_FMT "%5d"
1081 #define CAT_FMT "%20s %s:%d:%s:%s"
1084 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
1085 /* GST_LEVEL_NONE */
1086 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1087 /* GST_LEVEL_ERROR */
1088 FOREGROUND_RED | FOREGROUND_INTENSITY,
1089 /* GST_LEVEL_WARNING */
1090 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1091 /* GST_LEVEL_INFO */
1092 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1093 /* GST_LEVEL_DEBUG */
1094 FOREGROUND_GREEN | FOREGROUND_BLUE,
1096 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1097 /* GST_LEVEL_FIXME */
1098 FOREGROUND_RED | FOREGROUND_GREEN,
1099 /* GST_LEVEL_TRACE */
1100 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1101 /* placeholder for log level 8 */
1103 /* GST_LEVEL_MEMDUMP */
1104 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
1107 static const guchar available_colors[] = {
1108 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
1109 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
1110 FOREGROUND_GREEN | FOREGROUND_BLUE,
1112 #endif /* G_OS_WIN32 */
1113 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
1114 "\033[37m", /* GST_LEVEL_NONE */
1115 "\033[31;01m", /* GST_LEVEL_ERROR */
1116 "\033[33;01m", /* GST_LEVEL_WARNING */
1117 "\033[32;01m", /* GST_LEVEL_INFO */
1118 "\033[36m", /* GST_LEVEL_DEBUG */
1119 "\033[37m", /* GST_LEVEL_LOG */
1120 "\033[33;01m", /* GST_LEVEL_FIXME */
1121 "\033[37m", /* GST_LEVEL_TRACE */
1122 "\033[37m", /* placeholder for log level 8 */
1123 "\033[37m" /* GST_LEVEL_MEMDUMP */
1127 * gst_debug_log_default:
1128 * @category: category to log
1129 * @level: level of the message
1130 * @file: the file that emitted the message, usually the __FILE__ identifier
1131 * @function: the function that emitted the message
1132 * @line: the line from that the message was emitted, usually __LINE__
1133 * @message: the actual message
1134 * @object: (transfer none) (allow-none): the object this message relates to,
1136 * @user_data: the FILE* to log to
1138 * The default logging handler used by GStreamer. Logging functions get called
1139 * whenever a macro like GST_DEBUG or similar is used. By default this function
1140 * is setup to output the message and additional info to stderr (or the log file
1141 * specified via the GST_DEBUG_FILE environment variable) as received via
1144 * You can add other handlers by using gst_debug_add_log_function().
1145 * And you can remove this handler by calling
1146 * gst_debug_remove_log_function(gst_debug_log_default);
1149 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1150 const gchar * file, const gchar * function, gint line,
1151 GObject * object, GstDebugMessage * message, gpointer user_data)
1154 GstClockTime elapsed;
1156 GstDebugColorMode color_mode;
1157 const gchar *message_str;
1158 FILE *log_file = user_data ? user_data : stderr;
1161 /* Get message string first because printing it might call into our custom
1162 * printf format extension mechanism which in turn might log something, e.g.
1163 * from inside gst_structure_to_string() when something can't be serialised.
1164 * This means we either need to do this outside of any critical section or
1165 * use a recursive lock instead. As we always need the message string in all
1166 * code paths, we might just as well get it here first thing and outside of
1167 * the win_print_mutex critical section. */
1168 message_str = gst_debug_message_get (message);
1170 /* __FILE__ might be a file name or an absolute path or a
1171 * relative path, irrespective of the exact compiler used,
1172 * in which case we want to shorten it to the filename for
1175 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1176 file = gst_path_basename (file);
1180 color_mode = gst_debug_get_color_mode ();
1183 obj = gst_debug_print_object (object);
1188 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1190 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1192 G_LOCK (win_print_mutex);
1193 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1195 /* colors, non-windows */
1196 gchar *color = NULL;
1199 const gchar *levelcolor;
1201 color = gst_debug_construct_term_color (gst_debug_category_get_color
1204 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1205 levelcolor = levelcolormap[level];
1207 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1208 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1209 pidcolor, pid, clear, g_thread_self (), levelcolor,
1210 gst_debug_level_get_name (level), clear, color,
1211 gst_debug_category_get_name (category), file, line, function, obj,
1212 clear, message_str);
1218 /* colors, windows. */
1219 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1220 #define SET_COLOR(c) G_STMT_START { \
1221 if (log_file == stderr) \
1222 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1225 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1228 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1229 fprintf (log_file, PID_FMT, pid);
1233 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1236 SET_COLOR (levelcolormap_w32[level]);
1237 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1240 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1242 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1243 file, line, function, obj);
1247 fprintf (log_file, " %s\n", message_str);
1250 G_UNLOCK (win_print_mutex);
1253 /* no color, all platforms */
1254 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1255 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1256 pid, g_thread_self (), gst_debug_level_get_name (level),
1257 gst_debug_category_get_name (category), file, line, function, obj,
1268 * gst_debug_level_get_name:
1269 * @level: the level to get the name for
1271 * Get the string representation of a debugging level
1276 gst_debug_level_get_name (GstDebugLevel level)
1279 case GST_LEVEL_NONE:
1281 case GST_LEVEL_ERROR:
1283 case GST_LEVEL_WARNING:
1285 case GST_LEVEL_INFO:
1287 case GST_LEVEL_DEBUG:
1291 case GST_LEVEL_FIXME:
1293 case GST_LEVEL_TRACE:
1295 case GST_LEVEL_MEMDUMP:
1298 g_warning ("invalid level specified for gst_debug_level_get_name");
1304 * gst_debug_add_log_function:
1305 * @func: the function to use
1306 * @user_data: user data
1307 * @notify: called when @user_data is not used anymore
1309 * Adds the logging function to the list of logging functions.
1310 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1313 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1314 GDestroyNotify notify)
1316 LogFuncEntry *entry;
1320 func = gst_debug_log_default;
1322 entry = g_slice_new (LogFuncEntry);
1324 entry->user_data = user_data;
1325 entry->notify = notify;
1326 /* FIXME: we leak the old list here - other threads might access it right now
1327 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1328 * but that is waaay costly.
1329 * It'd probably be clever to use some kind of RCU here, but I don't know
1330 * anything about that.
1332 g_mutex_lock (&__log_func_mutex);
1333 list = g_slist_copy (__log_functions);
1334 __log_functions = g_slist_prepend (list, entry);
1335 g_mutex_unlock (&__log_func_mutex);
1337 if (gst_is_initialized ())
1338 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1343 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1345 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1347 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1351 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1353 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1355 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1359 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1362 GSList *new, *cleanup = NULL;
1365 g_mutex_lock (&__log_func_mutex);
1366 new = __log_functions;
1368 while ((found = g_slist_find_custom (new, data, func))) {
1369 if (new == __log_functions) {
1370 /* make a copy when we have the first hit, so that we modify the copy and
1371 * make that the new list later */
1372 new = g_slist_copy (new);
1375 cleanup = g_slist_prepend (cleanup, found->data);
1376 new = g_slist_delete_link (new, found);
1379 /* FIXME: We leak the old list here. See _add_log_function for why. */
1380 __log_functions = new;
1381 g_mutex_unlock (&__log_func_mutex);
1384 LogFuncEntry *entry = cleanup->data;
1387 entry->notify (entry->user_data);
1389 g_slice_free (LogFuncEntry, entry);
1390 cleanup = g_slist_delete_link (cleanup, cleanup);
1396 * gst_debug_remove_log_function:
1397 * @func: (scope call) (allow-none): the log function to remove, or %NULL to
1398 * remove the default log function
1400 * Removes all registered instances of the given logging functions.
1402 * Returns: How many instances of the function were removed
1405 gst_debug_remove_log_function (GstLogFunction func)
1410 func = gst_debug_log_default;
1413 gst_debug_remove_with_compare_func
1414 (gst_debug_compare_log_function_by_func, (gpointer) func);
1416 if (gst_is_initialized ()) {
1417 GST_DEBUG ("removed log function %p %d times from log function list", func,
1420 /* If the default log function is removed before gst_init() was called,
1421 * set a flag so we don't add it in gst_init() later */
1422 if (func == gst_debug_log_default) {
1423 add_default_log_func = FALSE;
1432 * gst_debug_remove_log_function_by_data:
1433 * @data: user data of the log function to remove
1435 * Removes all registered instances of log functions with the given user data.
1437 * Returns: How many instances of the function were removed
1440 gst_debug_remove_log_function_by_data (gpointer data)
1445 gst_debug_remove_with_compare_func
1446 (gst_debug_compare_log_function_by_data, data);
1448 if (gst_is_initialized ())
1450 ("removed %d log functions with user data %p from log function list",
1457 * gst_debug_set_colored:
1458 * @colored: Whether to use colored output or not
1460 * Sets or unsets the use of coloured debugging output.
1461 * Same as gst_debug_set_color_mode () with the argument being
1462 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1464 * This function may be called before gst_init().
1467 gst_debug_set_colored (gboolean colored)
1469 GstDebugColorMode new_mode;
1470 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1471 g_atomic_int_set (&__use_color, (gint) new_mode);
1475 * gst_debug_set_color_mode:
1476 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1478 * Changes the coloring mode for debug output.
1480 * This function may be called before gst_init().
1485 gst_debug_set_color_mode (GstDebugColorMode mode)
1487 g_atomic_int_set (&__use_color, mode);
1491 * gst_debug_set_color_mode_from_string:
1492 * @mode: The coloring mode for debug output. One of the following:
1493 * "on", "auto", "off", "disable", "unix".
1495 * Changes the coloring mode for debug output.
1497 * This function may be called before gst_init().
1502 gst_debug_set_color_mode_from_string (const gchar * mode)
1504 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1505 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1506 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1507 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1508 else if (strcmp (mode, "unix") == 0)
1509 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1513 * gst_debug_is_colored:
1515 * Checks if the debugging output should be colored.
1517 * Returns: %TRUE, if the debug output should be colored.
1520 gst_debug_is_colored (void)
1522 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1523 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1527 * gst_debug_get_color_mode:
1529 * Changes the coloring mode for debug output.
1531 * Returns: see @GstDebugColorMode for possible values.
1536 gst_debug_get_color_mode (void)
1538 return g_atomic_int_get (&__use_color);
1542 * gst_debug_set_active:
1543 * @active: Whether to use debugging output or not
1545 * If activated, debugging messages are sent to the debugging
1547 * It makes sense to deactivate it for speed issues.
1548 * > This function is not threadsafe. It makes sense to only call it
1549 * during initialization.
1552 gst_debug_set_active (gboolean active)
1554 _gst_debug_enabled = active;
1556 _gst_debug_min = GST_LEVEL_COUNT;
1558 _gst_debug_min = GST_LEVEL_NONE;
1562 * gst_debug_is_active:
1564 * Checks if debugging output is activated.
1566 * Returns: %TRUE, if debugging is activated
1569 gst_debug_is_active (void)
1571 return _gst_debug_enabled;
1575 * gst_debug_set_default_threshold:
1576 * @level: level to set
1578 * Sets the default threshold to the given level and updates all categories to
1579 * use this threshold.
1581 * This function may be called before gst_init().
1584 gst_debug_set_default_threshold (GstDebugLevel level)
1586 g_atomic_int_set (&__default_level, level);
1587 gst_debug_reset_all_thresholds ();
1591 * gst_debug_get_default_threshold:
1593 * Returns the default threshold that is used for new categories.
1595 * Returns: the default threshold level
1598 gst_debug_get_default_threshold (void)
1600 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1604 gst_debug_apply_entry (GstDebugCategory * cat, LevelNameEntry * entry)
1606 if (!g_pattern_match_string (entry->pat, cat->name))
1609 if (gst_is_initialized ())
1610 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1611 cat->name, entry->pat, entry->level);
1613 gst_debug_category_set_threshold (cat, entry->level);
1618 gst_debug_reset_threshold (gpointer category, gpointer unused)
1620 GstDebugCategory *cat = (GstDebugCategory *) category;
1623 g_mutex_lock (&__level_name_mutex);
1625 for (walk = __level_name; walk != NULL; walk = walk->next) {
1626 if (gst_debug_apply_entry (cat, walk->data))
1630 g_mutex_unlock (&__level_name_mutex);
1633 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1637 gst_debug_reset_all_thresholds (void)
1639 g_mutex_lock (&__cat_mutex);
1640 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1641 g_mutex_unlock (&__cat_mutex);
1645 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1647 GstDebugCategory *cat = (GstDebugCategory *) data;
1648 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1650 gst_debug_apply_entry (cat, entry);
1654 * gst_debug_set_threshold_for_name:
1655 * @name: name of the categories to set
1656 * @level: level to set them to
1658 * Sets all categories which match the given glob style pattern to the given
1662 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1665 LevelNameEntry *entry;
1667 g_return_if_fail (name != NULL);
1669 pat = g_pattern_spec_new (name);
1670 entry = g_slice_new (LevelNameEntry);
1672 entry->level = level;
1673 g_mutex_lock (&__level_name_mutex);
1674 __level_name = g_slist_prepend (__level_name, entry);
1675 g_mutex_unlock (&__level_name_mutex);
1676 g_mutex_lock (&__cat_mutex);
1677 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1678 g_mutex_unlock (&__cat_mutex);
1682 * gst_debug_unset_threshold_for_name:
1683 * @name: name of the categories to set
1685 * Resets all categories with the given name back to the default level.
1688 gst_debug_unset_threshold_for_name (const gchar * name)
1693 g_return_if_fail (name != NULL);
1695 pat = g_pattern_spec_new (name);
1696 g_mutex_lock (&__level_name_mutex);
1697 walk = __level_name;
1698 /* improve this if you want, it's mighty slow */
1700 LevelNameEntry *entry = walk->data;
1702 if (g_pattern_spec_equal (entry->pat, pat)) {
1703 __level_name = g_slist_remove_link (__level_name, walk);
1704 g_pattern_spec_free (entry->pat);
1705 g_slice_free (LevelNameEntry, entry);
1706 g_slist_free_1 (walk);
1707 walk = __level_name;
1709 walk = g_slist_next (walk);
1712 g_mutex_unlock (&__level_name_mutex);
1713 g_pattern_spec_free (pat);
1714 gst_debug_reset_all_thresholds ();
1718 _gst_debug_category_new (const gchar * name, guint color,
1719 const gchar * description)
1721 GstDebugCategory *cat, *catfound;
1723 g_return_val_if_fail (name != NULL, NULL);
1725 cat = g_slice_new (GstDebugCategory);
1726 cat->name = g_strdup (name);
1728 if (description != NULL) {
1729 cat->description = g_strdup (description);
1731 cat->description = g_strdup ("no description");
1733 g_atomic_int_set (&cat->threshold, 0);
1734 gst_debug_reset_threshold (cat, NULL);
1736 /* add to category list */
1737 g_mutex_lock (&__cat_mutex);
1738 catfound = _gst_debug_get_category_locked (name);
1740 g_free ((gpointer) cat->name);
1741 g_free ((gpointer) cat->description);
1742 g_slice_free (GstDebugCategory, cat);
1745 __categories = g_slist_prepend (__categories, cat);
1747 g_mutex_unlock (&__cat_mutex);
1753 * gst_debug_category_free:
1754 * @category: #GstDebugCategory to free.
1756 * Removes and frees the category and all associated resources.
1759 gst_debug_category_free (GstDebugCategory * category)
1761 if (category == NULL)
1764 /* remove from category list */
1765 g_mutex_lock (&__cat_mutex);
1766 __categories = g_slist_remove (__categories, category);
1767 g_mutex_unlock (&__cat_mutex);
1769 g_free ((gpointer) category->name);
1770 g_free ((gpointer) category->description);
1771 g_slice_free (GstDebugCategory, category);
1775 * gst_debug_category_set_threshold:
1776 * @category: a #GstDebugCategory to set threshold of.
1777 * @level: the #GstDebugLevel threshold to set.
1779 * Sets the threshold of the category to the given level. Debug information will
1780 * only be output if the threshold is lower or equal to the level of the
1781 * debugging message.
1782 * > Do not use this function in production code, because other functions may
1783 * > change the threshold of categories as side effect. It is however a nice
1784 * > function to use when debugging (even from gdb).
1787 gst_debug_category_set_threshold (GstDebugCategory * category,
1788 GstDebugLevel level)
1790 g_return_if_fail (category != NULL);
1792 if (level > _gst_debug_min) {
1793 _gst_debug_enabled = TRUE;
1794 _gst_debug_min = level;
1797 g_atomic_int_set (&category->threshold, level);
1801 * gst_debug_category_reset_threshold:
1802 * @category: a #GstDebugCategory to reset threshold of.
1804 * Resets the threshold of the category to the default level. Debug information
1805 * will only be output if the threshold is lower or equal to the level of the
1806 * debugging message.
1807 * Use this function to set the threshold back to where it was after using
1808 * gst_debug_category_set_threshold().
1811 gst_debug_category_reset_threshold (GstDebugCategory * category)
1813 gst_debug_reset_threshold (category, NULL);
1817 * gst_debug_category_get_threshold:
1818 * @category: a #GstDebugCategory to get threshold of.
1820 * Returns the threshold of a #GstDebugCategory.
1822 * Returns: the #GstDebugLevel that is used as threshold.
1825 gst_debug_category_get_threshold (GstDebugCategory * category)
1827 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1831 * gst_debug_category_get_name:
1832 * @category: a #GstDebugCategory to get name of.
1834 * Returns the name of a debug category.
1836 * Returns: the name of the category.
1839 gst_debug_category_get_name (GstDebugCategory * category)
1841 return category->name;
1845 * gst_debug_category_get_color:
1846 * @category: a #GstDebugCategory to get the color of.
1848 * Returns the color of a debug category used when printing output in this
1851 * Returns: the color of the category.
1854 gst_debug_category_get_color (GstDebugCategory * category)
1856 return category->color;
1860 * gst_debug_category_get_description:
1861 * @category: a #GstDebugCategory to get the description of.
1863 * Returns the description of a debug category.
1865 * Returns: the description of the category.
1868 gst_debug_category_get_description (GstDebugCategory * category)
1870 return category->description;
1874 * gst_debug_get_all_categories:
1876 * Returns a snapshot of a all categories that are currently in use . This list
1877 * may change anytime.
1878 * The caller has to free the list after use.
1880 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1884 gst_debug_get_all_categories (void)
1888 g_mutex_lock (&__cat_mutex);
1889 ret = g_slist_copy (__categories);
1890 g_mutex_unlock (&__cat_mutex);
1895 static GstDebugCategory *
1896 _gst_debug_get_category_locked (const gchar * name)
1898 GstDebugCategory *ret = NULL;
1901 for (node = __categories; node; node = g_slist_next (node)) {
1902 ret = (GstDebugCategory *) node->data;
1903 if (!strcmp (name, ret->name)) {
1911 _gst_debug_get_category (const gchar * name)
1913 GstDebugCategory *ret;
1915 g_mutex_lock (&__cat_mutex);
1916 ret = _gst_debug_get_category_locked (name);
1917 g_mutex_unlock (&__cat_mutex);
1923 parse_debug_category (gchar * str, const gchar ** category)
1928 /* works in place */
1931 if (str[0] != '\0') {
1940 parse_debug_level (gchar * str, GstDebugLevel * level)
1945 /* works in place */
1948 if (g_ascii_isdigit (str[0])) {
1951 l = strtoul (str, &endptr, 10);
1952 if (endptr > str && endptr[0] == 0) {
1953 *level = (GstDebugLevel) l;
1957 } else if (strcmp (str, "ERROR") == 0) {
1958 *level = GST_LEVEL_ERROR;
1959 } else if (strncmp (str, "WARN", 4) == 0) {
1960 *level = GST_LEVEL_WARNING;
1961 } else if (strcmp (str, "FIXME") == 0) {
1962 *level = GST_LEVEL_FIXME;
1963 } else if (strcmp (str, "INFO") == 0) {
1964 *level = GST_LEVEL_INFO;
1965 } else if (strcmp (str, "DEBUG") == 0) {
1966 *level = GST_LEVEL_DEBUG;
1967 } else if (strcmp (str, "LOG") == 0) {
1968 *level = GST_LEVEL_LOG;
1969 } else if (strcmp (str, "TRACE") == 0) {
1970 *level = GST_LEVEL_TRACE;
1971 } else if (strcmp (str, "MEMDUMP") == 0) {
1972 *level = GST_LEVEL_MEMDUMP;
1980 * gst_debug_set_threshold_from_string:
1981 * @list: comma-separated list of "category:level" pairs to be used
1982 * as debug logging levels
1983 * @reset: %TRUE to clear all previously-set debug levels before setting
1985 * %FALSE if adding the threshold described by @list to the one already set.
1987 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1988 * environment variable. You can use wildcards such as '*', but note that
1989 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1990 * everything to log level 2.
1995 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2003 gst_debug_set_default_threshold (GST_LEVEL_DEFAULT);
2005 split = g_strsplit (list, ",", 0);
2007 for (walk = split; *walk; walk++) {
2008 if (strchr (*walk, ':')) {
2009 gchar **values = g_strsplit (*walk, ":", 2);
2011 if (values[0] && values[1]) {
2012 GstDebugLevel level;
2013 const gchar *category;
2015 if (parse_debug_category (values[0], &category)
2016 && parse_debug_level (values[1], &level)) {
2017 gst_debug_set_threshold_for_name (category, level);
2019 /* bump min-level anyway to allow the category to be registered in the
2021 if (level > _gst_debug_min) {
2022 _gst_debug_min = level;
2027 g_strfreev (values);
2029 GstDebugLevel level;
2031 if (parse_debug_level (*walk, &level))
2032 gst_debug_set_default_threshold (level);
2039 /*** FUNCTION POINTERS ********************************************************/
2041 static GHashTable *__gst_function_pointers; /* NULL */
2042 static GMutex __dbg_functions_mutex;
2044 /* This function MUST NOT return NULL */
2046 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2054 if (G_UNLIKELY (func == NULL))
2057 g_mutex_lock (&__dbg_functions_mutex);
2058 if (G_LIKELY (__gst_function_pointers)) {
2059 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
2060 g_mutex_unlock (&__dbg_functions_mutex);
2061 if (G_LIKELY (ptrname))
2064 g_mutex_unlock (&__dbg_functions_mutex);
2066 /* we need to create an entry in the hash table for this one so we don't leak
2069 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
2070 const gchar *name = g_intern_string (dl_info.dli_sname);
2072 _gst_debug_register_funcptr (func, name);
2077 gchar *name = g_strdup_printf ("%p", (gpointer) func);
2078 const gchar *iname = g_intern_string (name);
2082 _gst_debug_register_funcptr (func, iname);
2088 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2090 gpointer ptr = (gpointer) func;
2092 g_mutex_lock (&__dbg_functions_mutex);
2094 if (!__gst_function_pointers)
2095 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
2096 if (!g_hash_table_lookup (__gst_function_pointers, ptr)) {
2097 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
2100 g_mutex_unlock (&__dbg_functions_mutex);
2104 _priv_gst_debug_cleanup (void)
2106 g_mutex_lock (&__dbg_functions_mutex);
2108 if (__gst_function_pointers) {
2109 g_hash_table_unref (__gst_function_pointers);
2110 __gst_function_pointers = NULL;
2113 g_mutex_unlock (&__dbg_functions_mutex);
2117 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
2118 const guint8 * mem, gsize mem_offset, gsize mem_size)
2120 gchar hexstr[50], ascstr[18], digitstr[4];
2132 while (i < mem_size) {
2133 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
2134 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
2135 g_strlcat (hexstr, digitstr, sizeof (hexstr));
2141 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
2142 (guint) mem_offset, hexstr, ascstr);
2146 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2147 const gchar * func, gint line, GObject * obj, const gchar * msg,
2148 const guint8 * data, guint length)
2152 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2153 "-------------------------------------------------------------------");
2155 if (msg != NULL && *msg != '\0') {
2156 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
2159 while (off < length) {
2162 /* gst_info_dump_mem_line will process 16 bytes at most */
2163 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
2164 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2168 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2169 "-------------------------------------------------------------------");
2172 #else /* !GST_DISABLE_GST_DEBUG */
2173 #ifndef GST_REMOVE_DISABLED
2176 _gst_debug_category_new (const gchar * name, guint color,
2177 const gchar * description)
2183 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2187 /* This function MUST NOT return NULL */
2189 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2195 _priv_gst_debug_cleanup (void)
2200 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2201 const gchar * file, const gchar * function, gint line,
2202 GObject * object, const gchar * format, ...)
2207 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2208 const gchar * file, const gchar * function, gint line,
2209 GObject * object, const gchar * format, va_list args)
2214 gst_debug_message_get (GstDebugMessage * message)
2220 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2221 const gchar * file, const gchar * function, gint line,
2222 GObject * object, GstDebugMessage * message, gpointer unused)
2227 gst_debug_level_get_name (GstDebugLevel level)
2233 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2234 GDestroyNotify notify)
2239 gst_debug_remove_log_function (GstLogFunction func)
2245 gst_debug_remove_log_function_by_data (gpointer data)
2251 gst_debug_set_active (gboolean active)
2256 gst_debug_is_active (void)
2262 gst_debug_set_colored (gboolean colored)
2267 gst_debug_set_color_mode (GstDebugColorMode mode)
2272 gst_debug_set_color_mode_from_string (const gchar * str)
2277 gst_debug_is_colored (void)
2283 gst_debug_get_color_mode (void)
2285 return GST_DEBUG_COLOR_MODE_OFF;
2289 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2294 gst_debug_set_default_threshold (GstDebugLevel level)
2299 gst_debug_get_default_threshold (void)
2301 return GST_LEVEL_NONE;
2305 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2310 gst_debug_unset_threshold_for_name (const gchar * name)
2315 gst_debug_category_free (GstDebugCategory * category)
2320 gst_debug_category_set_threshold (GstDebugCategory * category,
2321 GstDebugLevel level)
2326 gst_debug_category_reset_threshold (GstDebugCategory * category)
2331 gst_debug_category_get_threshold (GstDebugCategory * category)
2333 return GST_LEVEL_NONE;
2337 gst_debug_category_get_name (GstDebugCategory * category)
2343 gst_debug_category_get_color (GstDebugCategory * category)
2349 gst_debug_category_get_description (GstDebugCategory * category)
2355 gst_debug_get_all_categories (void)
2361 _gst_debug_get_category (const gchar * name)
2367 gst_debug_construct_term_color (guint colorinfo)
2369 return g_strdup ("00");
2373 gst_debug_construct_win_color (guint colorinfo)
2379 _priv_gst_in_valgrind (void)
2385 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2386 const gchar * func, gint line, GObject * obj, const gchar * msg,
2387 const guint8 * data, guint length)
2390 #endif /* GST_REMOVE_DISABLED */
2391 #endif /* GST_DISABLE_GST_DEBUG */
2393 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2394 * fallback function that cleans up the format string and replaces all pointer
2395 * extension formats with plain %p. */
2396 #ifdef GST_DISABLE_GST_DEBUG
2398 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2400 gchar *clean_format, *c;
2406 clean_format = g_strdup (format);
2408 while ((c = strstr (c, "%p\a"))) {
2409 if (c[3] < 'A' || c[3] > 'Z') {
2413 len = strlen (c + 4);
2414 memmove (c + 2, c + 4, len + 1);
2417 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2419 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2422 len = g_vasprintf (result, clean_format, args);
2424 g_free (clean_format);
2426 if (*result == NULL)
2434 * gst_info_vasprintf:
2435 * @result: (out): the resulting string
2436 * @format: a printf style format string
2437 * @args: the va_list of printf arguments for @format
2439 * Allocates and fills a string large enough (including the terminating null
2440 * byte) to hold the specified printf style @format and @args.
2442 * This function deals with the GStreamer specific printf specifiers
2443 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2444 * in your @format string, you do not need to use this function and can use
2445 * alternatives such as g_vasprintf().
2447 * Free @result with g_free().
2449 * Returns: the length of the string allocated into @result or -1 on any error
2454 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2456 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2457 * gst_private.h if the debug system is disabled which will remove the gst
2458 * specific printf format specifiers */
2459 return __gst_vasprintf (result, format, args);
2463 * gst_info_strdup_vprintf:
2464 * @format: a printf style format string
2465 * @args: the va_list of printf arguments for @format
2467 * Allocates, fills and returns a null terminated string from the printf style
2468 * @format string and @args.
2470 * See gst_info_vasprintf() for when this function is required.
2472 * Free with g_free().
2474 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2479 gst_info_strdup_vprintf (const gchar * format, va_list args)
2483 if (gst_info_vasprintf (&ret, format, args) < 0)
2490 * gst_info_strdup_printf:
2491 * @format: a printf style format string
2492 * @...: the printf arguments for @format
2494 * Allocates, fills and returns a 0-terminated string from the printf style
2495 * @format string and corresponding arguments.
2497 * See gst_info_vasprintf() for when this function is required.
2499 * Free with g_free().
2501 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2506 gst_info_strdup_printf (const gchar * format, ...)
2511 va_start (args, format);
2512 ret = gst_info_strdup_vprintf (format, args);
2520 * @format: a printf style format string
2521 * @...: the printf arguments for @format
2523 * Outputs a formatted message via the GLib print handler. The default print
2524 * handler simply outputs the message to stdout.
2526 * This function will not append a new-line character at the end, unlike
2527 * gst_println() which will.
2529 * All strings must be in ASCII or UTF-8 encoding.
2531 * This function differs from g_print() in that it supports all the additional
2532 * printf specifiers that are supported by GStreamer's debug logging system,
2533 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2535 * This function is primarily for printing debug output.
2540 gst_print (const gchar * format, ...)
2545 va_start (args, format);
2546 str = gst_info_strdup_vprintf (format, args);
2550 G_LOCK (win_print_mutex);
2553 g_print ("%s", str);
2556 G_UNLOCK (win_print_mutex);
2563 * @format: a printf style format string
2564 * @...: the printf arguments for @format
2566 * Outputs a formatted message via the GLib print handler. The default print
2567 * handler simply outputs the message to stdout.
2569 * This function will append a new-line character at the end, unlike
2570 * gst_print() which will not.
2572 * All strings must be in ASCII or UTF-8 encoding.
2574 * This function differs from g_print() in that it supports all the additional
2575 * printf specifiers that are supported by GStreamer's debug logging system,
2576 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2578 * This function is primarily for printing debug output.
2583 gst_println (const gchar * format, ...)
2588 va_start (args, format);
2589 str = gst_info_strdup_vprintf (format, args);
2593 G_LOCK (win_print_mutex);
2596 g_print ("%s\n", str);
2599 G_UNLOCK (win_print_mutex);
2606 * @format: a printf style format string
2607 * @...: the printf arguments for @format
2609 * Outputs a formatted message via the GLib error message handler. The default
2610 * handler simply outputs the message to stderr.
2612 * This function will not append a new-line character at the end, unlike
2613 * gst_printerrln() which will.
2615 * All strings must be in ASCII or UTF-8 encoding.
2617 * This function differs from g_printerr() in that it supports the additional
2618 * printf specifiers that are supported by GStreamer's debug logging system,
2619 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2621 * This function is primarily for printing debug output.
2626 gst_printerr (const gchar * format, ...)
2631 va_start (args, format);
2632 str = gst_info_strdup_vprintf (format, args);
2636 G_LOCK (win_print_mutex);
2639 g_printerr ("%s", str);
2642 G_UNLOCK (win_print_mutex);
2649 * @format: a printf style format string
2650 * @...: the printf arguments for @format
2652 * Outputs a formatted message via the GLib error message handler. The default
2653 * handler simply outputs the message to stderr.
2655 * This function will append a new-line character at the end, unlike
2656 * gst_printerr() which will not.
2658 * All strings must be in ASCII or UTF-8 encoding.
2660 * This function differs from g_printerr() in that it supports the additional
2661 * printf specifiers that are supported by GStreamer's debug logging system,
2662 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2664 * This function is primarily for printing debug output.
2669 gst_printerrln (const gchar * format, ...)
2674 va_start (args, format);
2675 str = gst_info_strdup_vprintf (format, args);
2679 G_LOCK (win_print_mutex);
2682 g_printerr ("%s\n", str);
2685 G_UNLOCK (win_print_mutex);
2693 append_debug_info (GString * trace, Dwfl * dwfl, const void *ip)
2697 Dwfl_Module *module;
2698 const gchar *function_name;
2700 if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
2703 if (dwfl_report_end (dwfl, NULL, NULL))
2706 addr = (uintptr_t) ip;
2707 module = dwfl_addrmodule (dwfl, addr);
2708 function_name = dwfl_module_addrname (module, addr);
2710 g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
2712 line = dwfl_getsrc (dwfl, addr);
2716 const gchar *filename = dwfl_lineinfo (line, &addr,
2717 &nline, NULL, NULL, NULL);
2719 g_string_append_printf (trace, "%s:%d", strrchr (filename,
2720 G_DIR_SEPARATOR) + 1, nline);
2722 const gchar *eflfile = NULL;
2724 dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
2725 g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
2730 #endif /* HAVE_DW */
2733 generate_unwind_trace (GstStackTraceFlags flags)
2737 unw_cursor_t cursor;
2738 gboolean use_libunwind = TRUE;
2739 GString *trace = g_string_new (NULL);
2743 Dwfl_Callbacks callbacks = {
2744 .find_elf = dwfl_linux_proc_find_elf,
2745 .find_debuginfo = dwfl_standard_find_debuginfo,
2748 if ((flags & GST_STACK_TRACE_SHOW_FULL))
2749 dwfl = dwfl_begin (&callbacks);
2750 #endif /* HAVE_DW */
2752 unret = unw_getcontext (&uc);
2754 GST_DEBUG ("Could not get libunwind context (%d)", unret);
2758 unret = unw_init_local (&cursor, &uc);
2760 GST_DEBUG ("Could not init libunwind context (%d)", unret);
2765 while (unw_step (&cursor) > 0) {
2770 unret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
2772 GST_DEBUG ("libunwind could read frame info (%d)", unret);
2777 if (append_debug_info (trace, dwfl, (void *) (ip - 4))) {
2778 use_libunwind = FALSE;
2779 g_string_append (trace, ")\n");
2782 #endif /* HAVE_DW */
2784 if (use_libunwind) {
2787 unw_word_t offset = 0;
2788 unw_get_proc_name (&cursor, name, sizeof (name), &offset);
2789 g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
2800 return g_string_free (trace, FALSE);
2803 #endif /* HAVE_UNWIND */
2805 #ifdef HAVE_BACKTRACE
2807 generate_backtrace_trace (void)
2810 void *buffer[BT_BUF_SIZE];
2814 trace = g_string_new (NULL);
2815 nptrs = backtrace (buffer, BT_BUF_SIZE);
2817 strings = backtrace_symbols (buffer, nptrs);
2822 for (j = 0; j < nptrs; j++)
2823 g_string_append_printf (trace, "%s\n", strings[j]);
2827 return g_string_free (trace, FALSE);
2830 #define generate_backtrace_trace() NULL
2831 #endif /* HAVE_BACKTRACE */
2835 dbghelp_initialize_symbols (HANDLE process)
2837 static gsize initialization_value = 0;
2839 if (g_once_init_enter (&initialization_value)) {
2840 GST_INFO ("Initializing Windows symbol handler");
2841 SymSetOptions (SYMOPT_LOAD_LINES);
2842 SymInitialize (process, NULL, TRUE);
2843 GST_INFO ("Initialized Windows symbol handler");
2845 g_once_init_leave (&initialization_value, 1);
2850 generate_dbghelp_trace (void)
2852 HANDLE process = GetCurrentProcess ();
2853 HANDLE thread = GetCurrentThread ();
2854 IMAGEHLP_MODULE64 module_info;
2857 STACKFRAME64 frame = { 0 };
2859 GString *trace = g_string_new (NULL);
2861 dbghelp_initialize_symbols (process);
2863 memset (&context, 0, sizeof (CONTEXT));
2864 context.ContextFlags = CONTEXT_FULL;
2866 RtlCaptureContext (&context);
2868 frame.AddrPC.Mode = AddrModeFlat;
2869 frame.AddrStack.Mode = AddrModeFlat;
2870 frame.AddrFrame.Mode = AddrModeFlat;
2872 #if (defined _M_IX86)
2873 machine = IMAGE_FILE_MACHINE_I386;
2874 frame.AddrFrame.Offset = context.Ebp;
2875 frame.AddrPC.Offset = context.Eip;
2876 frame.AddrStack.Offset = context.Esp;
2877 #elif (defined _M_X64)
2878 machine = IMAGE_FILE_MACHINE_AMD64;
2879 frame.AddrFrame.Offset = context.Rbp;
2880 frame.AddrPC.Offset = context.Rip;
2881 frame.AddrStack.Offset = context.Rsp;
2886 module_info.SizeOfStruct = sizeof (module_info);
2887 save_context = (machine == IMAGE_FILE_MACHINE_I386) ? NULL : &context;
2890 char buffer[sizeof (SYMBOL_INFO) + MAX_SYM_NAME * sizeof (TCHAR)];
2891 PSYMBOL_INFO symbol = (PSYMBOL_INFO) buffer;
2892 IMAGEHLP_LINE64 line;
2893 DWORD displacement = 0;
2895 symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
2896 symbol->MaxNameLen = MAX_SYM_NAME;
2898 line.SizeOfStruct = sizeof (line);
2900 if (!StackWalk64 (machine, process, thread, &frame, save_context, 0,
2901 SymFunctionTableAccess64, SymGetModuleBase64, 0))
2904 if (SymFromAddr (process, frame.AddrPC.Offset, 0, symbol))
2905 g_string_append_printf (trace, "%s ", symbol->Name);
2907 g_string_append (trace, "?? ");
2909 if (SymGetLineFromAddr64 (process, frame.AddrPC.Offset, &displacement,
2911 g_string_append_printf (trace, "(%s:%u)", line.FileName, line.LineNumber);
2912 else if (SymGetModuleInfo64 (process, frame.AddrPC.Offset, &module_info))
2913 g_string_append_printf (trace, "(%s)", module_info.ImageName);
2915 g_string_append_printf (trace, "(%s)", "??");
2917 g_string_append (trace, "\n");
2921 return g_string_free (trace, FALSE);
2923 #endif /* HAVE_DBGHELP */
2926 * gst_debug_get_stack_trace:
2927 * @flags: A set of #GstStackTraceFlags to determine how the stack
2928 * trace should look like. Pass 0 to retrieve a minimal backtrace.
2930 * Returns: (nullable): a stack trace, if libunwind or glibc backtrace are
2931 * present, else %NULL.
2936 gst_debug_get_stack_trace (GstStackTraceFlags flags)
2938 gchar *trace = NULL;
2939 #ifdef HAVE_BACKTRACE
2940 gboolean have_backtrace = TRUE;
2942 gboolean have_backtrace = FALSE;
2946 if ((flags & GST_STACK_TRACE_SHOW_FULL) || !have_backtrace)
2947 trace = generate_unwind_trace (flags);
2948 #endif /* HAVE_UNWIND */
2951 trace = generate_dbghelp_trace ();
2956 else if (have_backtrace)
2957 return generate_backtrace_trace ();
2963 * gst_debug_print_stack_trace:
2965 * If libunwind, glibc backtrace or DbgHelp are present
2966 * a stack trace is printed.
2969 gst_debug_print_stack_trace (void)
2971 gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
2975 G_LOCK (win_print_mutex);
2978 g_print ("%s\n", trace);
2981 G_UNLOCK (win_print_mutex);
2988 #ifndef GST_DISABLE_GST_DEBUG
2991 guint max_size_per_thread;
2992 guint thread_timeout;
2994 GHashTable *thread_index;
2995 } GstRingBufferLogger;
3007 G_LOCK_DEFINE_STATIC (ring_buffer_logger);
3008 static GstRingBufferLogger *ring_buffer_logger = NULL;
3011 gst_ring_buffer_logger_log (GstDebugCategory * category,
3012 GstDebugLevel level,
3014 const gchar * function,
3015 gint line, GObject * object, GstDebugMessage * message, gpointer user_data)
3017 GstRingBufferLogger *logger = user_data;
3020 GstClockTime elapsed;
3025 GstRingBufferLog *log;
3026 gint64 now = g_get_monotonic_time ();
3027 const gchar *message_str = gst_debug_message_get (message);
3029 /* __FILE__ might be a file name or an absolute path or a
3030 * relative path, irrespective of the exact compiler used,
3031 * in which case we want to shorten it to the filename for
3034 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
3035 file = gst_path_basename (file);
3039 obj = gst_debug_print_object (object);
3044 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
3046 thread = g_thread_self ();
3048 /* no color, all platforms */
3049 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
3051 g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
3052 pid, thread, gst_debug_level_get_name (level),
3053 gst_debug_category_get_name (category), file, line, function, obj,
3057 output_len = strlen (output);
3062 G_LOCK (ring_buffer_logger);
3064 if (logger->thread_timeout > 0) {
3067 /* Remove all threads that saw no output since thread_timeout seconds.
3068 * By construction these are all at the tail of the queue, and the queue
3069 * is ordered by last use, so we just need to look at the tail.
3071 while (logger->threads.tail) {
3072 log = logger->threads.tail->data;
3073 if (log->last_use + logger->thread_timeout * G_USEC_PER_SEC >= now)
3076 g_hash_table_remove (logger->thread_index, log->thread);
3077 while ((buf = g_queue_pop_head (&log->log)))
3080 g_queue_pop_tail (&logger->threads);
3084 /* Get logger for this thread, and put it back at the
3085 * head of the threads queue */
3086 log = g_hash_table_lookup (logger->thread_index, thread);
3088 log = g_new0 (GstRingBufferLog, 1);
3089 g_queue_init (&log->log);
3091 g_queue_push_head (&logger->threads, log);
3092 log->link = logger->threads.head;
3093 log->thread = thread;
3094 g_hash_table_insert (logger->thread_index, thread, log);
3096 g_queue_unlink (&logger->threads, log->link);
3097 g_queue_push_head_link (&logger->threads, log->link);
3099 log->last_use = now;
3101 if (output_len < logger->max_size_per_thread) {
3104 /* While using a GQueue here is not the most efficient thing to do, we
3105 * have to allocate a string for every output anyway and could just store
3106 * that instead of copying it to an actual ringbuffer.
3107 * Better than GQueue would be GstQueueArray, but that one is in
3108 * libgstbase and we can't use it here. That one allocation will not make
3109 * much of a difference anymore, considering the number of allocations
3110 * needed to get to this point...
3112 while (log->log_size + output_len > logger->max_size_per_thread) {
3113 buf = g_queue_pop_head (&log->log);
3114 log->log_size -= strlen (buf);
3117 g_queue_push_tail (&log->log, output);
3118 log->log_size += output_len;
3122 /* Can't really write anything as the line is bigger than the maximum
3123 * allowed log size already, so just remove everything */
3125 while ((buf = g_queue_pop_head (&log->log)))
3131 G_UNLOCK (ring_buffer_logger);
3135 * gst_debug_ring_buffer_logger_get_logs:
3137 * Fetches the current logs per thread from the ring buffer logger. See
3138 * gst_debug_add_ring_buffer_logger() for details.
3140 * Returns: (transfer full) (array zero-terminated): NULL-terminated array of
3141 * strings with the debug output per thread
3146 gst_debug_ring_buffer_logger_get_logs (void)
3148 gchar **logs, **tmp;
3151 g_return_val_if_fail (ring_buffer_logger != NULL, NULL);
3153 G_LOCK (ring_buffer_logger);
3155 tmp = logs = g_new0 (gchar *, ring_buffer_logger->threads.length + 1);
3156 for (l = ring_buffer_logger->threads.head; l; l = l->next) {
3157 GstRingBufferLog *log = l->data;
3162 *tmp = p = g_new0 (gchar, log->log_size + 1);
3164 for (l = log->log.head; l; l = l->next) {
3165 len = strlen (l->data);
3166 memcpy (p, l->data, len);
3173 G_UNLOCK (ring_buffer_logger);
3179 gst_ring_buffer_logger_free (GstRingBufferLogger * logger)
3181 G_LOCK (ring_buffer_logger);
3182 if (ring_buffer_logger == logger) {
3183 GstRingBufferLog *log;
3185 while ((log = g_queue_pop_head (&logger->threads))) {
3187 while ((buf = g_queue_pop_head (&log->log)))
3192 g_hash_table_unref (logger->thread_index);
3195 ring_buffer_logger = NULL;
3197 G_UNLOCK (ring_buffer_logger);
3201 * gst_debug_add_ring_buffer_logger:
3202 * @max_size_per_thread: Maximum size of log per thread in bytes
3203 * @thread_timeout: Timeout for threads in seconds
3205 * Adds a memory ringbuffer based debug logger that stores up to
3206 * @max_size_per_thread bytes of logs per thread and times out threads after
3207 * @thread_timeout seconds of inactivity.
3209 * Logs can be fetched with gst_debug_ring_buffer_logger_get_logs() and the
3210 * logger can be removed again with gst_debug_remove_ring_buffer_logger().
3211 * Only one logger at a time is possible.
3216 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3217 guint thread_timeout)
3219 GstRingBufferLogger *logger;
3221 G_LOCK (ring_buffer_logger);
3223 if (ring_buffer_logger) {
3224 g_warn_if_reached ();
3225 G_UNLOCK (ring_buffer_logger);
3229 logger = ring_buffer_logger = g_new0 (GstRingBufferLogger, 1);
3231 logger->max_size_per_thread = max_size_per_thread;
3232 logger->thread_timeout = thread_timeout;
3233 logger->thread_index = g_hash_table_new (g_direct_hash, g_direct_equal);
3234 g_queue_init (&logger->threads);
3236 gst_debug_add_log_function (gst_ring_buffer_logger_log, logger,
3237 (GDestroyNotify) gst_ring_buffer_logger_free);
3238 G_UNLOCK (ring_buffer_logger);
3242 * gst_debug_remove_ring_buffer_logger:
3244 * Removes any previously added ring buffer logger with
3245 * gst_debug_add_ring_buffer_logger().
3250 gst_debug_remove_ring_buffer_logger (void)
3252 gst_debug_remove_log_function (gst_ring_buffer_logger_log);
3255 #else /* GST_DISABLE_GST_DEBUG */
3256 #ifndef GST_REMOVE_DISABLED
3259 gst_debug_ring_buffer_logger_get_logs (void)
3265 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3266 guint thread_timeout)
3271 gst_debug_remove_ring_buffer_logger (void)
3275 #endif /* GST_REMOVE_DISABLED */
3276 #endif /* GST_DISABLE_GST_DEBUG */