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 */
158 extern gboolean gst_is_initialized (void);
160 /* we want these symbols exported even if debug is disabled, to maintain
161 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
162 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
164 /* disabled by default, as soon as some threshold is set > NONE,
165 * it becomes enabled. */
166 gboolean _gst_debug_enabled = FALSE;
167 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
169 GstDebugCategory *GST_CAT_DEFAULT = NULL;
171 GstDebugCategory *GST_CAT_GST_INIT = NULL;
172 GstDebugCategory *GST_CAT_MEMORY = NULL;
173 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
174 GstDebugCategory *GST_CAT_STATES = NULL;
175 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
177 GstDebugCategory *GST_CAT_BUFFER = NULL;
178 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
179 GstDebugCategory *GST_CAT_BUS = NULL;
180 GstDebugCategory *GST_CAT_CAPS = NULL;
181 GstDebugCategory *GST_CAT_CLOCK = NULL;
182 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
183 GstDebugCategory *GST_CAT_PADS = NULL;
184 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
185 GstDebugCategory *GST_CAT_PIPELINE = NULL;
186 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
187 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
188 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
189 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
190 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
191 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
192 GstDebugCategory *GST_CAT_EVENT = NULL;
193 GstDebugCategory *GST_CAT_MESSAGE = NULL;
194 GstDebugCategory *GST_CAT_PARAMS = NULL;
195 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
196 GstDebugCategory *GST_CAT_SIGNAL = NULL;
197 GstDebugCategory *GST_CAT_PROBE = NULL;
198 GstDebugCategory *GST_CAT_REGISTRY = NULL;
199 GstDebugCategory *GST_CAT_QOS = NULL;
200 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
201 GstDebugCategory *GST_CAT_META = NULL;
202 GstDebugCategory *GST_CAT_LOCKING = NULL;
203 GstDebugCategory *GST_CAT_CONTEXT = NULL;
204 GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL;
207 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
209 #ifndef GST_DISABLE_GST_DEBUG
211 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
212 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
216 #include <rld_interface.h>
217 typedef struct DL_INFO
219 const char *dli_fname;
221 const char *dli_sname;
225 long dli_reserved[4];
229 #define _RLD_DLADDR 14
230 int dladdr (void *address, Dl_info * dl);
233 dladdr (void *address, Dl_info * dl)
237 v = _rld_new_interface (_RLD_DLADDR, address, dl);
243 static const gchar *_gst_debug_filter = NULL;
245 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
246 static void gst_debug_reset_all_thresholds (void);
248 struct _GstDebugMessage
255 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
256 static GMutex __level_name_mutex;
257 static GSList *__level_name = NULL;
265 /* list of all categories */
266 static GMutex __cat_mutex;
267 static GSList *__categories = NULL;
269 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
272 /* all registered debug handlers */
277 GDestroyNotify notify;
280 static GMutex __log_func_mutex;
281 static GSList *__log_functions = NULL;
283 /* whether to add the default log function in gst_init() */
284 static gboolean add_default_log_func = TRUE;
286 #define PRETTY_TAGS_DEFAULT TRUE
287 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
289 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
290 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
292 /* FIXME: export this? */
294 _priv_gst_in_valgrind (void)
302 in_valgrind = GST_VG_UNCHECKED;
304 if (in_valgrind == GST_VG_UNCHECKED) {
305 #ifdef HAVE_VALGRIND_VALGRIND_H
306 if (RUNNING_ON_VALGRIND) {
307 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
308 in_valgrind = GST_VG_INSIDE;
310 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
311 in_valgrind = GST_VG_NO_VALGRIND;
314 in_valgrind = GST_VG_NO_VALGRIND;
316 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
317 in_valgrind == GST_VG_INSIDE);
319 return (in_valgrind == GST_VG_INSIDE);
323 _replace_pattern_in_gst_debug_file_name (gchar * name, const char *token,
327 if ((token_start = strstr (name, token))) {
328 gsize token_len = strlen (token);
329 gchar *name_prefix = name;
330 gchar *name_suffix = token_start + token_len;
331 token_start[0] = '\0';
332 name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
333 g_free (name_prefix);
339 _priv_gst_debug_file_name (const gchar * env)
343 name = g_strdup (env);
344 name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
345 name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
350 /* Initialize the debugging system */
352 _priv_gst_debug_init (void)
357 if (add_default_log_func) {
358 env = g_getenv ("GST_DEBUG_FILE");
359 if (env != NULL && *env != '\0') {
360 if (strcmp (env, "-") == 0) {
363 gchar *name = _priv_gst_debug_file_name (env);
364 log_file = g_fopen (name, "w");
366 if (log_file == NULL) {
367 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
376 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
379 __gst_printf_pointer_extension_set_func
380 (gst_info_printf_pointer_extension_func);
382 /* do NOT use a single debug function before this line has been run */
383 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
384 GST_DEBUG_UNDERLINE, NULL);
385 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
386 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
388 /* FIXME: add descriptions here */
389 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
391 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
392 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
393 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
394 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
395 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
396 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
397 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
398 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
399 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
400 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
401 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
402 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
403 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
404 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
405 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
406 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
407 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
408 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
410 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
412 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
413 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
414 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
415 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
416 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
417 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
418 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
419 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
420 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
421 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
422 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
423 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
424 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
425 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
426 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
427 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
429 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
430 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
431 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
432 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
433 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
434 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
435 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
436 GST_DEBUG_BOLD, NULL);
437 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
438 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
439 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
440 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
441 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
442 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
443 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
444 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
445 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
446 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
447 _priv_GST_CAT_PROTECTION =
448 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
450 /* print out the valgrind message if we're in valgrind */
451 _priv_gst_in_valgrind ();
453 env = g_getenv ("GST_DEBUG_OPTIONS");
455 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
457 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
461 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
462 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
463 env = g_getenv ("GST_DEBUG_COLOR_MODE");
465 gst_debug_set_color_mode_from_string (env);
467 _gst_debug_filter = g_getenv ("GST_DEBUG");
468 if (_gst_debug_filter) {
469 gst_debug_set_threshold_from_string (_gst_debug_filter, FALSE);
473 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
474 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
478 * @category: category to log
479 * @level: level of the message is in
480 * @file: the file that emitted the message, usually the __FILE__ identifier
481 * @function: the function that emitted the message
482 * @line: the line from that the message was emitted, usually __LINE__
483 * @object: (transfer none) (allow-none): the object this message relates to,
485 * @format: a printf style format string
486 * @...: optional arguments for the format
488 * Logs the given message using the currently registered debugging handlers.
491 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
492 const gchar * file, const gchar * function, gint line,
493 GObject * object, const gchar * format, ...)
497 va_start (var_args, format);
498 gst_debug_log_valist (category, level, file, function, line, object, format,
503 /* based on g_basename(), which we can't use because it was deprecated */
504 static inline const gchar *
505 gst_path_basename (const gchar * file_name)
507 register const gchar *base;
509 base = strrchr (file_name, G_DIR_SEPARATOR);
512 const gchar *q = strrchr (file_name, '/');
513 if (base == NULL || (q != NULL && q > base))
520 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
521 return file_name + 2;
527 * gst_debug_log_valist:
528 * @category: category to log
529 * @level: level of the message is in
530 * @file: the file that emitted the message, usually the __FILE__ identifier
531 * @function: the function that emitted the message
532 * @line: the line from that the message was emitted, usually __LINE__
533 * @object: (transfer none) (allow-none): the object this message relates to,
535 * @format: a printf style format string
536 * @args: optional arguments for the format
538 * Logs the given message using the currently registered debugging handlers.
541 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
542 const gchar * file, const gchar * function, gint line,
543 GObject * object, const gchar * format, va_list args)
545 GstDebugMessage message;
549 g_return_if_fail (category != NULL);
551 if (level > gst_debug_category_get_threshold (category))
554 g_return_if_fail (file != NULL);
555 g_return_if_fail (function != NULL);
556 g_return_if_fail (format != NULL);
558 message.message = NULL;
559 message.format = format;
560 G_VA_COPY (message.arguments, args);
562 handler = __log_functions;
564 entry = handler->data;
565 handler = g_slist_next (handler);
566 entry->func (category, level, file, function, line, object, &message,
569 g_free (message.message);
570 va_end (message.arguments);
574 * gst_debug_message_get:
575 * @message: a debug message
577 * Gets the string representation of a #GstDebugMessage. This function is used
578 * in debug handlers to extract the message.
580 * Returns: the string representation of a #GstDebugMessage.
583 gst_debug_message_get (GstDebugMessage * message)
585 if (message->message == NULL) {
588 len = __gst_vasprintf (&message->message, message->format,
592 message->message = NULL;
594 return message->message;
597 #define MAX_BUFFER_DUMP_STRING_LEN 100
599 /* structure_to_pretty_string:
600 * @str: a serialized #GstStructure
602 * If the serialized structure contains large buffers such as images the hex
603 * representation of those buffers will be shortened so that the string remains
606 * Returns: the filtered string
609 prettify_structure_string (gchar * str)
611 gchar *pos = str, *end;
613 while ((pos = strstr (pos, "(buffer)"))) {
616 pos += strlen ("(buffer)");
617 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
619 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
620 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
621 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
622 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
623 strlen (pos + count) + 1);
624 pos += MAX_BUFFER_DUMP_STRING_LEN;
631 static inline gchar *
632 gst_info_structure_to_string (const GstStructure * s)
635 gchar *str = gst_structure_to_string (s);
636 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
637 return prettify_structure_string (str);
644 static inline gchar *
645 gst_info_describe_buffer (GstBuffer * buffer)
647 const gchar *offset_str = "none";
648 const gchar *offset_end_str = "none";
649 gchar offset_buf[32], offset_end_buf[32];
651 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
652 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
653 GST_BUFFER_OFFSET (buffer));
654 offset_str = offset_buf;
656 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
657 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
658 GST_BUFFER_OFFSET_END (buffer));
659 offset_end_str = offset_end_buf;
662 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
663 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
664 ", offset %s, offset_end %s, flags 0x%x", buffer,
665 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
666 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
667 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
668 gst_buffer_get_size (buffer), offset_str, offset_end_str,
669 GST_BUFFER_FLAGS (buffer));
672 static inline gchar *
673 gst_info_describe_buffer_list (GstBufferList * list)
675 GstClockTime pts = GST_CLOCK_TIME_NONE;
676 GstClockTime dts = GST_CLOCK_TIME_NONE;
677 gsize total_size = 0;
680 n = gst_buffer_list_length (list);
681 for (i = 0; i < n; ++i) {
682 GstBuffer *buf = gst_buffer_list_get (list, i);
685 pts = GST_BUFFER_PTS (buf);
686 dts = GST_BUFFER_DTS (buf);
689 total_size += gst_buffer_get_size (buf);
692 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
693 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
694 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
697 static inline gchar *
698 gst_info_describe_event (GstEvent * event)
702 s = gst_info_structure_to_string (gst_event_get_structure (event));
703 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
704 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
705 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
711 static inline gchar *
712 gst_info_describe_message (GstMessage * message)
716 s = gst_info_structure_to_string (gst_message_get_structure (message));
717 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
718 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
719 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
720 GST_MESSAGE_SEQNUM (message),
721 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
727 static inline gchar *
728 gst_info_describe_query (GstQuery * query)
732 s = gst_info_structure_to_string (gst_query_get_structure (query));
733 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
734 query, (s ? s : "(NULL)"));
739 static inline gchar *
740 gst_info_describe_stream (GstStream * stream)
742 gchar *ret, *caps_str = NULL, *tags_str = NULL;
746 caps = gst_stream_get_caps (stream);
748 caps_str = gst_caps_to_string (caps);
749 gst_caps_unref (caps);
752 tags = gst_stream_get_tags (stream);
754 tags_str = gst_tag_list_to_string (tags);
755 gst_tag_list_unref (tags);
759 g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]",
760 gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream,
761 gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream),
762 caps_str ? caps_str : "", tags_str ? tags_str : "");
770 static inline gchar *
771 gst_info_describe_stream_collection (GstStreamCollection * collection)
774 GString *streams_str;
777 streams_str = g_string_new ("<");
778 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
779 GstStream *stream = gst_stream_collection_get_stream (collection, i);
782 s = gst_info_describe_stream (stream);
783 g_string_append_printf (streams_str, " %s,", s);
786 g_string_append (streams_str, " >");
788 ret = g_strdup_printf ("collection %p (%d streams) %s", collection,
789 gst_stream_collection_get_size (collection), streams_str->str);
791 g_string_free (streams_str, TRUE);
796 gst_debug_print_object (gpointer ptr)
798 GObject *object = (GObject *) ptr;
801 /* This is a cute trick to detect unmapped memory, but is unportable,
802 * slow, screws around with madvise, and not actually that useful. */
806 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
807 if (ret == -1 && errno == ENOMEM) {
808 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
813 /* nicely printed object */
814 if (object == NULL) {
815 return g_strdup ("(NULL)");
817 if (GST_IS_CAPS (ptr)) {
818 return gst_caps_to_string ((const GstCaps *) ptr);
820 if (GST_IS_STRUCTURE (ptr)) {
821 return gst_info_structure_to_string ((const GstStructure *) ptr);
823 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
824 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
826 if (GST_IS_TAG_LIST (ptr)) {
827 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
828 if (G_UNLIKELY (pretty_tags))
829 return prettify_structure_string (str);
833 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
834 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
836 if (GST_IS_BUFFER (ptr)) {
837 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
839 if (GST_IS_BUFFER_LIST (ptr)) {
840 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
843 if (*(guint32 *) ptr == 0xffffffff) {
844 return g_strdup_printf ("<poisoned@%p>", ptr);
847 if (GST_IS_MESSAGE (object)) {
848 return gst_info_describe_message (GST_MESSAGE_CAST (object));
850 if (GST_IS_QUERY (object)) {
851 return gst_info_describe_query (GST_QUERY_CAST (object));
853 if (GST_IS_EVENT (object)) {
854 return gst_info_describe_event (GST_EVENT_CAST (object));
856 if (GST_IS_CONTEXT (object)) {
857 GstContext *context = GST_CONTEXT_CAST (object);
860 const GstStructure *structure;
862 type = gst_context_get_context_type (context);
863 structure = gst_context_get_structure (context);
865 s = gst_info_structure_to_string (structure);
867 ret = g_strdup_printf ("context '%s'='%s'", type, s);
871 if (GST_IS_STREAM (object)) {
872 return gst_info_describe_stream (GST_STREAM_CAST (object));
874 if (GST_IS_STREAM_COLLECTION (object)) {
876 gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST
879 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
880 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
882 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
883 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
885 if (G_IS_OBJECT (object)) {
886 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
889 return g_strdup_printf ("%p", ptr);
893 gst_debug_print_segment (gpointer ptr)
895 GstSegment *segment = (GstSegment *) ptr;
897 /* nicely printed segment */
898 if (segment == NULL) {
899 return g_strdup ("(NULL)");
902 switch (segment->format) {
903 case GST_FORMAT_UNDEFINED:{
904 return g_strdup_printf ("UNDEFINED segment");
906 case GST_FORMAT_TIME:{
907 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
908 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
909 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
910 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
911 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
912 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
913 segment->rate, segment->applied_rate, (guint) segment->flags,
914 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
915 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
918 const gchar *format_name;
920 format_name = gst_format_get_name (segment->format);
921 if (G_UNLIKELY (format_name == NULL))
922 format_name = "(UNKNOWN FORMAT)";
923 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
924 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
925 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
926 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
927 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
928 segment->offset, segment->stop, segment->rate, segment->applied_rate,
929 (guint) segment->flags, segment->time, segment->base,
930 segment->position, segment->duration);
936 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
940 if (format[0] == 'p' && format[1] == '\a') {
942 case 'A': /* GST_PTR_FORMAT */
943 s = gst_debug_print_object (ptr);
945 case 'B': /* GST_SEGMENT_FORMAT */
946 s = gst_debug_print_segment (ptr);
948 case 'a': /* GST_WRAPPED_PTR_FORMAT */
949 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
952 /* must have been compiled against a newer version with an extension
953 * we don't known about yet - just ignore and fallback to %p below */
958 s = g_strdup_printf ("%p", ptr);
964 * gst_debug_construct_term_color:
965 * @colorinfo: the color info
967 * Constructs a string that can be used for getting the desired color in color
969 * You need to free the string after use.
971 * Returns: (transfer full) (type gchar*): a string containing the color
975 gst_debug_construct_term_color (guint colorinfo)
979 color = g_string_new ("\033[00");
981 if (colorinfo & GST_DEBUG_BOLD) {
982 g_string_append_len (color, ";01", 3);
984 if (colorinfo & GST_DEBUG_UNDERLINE) {
985 g_string_append_len (color, ";04", 3);
987 if (colorinfo & GST_DEBUG_FG_MASK) {
988 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
990 if (colorinfo & GST_DEBUG_BG_MASK) {
991 g_string_append_printf (color, ";4%1d",
992 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
994 g_string_append_c (color, 'm');
996 return g_string_free (color, FALSE);
1000 * gst_debug_construct_win_color:
1001 * @colorinfo: the color info
1003 * Constructs an integer that can be used for getting the desired color in
1004 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
1005 * ignore this attribute.
1007 * This function returns 0 on non-windows machines.
1009 * Returns: an integer containing the color definition
1012 gst_debug_construct_win_color (guint colorinfo)
1016 static const guchar ansi_to_win_fg[8] = {
1018 FOREGROUND_RED, /* red */
1019 FOREGROUND_GREEN, /* green */
1020 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
1021 FOREGROUND_BLUE, /* blue */
1022 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
1023 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
1024 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
1026 static const guchar ansi_to_win_bg[8] = {
1030 BACKGROUND_RED | BACKGROUND_GREEN,
1032 BACKGROUND_RED | BACKGROUND_BLUE,
1033 BACKGROUND_GREEN | FOREGROUND_BLUE,
1034 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
1037 /* we draw black as white, as cmd.exe can only have black bg */
1038 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
1039 color = ansi_to_win_fg[7];
1041 if (colorinfo & GST_DEBUG_UNDERLINE) {
1042 color |= BACKGROUND_INTENSITY;
1044 if (colorinfo & GST_DEBUG_BOLD) {
1045 color |= FOREGROUND_INTENSITY;
1047 if (colorinfo & GST_DEBUG_FG_MASK) {
1048 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
1050 if (colorinfo & GST_DEBUG_BG_MASK) {
1051 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
1057 /* width of %p varies depending on actual value of pointer, which can make
1058 * output unevenly aligned if multiple threads are involved, hence the %14p
1059 * (should really be %18p, but %14p seems a good compromise between too many
1060 * white spaces and likely unalignment on my system) */
1061 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
1062 #define PTR_FMT "%14p"
1064 #define PTR_FMT "%10p"
1066 #define PID_FMT "%5d"
1067 #define CAT_FMT "%20s %s:%d:%s:%s"
1070 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
1071 /* GST_LEVEL_NONE */
1072 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1073 /* GST_LEVEL_ERROR */
1074 FOREGROUND_RED | FOREGROUND_INTENSITY,
1075 /* GST_LEVEL_WARNING */
1076 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1077 /* GST_LEVEL_INFO */
1078 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1079 /* GST_LEVEL_DEBUG */
1080 FOREGROUND_GREEN | FOREGROUND_BLUE,
1082 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1083 /* GST_LEVEL_FIXME */
1084 FOREGROUND_RED | FOREGROUND_GREEN,
1085 /* GST_LEVEL_TRACE */
1086 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1087 /* placeholder for log level 8 */
1089 /* GST_LEVEL_MEMDUMP */
1090 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
1093 static const guchar available_colors[] = {
1094 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
1095 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
1096 FOREGROUND_GREEN | FOREGROUND_BLUE,
1098 #endif /* G_OS_WIN32 */
1099 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
1100 "\033[37m", /* GST_LEVEL_NONE */
1101 "\033[31;01m", /* GST_LEVEL_ERROR */
1102 "\033[33;01m", /* GST_LEVEL_WARNING */
1103 "\033[32;01m", /* GST_LEVEL_INFO */
1104 "\033[36m", /* GST_LEVEL_DEBUG */
1105 "\033[37m", /* GST_LEVEL_LOG */
1106 "\033[33;01m", /* GST_LEVEL_FIXME */
1107 "\033[37m", /* GST_LEVEL_TRACE */
1108 "\033[37m", /* placeholder for log level 8 */
1109 "\033[37m" /* GST_LEVEL_MEMDUMP */
1113 * gst_debug_log_default:
1114 * @category: category to log
1115 * @level: level of the message
1116 * @file: the file that emitted the message, usually the __FILE__ identifier
1117 * @function: the function that emitted the message
1118 * @line: the line from that the message was emitted, usually __LINE__
1119 * @message: the actual message
1120 * @object: (transfer none) (allow-none): the object this message relates to,
1122 * @user_data: the FILE* to log to
1124 * The default logging handler used by GStreamer. Logging functions get called
1125 * whenever a macro like GST_DEBUG or similar is used. By default this function
1126 * is setup to output the message and additional info to stderr (or the log file
1127 * specified via the GST_DEBUG_FILE environment variable) as received via
1130 * You can add other handlers by using gst_debug_add_log_function().
1131 * And you can remove this handler by calling
1132 * gst_debug_remove_log_function(gst_debug_log_default);
1135 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1136 const gchar * file, const gchar * function, gint line,
1137 GObject * object, GstDebugMessage * message, gpointer user_data)
1140 GstClockTime elapsed;
1142 GstDebugColorMode color_mode;
1143 FILE *log_file = user_data ? user_data : stderr;
1146 /* __FILE__ might be a file name or an absolute path or a
1147 * relative path, irrespective of the exact compiler used,
1148 * in which case we want to shorten it to the filename for
1151 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1152 file = gst_path_basename (file);
1156 color_mode = gst_debug_get_color_mode ();
1159 obj = gst_debug_print_object (object);
1164 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1166 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1168 /* We take a lock to keep colors and content together.
1169 * Maybe there is a better way but for now this will do the right
1171 static GMutex win_print_mutex;
1172 g_mutex_lock (&win_print_mutex);
1173 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1175 /* colors, non-windows */
1176 gchar *color = NULL;
1179 const gchar *levelcolor;
1181 color = gst_debug_construct_term_color (gst_debug_category_get_color
1184 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1185 levelcolor = levelcolormap[level];
1187 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1188 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1189 pidcolor, pid, clear, g_thread_self (), levelcolor,
1190 gst_debug_level_get_name (level), clear, color,
1191 gst_debug_category_get_name (category), file, line, function, obj,
1192 clear, gst_debug_message_get (message));
1198 /* colors, windows. */
1199 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1200 #define SET_COLOR(c) G_STMT_START { \
1201 if (log_file == stderr) \
1202 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1205 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1208 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1209 fprintf (log_file, PID_FMT, pid);
1213 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1216 SET_COLOR (levelcolormap_w32[level]);
1217 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1220 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1222 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1223 file, line, function, obj);
1227 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1230 g_mutex_unlock (&win_print_mutex);
1233 /* no color, all platforms */
1234 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1235 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1236 pid, g_thread_self (), gst_debug_level_get_name (level),
1237 gst_debug_category_get_name (category), file, line, function, obj,
1238 gst_debug_message_get (message));
1248 * gst_debug_level_get_name:
1249 * @level: the level to get the name for
1251 * Get the string representation of a debugging level
1256 gst_debug_level_get_name (GstDebugLevel level)
1259 case GST_LEVEL_NONE:
1261 case GST_LEVEL_ERROR:
1263 case GST_LEVEL_WARNING:
1265 case GST_LEVEL_INFO:
1267 case GST_LEVEL_DEBUG:
1271 case GST_LEVEL_FIXME:
1273 case GST_LEVEL_TRACE:
1275 case GST_LEVEL_MEMDUMP:
1278 g_warning ("invalid level specified for gst_debug_level_get_name");
1284 * gst_debug_add_log_function:
1285 * @func: the function to use
1286 * @user_data: user data
1287 * @notify: called when @user_data is not used anymore
1289 * Adds the logging function to the list of logging functions.
1290 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1293 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1294 GDestroyNotify notify)
1296 LogFuncEntry *entry;
1300 func = gst_debug_log_default;
1302 entry = g_slice_new (LogFuncEntry);
1304 entry->user_data = user_data;
1305 entry->notify = notify;
1306 /* FIXME: we leak the old list here - other threads might access it right now
1307 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1308 * but that is waaay costly.
1309 * It'd probably be clever to use some kind of RCU here, but I don't know
1310 * anything about that.
1312 g_mutex_lock (&__log_func_mutex);
1313 list = g_slist_copy (__log_functions);
1314 __log_functions = g_slist_prepend (list, entry);
1315 g_mutex_unlock (&__log_func_mutex);
1317 if (gst_is_initialized ())
1318 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1323 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1325 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1327 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1331 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1333 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1335 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1339 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1342 GSList *new, *cleanup = NULL;
1345 g_mutex_lock (&__log_func_mutex);
1346 new = __log_functions;
1348 while ((found = g_slist_find_custom (new, data, func))) {
1349 if (new == __log_functions) {
1350 /* make a copy when we have the first hit, so that we modify the copy and
1351 * make that the new list later */
1352 new = g_slist_copy (new);
1355 cleanup = g_slist_prepend (cleanup, found->data);
1356 new = g_slist_delete_link (new, found);
1359 /* FIXME: We leak the old list here. See _add_log_function for why. */
1360 __log_functions = new;
1361 g_mutex_unlock (&__log_func_mutex);
1364 LogFuncEntry *entry = cleanup->data;
1367 entry->notify (entry->user_data);
1369 g_slice_free (LogFuncEntry, entry);
1370 cleanup = g_slist_delete_link (cleanup, cleanup);
1376 * gst_debug_remove_log_function:
1377 * @func: (scope call) (allow-none): the log function to remove, or %NULL to
1378 * remove the default log function
1380 * Removes all registered instances of the given logging functions.
1382 * Returns: How many instances of the function were removed
1385 gst_debug_remove_log_function (GstLogFunction func)
1390 func = gst_debug_log_default;
1393 gst_debug_remove_with_compare_func
1394 (gst_debug_compare_log_function_by_func, (gpointer) func);
1396 if (gst_is_initialized ()) {
1397 GST_DEBUG ("removed log function %p %d times from log function list", func,
1400 /* If the default log function is removed before gst_init() was called,
1401 * set a flag so we don't add it in gst_init() later */
1402 if (func == gst_debug_log_default) {
1403 add_default_log_func = FALSE;
1412 * gst_debug_remove_log_function_by_data:
1413 * @data: user data of the log function to remove
1415 * Removes all registered instances of log functions with the given user data.
1417 * Returns: How many instances of the function were removed
1420 gst_debug_remove_log_function_by_data (gpointer data)
1425 gst_debug_remove_with_compare_func
1426 (gst_debug_compare_log_function_by_data, data);
1428 if (gst_is_initialized ())
1430 ("removed %d log functions with user data %p from log function list",
1437 * gst_debug_set_colored:
1438 * @colored: Whether to use colored output or not
1440 * Sets or unsets the use of coloured debugging output.
1441 * Same as gst_debug_set_color_mode () with the argument being
1442 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1444 * This function may be called before gst_init().
1447 gst_debug_set_colored (gboolean colored)
1449 GstDebugColorMode new_mode;
1450 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1451 g_atomic_int_set (&__use_color, (gint) new_mode);
1455 * gst_debug_set_color_mode:
1456 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1458 * Changes the coloring mode for debug output.
1460 * This function may be called before gst_init().
1465 gst_debug_set_color_mode (GstDebugColorMode mode)
1467 g_atomic_int_set (&__use_color, mode);
1471 * gst_debug_set_color_mode_from_string:
1472 * @mode: The coloring mode for debug output. One of the following:
1473 * "on", "auto", "off", "disable", "unix".
1475 * Changes the coloring mode for debug output.
1477 * This function may be called before gst_init().
1482 gst_debug_set_color_mode_from_string (const gchar * mode)
1484 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1485 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1486 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1487 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1488 else if (strcmp (mode, "unix") == 0)
1489 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1493 * gst_debug_is_colored:
1495 * Checks if the debugging output should be colored.
1497 * Returns: %TRUE, if the debug output should be colored.
1500 gst_debug_is_colored (void)
1502 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1503 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1507 * gst_debug_get_color_mode:
1509 * Changes the coloring mode for debug output.
1511 * Returns: see @GstDebugColorMode for possible values.
1516 gst_debug_get_color_mode (void)
1518 return g_atomic_int_get (&__use_color);
1522 * gst_debug_set_active:
1523 * @active: Whether to use debugging output or not
1525 * If activated, debugging messages are sent to the debugging
1527 * It makes sense to deactivate it for speed issues.
1528 * > This function is not threadsafe. It makes sense to only call it
1529 * during initialization.
1532 gst_debug_set_active (gboolean active)
1534 _gst_debug_enabled = active;
1536 _gst_debug_min = GST_LEVEL_COUNT;
1538 _gst_debug_min = GST_LEVEL_NONE;
1542 * gst_debug_is_active:
1544 * Checks if debugging output is activated.
1546 * Returns: %TRUE, if debugging is activated
1549 gst_debug_is_active (void)
1551 return _gst_debug_enabled;
1555 * gst_debug_set_default_threshold:
1556 * @level: level to set
1558 * Sets the default threshold to the given level and updates all categories to
1559 * use this threshold.
1561 * This function may be called before gst_init().
1564 gst_debug_set_default_threshold (GstDebugLevel level)
1566 g_atomic_int_set (&__default_level, level);
1567 gst_debug_reset_all_thresholds ();
1571 * gst_debug_get_default_threshold:
1573 * Returns the default threshold that is used for new categories.
1575 * Returns: the default threshold level
1578 gst_debug_get_default_threshold (void)
1580 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1584 gst_debug_reset_threshold (gpointer category, gpointer unused)
1586 GstDebugCategory *cat = (GstDebugCategory *) category;
1589 g_mutex_lock (&__level_name_mutex);
1590 walk = __level_name;
1592 LevelNameEntry *entry = walk->data;
1594 walk = g_slist_next (walk);
1595 if (g_pattern_match_string (entry->pat, cat->name)) {
1596 if (gst_is_initialized ())
1597 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1598 cat->name, entry->pat, entry->level);
1599 gst_debug_category_set_threshold (cat, entry->level);
1603 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1606 g_mutex_unlock (&__level_name_mutex);
1610 gst_debug_reset_all_thresholds (void)
1612 g_mutex_lock (&__cat_mutex);
1613 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1614 g_mutex_unlock (&__cat_mutex);
1618 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1620 GstDebugCategory *cat = (GstDebugCategory *) data;
1621 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1623 if (g_pattern_match_string (entry->pat, cat->name)) {
1624 if (gst_is_initialized ())
1625 GST_TRACE ("category %s matches pattern %p - gets set to level %d",
1626 cat->name, entry->pat, entry->level);
1627 gst_debug_category_set_threshold (cat, entry->level);
1632 * gst_debug_set_threshold_for_name:
1633 * @name: name of the categories to set
1634 * @level: level to set them to
1636 * Sets all categories which match the given glob style pattern to the given
1640 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1643 LevelNameEntry *entry;
1645 g_return_if_fail (name != NULL);
1647 pat = g_pattern_spec_new (name);
1648 entry = g_slice_new (LevelNameEntry);
1650 entry->level = level;
1651 g_mutex_lock (&__level_name_mutex);
1652 __level_name = g_slist_prepend (__level_name, entry);
1653 g_mutex_unlock (&__level_name_mutex);
1654 g_mutex_lock (&__cat_mutex);
1655 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1656 g_mutex_unlock (&__cat_mutex);
1660 * gst_debug_unset_threshold_for_name:
1661 * @name: name of the categories to set
1663 * Resets all categories with the given name back to the default level.
1666 gst_debug_unset_threshold_for_name (const gchar * name)
1671 g_return_if_fail (name != NULL);
1673 pat = g_pattern_spec_new (name);
1674 g_mutex_lock (&__level_name_mutex);
1675 walk = __level_name;
1676 /* improve this if you want, it's mighty slow */
1678 LevelNameEntry *entry = walk->data;
1680 if (g_pattern_spec_equal (entry->pat, pat)) {
1681 __level_name = g_slist_remove_link (__level_name, walk);
1682 g_pattern_spec_free (entry->pat);
1683 g_slice_free (LevelNameEntry, entry);
1684 g_slist_free_1 (walk);
1685 walk = __level_name;
1687 walk = g_slist_next (walk);
1690 g_mutex_unlock (&__level_name_mutex);
1691 g_pattern_spec_free (pat);
1692 gst_debug_reset_all_thresholds ();
1696 _gst_debug_category_new (const gchar * name, guint color,
1697 const gchar * description)
1699 GstDebugCategory *cat, *catfound;
1701 g_return_val_if_fail (name != NULL, NULL);
1703 cat = g_slice_new (GstDebugCategory);
1704 cat->name = g_strdup (name);
1706 if (description != NULL) {
1707 cat->description = g_strdup (description);
1709 cat->description = g_strdup ("no description");
1711 g_atomic_int_set (&cat->threshold, 0);
1712 gst_debug_reset_threshold (cat, NULL);
1714 /* add to category list */
1715 g_mutex_lock (&__cat_mutex);
1716 catfound = _gst_debug_get_category_locked (name);
1718 g_free ((gpointer) cat->name);
1719 g_free ((gpointer) cat->description);
1720 g_slice_free (GstDebugCategory, cat);
1723 __categories = g_slist_prepend (__categories, cat);
1725 g_mutex_unlock (&__cat_mutex);
1727 /* ensure the filter is applied to categories registered after _debug_init */
1728 if (_gst_debug_filter) {
1729 gst_debug_set_threshold_from_string (_gst_debug_filter, FALSE);
1736 * gst_debug_category_free:
1737 * @category: #GstDebugCategory to free.
1739 * Removes and frees the category and all associated resources.
1742 gst_debug_category_free (GstDebugCategory * category)
1744 if (category == NULL)
1747 /* remove from category list */
1748 g_mutex_lock (&__cat_mutex);
1749 __categories = g_slist_remove (__categories, category);
1750 g_mutex_unlock (&__cat_mutex);
1752 g_free ((gpointer) category->name);
1753 g_free ((gpointer) category->description);
1754 g_slice_free (GstDebugCategory, category);
1758 * gst_debug_category_set_threshold:
1759 * @category: a #GstDebugCategory to set threshold of.
1760 * @level: the #GstDebugLevel threshold to set.
1762 * Sets the threshold of the category to the given level. Debug information will
1763 * only be output if the threshold is lower or equal to the level of the
1764 * debugging message.
1765 * > Do not use this function in production code, because other functions may
1766 * > change the threshold of categories as side effect. It is however a nice
1767 * > function to use when debugging (even from gdb).
1770 gst_debug_category_set_threshold (GstDebugCategory * category,
1771 GstDebugLevel level)
1773 g_return_if_fail (category != NULL);
1775 if (level > _gst_debug_min) {
1776 _gst_debug_enabled = TRUE;
1777 _gst_debug_min = level;
1780 g_atomic_int_set (&category->threshold, level);
1784 * gst_debug_category_reset_threshold:
1785 * @category: a #GstDebugCategory to reset threshold of.
1787 * Resets the threshold of the category to the default level. Debug information
1788 * will only be output if the threshold is lower or equal to the level of the
1789 * debugging message.
1790 * Use this function to set the threshold back to where it was after using
1791 * gst_debug_category_set_threshold().
1794 gst_debug_category_reset_threshold (GstDebugCategory * category)
1796 gst_debug_reset_threshold (category, NULL);
1800 * gst_debug_category_get_threshold:
1801 * @category: a #GstDebugCategory to get threshold of.
1803 * Returns the threshold of a #GstDebugCategory.
1805 * Returns: the #GstDebugLevel that is used as threshold.
1808 gst_debug_category_get_threshold (GstDebugCategory * category)
1810 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1814 * gst_debug_category_get_name:
1815 * @category: a #GstDebugCategory to get name of.
1817 * Returns the name of a debug category.
1819 * Returns: the name of the category.
1822 gst_debug_category_get_name (GstDebugCategory * category)
1824 return category->name;
1828 * gst_debug_category_get_color:
1829 * @category: a #GstDebugCategory to get the color of.
1831 * Returns the color of a debug category used when printing output in this
1834 * Returns: the color of the category.
1837 gst_debug_category_get_color (GstDebugCategory * category)
1839 return category->color;
1843 * gst_debug_category_get_description:
1844 * @category: a #GstDebugCategory to get the description of.
1846 * Returns the description of a debug category.
1848 * Returns: the description of the category.
1851 gst_debug_category_get_description (GstDebugCategory * category)
1853 return category->description;
1857 * gst_debug_get_all_categories:
1859 * Returns a snapshot of a all categories that are currently in use . This list
1860 * may change anytime.
1861 * The caller has to free the list after use.
1863 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1867 gst_debug_get_all_categories (void)
1871 g_mutex_lock (&__cat_mutex);
1872 ret = g_slist_copy (__categories);
1873 g_mutex_unlock (&__cat_mutex);
1878 static GstDebugCategory *
1879 _gst_debug_get_category_locked (const gchar * name)
1881 GstDebugCategory *ret = NULL;
1884 for (node = __categories; node; node = g_slist_next (node)) {
1885 ret = (GstDebugCategory *) node->data;
1886 if (!strcmp (name, ret->name)) {
1894 _gst_debug_get_category (const gchar * name)
1896 GstDebugCategory *ret;
1898 g_mutex_lock (&__cat_mutex);
1899 ret = _gst_debug_get_category_locked (name);
1900 g_mutex_unlock (&__cat_mutex);
1906 parse_debug_category (gchar * str, const gchar ** category)
1911 /* works in place */
1914 if (str[0] != '\0') {
1923 parse_debug_level (gchar * str, GstDebugLevel * level)
1928 /* works in place */
1931 if (g_ascii_isdigit (str[0])) {
1934 l = strtoul (str, &endptr, 10);
1935 if (endptr > str && endptr[0] == 0) {
1936 *level = (GstDebugLevel) l;
1940 } else if (strcmp (str, "ERROR") == 0) {
1941 *level = GST_LEVEL_ERROR;
1942 } else if (strncmp (str, "WARN", 4) == 0) {
1943 *level = GST_LEVEL_WARNING;
1944 } else if (strcmp (str, "FIXME") == 0) {
1945 *level = GST_LEVEL_FIXME;
1946 } else if (strcmp (str, "INFO") == 0) {
1947 *level = GST_LEVEL_INFO;
1948 } else if (strcmp (str, "DEBUG") == 0) {
1949 *level = GST_LEVEL_DEBUG;
1950 } else if (strcmp (str, "LOG") == 0) {
1951 *level = GST_LEVEL_LOG;
1952 } else if (strcmp (str, "TRACE") == 0) {
1953 *level = GST_LEVEL_TRACE;
1954 } else if (strcmp (str, "MEMDUMP") == 0) {
1955 *level = GST_LEVEL_MEMDUMP;
1963 * gst_debug_set_threshold_from_string:
1964 * @list: comma-separated list of "category:level" pairs to be used
1965 * as debug logging levels
1966 * @reset: %TRUE to clear all previously-set debug levels before setting
1968 * %FALSE if adding the threshold described by @list to the one already set.
1970 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1971 * environment variable. You can use wildcards such as '*', but note that
1972 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1973 * everything to log level 2.
1978 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1986 gst_debug_set_default_threshold (0);
1988 split = g_strsplit (list, ",", 0);
1990 for (walk = split; *walk; walk++) {
1991 if (strchr (*walk, ':')) {
1992 gchar **values = g_strsplit (*walk, ":", 2);
1994 if (values[0] && values[1]) {
1995 GstDebugLevel level;
1996 const gchar *category;
1998 if (parse_debug_category (values[0], &category)
1999 && parse_debug_level (values[1], &level)) {
2000 gst_debug_set_threshold_for_name (category, level);
2002 /* bump min-level anyway to allow the category to be registered in the
2004 if (level > _gst_debug_min) {
2005 _gst_debug_min = level;
2010 g_strfreev (values);
2012 GstDebugLevel level;
2014 if (parse_debug_level (*walk, &level))
2015 gst_debug_set_default_threshold (level);
2022 /*** FUNCTION POINTERS ********************************************************/
2024 static GHashTable *__gst_function_pointers; /* NULL */
2025 static GMutex __dbg_functions_mutex;
2027 /* This function MUST NOT return NULL */
2029 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2037 if (G_UNLIKELY (func == NULL))
2040 g_mutex_lock (&__dbg_functions_mutex);
2041 if (G_LIKELY (__gst_function_pointers)) {
2042 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
2043 g_mutex_unlock (&__dbg_functions_mutex);
2044 if (G_LIKELY (ptrname))
2047 g_mutex_unlock (&__dbg_functions_mutex);
2049 /* we need to create an entry in the hash table for this one so we don't leak
2052 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
2053 gchar *name = g_strdup (dl_info.dli_sname);
2055 _gst_debug_register_funcptr (func, name);
2060 gchar *name = g_strdup_printf ("%p", (gpointer) func);
2062 _gst_debug_register_funcptr (func, name);
2068 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2070 gpointer ptr = (gpointer) func;
2072 g_mutex_lock (&__dbg_functions_mutex);
2074 if (!__gst_function_pointers)
2075 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
2076 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
2077 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
2079 g_mutex_unlock (&__dbg_functions_mutex);
2083 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
2084 const guint8 * mem, gsize mem_offset, gsize mem_size)
2086 gchar hexstr[50], ascstr[18], digitstr[4];
2098 while (i < mem_size) {
2099 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
2100 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
2101 g_strlcat (hexstr, digitstr, sizeof (hexstr));
2107 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
2108 (guint) mem_offset, hexstr, ascstr);
2112 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2113 const gchar * func, gint line, GObject * obj, const gchar * msg,
2114 const guint8 * data, guint length)
2118 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2119 "-------------------------------------------------------------------");
2121 if (msg != NULL && *msg != '\0') {
2122 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
2125 while (off < length) {
2128 /* gst_info_dump_mem_line will process 16 bytes at most */
2129 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
2130 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2134 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2135 "-------------------------------------------------------------------");
2138 #else /* !GST_DISABLE_GST_DEBUG */
2139 #ifndef GST_REMOVE_DISABLED
2142 _gst_debug_category_new (const gchar * name, guint color,
2143 const gchar * description)
2149 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2153 /* This function MUST NOT return NULL */
2155 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2161 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2162 const gchar * file, const gchar * function, gint line,
2163 GObject * object, const gchar * format, ...)
2168 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2169 const gchar * file, const gchar * function, gint line,
2170 GObject * object, const gchar * format, va_list args)
2175 gst_debug_message_get (GstDebugMessage * message)
2181 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2182 const gchar * file, const gchar * function, gint line,
2183 GObject * object, GstDebugMessage * message, gpointer unused)
2188 gst_debug_level_get_name (GstDebugLevel level)
2194 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2195 GDestroyNotify notify)
2200 gst_debug_remove_log_function (GstLogFunction func)
2206 gst_debug_remove_log_function_by_data (gpointer data)
2212 gst_debug_set_active (gboolean active)
2217 gst_debug_is_active (void)
2223 gst_debug_set_colored (gboolean colored)
2228 gst_debug_set_color_mode (GstDebugColorMode mode)
2233 gst_debug_set_color_mode_from_string (const gchar * str)
2238 gst_debug_is_colored (void)
2244 gst_debug_get_color_mode (void)
2246 return GST_DEBUG_COLOR_MODE_OFF;
2250 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2255 gst_debug_set_default_threshold (GstDebugLevel level)
2260 gst_debug_get_default_threshold (void)
2262 return GST_LEVEL_NONE;
2266 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2271 gst_debug_unset_threshold_for_name (const gchar * name)
2276 gst_debug_category_free (GstDebugCategory * category)
2281 gst_debug_category_set_threshold (GstDebugCategory * category,
2282 GstDebugLevel level)
2287 gst_debug_category_reset_threshold (GstDebugCategory * category)
2292 gst_debug_category_get_threshold (GstDebugCategory * category)
2294 return GST_LEVEL_NONE;
2298 gst_debug_category_get_name (GstDebugCategory * category)
2304 gst_debug_category_get_color (GstDebugCategory * category)
2310 gst_debug_category_get_description (GstDebugCategory * category)
2316 gst_debug_get_all_categories (void)
2322 _gst_debug_get_category (const gchar * name)
2328 gst_debug_construct_term_color (guint colorinfo)
2330 return g_strdup ("00");
2334 gst_debug_construct_win_color (guint colorinfo)
2340 _priv_gst_in_valgrind (void)
2346 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2347 const gchar * func, gint line, GObject * obj, const gchar * msg,
2348 const guint8 * data, guint length)
2351 #endif /* GST_REMOVE_DISABLED */
2352 #endif /* GST_DISABLE_GST_DEBUG */
2354 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2355 * fallback function that cleans up the format string and replaces all pointer
2356 * extension formats with plain %p. */
2357 #ifdef GST_DISABLE_GST_DEBUG
2359 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2361 gchar *clean_format, *c;
2367 clean_format = g_strdup (format);
2369 while ((c = strstr (c, "%p\a"))) {
2370 if (c[3] < 'A' || c[3] > 'Z') {
2374 len = strlen (c + 4);
2375 memmove (c + 2, c + 4, len + 1);
2378 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2380 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2383 len = g_vasprintf (result, clean_format, args);
2385 g_free (clean_format);
2387 if (*result == NULL)
2395 * gst_info_vasprintf:
2396 * @result: (out): the resulting string
2397 * @format: a printf style format string
2398 * @args: the va_list of printf arguments for @format
2400 * Allocates and fills a string large enough (including the terminating null
2401 * byte) to hold the specified printf style @format and @args.
2403 * This function deals with the GStreamer specific printf specifiers
2404 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2405 * in your @format string, you do not need to use this function and can use
2406 * alternatives such as g_vasprintf().
2408 * Free @result with g_free().
2410 * Returns: the length of the string allocated into @result or -1 on any error
2415 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2417 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2418 * gst_private.h if the debug system is disabled which will remove the gst
2419 * specific printf format specifiers */
2420 return __gst_vasprintf (result, format, args);
2424 * gst_info_strdup_vprintf:
2425 * @format: a printf style format string
2426 * @args: the va_list of printf arguments for @format
2428 * Allocates, fills and returns a null terminated string from the printf style
2429 * @format string and @args.
2431 * See gst_info_vasprintf() for when this function is required.
2433 * Free with g_free().
2435 * Returns: a newly allocated null terminated string or %NULL on any error
2440 gst_info_strdup_vprintf (const gchar * format, va_list args)
2444 if (gst_info_vasprintf (&ret, format, args) < 0)
2451 * gst_info_strdup_printf:
2452 * @format: a printf style format string
2453 * @...: the printf arguments for @format
2455 * Allocates, fills and returns a 0-terminated string from the printf style
2456 * @format string and corresponding arguments.
2458 * See gst_info_vasprintf() for when this function is required.
2460 * Free with g_free().
2462 * Returns: a newly allocated null terminated string or %NULL on any error
2467 gst_info_strdup_printf (const gchar * format, ...)
2472 va_start (args, format);
2473 ret = gst_info_strdup_vprintf (format, args);
2481 * @format: a printf style format string
2482 * @...: the printf arguments for @format
2484 * Outputs a formatted message via the GLib print handler. The default print
2485 * handler simply outputs the message to stdout.
2487 * This function will not append a new-line character at the end, unlike
2488 * gst_println() which will.
2490 * All strings must be in ASCII or UTF-8 encoding.
2492 * This function differs from g_print() in that it supports all the additional
2493 * printf specifiers that are supported by GStreamer's debug logging system,
2494 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2496 * This function is primarily for printing debug output.
2501 gst_print (const gchar * format, ...)
2506 va_start (args, format);
2507 str = gst_info_strdup_vprintf (format, args);
2510 g_print ("%s", str);
2516 * @format: a printf style format string
2517 * @...: the printf arguments for @format
2519 * Outputs a formatted message via the GLib print handler. The default print
2520 * handler simply outputs the message to stdout.
2522 * This function will append a new-line character at the end, unlike
2523 * gst_print() which will not.
2525 * All strings must be in ASCII or UTF-8 encoding.
2527 * This function differs from g_print() in that it supports all the additional
2528 * printf specifiers that are supported by GStreamer's debug logging system,
2529 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2531 * This function is primarily for printing debug output.
2536 gst_println (const gchar * format, ...)
2541 va_start (args, format);
2542 str = gst_info_strdup_vprintf (format, args);
2545 g_print ("%s\n", str);
2551 * @format: a printf style format string
2552 * @...: the printf arguments for @format
2554 * Outputs a formatted message via the GLib error message handler. The default
2555 * handler simply outputs the message to stderr.
2557 * This function will not append a new-line character at the end, unlike
2558 * gst_printerrln() which will.
2560 * All strings must be in ASCII or UTF-8 encoding.
2562 * This function differs from g_printerr() in that it supports the additional
2563 * printf specifiers that are supported by GStreamer's debug logging system,
2564 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2566 * This function is primarily for printing debug output.
2571 gst_printerr (const gchar * format, ...)
2576 va_start (args, format);
2577 str = gst_info_strdup_vprintf (format, args);
2580 g_printerr ("%s", str);
2586 * @format: a printf style format string
2587 * @...: the printf arguments for @format
2589 * Outputs a formatted message via the GLib error message handler. The default
2590 * handler simply outputs the message to stderr.
2592 * This function will append a new-line character at the end, unlike
2593 * gst_printerr() which will not.
2595 * All strings must be in ASCII or UTF-8 encoding.
2597 * This function differs from g_printerr() in that it supports the additional
2598 * printf specifiers that are supported by GStreamer's debug logging system,
2599 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2601 * This function is primarily for printing debug output.
2606 gst_printerrln (const gchar * format, ...)
2611 va_start (args, format);
2612 str = gst_info_strdup_vprintf (format, args);
2615 g_printerr ("%s\n", str);
2622 append_debug_info (GString * trace, Dwfl * dwfl, const void *ip)
2626 Dwfl_Module *module;
2627 const gchar *function_name;
2629 if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
2632 if (dwfl_report_end (dwfl, NULL, NULL))
2635 addr = (uintptr_t) ip;
2636 module = dwfl_addrmodule (dwfl, addr);
2637 function_name = dwfl_module_addrname (module, addr);
2639 g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
2641 line = dwfl_getsrc (dwfl, addr);
2645 const gchar *filename = dwfl_lineinfo (line, &addr,
2646 &nline, NULL, NULL, NULL);
2648 g_string_append_printf (trace, "%s:%d", strrchr (filename,
2649 G_DIR_SEPARATOR) + 1, nline);
2651 const gchar *eflfile = NULL;
2653 dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
2654 g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
2659 #endif /* HAVE_DW */
2662 generate_unwind_trace (GstStackTraceFlags flags)
2666 unw_cursor_t cursor;
2667 gboolean use_libunwind = TRUE;
2668 GString *trace = g_string_new (NULL);
2672 Dwfl_Callbacks callbacks = {
2673 .find_elf = dwfl_linux_proc_find_elf,
2674 .find_debuginfo = dwfl_standard_find_debuginfo,
2677 if ((flags & GST_STACK_TRACE_SHOW_FULL))
2678 dwfl = dwfl_begin (&callbacks);
2679 #endif /* HAVE_DW */
2681 unret = unw_getcontext (&uc);
2683 GST_DEBUG ("Could not get libunwind context (%d)", unret);
2687 unret = unw_init_local (&cursor, &uc);
2689 GST_DEBUG ("Could not init libunwind context (%d)", unret);
2694 while (unw_step (&cursor) > 0) {
2699 unret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
2701 GST_DEBUG ("libunwind could read frame info (%d)", unret);
2706 if (append_debug_info (trace, dwfl, (void *) (ip - 4))) {
2707 use_libunwind = FALSE;
2708 g_string_append (trace, ")\n");
2711 #endif /* HAVE_DW */
2713 if (use_libunwind) {
2716 unw_word_t offset = 0;
2717 unw_get_proc_name (&cursor, name, sizeof (name), &offset);
2718 g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
2729 return g_string_free (trace, FALSE);
2732 #endif /* HAVE_UNWIND */
2734 #ifdef HAVE_BACKTRACE
2736 generate_backtrace_trace (void)
2739 void *buffer[BT_BUF_SIZE];
2743 trace = g_string_new (NULL);
2744 nptrs = backtrace (buffer, BT_BUF_SIZE);
2746 strings = backtrace_symbols (buffer, nptrs);
2751 for (j = 0; j < nptrs; j++)
2752 g_string_append_printf (trace, "%s\n", strings[j]);
2754 return g_string_free (trace, FALSE);
2757 #define generate_backtrace_trace() NULL
2758 #endif /* HAVE_BACKTRACE */
2761 * gst_debug_get_stack_trace:
2762 * @flags: A set of #GstStackTraceFlags to determine how the stack
2763 * trace should look like. Pass 0 to retrieve a minimal backtrace.
2765 * If libunwind or glibc backtrace are present, a stack trace
2771 gst_debug_get_stack_trace (GstStackTraceFlags flags)
2773 gchar *trace = NULL;
2774 #ifdef HAVE_BACKTRACE
2775 gboolean have_backtrace = TRUE;
2777 gboolean have_backtrace = FALSE;
2781 if ((flags & GST_STACK_TRACE_SHOW_FULL) || !have_backtrace)
2782 trace = generate_unwind_trace (flags);
2783 #endif /* HAVE_UNWIND */
2787 else if (have_backtrace)
2788 return generate_backtrace_trace ();
2794 * gst_debug_print_stack_trace:
2796 * If libunwind or glibc backtrace are present
2797 * a stack trace is printed.
2800 gst_debug_print_stack_trace (void)
2802 gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
2805 g_print ("%s\n", trace);
2810 #ifndef GST_DISABLE_GST_DEBUG
2813 guint max_size_per_thread;
2814 guint thread_timeout;
2816 GHashTable *thread_index;
2817 } GstRingBufferLogger;
2829 G_LOCK_DEFINE_STATIC (ring_buffer_logger);
2830 static GstRingBufferLogger *ring_buffer_logger = NULL;
2833 gst_ring_buffer_logger_log (GstDebugCategory * category,
2834 GstDebugLevel level,
2836 const gchar * function,
2837 gint line, GObject * object, GstDebugMessage * message, gpointer user_data)
2839 GstRingBufferLogger *logger = user_data;
2842 GstClockTime elapsed;
2847 GstRingBufferLog *log;
2848 gint64 now = g_get_monotonic_time ();
2849 const gchar *message_str = gst_debug_message_get (message);
2851 G_LOCK (ring_buffer_logger);
2853 if (logger->thread_timeout > 0) {
2854 /* Remove all threads that saw no output since thread_timeout seconds.
2855 * By construction these are all at the tail of the queue, and the queue
2856 * is ordered by last use, so we just need to look at the tail.
2858 while (logger->threads.tail) {
2859 log = logger->threads.tail->data;
2860 if (log->last_use + logger->thread_timeout * G_USEC_PER_SEC >= now)
2863 g_hash_table_remove (logger->thread_index, log->thread);
2864 while ((output = g_queue_pop_head (&log->log)))
2867 g_queue_pop_tail (&logger->threads);
2871 /* Get logger for this thread, and put it back at the
2872 * head of the threads queue */
2873 thread = g_thread_self ();
2874 log = g_hash_table_lookup (logger->thread_index, thread);
2876 log = g_new0 (GstRingBufferLog, 1);
2877 g_queue_init (&log->log);
2879 g_queue_push_head (&logger->threads, log);
2880 log->link = logger->threads.head;
2881 log->thread = thread;
2882 g_hash_table_insert (logger->thread_index, thread, log);
2884 g_queue_unlink (&logger->threads, log->link);
2885 g_queue_push_head_link (&logger->threads, log->link);
2887 log->last_use = now;
2889 /* __FILE__ might be a file name or an absolute path or a
2890 * relative path, irrespective of the exact compiler used,
2891 * in which case we want to shorten it to the filename for
2894 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
2895 file = gst_path_basename (file);
2901 obj = gst_debug_print_object (object);
2906 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
2908 /* no color, all platforms */
2909 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
2911 g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
2912 pid, thread, gst_debug_level_get_name (level),
2913 gst_debug_category_get_name (category), file, line, function, obj,
2917 output_len = strlen (output);
2919 if (output_len < logger->max_size_per_thread) {
2922 /* While using a GQueue here is not the most efficient thing to do, we
2923 * have to allocate a string for every output anyway and could just store
2924 * that instead of copying it to an actual ringbuffer.
2925 * Better than GQueue would be GstQueueArray, but that one is in
2926 * libgstbase and we can't use it here. That one allocation will not make
2927 * much of a difference anymore, considering the number of allocations
2928 * needed to get to this point...
2930 while (log->log_size + output_len > logger->max_size_per_thread) {
2931 buf = g_queue_pop_head (&log->log);
2932 log->log_size -= strlen (buf);
2935 g_queue_push_tail (&log->log, output);
2936 log->log_size += output_len;
2940 /* Can't really write anything as the line is bigger than the maximum
2941 * allowed log size already, so just remove everything */
2943 while ((buf = g_queue_pop_head (&log->log)))
2952 G_UNLOCK (ring_buffer_logger);
2956 * gst_debug_ring_buffer_logger_get_logs:
2958 * Fetches the current logs per thread from the ring buffer logger. See
2959 * gst_debug_add_ring_buffer_logger() for details.
2961 * Returns: (transfer full) (array zero-terminated): NULL-terminated array of
2962 * strings with the debug output per thread
2967 gst_debug_ring_buffer_logger_get_logs (void)
2969 gchar **logs, **tmp;
2972 g_return_val_if_fail (ring_buffer_logger != NULL, NULL);
2974 G_LOCK (ring_buffer_logger);
2976 tmp = logs = g_new0 (gchar *, ring_buffer_logger->threads.length + 1);
2977 for (l = ring_buffer_logger->threads.head; l; l = l->next) {
2978 GstRingBufferLog *log = l->data;
2983 *tmp = p = g_new0 (gchar, log->log_size + 1);
2985 for (l = log->log.head; l; l = l->next) {
2986 len = strlen (l->data);
2987 memcpy (p, l->data, len);
2994 G_UNLOCK (ring_buffer_logger);
3000 gst_ring_buffer_logger_free (GstRingBufferLogger * logger)
3002 G_LOCK (ring_buffer_logger);
3003 if (ring_buffer_logger == logger) {
3004 GstRingBufferLog *log;
3006 while ((log = g_queue_pop_head (&logger->threads))) {
3008 while ((buf = g_queue_pop_head (&log->log)))
3013 g_hash_table_unref (logger->thread_index);
3016 ring_buffer_logger = NULL;
3018 G_UNLOCK (ring_buffer_logger);
3022 * gst_debug_add_ring_buffer_logger:
3023 * @max_size_per_thread: Maximum size of log per thread in bytes
3024 * @thread_timeout: Timeout for threads in seconds
3026 * Adds a memory ringbuffer based debug logger that stores up to
3027 * @max_size_per_thread bytes of logs per thread and times out threads after
3028 * @thread_timeout seconds of inactivity.
3030 * Logs can be fetched with gst_debug_ring_buffer_logger_get_logs() and the
3031 * logger can be removed again with gst_debug_remove_ring_buffer_logger().
3032 * Only one logger at a time is possible.
3037 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3038 guint thread_timeout)
3040 GstRingBufferLogger *logger;
3042 G_LOCK (ring_buffer_logger);
3044 if (ring_buffer_logger) {
3045 g_warn_if_reached ();
3046 G_UNLOCK (ring_buffer_logger);
3050 logger = ring_buffer_logger = g_new0 (GstRingBufferLogger, 1);
3052 logger->max_size_per_thread = max_size_per_thread;
3053 logger->thread_timeout = thread_timeout;
3054 logger->thread_index = g_hash_table_new (g_direct_hash, g_direct_equal);
3055 g_queue_init (&logger->threads);
3057 gst_debug_add_log_function (gst_ring_buffer_logger_log, logger,
3058 (GDestroyNotify) gst_ring_buffer_logger_free);
3059 G_UNLOCK (ring_buffer_logger);
3063 * gst_debug_remove_ring_buffer_logger:
3065 * Removes any previously added ring buffer logger with
3066 * gst_debug_add_ring_buffer_logger().
3071 gst_debug_remove_ring_buffer_logger (void)
3073 gst_debug_remove_log_function (gst_ring_buffer_logger_log);
3076 #else /* GST_DISABLE_GST_DEBUG */
3077 #ifndef GST_REMOVE_DISABLED
3080 gst_debug_ring_buffer_logger_get_logs (void)
3086 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3087 guint thread_timeout)
3092 gst_debug_remove_ring_buffer_logger (void)
3096 #endif /* GST_REMOVE_DISABLED */
3097 #endif /* GST_DISABLE_GST_DEBUG */