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 void gst_debug_reset_threshold (gpointer category, gpointer unused);
244 static void gst_debug_reset_all_thresholds (void);
246 struct _GstDebugMessage
253 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
254 static GMutex __level_name_mutex;
255 static GSList *__level_name = NULL;
263 /* list of all categories */
264 static GMutex __cat_mutex;
265 static GSList *__categories = NULL;
267 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
270 /* all registered debug handlers */
275 GDestroyNotify notify;
278 static GMutex __log_func_mutex;
279 static GSList *__log_functions = NULL;
281 /* whether to add the default log function in gst_init() */
282 static gboolean add_default_log_func = TRUE;
284 #define PRETTY_TAGS_DEFAULT TRUE
285 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
287 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
288 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
290 /* FIXME: export this? */
292 _priv_gst_in_valgrind (void)
300 in_valgrind = GST_VG_UNCHECKED;
302 if (in_valgrind == GST_VG_UNCHECKED) {
303 #ifdef HAVE_VALGRIND_VALGRIND_H
304 if (RUNNING_ON_VALGRIND) {
305 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
306 in_valgrind = GST_VG_INSIDE;
308 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
309 in_valgrind = GST_VG_NO_VALGRIND;
312 in_valgrind = GST_VG_NO_VALGRIND;
314 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
315 in_valgrind == GST_VG_INSIDE);
317 return (in_valgrind == GST_VG_INSIDE);
321 _replace_pattern_in_gst_debug_file_name (gchar * name, const char *token,
325 if ((token_start = strstr (name, token))) {
326 gsize token_len = strlen (token);
327 gchar *name_prefix = name;
328 gchar *name_suffix = token_start + token_len;
329 token_start[0] = '\0';
330 name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
331 g_free (name_prefix);
337 _priv_gst_debug_file_name (const gchar * env)
341 name = g_strdup (env);
342 name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
343 name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
348 /* Initialize the debugging system */
350 _priv_gst_debug_init (void)
355 if (add_default_log_func) {
356 env = g_getenv ("GST_DEBUG_FILE");
357 if (env != NULL && *env != '\0') {
358 if (strcmp (env, "-") == 0) {
361 gchar *name = _priv_gst_debug_file_name (env);
362 log_file = g_fopen (name, "w");
364 if (log_file == NULL) {
365 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
374 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
377 __gst_printf_pointer_extension_set_func
378 (gst_info_printf_pointer_extension_func);
380 /* do NOT use a single debug function before this line has been run */
381 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
382 GST_DEBUG_UNDERLINE, NULL);
383 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
384 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
386 /* FIXME: add descriptions here */
387 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
388 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
389 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
391 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
392 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
393 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
394 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
395 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
396 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
397 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
398 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
399 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
400 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
401 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
402 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
403 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
404 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
405 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
406 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
407 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
408 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
410 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
412 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
413 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
414 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
415 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
416 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
417 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
418 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
419 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
420 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
421 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
422 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
423 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
424 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
425 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
427 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
428 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
429 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
430 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
431 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
432 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
433 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
434 GST_DEBUG_BOLD, NULL);
435 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
436 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
437 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
438 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
439 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
440 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
441 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
442 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
443 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
444 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
445 _priv_GST_CAT_PROTECTION =
446 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
448 /* print out the valgrind message if we're in valgrind */
449 _priv_gst_in_valgrind ();
451 env = g_getenv ("GST_DEBUG_OPTIONS");
453 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
455 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
459 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
460 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
461 env = g_getenv ("GST_DEBUG_COLOR_MODE");
463 gst_debug_set_color_mode_from_string (env);
465 env = g_getenv ("GST_DEBUG");
467 gst_debug_set_threshold_from_string (env, FALSE);
470 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
471 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
475 * @category: category to log
476 * @level: level of the message is in
477 * @file: the file that emitted the message, usually the __FILE__ identifier
478 * @function: the function that emitted the message
479 * @line: the line from that the message was emitted, usually __LINE__
480 * @object: (transfer none) (allow-none): the object this message relates to,
482 * @format: a printf style format string
483 * @...: optional arguments for the format
485 * Logs the given message using the currently registered debugging handlers.
488 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
489 const gchar * file, const gchar * function, gint line,
490 GObject * object, const gchar * format, ...)
494 va_start (var_args, format);
495 gst_debug_log_valist (category, level, file, function, line, object, format,
500 /* based on g_basename(), which we can't use because it was deprecated */
501 static inline const gchar *
502 gst_path_basename (const gchar * file_name)
504 register const gchar *base;
506 base = strrchr (file_name, G_DIR_SEPARATOR);
509 const gchar *q = strrchr (file_name, '/');
510 if (base == NULL || (q != NULL && q > base))
517 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
518 return file_name + 2;
524 * gst_debug_log_valist:
525 * @category: category to log
526 * @level: level of the message is in
527 * @file: the file that emitted the message, usually the __FILE__ identifier
528 * @function: the function that emitted the message
529 * @line: the line from that the message was emitted, usually __LINE__
530 * @object: (transfer none) (allow-none): the object this message relates to,
532 * @format: a printf style format string
533 * @args: optional arguments for the format
535 * Logs the given message using the currently registered debugging handlers.
538 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
539 const gchar * file, const gchar * function, gint line,
540 GObject * object, const gchar * format, va_list args)
542 GstDebugMessage message;
546 g_return_if_fail (category != NULL);
548 if (level > gst_debug_category_get_threshold (category))
551 g_return_if_fail (file != NULL);
552 g_return_if_fail (function != NULL);
553 g_return_if_fail (format != NULL);
555 message.message = NULL;
556 message.format = format;
557 G_VA_COPY (message.arguments, args);
559 handler = __log_functions;
561 entry = handler->data;
562 handler = g_slist_next (handler);
563 entry->func (category, level, file, function, line, object, &message,
566 g_free (message.message);
567 va_end (message.arguments);
571 * gst_debug_message_get:
572 * @message: a debug message
574 * Gets the string representation of a #GstDebugMessage. This function is used
575 * in debug handlers to extract the message.
577 * Returns: (nullable): the string representation of a #GstDebugMessage.
580 gst_debug_message_get (GstDebugMessage * message)
582 if (message->message == NULL) {
585 len = __gst_vasprintf (&message->message, message->format,
589 message->message = NULL;
591 return message->message;
594 #define MAX_BUFFER_DUMP_STRING_LEN 100
596 /* structure_to_pretty_string:
597 * @str: a serialized #GstStructure
599 * If the serialized structure contains large buffers such as images the hex
600 * representation of those buffers will be shortened so that the string remains
603 * Returns: the filtered string
606 prettify_structure_string (gchar * str)
608 gchar *pos = str, *end;
610 while ((pos = strstr (pos, "(buffer)"))) {
613 pos += strlen ("(buffer)");
614 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
616 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
617 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
618 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
619 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
620 strlen (pos + count) + 1);
621 pos += MAX_BUFFER_DUMP_STRING_LEN;
628 static inline gchar *
629 gst_info_structure_to_string (const GstStructure * s)
632 gchar *str = gst_structure_to_string (s);
633 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
634 return prettify_structure_string (str);
641 static inline gchar *
642 gst_info_describe_buffer (GstBuffer * buffer)
644 const gchar *offset_str = "none";
645 const gchar *offset_end_str = "none";
646 gchar offset_buf[32], offset_end_buf[32];
648 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
649 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
650 GST_BUFFER_OFFSET (buffer));
651 offset_str = offset_buf;
653 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
654 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
655 GST_BUFFER_OFFSET_END (buffer));
656 offset_end_str = offset_end_buf;
659 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
660 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
661 ", offset %s, offset_end %s, flags 0x%x", buffer,
662 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
663 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
664 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
665 gst_buffer_get_size (buffer), offset_str, offset_end_str,
666 GST_BUFFER_FLAGS (buffer));
669 static inline gchar *
670 gst_info_describe_buffer_list (GstBufferList * list)
672 GstClockTime pts = GST_CLOCK_TIME_NONE;
673 GstClockTime dts = GST_CLOCK_TIME_NONE;
674 gsize total_size = 0;
677 n = gst_buffer_list_length (list);
678 for (i = 0; i < n; ++i) {
679 GstBuffer *buf = gst_buffer_list_get (list, i);
682 pts = GST_BUFFER_PTS (buf);
683 dts = GST_BUFFER_DTS (buf);
686 total_size += gst_buffer_get_size (buf);
689 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
690 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
691 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
694 static inline gchar *
695 gst_info_describe_event (GstEvent * event)
699 s = gst_info_structure_to_string (gst_event_get_structure (event));
700 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
701 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
702 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
708 static inline gchar *
709 gst_info_describe_message (GstMessage * message)
713 s = gst_info_structure_to_string (gst_message_get_structure (message));
714 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
715 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
716 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
717 GST_MESSAGE_SEQNUM (message),
718 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
724 static inline gchar *
725 gst_info_describe_query (GstQuery * query)
729 s = gst_info_structure_to_string (gst_query_get_structure (query));
730 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
731 query, (s ? s : "(NULL)"));
736 static inline gchar *
737 gst_info_describe_stream (GstStream * stream)
739 gchar *ret, *caps_str = NULL, *tags_str = NULL;
743 caps = gst_stream_get_caps (stream);
745 caps_str = gst_caps_to_string (caps);
746 gst_caps_unref (caps);
749 tags = gst_stream_get_tags (stream);
751 tags_str = gst_tag_list_to_string (tags);
752 gst_tag_list_unref (tags);
756 g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]",
757 gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream,
758 gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream),
759 caps_str ? caps_str : "", tags_str ? tags_str : "");
767 static inline gchar *
768 gst_info_describe_stream_collection (GstStreamCollection * collection)
771 GString *streams_str;
774 streams_str = g_string_new ("<");
775 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
776 GstStream *stream = gst_stream_collection_get_stream (collection, i);
779 s = gst_info_describe_stream (stream);
780 g_string_append_printf (streams_str, " %s,", s);
783 g_string_append (streams_str, " >");
785 ret = g_strdup_printf ("collection %p (%d streams) %s", collection,
786 gst_stream_collection_get_size (collection), streams_str->str);
788 g_string_free (streams_str, TRUE);
793 gst_debug_print_object (gpointer ptr)
795 GObject *object = (GObject *) ptr;
798 /* This is a cute trick to detect unmapped memory, but is unportable,
799 * slow, screws around with madvise, and not actually that useful. */
803 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
804 if (ret == -1 && errno == ENOMEM) {
805 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
810 /* nicely printed object */
811 if (object == NULL) {
812 return g_strdup ("(NULL)");
814 if (GST_IS_CAPS (ptr)) {
815 return gst_caps_to_string ((const GstCaps *) ptr);
817 if (GST_IS_STRUCTURE (ptr)) {
818 return gst_info_structure_to_string ((const GstStructure *) ptr);
820 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
821 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
823 if (GST_IS_TAG_LIST (ptr)) {
824 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
825 if (G_UNLIKELY (pretty_tags))
826 return prettify_structure_string (str);
830 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
831 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
833 if (GST_IS_BUFFER (ptr)) {
834 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
836 if (GST_IS_BUFFER_LIST (ptr)) {
837 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
840 if (*(guint32 *) ptr == 0xffffffff) {
841 return g_strdup_printf ("<poisoned@%p>", ptr);
844 if (GST_IS_MESSAGE (object)) {
845 return gst_info_describe_message (GST_MESSAGE_CAST (object));
847 if (GST_IS_QUERY (object)) {
848 return gst_info_describe_query (GST_QUERY_CAST (object));
850 if (GST_IS_EVENT (object)) {
851 return gst_info_describe_event (GST_EVENT_CAST (object));
853 if (GST_IS_CONTEXT (object)) {
854 GstContext *context = GST_CONTEXT_CAST (object);
857 const GstStructure *structure;
859 type = gst_context_get_context_type (context);
860 structure = gst_context_get_structure (context);
862 s = gst_info_structure_to_string (structure);
864 ret = g_strdup_printf ("context '%s'='%s'", type, s);
868 if (GST_IS_STREAM (object)) {
869 return gst_info_describe_stream (GST_STREAM_CAST (object));
871 if (GST_IS_STREAM_COLLECTION (object)) {
873 gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST
876 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
877 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
879 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
880 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
882 if (G_IS_OBJECT (object)) {
883 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
886 return g_strdup_printf ("%p", ptr);
890 gst_debug_print_segment (gpointer ptr)
892 GstSegment *segment = (GstSegment *) ptr;
894 /* nicely printed segment */
895 if (segment == NULL) {
896 return g_strdup ("(NULL)");
899 switch (segment->format) {
900 case GST_FORMAT_UNDEFINED:{
901 return g_strdup_printf ("UNDEFINED segment");
903 case GST_FORMAT_TIME:{
904 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
905 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
906 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
907 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
908 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
909 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
910 segment->rate, segment->applied_rate, (guint) segment->flags,
911 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
912 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
915 const gchar *format_name;
917 format_name = gst_format_get_name (segment->format);
918 if (G_UNLIKELY (format_name == NULL))
919 format_name = "(UNKNOWN FORMAT)";
920 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
921 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
922 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
923 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
924 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
925 segment->offset, segment->stop, segment->rate, segment->applied_rate,
926 (guint) segment->flags, segment->time, segment->base,
927 segment->position, segment->duration);
933 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
937 if (format[0] == 'p' && format[1] == '\a') {
939 case 'A': /* GST_PTR_FORMAT */
940 s = gst_debug_print_object (ptr);
942 case 'B': /* GST_SEGMENT_FORMAT */
943 s = gst_debug_print_segment (ptr);
945 case 'a': /* GST_WRAPPED_PTR_FORMAT */
946 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
949 /* must have been compiled against a newer version with an extension
950 * we don't known about yet - just ignore and fallback to %p below */
955 s = g_strdup_printf ("%p", ptr);
961 * gst_debug_construct_term_color:
962 * @colorinfo: the color info
964 * Constructs a string that can be used for getting the desired color in color
966 * You need to free the string after use.
968 * Returns: (transfer full) (type gchar*): a string containing the color
972 gst_debug_construct_term_color (guint colorinfo)
976 color = g_string_new ("\033[00");
978 if (colorinfo & GST_DEBUG_BOLD) {
979 g_string_append_len (color, ";01", 3);
981 if (colorinfo & GST_DEBUG_UNDERLINE) {
982 g_string_append_len (color, ";04", 3);
984 if (colorinfo & GST_DEBUG_FG_MASK) {
985 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
987 if (colorinfo & GST_DEBUG_BG_MASK) {
988 g_string_append_printf (color, ";4%1d",
989 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
991 g_string_append_c (color, 'm');
993 return g_string_free (color, FALSE);
997 * gst_debug_construct_win_color:
998 * @colorinfo: the color info
1000 * Constructs an integer that can be used for getting the desired color in
1001 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
1002 * ignore this attribute.
1004 * This function returns 0 on non-windows machines.
1006 * Returns: an integer containing the color definition
1009 gst_debug_construct_win_color (guint colorinfo)
1013 static const guchar ansi_to_win_fg[8] = {
1015 FOREGROUND_RED, /* red */
1016 FOREGROUND_GREEN, /* green */
1017 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
1018 FOREGROUND_BLUE, /* blue */
1019 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
1020 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
1021 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
1023 static const guchar ansi_to_win_bg[8] = {
1027 BACKGROUND_RED | BACKGROUND_GREEN,
1029 BACKGROUND_RED | BACKGROUND_BLUE,
1030 BACKGROUND_GREEN | FOREGROUND_BLUE,
1031 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
1034 /* we draw black as white, as cmd.exe can only have black bg */
1035 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
1036 color = ansi_to_win_fg[7];
1038 if (colorinfo & GST_DEBUG_UNDERLINE) {
1039 color |= BACKGROUND_INTENSITY;
1041 if (colorinfo & GST_DEBUG_BOLD) {
1042 color |= FOREGROUND_INTENSITY;
1044 if (colorinfo & GST_DEBUG_FG_MASK) {
1045 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
1047 if (colorinfo & GST_DEBUG_BG_MASK) {
1048 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
1054 /* width of %p varies depending on actual value of pointer, which can make
1055 * output unevenly aligned if multiple threads are involved, hence the %14p
1056 * (should really be %18p, but %14p seems a good compromise between too many
1057 * white spaces and likely unalignment on my system) */
1058 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
1059 #define PTR_FMT "%14p"
1061 #define PTR_FMT "%10p"
1063 #define PID_FMT "%5d"
1064 #define CAT_FMT "%20s %s:%d:%s:%s"
1067 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
1068 /* GST_LEVEL_NONE */
1069 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1070 /* GST_LEVEL_ERROR */
1071 FOREGROUND_RED | FOREGROUND_INTENSITY,
1072 /* GST_LEVEL_WARNING */
1073 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1074 /* GST_LEVEL_INFO */
1075 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1076 /* GST_LEVEL_DEBUG */
1077 FOREGROUND_GREEN | FOREGROUND_BLUE,
1079 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1080 /* GST_LEVEL_FIXME */
1081 FOREGROUND_RED | FOREGROUND_GREEN,
1082 /* GST_LEVEL_TRACE */
1083 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1084 /* placeholder for log level 8 */
1086 /* GST_LEVEL_MEMDUMP */
1087 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
1090 static const guchar available_colors[] = {
1091 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
1092 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
1093 FOREGROUND_GREEN | FOREGROUND_BLUE,
1095 #endif /* G_OS_WIN32 */
1096 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
1097 "\033[37m", /* GST_LEVEL_NONE */
1098 "\033[31;01m", /* GST_LEVEL_ERROR */
1099 "\033[33;01m", /* GST_LEVEL_WARNING */
1100 "\033[32;01m", /* GST_LEVEL_INFO */
1101 "\033[36m", /* GST_LEVEL_DEBUG */
1102 "\033[37m", /* GST_LEVEL_LOG */
1103 "\033[33;01m", /* GST_LEVEL_FIXME */
1104 "\033[37m", /* GST_LEVEL_TRACE */
1105 "\033[37m", /* placeholder for log level 8 */
1106 "\033[37m" /* GST_LEVEL_MEMDUMP */
1110 * gst_debug_log_default:
1111 * @category: category to log
1112 * @level: level of the message
1113 * @file: the file that emitted the message, usually the __FILE__ identifier
1114 * @function: the function that emitted the message
1115 * @line: the line from that the message was emitted, usually __LINE__
1116 * @message: the actual message
1117 * @object: (transfer none) (allow-none): the object this message relates to,
1119 * @user_data: the FILE* to log to
1121 * The default logging handler used by GStreamer. Logging functions get called
1122 * whenever a macro like GST_DEBUG or similar is used. By default this function
1123 * is setup to output the message and additional info to stderr (or the log file
1124 * specified via the GST_DEBUG_FILE environment variable) as received via
1127 * You can add other handlers by using gst_debug_add_log_function().
1128 * And you can remove this handler by calling
1129 * gst_debug_remove_log_function(gst_debug_log_default);
1132 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1133 const gchar * file, const gchar * function, gint line,
1134 GObject * object, GstDebugMessage * message, gpointer user_data)
1137 GstClockTime elapsed;
1139 GstDebugColorMode color_mode;
1140 FILE *log_file = user_data ? user_data : stderr;
1143 /* __FILE__ might be a file name or an absolute path or a
1144 * relative path, irrespective of the exact compiler used,
1145 * in which case we want to shorten it to the filename for
1148 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1149 file = gst_path_basename (file);
1153 color_mode = gst_debug_get_color_mode ();
1156 obj = gst_debug_print_object (object);
1161 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1163 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1165 /* We take a lock to keep colors and content together.
1166 * Maybe there is a better way but for now this will do the right
1168 static GMutex win_print_mutex;
1169 g_mutex_lock (&win_print_mutex);
1170 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1172 /* colors, non-windows */
1173 gchar *color = NULL;
1176 const gchar *levelcolor;
1178 color = gst_debug_construct_term_color (gst_debug_category_get_color
1181 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1182 levelcolor = levelcolormap[level];
1184 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1185 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1186 pidcolor, pid, clear, g_thread_self (), levelcolor,
1187 gst_debug_level_get_name (level), clear, color,
1188 gst_debug_category_get_name (category), file, line, function, obj,
1189 clear, gst_debug_message_get (message));
1195 /* colors, windows. */
1196 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1197 #define SET_COLOR(c) G_STMT_START { \
1198 if (log_file == stderr) \
1199 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1202 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1205 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1206 fprintf (log_file, PID_FMT, pid);
1210 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1213 SET_COLOR (levelcolormap_w32[level]);
1214 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1217 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1219 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1220 file, line, function, obj);
1224 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1227 g_mutex_unlock (&win_print_mutex);
1230 /* no color, all platforms */
1231 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1232 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1233 pid, g_thread_self (), gst_debug_level_get_name (level),
1234 gst_debug_category_get_name (category), file, line, function, obj,
1235 gst_debug_message_get (message));
1245 * gst_debug_level_get_name:
1246 * @level: the level to get the name for
1248 * Get the string representation of a debugging level
1253 gst_debug_level_get_name (GstDebugLevel level)
1256 case GST_LEVEL_NONE:
1258 case GST_LEVEL_ERROR:
1260 case GST_LEVEL_WARNING:
1262 case GST_LEVEL_INFO:
1264 case GST_LEVEL_DEBUG:
1268 case GST_LEVEL_FIXME:
1270 case GST_LEVEL_TRACE:
1272 case GST_LEVEL_MEMDUMP:
1275 g_warning ("invalid level specified for gst_debug_level_get_name");
1281 * gst_debug_add_log_function:
1282 * @func: the function to use
1283 * @user_data: user data
1284 * @notify: called when @user_data is not used anymore
1286 * Adds the logging function to the list of logging functions.
1287 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1290 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1291 GDestroyNotify notify)
1293 LogFuncEntry *entry;
1297 func = gst_debug_log_default;
1299 entry = g_slice_new (LogFuncEntry);
1301 entry->user_data = user_data;
1302 entry->notify = notify;
1303 /* FIXME: we leak the old list here - other threads might access it right now
1304 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1305 * but that is waaay costly.
1306 * It'd probably be clever to use some kind of RCU here, but I don't know
1307 * anything about that.
1309 g_mutex_lock (&__log_func_mutex);
1310 list = g_slist_copy (__log_functions);
1311 __log_functions = g_slist_prepend (list, entry);
1312 g_mutex_unlock (&__log_func_mutex);
1314 if (gst_is_initialized ())
1315 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1320 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1322 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1324 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1328 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1330 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1332 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1336 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1339 GSList *new, *cleanup = NULL;
1342 g_mutex_lock (&__log_func_mutex);
1343 new = __log_functions;
1345 while ((found = g_slist_find_custom (new, data, func))) {
1346 if (new == __log_functions) {
1347 /* make a copy when we have the first hit, so that we modify the copy and
1348 * make that the new list later */
1349 new = g_slist_copy (new);
1352 cleanup = g_slist_prepend (cleanup, found->data);
1353 new = g_slist_delete_link (new, found);
1356 /* FIXME: We leak the old list here. See _add_log_function for why. */
1357 __log_functions = new;
1358 g_mutex_unlock (&__log_func_mutex);
1361 LogFuncEntry *entry = cleanup->data;
1364 entry->notify (entry->user_data);
1366 g_slice_free (LogFuncEntry, entry);
1367 cleanup = g_slist_delete_link (cleanup, cleanup);
1373 * gst_debug_remove_log_function:
1374 * @func: (scope call) (allow-none): the log function to remove, or %NULL to
1375 * remove the default log function
1377 * Removes all registered instances of the given logging functions.
1379 * Returns: How many instances of the function were removed
1382 gst_debug_remove_log_function (GstLogFunction func)
1387 func = gst_debug_log_default;
1390 gst_debug_remove_with_compare_func
1391 (gst_debug_compare_log_function_by_func, (gpointer) func);
1393 if (gst_is_initialized ()) {
1394 GST_DEBUG ("removed log function %p %d times from log function list", func,
1397 /* If the default log function is removed before gst_init() was called,
1398 * set a flag so we don't add it in gst_init() later */
1399 if (func == gst_debug_log_default) {
1400 add_default_log_func = FALSE;
1409 * gst_debug_remove_log_function_by_data:
1410 * @data: user data of the log function to remove
1412 * Removes all registered instances of log functions with the given user data.
1414 * Returns: How many instances of the function were removed
1417 gst_debug_remove_log_function_by_data (gpointer data)
1422 gst_debug_remove_with_compare_func
1423 (gst_debug_compare_log_function_by_data, data);
1425 if (gst_is_initialized ())
1427 ("removed %d log functions with user data %p from log function list",
1434 * gst_debug_set_colored:
1435 * @colored: Whether to use colored output or not
1437 * Sets or unsets the use of coloured debugging output.
1438 * Same as gst_debug_set_color_mode () with the argument being
1439 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1441 * This function may be called before gst_init().
1444 gst_debug_set_colored (gboolean colored)
1446 GstDebugColorMode new_mode;
1447 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1448 g_atomic_int_set (&__use_color, (gint) new_mode);
1452 * gst_debug_set_color_mode:
1453 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1455 * Changes the coloring mode for debug output.
1457 * This function may be called before gst_init().
1462 gst_debug_set_color_mode (GstDebugColorMode mode)
1464 g_atomic_int_set (&__use_color, mode);
1468 * gst_debug_set_color_mode_from_string:
1469 * @mode: The coloring mode for debug output. One of the following:
1470 * "on", "auto", "off", "disable", "unix".
1472 * Changes the coloring mode for debug output.
1474 * This function may be called before gst_init().
1479 gst_debug_set_color_mode_from_string (const gchar * mode)
1481 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1482 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1483 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1484 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1485 else if (strcmp (mode, "unix") == 0)
1486 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1490 * gst_debug_is_colored:
1492 * Checks if the debugging output should be colored.
1494 * Returns: %TRUE, if the debug output should be colored.
1497 gst_debug_is_colored (void)
1499 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1500 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1504 * gst_debug_get_color_mode:
1506 * Changes the coloring mode for debug output.
1508 * Returns: see @GstDebugColorMode for possible values.
1513 gst_debug_get_color_mode (void)
1515 return g_atomic_int_get (&__use_color);
1519 * gst_debug_set_active:
1520 * @active: Whether to use debugging output or not
1522 * If activated, debugging messages are sent to the debugging
1524 * It makes sense to deactivate it for speed issues.
1525 * > This function is not threadsafe. It makes sense to only call it
1526 * during initialization.
1529 gst_debug_set_active (gboolean active)
1531 _gst_debug_enabled = active;
1533 _gst_debug_min = GST_LEVEL_COUNT;
1535 _gst_debug_min = GST_LEVEL_NONE;
1539 * gst_debug_is_active:
1541 * Checks if debugging output is activated.
1543 * Returns: %TRUE, if debugging is activated
1546 gst_debug_is_active (void)
1548 return _gst_debug_enabled;
1552 * gst_debug_set_default_threshold:
1553 * @level: level to set
1555 * Sets the default threshold to the given level and updates all categories to
1556 * use this threshold.
1558 * This function may be called before gst_init().
1561 gst_debug_set_default_threshold (GstDebugLevel level)
1563 g_atomic_int_set (&__default_level, level);
1564 gst_debug_reset_all_thresholds ();
1568 * gst_debug_get_default_threshold:
1570 * Returns the default threshold that is used for new categories.
1572 * Returns: the default threshold level
1575 gst_debug_get_default_threshold (void)
1577 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1581 gst_debug_reset_threshold (gpointer category, gpointer unused)
1583 GstDebugCategory *cat = (GstDebugCategory *) category;
1586 g_mutex_lock (&__level_name_mutex);
1587 walk = __level_name;
1589 LevelNameEntry *entry = walk->data;
1591 walk = g_slist_next (walk);
1592 if (g_pattern_match_string (entry->pat, cat->name)) {
1593 if (gst_is_initialized ())
1594 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1595 cat->name, entry->pat, entry->level);
1596 gst_debug_category_set_threshold (cat, entry->level);
1600 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1603 g_mutex_unlock (&__level_name_mutex);
1607 gst_debug_reset_all_thresholds (void)
1609 g_mutex_lock (&__cat_mutex);
1610 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1611 g_mutex_unlock (&__cat_mutex);
1615 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1617 GstDebugCategory *cat = (GstDebugCategory *) data;
1618 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1620 if (g_pattern_match_string (entry->pat, cat->name)) {
1621 if (gst_is_initialized ())
1622 GST_TRACE ("category %s matches pattern %p - gets set to level %d",
1623 cat->name, entry->pat, entry->level);
1624 gst_debug_category_set_threshold (cat, entry->level);
1629 * gst_debug_set_threshold_for_name:
1630 * @name: name of the categories to set
1631 * @level: level to set them to
1633 * Sets all categories which match the given glob style pattern to the given
1637 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1640 LevelNameEntry *entry;
1642 g_return_if_fail (name != NULL);
1644 pat = g_pattern_spec_new (name);
1645 entry = g_slice_new (LevelNameEntry);
1647 entry->level = level;
1648 g_mutex_lock (&__level_name_mutex);
1649 __level_name = g_slist_prepend (__level_name, entry);
1650 g_mutex_unlock (&__level_name_mutex);
1651 g_mutex_lock (&__cat_mutex);
1652 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1653 g_mutex_unlock (&__cat_mutex);
1657 * gst_debug_unset_threshold_for_name:
1658 * @name: name of the categories to set
1660 * Resets all categories with the given name back to the default level.
1663 gst_debug_unset_threshold_for_name (const gchar * name)
1668 g_return_if_fail (name != NULL);
1670 pat = g_pattern_spec_new (name);
1671 g_mutex_lock (&__level_name_mutex);
1672 walk = __level_name;
1673 /* improve this if you want, it's mighty slow */
1675 LevelNameEntry *entry = walk->data;
1677 if (g_pattern_spec_equal (entry->pat, pat)) {
1678 __level_name = g_slist_remove_link (__level_name, walk);
1679 g_pattern_spec_free (entry->pat);
1680 g_slice_free (LevelNameEntry, entry);
1681 g_slist_free_1 (walk);
1682 walk = __level_name;
1684 walk = g_slist_next (walk);
1687 g_mutex_unlock (&__level_name_mutex);
1688 g_pattern_spec_free (pat);
1689 gst_debug_reset_all_thresholds ();
1693 gst_debug_apply_patterns_to_category (GstDebugCategory * cat)
1697 g_mutex_lock (&__level_name_mutex);
1698 for (l = __level_name; l != NULL; l = l->next) {
1699 for_each_threshold_by_entry (cat, (LevelNameEntry *) l->data);
1701 g_mutex_unlock (&__level_name_mutex);
1705 _gst_debug_category_new (const gchar * name, guint color,
1706 const gchar * description)
1708 GstDebugCategory *cat, *catfound;
1710 g_return_val_if_fail (name != NULL, NULL);
1712 cat = g_slice_new (GstDebugCategory);
1713 cat->name = g_strdup (name);
1715 if (description != NULL) {
1716 cat->description = g_strdup (description);
1718 cat->description = g_strdup ("no description");
1720 g_atomic_int_set (&cat->threshold, 0);
1721 gst_debug_reset_threshold (cat, NULL);
1723 /* add to category list */
1724 g_mutex_lock (&__cat_mutex);
1725 catfound = _gst_debug_get_category_locked (name);
1727 g_free ((gpointer) cat->name);
1728 g_free ((gpointer) cat->description);
1729 g_slice_free (GstDebugCategory, cat);
1732 __categories = g_slist_prepend (__categories, cat);
1734 g_mutex_unlock (&__cat_mutex);
1736 /* ensure the filter is applied to categories registered after _debug_init */
1737 gst_debug_apply_patterns_to_category (cat);
1743 * gst_debug_category_free:
1744 * @category: #GstDebugCategory to free.
1746 * Removes and frees the category and all associated resources.
1749 gst_debug_category_free (GstDebugCategory * category)
1751 if (category == NULL)
1754 /* remove from category list */
1755 g_mutex_lock (&__cat_mutex);
1756 __categories = g_slist_remove (__categories, category);
1757 g_mutex_unlock (&__cat_mutex);
1759 g_free ((gpointer) category->name);
1760 g_free ((gpointer) category->description);
1761 g_slice_free (GstDebugCategory, category);
1765 * gst_debug_category_set_threshold:
1766 * @category: a #GstDebugCategory to set threshold of.
1767 * @level: the #GstDebugLevel threshold to set.
1769 * Sets the threshold of the category to the given level. Debug information will
1770 * only be output if the threshold is lower or equal to the level of the
1771 * debugging message.
1772 * > Do not use this function in production code, because other functions may
1773 * > change the threshold of categories as side effect. It is however a nice
1774 * > function to use when debugging (even from gdb).
1777 gst_debug_category_set_threshold (GstDebugCategory * category,
1778 GstDebugLevel level)
1780 g_return_if_fail (category != NULL);
1782 if (level > _gst_debug_min) {
1783 _gst_debug_enabled = TRUE;
1784 _gst_debug_min = level;
1787 g_atomic_int_set (&category->threshold, level);
1791 * gst_debug_category_reset_threshold:
1792 * @category: a #GstDebugCategory to reset threshold of.
1794 * Resets the threshold of the category to the default level. Debug information
1795 * will only be output if the threshold is lower or equal to the level of the
1796 * debugging message.
1797 * Use this function to set the threshold back to where it was after using
1798 * gst_debug_category_set_threshold().
1801 gst_debug_category_reset_threshold (GstDebugCategory * category)
1803 gst_debug_reset_threshold (category, NULL);
1807 * gst_debug_category_get_threshold:
1808 * @category: a #GstDebugCategory to get threshold of.
1810 * Returns the threshold of a #GstDebugCategory.
1812 * Returns: the #GstDebugLevel that is used as threshold.
1815 gst_debug_category_get_threshold (GstDebugCategory * category)
1817 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1821 * gst_debug_category_get_name:
1822 * @category: a #GstDebugCategory to get name of.
1824 * Returns the name of a debug category.
1826 * Returns: the name of the category.
1829 gst_debug_category_get_name (GstDebugCategory * category)
1831 return category->name;
1835 * gst_debug_category_get_color:
1836 * @category: a #GstDebugCategory to get the color of.
1838 * Returns the color of a debug category used when printing output in this
1841 * Returns: the color of the category.
1844 gst_debug_category_get_color (GstDebugCategory * category)
1846 return category->color;
1850 * gst_debug_category_get_description:
1851 * @category: a #GstDebugCategory to get the description of.
1853 * Returns the description of a debug category.
1855 * Returns: the description of the category.
1858 gst_debug_category_get_description (GstDebugCategory * category)
1860 return category->description;
1864 * gst_debug_get_all_categories:
1866 * Returns a snapshot of a all categories that are currently in use . This list
1867 * may change anytime.
1868 * The caller has to free the list after use.
1870 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1874 gst_debug_get_all_categories (void)
1878 g_mutex_lock (&__cat_mutex);
1879 ret = g_slist_copy (__categories);
1880 g_mutex_unlock (&__cat_mutex);
1885 static GstDebugCategory *
1886 _gst_debug_get_category_locked (const gchar * name)
1888 GstDebugCategory *ret = NULL;
1891 for (node = __categories; node; node = g_slist_next (node)) {
1892 ret = (GstDebugCategory *) node->data;
1893 if (!strcmp (name, ret->name)) {
1901 _gst_debug_get_category (const gchar * name)
1903 GstDebugCategory *ret;
1905 g_mutex_lock (&__cat_mutex);
1906 ret = _gst_debug_get_category_locked (name);
1907 g_mutex_unlock (&__cat_mutex);
1913 parse_debug_category (gchar * str, const gchar ** category)
1918 /* works in place */
1921 if (str[0] != '\0') {
1930 parse_debug_level (gchar * str, GstDebugLevel * level)
1935 /* works in place */
1938 if (g_ascii_isdigit (str[0])) {
1941 l = strtoul (str, &endptr, 10);
1942 if (endptr > str && endptr[0] == 0) {
1943 *level = (GstDebugLevel) l;
1947 } else if (strcmp (str, "ERROR") == 0) {
1948 *level = GST_LEVEL_ERROR;
1949 } else if (strncmp (str, "WARN", 4) == 0) {
1950 *level = GST_LEVEL_WARNING;
1951 } else if (strcmp (str, "FIXME") == 0) {
1952 *level = GST_LEVEL_FIXME;
1953 } else if (strcmp (str, "INFO") == 0) {
1954 *level = GST_LEVEL_INFO;
1955 } else if (strcmp (str, "DEBUG") == 0) {
1956 *level = GST_LEVEL_DEBUG;
1957 } else if (strcmp (str, "LOG") == 0) {
1958 *level = GST_LEVEL_LOG;
1959 } else if (strcmp (str, "TRACE") == 0) {
1960 *level = GST_LEVEL_TRACE;
1961 } else if (strcmp (str, "MEMDUMP") == 0) {
1962 *level = GST_LEVEL_MEMDUMP;
1970 * gst_debug_set_threshold_from_string:
1971 * @list: comma-separated list of "category:level" pairs to be used
1972 * as debug logging levels
1973 * @reset: %TRUE to clear all previously-set debug levels before setting
1975 * %FALSE if adding the threshold described by @list to the one already set.
1977 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1978 * environment variable. You can use wildcards such as '*', but note that
1979 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1980 * everything to log level 2.
1985 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1993 gst_debug_set_default_threshold (GST_LEVEL_DEFAULT);
1995 split = g_strsplit (list, ",", 0);
1997 for (walk = split; *walk; walk++) {
1998 if (strchr (*walk, ':')) {
1999 gchar **values = g_strsplit (*walk, ":", 2);
2001 if (values[0] && values[1]) {
2002 GstDebugLevel level;
2003 const gchar *category;
2005 if (parse_debug_category (values[0], &category)
2006 && parse_debug_level (values[1], &level)) {
2007 gst_debug_set_threshold_for_name (category, level);
2009 /* bump min-level anyway to allow the category to be registered in the
2011 if (level > _gst_debug_min) {
2012 _gst_debug_min = level;
2017 g_strfreev (values);
2019 GstDebugLevel level;
2021 if (parse_debug_level (*walk, &level))
2022 gst_debug_set_default_threshold (level);
2029 /*** FUNCTION POINTERS ********************************************************/
2031 static GHashTable *__gst_function_pointers; /* NULL */
2032 static GMutex __dbg_functions_mutex;
2034 /* This function MUST NOT return NULL */
2036 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2044 if (G_UNLIKELY (func == NULL))
2047 g_mutex_lock (&__dbg_functions_mutex);
2048 if (G_LIKELY (__gst_function_pointers)) {
2049 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
2050 g_mutex_unlock (&__dbg_functions_mutex);
2051 if (G_LIKELY (ptrname))
2054 g_mutex_unlock (&__dbg_functions_mutex);
2056 /* we need to create an entry in the hash table for this one so we don't leak
2059 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
2060 gchar *name = g_strdup (dl_info.dli_sname);
2062 _gst_debug_register_funcptr (func, name);
2067 gchar *name = g_strdup_printf ("%p", (gpointer) func);
2069 _gst_debug_register_funcptr (func, name);
2075 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2077 gpointer ptr = (gpointer) func;
2079 g_mutex_lock (&__dbg_functions_mutex);
2081 if (!__gst_function_pointers)
2082 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
2083 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
2084 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
2086 g_mutex_unlock (&__dbg_functions_mutex);
2090 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
2091 const guint8 * mem, gsize mem_offset, gsize mem_size)
2093 gchar hexstr[50], ascstr[18], digitstr[4];
2105 while (i < mem_size) {
2106 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
2107 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
2108 g_strlcat (hexstr, digitstr, sizeof (hexstr));
2114 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
2115 (guint) mem_offset, hexstr, ascstr);
2119 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2120 const gchar * func, gint line, GObject * obj, const gchar * msg,
2121 const guint8 * data, guint length)
2125 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2126 "-------------------------------------------------------------------");
2128 if (msg != NULL && *msg != '\0') {
2129 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
2132 while (off < length) {
2135 /* gst_info_dump_mem_line will process 16 bytes at most */
2136 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
2137 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2141 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2142 "-------------------------------------------------------------------");
2145 #else /* !GST_DISABLE_GST_DEBUG */
2146 #ifndef GST_REMOVE_DISABLED
2149 _gst_debug_category_new (const gchar * name, guint color,
2150 const gchar * description)
2156 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2160 /* This function MUST NOT return NULL */
2162 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2168 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2169 const gchar * file, const gchar * function, gint line,
2170 GObject * object, const gchar * format, ...)
2175 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2176 const gchar * file, const gchar * function, gint line,
2177 GObject * object, const gchar * format, va_list args)
2182 gst_debug_message_get (GstDebugMessage * message)
2188 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2189 const gchar * file, const gchar * function, gint line,
2190 GObject * object, GstDebugMessage * message, gpointer unused)
2195 gst_debug_level_get_name (GstDebugLevel level)
2201 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2202 GDestroyNotify notify)
2207 gst_debug_remove_log_function (GstLogFunction func)
2213 gst_debug_remove_log_function_by_data (gpointer data)
2219 gst_debug_set_active (gboolean active)
2224 gst_debug_is_active (void)
2230 gst_debug_set_colored (gboolean colored)
2235 gst_debug_set_color_mode (GstDebugColorMode mode)
2240 gst_debug_set_color_mode_from_string (const gchar * str)
2245 gst_debug_is_colored (void)
2251 gst_debug_get_color_mode (void)
2253 return GST_DEBUG_COLOR_MODE_OFF;
2257 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2262 gst_debug_set_default_threshold (GstDebugLevel level)
2267 gst_debug_get_default_threshold (void)
2269 return GST_LEVEL_NONE;
2273 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2278 gst_debug_unset_threshold_for_name (const gchar * name)
2283 gst_debug_category_free (GstDebugCategory * category)
2288 gst_debug_category_set_threshold (GstDebugCategory * category,
2289 GstDebugLevel level)
2294 gst_debug_category_reset_threshold (GstDebugCategory * category)
2299 gst_debug_category_get_threshold (GstDebugCategory * category)
2301 return GST_LEVEL_NONE;
2305 gst_debug_category_get_name (GstDebugCategory * category)
2311 gst_debug_category_get_color (GstDebugCategory * category)
2317 gst_debug_category_get_description (GstDebugCategory * category)
2323 gst_debug_get_all_categories (void)
2329 _gst_debug_get_category (const gchar * name)
2335 gst_debug_construct_term_color (guint colorinfo)
2337 return g_strdup ("00");
2341 gst_debug_construct_win_color (guint colorinfo)
2347 _priv_gst_in_valgrind (void)
2353 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2354 const gchar * func, gint line, GObject * obj, const gchar * msg,
2355 const guint8 * data, guint length)
2358 #endif /* GST_REMOVE_DISABLED */
2359 #endif /* GST_DISABLE_GST_DEBUG */
2361 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2362 * fallback function that cleans up the format string and replaces all pointer
2363 * extension formats with plain %p. */
2364 #ifdef GST_DISABLE_GST_DEBUG
2366 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2368 gchar *clean_format, *c;
2374 clean_format = g_strdup (format);
2376 while ((c = strstr (c, "%p\a"))) {
2377 if (c[3] < 'A' || c[3] > 'Z') {
2381 len = strlen (c + 4);
2382 memmove (c + 2, c + 4, len + 1);
2385 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2387 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2390 len = g_vasprintf (result, clean_format, args);
2392 g_free (clean_format);
2394 if (*result == NULL)
2402 * gst_info_vasprintf:
2403 * @result: (out): the resulting string
2404 * @format: a printf style format string
2405 * @args: the va_list of printf arguments for @format
2407 * Allocates and fills a string large enough (including the terminating null
2408 * byte) to hold the specified printf style @format and @args.
2410 * This function deals with the GStreamer specific printf specifiers
2411 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2412 * in your @format string, you do not need to use this function and can use
2413 * alternatives such as g_vasprintf().
2415 * Free @result with g_free().
2417 * Returns: the length of the string allocated into @result or -1 on any error
2422 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2424 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2425 * gst_private.h if the debug system is disabled which will remove the gst
2426 * specific printf format specifiers */
2427 return __gst_vasprintf (result, format, args);
2431 * gst_info_strdup_vprintf:
2432 * @format: a printf style format string
2433 * @args: the va_list of printf arguments for @format
2435 * Allocates, fills and returns a null terminated string from the printf style
2436 * @format string and @args.
2438 * See gst_info_vasprintf() for when this function is required.
2440 * Free with g_free().
2442 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2447 gst_info_strdup_vprintf (const gchar * format, va_list args)
2451 if (gst_info_vasprintf (&ret, format, args) < 0)
2458 * gst_info_strdup_printf:
2459 * @format: a printf style format string
2460 * @...: the printf arguments for @format
2462 * Allocates, fills and returns a 0-terminated string from the printf style
2463 * @format string and corresponding arguments.
2465 * See gst_info_vasprintf() for when this function is required.
2467 * Free with g_free().
2469 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2474 gst_info_strdup_printf (const gchar * format, ...)
2479 va_start (args, format);
2480 ret = gst_info_strdup_vprintf (format, args);
2488 * @format: a printf style format string
2489 * @...: the printf arguments for @format
2491 * Outputs a formatted message via the GLib print handler. The default print
2492 * handler simply outputs the message to stdout.
2494 * This function will not append a new-line character at the end, unlike
2495 * gst_println() which will.
2497 * All strings must be in ASCII or UTF-8 encoding.
2499 * This function differs from g_print() in that it supports all the additional
2500 * printf specifiers that are supported by GStreamer's debug logging system,
2501 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2503 * This function is primarily for printing debug output.
2508 gst_print (const gchar * format, ...)
2513 va_start (args, format);
2514 str = gst_info_strdup_vprintf (format, args);
2517 g_print ("%s", str);
2523 * @format: a printf style format string
2524 * @...: the printf arguments for @format
2526 * Outputs a formatted message via the GLib print handler. The default print
2527 * handler simply outputs the message to stdout.
2529 * This function will append a new-line character at the end, unlike
2530 * gst_print() which will not.
2532 * All strings must be in ASCII or UTF-8 encoding.
2534 * This function differs from g_print() in that it supports all the additional
2535 * printf specifiers that are supported by GStreamer's debug logging system,
2536 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2538 * This function is primarily for printing debug output.
2543 gst_println (const gchar * format, ...)
2548 va_start (args, format);
2549 str = gst_info_strdup_vprintf (format, args);
2552 g_print ("%s\n", str);
2558 * @format: a printf style format string
2559 * @...: the printf arguments for @format
2561 * Outputs a formatted message via the GLib error message handler. The default
2562 * handler simply outputs the message to stderr.
2564 * This function will not append a new-line character at the end, unlike
2565 * gst_printerrln() which will.
2567 * All strings must be in ASCII or UTF-8 encoding.
2569 * This function differs from g_printerr() in that it supports the additional
2570 * printf specifiers that are supported by GStreamer's debug logging system,
2571 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2573 * This function is primarily for printing debug output.
2578 gst_printerr (const gchar * format, ...)
2583 va_start (args, format);
2584 str = gst_info_strdup_vprintf (format, args);
2587 g_printerr ("%s", str);
2593 * @format: a printf style format string
2594 * @...: the printf arguments for @format
2596 * Outputs a formatted message via the GLib error message handler. The default
2597 * handler simply outputs the message to stderr.
2599 * This function will append a new-line character at the end, unlike
2600 * gst_printerr() which will not.
2602 * All strings must be in ASCII or UTF-8 encoding.
2604 * This function differs from g_printerr() in that it supports the additional
2605 * printf specifiers that are supported by GStreamer's debug logging system,
2606 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2608 * This function is primarily for printing debug output.
2613 gst_printerrln (const gchar * format, ...)
2618 va_start (args, format);
2619 str = gst_info_strdup_vprintf (format, args);
2622 g_printerr ("%s\n", str);
2629 append_debug_info (GString * trace, Dwfl * dwfl, const void *ip)
2633 Dwfl_Module *module;
2634 const gchar *function_name;
2636 if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
2639 if (dwfl_report_end (dwfl, NULL, NULL))
2642 addr = (uintptr_t) ip;
2643 module = dwfl_addrmodule (dwfl, addr);
2644 function_name = dwfl_module_addrname (module, addr);
2646 g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
2648 line = dwfl_getsrc (dwfl, addr);
2652 const gchar *filename = dwfl_lineinfo (line, &addr,
2653 &nline, NULL, NULL, NULL);
2655 g_string_append_printf (trace, "%s:%d", strrchr (filename,
2656 G_DIR_SEPARATOR) + 1, nline);
2658 const gchar *eflfile = NULL;
2660 dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
2661 g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
2666 #endif /* HAVE_DW */
2669 generate_unwind_trace (GstStackTraceFlags flags)
2673 unw_cursor_t cursor;
2674 gboolean use_libunwind = TRUE;
2675 GString *trace = g_string_new (NULL);
2679 Dwfl_Callbacks callbacks = {
2680 .find_elf = dwfl_linux_proc_find_elf,
2681 .find_debuginfo = dwfl_standard_find_debuginfo,
2684 if ((flags & GST_STACK_TRACE_SHOW_FULL))
2685 dwfl = dwfl_begin (&callbacks);
2686 #endif /* HAVE_DW */
2688 unret = unw_getcontext (&uc);
2690 GST_DEBUG ("Could not get libunwind context (%d)", unret);
2694 unret = unw_init_local (&cursor, &uc);
2696 GST_DEBUG ("Could not init libunwind context (%d)", unret);
2701 while (unw_step (&cursor) > 0) {
2706 unret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
2708 GST_DEBUG ("libunwind could read frame info (%d)", unret);
2713 if (append_debug_info (trace, dwfl, (void *) (ip - 4))) {
2714 use_libunwind = FALSE;
2715 g_string_append (trace, ")\n");
2718 #endif /* HAVE_DW */
2720 if (use_libunwind) {
2723 unw_word_t offset = 0;
2724 unw_get_proc_name (&cursor, name, sizeof (name), &offset);
2725 g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
2736 return g_string_free (trace, FALSE);
2739 #endif /* HAVE_UNWIND */
2741 #ifdef HAVE_BACKTRACE
2743 generate_backtrace_trace (void)
2746 void *buffer[BT_BUF_SIZE];
2750 trace = g_string_new (NULL);
2751 nptrs = backtrace (buffer, BT_BUF_SIZE);
2753 strings = backtrace_symbols (buffer, nptrs);
2758 for (j = 0; j < nptrs; j++)
2759 g_string_append_printf (trace, "%s\n", strings[j]);
2763 return g_string_free (trace, FALSE);
2766 #define generate_backtrace_trace() NULL
2767 #endif /* HAVE_BACKTRACE */
2770 * gst_debug_get_stack_trace:
2771 * @flags: A set of #GstStackTraceFlags to determine how the stack
2772 * trace should look like. Pass 0 to retrieve a minimal backtrace.
2774 * Returns: (nullable): a stack trace, if libunwind or glibc backtrace are
2775 * present, else %NULL.
2780 gst_debug_get_stack_trace (GstStackTraceFlags flags)
2782 gchar *trace = NULL;
2783 #ifdef HAVE_BACKTRACE
2784 gboolean have_backtrace = TRUE;
2786 gboolean have_backtrace = FALSE;
2790 if ((flags & GST_STACK_TRACE_SHOW_FULL) || !have_backtrace)
2791 trace = generate_unwind_trace (flags);
2792 #endif /* HAVE_UNWIND */
2796 else if (have_backtrace)
2797 return generate_backtrace_trace ();
2803 * gst_debug_print_stack_trace:
2805 * If libunwind or glibc backtrace are present
2806 * a stack trace is printed.
2809 gst_debug_print_stack_trace (void)
2811 gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
2814 g_print ("%s\n", trace);
2819 #ifndef GST_DISABLE_GST_DEBUG
2822 guint max_size_per_thread;
2823 guint thread_timeout;
2825 GHashTable *thread_index;
2826 } GstRingBufferLogger;
2838 G_LOCK_DEFINE_STATIC (ring_buffer_logger);
2839 static GstRingBufferLogger *ring_buffer_logger = NULL;
2842 gst_ring_buffer_logger_log (GstDebugCategory * category,
2843 GstDebugLevel level,
2845 const gchar * function,
2846 gint line, GObject * object, GstDebugMessage * message, gpointer user_data)
2848 GstRingBufferLogger *logger = user_data;
2851 GstClockTime elapsed;
2856 GstRingBufferLog *log;
2857 gint64 now = g_get_monotonic_time ();
2858 const gchar *message_str = gst_debug_message_get (message);
2860 G_LOCK (ring_buffer_logger);
2862 if (logger->thread_timeout > 0) {
2863 /* Remove all threads that saw no output since thread_timeout seconds.
2864 * By construction these are all at the tail of the queue, and the queue
2865 * is ordered by last use, so we just need to look at the tail.
2867 while (logger->threads.tail) {
2868 log = logger->threads.tail->data;
2869 if (log->last_use + logger->thread_timeout * G_USEC_PER_SEC >= now)
2872 g_hash_table_remove (logger->thread_index, log->thread);
2873 while ((output = g_queue_pop_head (&log->log)))
2876 g_queue_pop_tail (&logger->threads);
2880 /* Get logger for this thread, and put it back at the
2881 * head of the threads queue */
2882 thread = g_thread_self ();
2883 log = g_hash_table_lookup (logger->thread_index, thread);
2885 log = g_new0 (GstRingBufferLog, 1);
2886 g_queue_init (&log->log);
2888 g_queue_push_head (&logger->threads, log);
2889 log->link = logger->threads.head;
2890 log->thread = thread;
2891 g_hash_table_insert (logger->thread_index, thread, log);
2893 g_queue_unlink (&logger->threads, log->link);
2894 g_queue_push_head_link (&logger->threads, log->link);
2896 log->last_use = now;
2898 /* __FILE__ might be a file name or an absolute path or a
2899 * relative path, irrespective of the exact compiler used,
2900 * in which case we want to shorten it to the filename for
2903 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
2904 file = gst_path_basename (file);
2910 obj = gst_debug_print_object (object);
2915 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
2917 /* no color, all platforms */
2918 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
2920 g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
2921 pid, thread, gst_debug_level_get_name (level),
2922 gst_debug_category_get_name (category), file, line, function, obj,
2926 output_len = strlen (output);
2928 if (output_len < logger->max_size_per_thread) {
2931 /* While using a GQueue here is not the most efficient thing to do, we
2932 * have to allocate a string for every output anyway and could just store
2933 * that instead of copying it to an actual ringbuffer.
2934 * Better than GQueue would be GstQueueArray, but that one is in
2935 * libgstbase and we can't use it here. That one allocation will not make
2936 * much of a difference anymore, considering the number of allocations
2937 * needed to get to this point...
2939 while (log->log_size + output_len > logger->max_size_per_thread) {
2940 buf = g_queue_pop_head (&log->log);
2941 log->log_size -= strlen (buf);
2944 g_queue_push_tail (&log->log, output);
2945 log->log_size += output_len;
2949 /* Can't really write anything as the line is bigger than the maximum
2950 * allowed log size already, so just remove everything */
2952 while ((buf = g_queue_pop_head (&log->log)))
2961 G_UNLOCK (ring_buffer_logger);
2965 * gst_debug_ring_buffer_logger_get_logs:
2967 * Fetches the current logs per thread from the ring buffer logger. See
2968 * gst_debug_add_ring_buffer_logger() for details.
2970 * Returns: (transfer full) (array zero-terminated): NULL-terminated array of
2971 * strings with the debug output per thread
2976 gst_debug_ring_buffer_logger_get_logs (void)
2978 gchar **logs, **tmp;
2981 g_return_val_if_fail (ring_buffer_logger != NULL, NULL);
2983 G_LOCK (ring_buffer_logger);
2985 tmp = logs = g_new0 (gchar *, ring_buffer_logger->threads.length + 1);
2986 for (l = ring_buffer_logger->threads.head; l; l = l->next) {
2987 GstRingBufferLog *log = l->data;
2992 *tmp = p = g_new0 (gchar, log->log_size + 1);
2994 for (l = log->log.head; l; l = l->next) {
2995 len = strlen (l->data);
2996 memcpy (p, l->data, len);
3003 G_UNLOCK (ring_buffer_logger);
3009 gst_ring_buffer_logger_free (GstRingBufferLogger * logger)
3011 G_LOCK (ring_buffer_logger);
3012 if (ring_buffer_logger == logger) {
3013 GstRingBufferLog *log;
3015 while ((log = g_queue_pop_head (&logger->threads))) {
3017 while ((buf = g_queue_pop_head (&log->log)))
3022 g_hash_table_unref (logger->thread_index);
3025 ring_buffer_logger = NULL;
3027 G_UNLOCK (ring_buffer_logger);
3031 * gst_debug_add_ring_buffer_logger:
3032 * @max_size_per_thread: Maximum size of log per thread in bytes
3033 * @thread_timeout: Timeout for threads in seconds
3035 * Adds a memory ringbuffer based debug logger that stores up to
3036 * @max_size_per_thread bytes of logs per thread and times out threads after
3037 * @thread_timeout seconds of inactivity.
3039 * Logs can be fetched with gst_debug_ring_buffer_logger_get_logs() and the
3040 * logger can be removed again with gst_debug_remove_ring_buffer_logger().
3041 * Only one logger at a time is possible.
3046 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3047 guint thread_timeout)
3049 GstRingBufferLogger *logger;
3051 G_LOCK (ring_buffer_logger);
3053 if (ring_buffer_logger) {
3054 g_warn_if_reached ();
3055 G_UNLOCK (ring_buffer_logger);
3059 logger = ring_buffer_logger = g_new0 (GstRingBufferLogger, 1);
3061 logger->max_size_per_thread = max_size_per_thread;
3062 logger->thread_timeout = thread_timeout;
3063 logger->thread_index = g_hash_table_new (g_direct_hash, g_direct_equal);
3064 g_queue_init (&logger->threads);
3066 gst_debug_add_log_function (gst_ring_buffer_logger_log, logger,
3067 (GDestroyNotify) gst_ring_buffer_logger_free);
3068 G_UNLOCK (ring_buffer_logger);
3072 * gst_debug_remove_ring_buffer_logger:
3074 * Removes any previously added ring buffer logger with
3075 * gst_debug_add_ring_buffer_logger().
3080 gst_debug_remove_ring_buffer_logger (void)
3082 gst_debug_remove_log_function (gst_ring_buffer_logger_log);
3085 #else /* GST_DISABLE_GST_DEBUG */
3086 #ifndef GST_REMOVE_DISABLED
3089 gst_debug_ring_buffer_logger_get_logs (void)
3095 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3096 guint thread_timeout)
3101 gst_debug_remove_ring_buffer_logger (void)
3105 #endif /* GST_REMOVE_DISABLED */
3106 #endif /* GST_DISABLE_GST_DEBUG */