2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5 * Copyright (C) 2008-2009 Tim-Philipp Müller <tim centricular net>
7 * gstinfo.c: debugging functions
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
28 * @short_description: Debugging and logging facilities
29 * @see_also: #gst-running for command line parameters
30 * and environment variables that affect the debugging output.
32 * GStreamer's debugging subsystem is an easy way to get information about what
33 * the application is doing. It is not meant for programming errors. Use GLib
34 * methods (g_warning and friends) for that.
36 * The debugging subsystem works only after GStreamer has been initialized
37 * - for example by calling gst_init().
39 * The debugging subsystem is used to log informational messages while the
40 * application runs. Each messages has some properties attached to it. Among
41 * these properties are the debugging category, the severity (called "level"
42 * here) and an optional #GObject it belongs to. Each of these messages is sent
43 * to all registered debugging handlers, which then handle the messages.
44 * GStreamer attaches a default handler on startup, which outputs requested
47 * Messages are output by using shortcut macros like #GST_DEBUG,
48 * #GST_CAT_ERROR_OBJECT or similar. These all expand to calling gst_debug_log()
49 * with the right parameters.
50 * The only thing a developer will probably want to do is define his own
51 * categories. This is easily done with 3 lines. At the top of your code,
53 * the variables and set the default category.
54 * |[<!-- language="C" -->
55 * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically)
56 * #define GST_CAT_DEFAULT my_category // set as default
58 * After that you only need to initialize the category.
59 * |[<!-- language="C" -->
60 * GST_DEBUG_CATEGORY_INIT (my_category, "my category",
61 * 0, "This is my very own");
63 * Initialization must be done before the category is used first.
65 * in their plugin_init function, libraries and applications should do that
66 * during their initialization.
68 * The whole debugging subsystem can be disabled at build time with passing the
69 * --disable-gst-debug switch to configure. If this is done, every function,
70 * macro and even structs described in this file evaluate to default values or
72 * So don't take addresses of these functions or use other tricks.
73 * If you must do that for some reason, there is still an option.
75 * subsystem was compiled out, GST_DISABLE_GST_DEBUG is defined in
77 * so you can check that before doing your trick.
78 * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
79 * speed increase and will reduce the size of your compiled code. The GStreamer
80 * library itself becomes around 10% smaller.
82 * Please note that there are naming conventions for the names of debugging
83 * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
87 #include "gst_private.h"
90 #undef gst_debug_remove_log_function
91 #undef gst_debug_add_log_function
93 #ifndef GST_DISABLE_GST_DEBUG
97 #include <stdio.h> /* fprintf */
98 #include <glib/gstdio.h>
101 # include <unistd.h> /* getpid on UNIX */
103 #ifdef HAVE_PROCESS_H
104 # include <process.h> /* getpid on win32 */
106 #include <string.h> /* G_VA_COPY */
108 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
109 # include <windows.h> /* GetStdHandle, windows console */
112 #include "gst_private.h"
113 #include "gstutils.h"
114 #include "gstquark.h"
115 #include "gstsegment.h"
116 #include "gstvalue.h"
117 #include "gstcapsfeatures.h"
119 #ifdef HAVE_VALGRIND_VALGRIND_H
120 # include <valgrind/valgrind.h>
122 #include <glib/gprintf.h> /* g_sprintf */
124 /* our own printf implementation with custom extensions to %p for caps etc. */
125 #include "printf/printf.h"
126 #include "printf/printf-extension.h"
128 static char *gst_info_printf_pointer_extension_func (const char *format,
130 #else /* GST_DISABLE_GST_DEBUG */
132 #include <glib/gprintf.h>
133 #endif /* !GST_DISABLE_GST_DEBUG */
136 /* No need for remote debugging so turn on the 'local only' optimizations in
138 #define UNW_LOCAL_ONLY
140 #include <libunwind.h>
149 #include <elfutils/libdwfl.h>
151 #endif /* HAVE_UNWIND */
153 #ifdef HAVE_BACKTRACE
154 #include <execinfo.h>
155 #define BT_BUF_SIZE 100
156 #endif /* HAVE_BACKTRACE */
161 #include <tlhelp32.h>
162 #endif /* HAVE_DBGHELP */
164 extern gboolean gst_is_initialized (void);
166 /* we want these symbols exported even if debug is disabled, to maintain
167 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
168 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
170 /* disabled by default, as soon as some threshold is set > NONE,
171 * it becomes enabled. */
172 gboolean _gst_debug_enabled = FALSE;
173 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
175 GstDebugCategory *GST_CAT_DEFAULT = NULL;
177 GstDebugCategory *GST_CAT_GST_INIT = NULL;
178 GstDebugCategory *GST_CAT_MEMORY = NULL;
179 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
180 GstDebugCategory *GST_CAT_STATES = NULL;
181 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
183 GstDebugCategory *GST_CAT_BUFFER = NULL;
184 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
185 GstDebugCategory *GST_CAT_BUS = NULL;
186 GstDebugCategory *GST_CAT_CAPS = NULL;
187 GstDebugCategory *GST_CAT_CLOCK = NULL;
188 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
189 GstDebugCategory *GST_CAT_PADS = NULL;
190 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
191 GstDebugCategory *GST_CAT_PIPELINE = NULL;
192 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
193 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
194 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
195 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
196 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
197 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
198 GstDebugCategory *GST_CAT_EVENT = NULL;
199 GstDebugCategory *GST_CAT_MESSAGE = NULL;
200 GstDebugCategory *GST_CAT_PARAMS = NULL;
201 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
202 GstDebugCategory *GST_CAT_SIGNAL = NULL;
203 GstDebugCategory *GST_CAT_PROBE = NULL;
204 GstDebugCategory *GST_CAT_REGISTRY = NULL;
205 GstDebugCategory *GST_CAT_QOS = NULL;
206 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
207 GstDebugCategory *GST_CAT_META = NULL;
208 GstDebugCategory *GST_CAT_LOCKING = NULL;
209 GstDebugCategory *GST_CAT_CONTEXT = NULL;
210 GstDebugCategory *_priv_GST_CAT_PROTECTION = NULL;
213 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
215 #ifndef GST_DISABLE_GST_DEBUG
217 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
218 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
222 #include <rld_interface.h>
223 typedef struct DL_INFO
225 const char *dli_fname;
227 const char *dli_sname;
231 long dli_reserved[4];
235 #define _RLD_DLADDR 14
236 int dladdr (void *address, Dl_info * dl);
239 dladdr (void *address, Dl_info * dl)
243 v = _rld_new_interface (_RLD_DLADDR, address, dl);
249 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
250 static void gst_debug_reset_all_thresholds (void);
252 struct _GstDebugMessage
259 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
260 static GMutex __level_name_mutex;
261 static GSList *__level_name = NULL;
269 /* list of all categories */
270 static GMutex __cat_mutex;
271 static GSList *__categories = NULL;
273 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
276 /* all registered debug handlers */
281 GDestroyNotify notify;
284 static GMutex __log_func_mutex;
285 static GSList *__log_functions = NULL;
287 /* whether to add the default log function in gst_init() */
288 static gboolean add_default_log_func = TRUE;
290 #define PRETTY_TAGS_DEFAULT TRUE
291 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
293 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
294 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
296 /* FIXME: export this? */
298 _priv_gst_in_valgrind (void)
306 in_valgrind = GST_VG_UNCHECKED;
308 if (in_valgrind == GST_VG_UNCHECKED) {
309 #ifdef HAVE_VALGRIND_VALGRIND_H
310 if (RUNNING_ON_VALGRIND) {
311 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
312 in_valgrind = GST_VG_INSIDE;
314 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
315 in_valgrind = GST_VG_NO_VALGRIND;
318 in_valgrind = GST_VG_NO_VALGRIND;
320 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
321 in_valgrind == GST_VG_INSIDE);
323 return (in_valgrind == GST_VG_INSIDE);
327 _replace_pattern_in_gst_debug_file_name (gchar * name, const char *token,
331 if ((token_start = strstr (name, token))) {
332 gsize token_len = strlen (token);
333 gchar *name_prefix = name;
334 gchar *name_suffix = token_start + token_len;
335 token_start[0] = '\0';
336 name = g_strdup_printf ("%s%u%s", name_prefix, val, name_suffix);
337 g_free (name_prefix);
343 _priv_gst_debug_file_name (const gchar * env)
347 name = g_strdup (env);
348 name = _replace_pattern_in_gst_debug_file_name (name, "%p", getpid ());
349 name = _replace_pattern_in_gst_debug_file_name (name, "%r", g_random_int ());
354 /* Initialize the debugging system */
356 _priv_gst_debug_init (void)
361 if (add_default_log_func) {
362 env = g_getenv ("GST_DEBUG_FILE");
363 if (env != NULL && *env != '\0') {
364 if (strcmp (env, "-") == 0) {
367 gchar *name = _priv_gst_debug_file_name (env);
368 log_file = g_fopen (name, "w");
370 if (log_file == NULL) {
371 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
380 gst_debug_add_log_function (gst_debug_log_default, log_file, NULL);
383 __gst_printf_pointer_extension_set_func
384 (gst_info_printf_pointer_extension_func);
386 /* do NOT use a single debug function before this line has been run */
387 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
388 GST_DEBUG_UNDERLINE, NULL);
389 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
392 /* FIXME: add descriptions here */
393 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
394 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
395 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
396 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
397 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
398 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
399 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
400 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
401 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
402 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
403 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
404 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
405 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
406 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
407 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
408 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
410 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
412 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
413 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
414 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
415 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
416 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
417 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
418 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
419 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
420 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
421 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
422 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
423 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
424 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
425 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
426 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
427 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
428 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
429 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
430 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
431 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
433 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
434 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
435 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
436 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
437 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
438 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
439 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
440 GST_DEBUG_BOLD, NULL);
441 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
442 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
443 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
444 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
445 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
446 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
447 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
448 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
449 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
450 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
451 _priv_GST_CAT_PROTECTION =
452 _gst_debug_category_new ("GST_PROTECTION", 0, "protection");
454 /* print out the valgrind message if we're in valgrind */
455 _priv_gst_in_valgrind ();
457 env = g_getenv ("GST_DEBUG_OPTIONS");
459 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
461 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
465 if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
466 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
467 env = g_getenv ("GST_DEBUG_COLOR_MODE");
469 gst_debug_set_color_mode_from_string (env);
471 env = g_getenv ("GST_DEBUG");
473 gst_debug_set_threshold_from_string (env, FALSE);
476 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
477 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
481 * @category: category to log
482 * @level: level of the message is in
483 * @file: the file that emitted the message, usually the __FILE__ identifier
484 * @function: the function that emitted the message
485 * @line: the line from that the message was emitted, usually __LINE__
486 * @object: (transfer none) (allow-none): the object this message relates to,
488 * @format: a printf style format string
489 * @...: optional arguments for the format
491 * Logs the given message using the currently registered debugging handlers.
494 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
495 const gchar * file, const gchar * function, gint line,
496 GObject * object, const gchar * format, ...)
500 va_start (var_args, format);
501 gst_debug_log_valist (category, level, file, function, line, object, format,
506 /* based on g_basename(), which we can't use because it was deprecated */
507 static inline const gchar *
508 gst_path_basename (const gchar * file_name)
510 register const gchar *base;
512 base = strrchr (file_name, G_DIR_SEPARATOR);
515 const gchar *q = strrchr (file_name, '/');
516 if (base == NULL || (q != NULL && q > base))
523 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
524 return file_name + 2;
530 * gst_debug_log_valist:
531 * @category: category to log
532 * @level: level of the message is in
533 * @file: the file that emitted the message, usually the __FILE__ identifier
534 * @function: the function that emitted the message
535 * @line: the line from that the message was emitted, usually __LINE__
536 * @object: (transfer none) (allow-none): the object this message relates to,
538 * @format: a printf style format string
539 * @args: optional arguments for the format
541 * Logs the given message using the currently registered debugging handlers.
544 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
545 const gchar * file, const gchar * function, gint line,
546 GObject * object, const gchar * format, va_list args)
548 GstDebugMessage message;
552 g_return_if_fail (category != NULL);
554 if (level > gst_debug_category_get_threshold (category))
557 g_return_if_fail (file != NULL);
558 g_return_if_fail (function != NULL);
559 g_return_if_fail (format != NULL);
561 message.message = NULL;
562 message.format = format;
563 G_VA_COPY (message.arguments, args);
565 handler = __log_functions;
567 entry = handler->data;
568 handler = g_slist_next (handler);
569 entry->func (category, level, file, function, line, object, &message,
572 g_free (message.message);
573 va_end (message.arguments);
577 * gst_debug_message_get:
578 * @message: a debug message
580 * Gets the string representation of a #GstDebugMessage. This function is used
581 * in debug handlers to extract the message.
583 * Returns: (nullable): the string representation of a #GstDebugMessage.
586 gst_debug_message_get (GstDebugMessage * message)
588 if (message->message == NULL) {
591 len = __gst_vasprintf (&message->message, message->format,
595 message->message = NULL;
597 return message->message;
600 #define MAX_BUFFER_DUMP_STRING_LEN 100
602 /* structure_to_pretty_string:
603 * @str: a serialized #GstStructure
605 * If the serialized structure contains large buffers such as images the hex
606 * representation of those buffers will be shortened so that the string remains
609 * Returns: the filtered string
612 prettify_structure_string (gchar * str)
614 gchar *pos = str, *end;
616 while ((pos = strstr (pos, "(buffer)"))) {
619 pos += strlen ("(buffer)");
620 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
622 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
623 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
624 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
625 memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
626 strlen (pos + count) + 1);
627 pos += MAX_BUFFER_DUMP_STRING_LEN;
634 static inline gchar *
635 gst_info_structure_to_string (const GstStructure * s)
638 gchar *str = gst_structure_to_string (s);
639 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
640 return prettify_structure_string (str);
647 static inline gchar *
648 gst_info_describe_buffer (GstBuffer * buffer)
650 const gchar *offset_str = "none";
651 const gchar *offset_end_str = "none";
652 gchar offset_buf[32], offset_end_buf[32];
654 if (GST_BUFFER_OFFSET_IS_VALID (buffer)) {
655 g_snprintf (offset_buf, sizeof (offset_buf), "%" G_GUINT64_FORMAT,
656 GST_BUFFER_OFFSET (buffer));
657 offset_str = offset_buf;
659 if (GST_BUFFER_OFFSET_END_IS_VALID (buffer)) {
660 g_snprintf (offset_end_buf, sizeof (offset_end_buf), "%" G_GUINT64_FORMAT,
661 GST_BUFFER_OFFSET_END (buffer));
662 offset_end_str = offset_end_buf;
665 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
666 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
667 ", offset %s, offset_end %s, flags 0x%x", buffer,
668 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
669 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
670 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
671 gst_buffer_get_size (buffer), offset_str, offset_end_str,
672 GST_BUFFER_FLAGS (buffer));
675 static inline gchar *
676 gst_info_describe_buffer_list (GstBufferList * list)
678 GstClockTime pts = GST_CLOCK_TIME_NONE;
679 GstClockTime dts = GST_CLOCK_TIME_NONE;
680 gsize total_size = 0;
683 n = gst_buffer_list_length (list);
684 for (i = 0; i < n; ++i) {
685 GstBuffer *buf = gst_buffer_list_get (list, i);
688 pts = GST_BUFFER_PTS (buf);
689 dts = GST_BUFFER_DTS (buf);
692 total_size += gst_buffer_get_size (buf);
695 return g_strdup_printf ("bufferlist: %p, %u buffers, pts %" GST_TIME_FORMAT
696 ", dts %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT, list, n,
697 GST_TIME_ARGS (pts), GST_TIME_ARGS (dts), total_size);
700 static inline gchar *
701 gst_info_describe_event (GstEvent * event)
705 s = gst_info_structure_to_string (gst_event_get_structure (event));
706 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
707 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
708 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
714 static inline gchar *
715 gst_info_describe_message (GstMessage * message)
719 s = gst_info_structure_to_string (gst_message_get_structure (message));
720 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
721 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
722 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
723 GST_MESSAGE_SEQNUM (message),
724 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
730 static inline gchar *
731 gst_info_describe_query (GstQuery * query)
735 s = gst_info_structure_to_string (gst_query_get_structure (query));
736 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
737 query, (s ? s : "(NULL)"));
742 static inline gchar *
743 gst_info_describe_stream (GstStream * stream)
745 gchar *ret, *caps_str = NULL, *tags_str = NULL;
749 caps = gst_stream_get_caps (stream);
751 caps_str = gst_caps_to_string (caps);
752 gst_caps_unref (caps);
755 tags = gst_stream_get_tags (stream);
757 tags_str = gst_tag_list_to_string (tags);
758 gst_tag_list_unref (tags);
762 g_strdup_printf ("stream %s %p, ID %s, flags 0x%x, caps [%s], tags [%s]",
763 gst_stream_type_get_name (gst_stream_get_stream_type (stream)), stream,
764 gst_stream_get_stream_id (stream), gst_stream_get_stream_flags (stream),
765 caps_str ? caps_str : "", tags_str ? tags_str : "");
773 static inline gchar *
774 gst_info_describe_stream_collection (GstStreamCollection * collection)
777 GString *streams_str;
780 streams_str = g_string_new ("<");
781 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
782 GstStream *stream = gst_stream_collection_get_stream (collection, i);
785 s = gst_info_describe_stream (stream);
786 g_string_append_printf (streams_str, " %s,", s);
789 g_string_append (streams_str, " >");
791 ret = g_strdup_printf ("collection %p (%d streams) %s", collection,
792 gst_stream_collection_get_size (collection), streams_str->str);
794 g_string_free (streams_str, TRUE);
799 gst_debug_print_object (gpointer ptr)
801 GObject *object = (GObject *) ptr;
804 /* This is a cute trick to detect unmapped memory, but is unportable,
805 * slow, screws around with madvise, and not actually that useful. */
809 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
810 if (ret == -1 && errno == ENOMEM) {
811 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
816 /* nicely printed object */
817 if (object == NULL) {
818 return g_strdup ("(NULL)");
820 if (GST_IS_CAPS (ptr)) {
821 return gst_caps_to_string ((const GstCaps *) ptr);
823 if (GST_IS_STRUCTURE (ptr)) {
824 return gst_info_structure_to_string ((const GstStructure *) ptr);
826 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
827 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
829 if (GST_IS_TAG_LIST (ptr)) {
830 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
831 if (G_UNLIKELY (pretty_tags))
832 return prettify_structure_string (str);
836 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
837 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
839 if (GST_IS_BUFFER (ptr)) {
840 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
842 if (GST_IS_BUFFER_LIST (ptr)) {
843 return gst_info_describe_buffer_list (GST_BUFFER_LIST_CAST (ptr));
846 if (*(guint32 *) ptr == 0xffffffff) {
847 return g_strdup_printf ("<poisoned@%p>", ptr);
850 if (GST_IS_MESSAGE (object)) {
851 return gst_info_describe_message (GST_MESSAGE_CAST (object));
853 if (GST_IS_QUERY (object)) {
854 return gst_info_describe_query (GST_QUERY_CAST (object));
856 if (GST_IS_EVENT (object)) {
857 return gst_info_describe_event (GST_EVENT_CAST (object));
859 if (GST_IS_CONTEXT (object)) {
860 GstContext *context = GST_CONTEXT_CAST (object);
863 const GstStructure *structure;
865 type = gst_context_get_context_type (context);
866 structure = gst_context_get_structure (context);
868 s = gst_info_structure_to_string (structure);
870 ret = g_strdup_printf ("context '%s'='%s'", type, s);
874 if (GST_IS_STREAM (object)) {
875 return gst_info_describe_stream (GST_STREAM_CAST (object));
877 if (GST_IS_STREAM_COLLECTION (object)) {
879 gst_info_describe_stream_collection (GST_STREAM_COLLECTION_CAST
882 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
883 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
885 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
886 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
888 if (G_IS_OBJECT (object)) {
889 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
892 return g_strdup_printf ("%p", ptr);
896 gst_debug_print_segment (gpointer ptr)
898 GstSegment *segment = (GstSegment *) ptr;
900 /* nicely printed segment */
901 if (segment == NULL) {
902 return g_strdup ("(NULL)");
905 switch (segment->format) {
906 case GST_FORMAT_UNDEFINED:{
907 return g_strdup_printf ("UNDEFINED segment");
909 case GST_FORMAT_TIME:{
910 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
911 ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
912 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
913 ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
914 ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
915 GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
916 segment->rate, segment->applied_rate, (guint) segment->flags,
917 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
918 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
921 const gchar *format_name;
923 format_name = gst_format_get_name (segment->format);
924 if (G_UNLIKELY (format_name == NULL))
925 format_name = "(UNKNOWN FORMAT)";
926 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
927 ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
928 ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
929 ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
930 ", duration %" G_GINT64_FORMAT, format_name, segment->start,
931 segment->offset, segment->stop, segment->rate, segment->applied_rate,
932 (guint) segment->flags, segment->time, segment->base,
933 segment->position, segment->duration);
939 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
943 if (format[0] == 'p' && format[1] == '\a') {
945 case 'A': /* GST_PTR_FORMAT */
946 s = gst_debug_print_object (ptr);
948 case 'B': /* GST_SEGMENT_FORMAT */
949 s = gst_debug_print_segment (ptr);
951 case 'a': /* GST_WRAPPED_PTR_FORMAT */
952 s = priv_gst_string_take_and_wrap (gst_debug_print_object (ptr));
955 /* must have been compiled against a newer version with an extension
956 * we don't known about yet - just ignore and fallback to %p below */
961 s = g_strdup_printf ("%p", ptr);
967 * gst_debug_construct_term_color:
968 * @colorinfo: the color info
970 * Constructs a string that can be used for getting the desired color in color
972 * You need to free the string after use.
974 * Returns: (transfer full) (type gchar*): a string containing the color
978 gst_debug_construct_term_color (guint colorinfo)
982 color = g_string_new ("\033[00");
984 if (colorinfo & GST_DEBUG_BOLD) {
985 g_string_append_len (color, ";01", 3);
987 if (colorinfo & GST_DEBUG_UNDERLINE) {
988 g_string_append_len (color, ";04", 3);
990 if (colorinfo & GST_DEBUG_FG_MASK) {
991 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
993 if (colorinfo & GST_DEBUG_BG_MASK) {
994 g_string_append_printf (color, ";4%1d",
995 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
997 g_string_append_c (color, 'm');
999 return g_string_free (color, FALSE);
1003 * gst_debug_construct_win_color:
1004 * @colorinfo: the color info
1006 * Constructs an integer that can be used for getting the desired color in
1007 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
1008 * ignore this attribute.
1010 * This function returns 0 on non-windows machines.
1012 * Returns: an integer containing the color definition
1015 gst_debug_construct_win_color (guint colorinfo)
1019 static const guchar ansi_to_win_fg[8] = {
1021 FOREGROUND_RED, /* red */
1022 FOREGROUND_GREEN, /* green */
1023 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
1024 FOREGROUND_BLUE, /* blue */
1025 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
1026 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
1027 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
1029 static const guchar ansi_to_win_bg[8] = {
1033 BACKGROUND_RED | BACKGROUND_GREEN,
1035 BACKGROUND_RED | BACKGROUND_BLUE,
1036 BACKGROUND_GREEN | FOREGROUND_BLUE,
1037 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
1040 /* we draw black as white, as cmd.exe can only have black bg */
1041 if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
1042 color = ansi_to_win_fg[7];
1044 if (colorinfo & GST_DEBUG_UNDERLINE) {
1045 color |= BACKGROUND_INTENSITY;
1047 if (colorinfo & GST_DEBUG_BOLD) {
1048 color |= FOREGROUND_INTENSITY;
1050 if (colorinfo & GST_DEBUG_FG_MASK) {
1051 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
1053 if (colorinfo & GST_DEBUG_BG_MASK) {
1054 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
1060 /* width of %p varies depending on actual value of pointer, which can make
1061 * output unevenly aligned if multiple threads are involved, hence the %14p
1062 * (should really be %18p, but %14p seems a good compromise between too many
1063 * white spaces and likely unalignment on my system) */
1064 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
1065 #define PTR_FMT "%14p"
1067 #define PTR_FMT "%10p"
1069 #define PID_FMT "%5d"
1070 #define CAT_FMT "%20s %s:%d:%s:%s"
1073 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
1074 /* GST_LEVEL_NONE */
1075 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1076 /* GST_LEVEL_ERROR */
1077 FOREGROUND_RED | FOREGROUND_INTENSITY,
1078 /* GST_LEVEL_WARNING */
1079 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1080 /* GST_LEVEL_INFO */
1081 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
1082 /* GST_LEVEL_DEBUG */
1083 FOREGROUND_GREEN | FOREGROUND_BLUE,
1085 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1086 /* GST_LEVEL_FIXME */
1087 FOREGROUND_RED | FOREGROUND_GREEN,
1088 /* GST_LEVEL_TRACE */
1089 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
1090 /* placeholder for log level 8 */
1092 /* GST_LEVEL_MEMDUMP */
1093 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
1096 static const guchar available_colors[] = {
1097 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
1098 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
1099 FOREGROUND_GREEN | FOREGROUND_BLUE,
1101 #endif /* G_OS_WIN32 */
1102 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
1103 "\033[37m", /* GST_LEVEL_NONE */
1104 "\033[31;01m", /* GST_LEVEL_ERROR */
1105 "\033[33;01m", /* GST_LEVEL_WARNING */
1106 "\033[32;01m", /* GST_LEVEL_INFO */
1107 "\033[36m", /* GST_LEVEL_DEBUG */
1108 "\033[37m", /* GST_LEVEL_LOG */
1109 "\033[33;01m", /* GST_LEVEL_FIXME */
1110 "\033[37m", /* GST_LEVEL_TRACE */
1111 "\033[37m", /* placeholder for log level 8 */
1112 "\033[37m" /* GST_LEVEL_MEMDUMP */
1116 * gst_debug_log_default:
1117 * @category: category to log
1118 * @level: level of the message
1119 * @file: the file that emitted the message, usually the __FILE__ identifier
1120 * @function: the function that emitted the message
1121 * @line: the line from that the message was emitted, usually __LINE__
1122 * @message: the actual message
1123 * @object: (transfer none) (allow-none): the object this message relates to,
1125 * @user_data: the FILE* to log to
1127 * The default logging handler used by GStreamer. Logging functions get called
1128 * whenever a macro like GST_DEBUG or similar is used. By default this function
1129 * is setup to output the message and additional info to stderr (or the log file
1130 * specified via the GST_DEBUG_FILE environment variable) as received via
1133 * You can add other handlers by using gst_debug_add_log_function().
1134 * And you can remove this handler by calling
1135 * gst_debug_remove_log_function(gst_debug_log_default);
1138 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1139 const gchar * file, const gchar * function, gint line,
1140 GObject * object, GstDebugMessage * message, gpointer user_data)
1143 GstClockTime elapsed;
1145 GstDebugColorMode color_mode;
1146 const gchar *message_str;
1147 FILE *log_file = user_data ? user_data : stderr;
1150 /* Get message string first because printing it might call into our custom
1151 * printf format extension mechanism which in turn might log something, e.g.
1152 * from inside gst_structure_to_string() when something can't be serialised.
1153 * This means we either need to do this outside of any critical section or
1154 * use a recursive lock instead. As we always need the message string in all
1155 * code paths, we might just as well get it here first thing and outside of
1156 * the win_print_mutex critical section. */
1157 message_str = gst_debug_message_get (message);
1159 /* __FILE__ might be a file name or an absolute path or a
1160 * relative path, irrespective of the exact compiler used,
1161 * in which case we want to shorten it to the filename for
1164 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
1165 file = gst_path_basename (file);
1169 color_mode = gst_debug_get_color_mode ();
1172 obj = gst_debug_print_object (object);
1177 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
1179 if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1181 /* We take a lock to keep colors and content together.
1182 * Maybe there is a better way but for now this will do the right
1184 static GMutex win_print_mutex;
1185 g_mutex_lock (&win_print_mutex);
1186 if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1188 /* colors, non-windows */
1189 gchar *color = NULL;
1192 const gchar *levelcolor;
1194 color = gst_debug_construct_term_color (gst_debug_category_get_color
1197 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1198 levelcolor = levelcolormap[level];
1200 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1201 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1202 pidcolor, pid, clear, g_thread_self (), levelcolor,
1203 gst_debug_level_get_name (level), clear, color,
1204 gst_debug_category_get_name (category), file, line, function, obj,
1205 clear, message_str);
1211 /* colors, windows. */
1212 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1213 #define SET_COLOR(c) G_STMT_START { \
1214 if (log_file == stderr) \
1215 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1218 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1221 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1222 fprintf (log_file, PID_FMT, pid);
1226 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1229 SET_COLOR (levelcolormap_w32[level]);
1230 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1233 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1235 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1236 file, line, function, obj);
1240 fprintf (log_file, " %s\n", message_str);
1243 g_mutex_unlock (&win_print_mutex);
1246 /* no color, all platforms */
1247 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1248 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1249 pid, g_thread_self (), gst_debug_level_get_name (level),
1250 gst_debug_category_get_name (category), file, line, function, obj,
1261 * gst_debug_level_get_name:
1262 * @level: the level to get the name for
1264 * Get the string representation of a debugging level
1269 gst_debug_level_get_name (GstDebugLevel level)
1272 case GST_LEVEL_NONE:
1274 case GST_LEVEL_ERROR:
1276 case GST_LEVEL_WARNING:
1278 case GST_LEVEL_INFO:
1280 case GST_LEVEL_DEBUG:
1284 case GST_LEVEL_FIXME:
1286 case GST_LEVEL_TRACE:
1288 case GST_LEVEL_MEMDUMP:
1291 g_warning ("invalid level specified for gst_debug_level_get_name");
1297 * gst_debug_add_log_function:
1298 * @func: the function to use
1299 * @user_data: user data
1300 * @notify: called when @user_data is not used anymore
1302 * Adds the logging function to the list of logging functions.
1303 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1306 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1307 GDestroyNotify notify)
1309 LogFuncEntry *entry;
1313 func = gst_debug_log_default;
1315 entry = g_slice_new (LogFuncEntry);
1317 entry->user_data = user_data;
1318 entry->notify = notify;
1319 /* FIXME: we leak the old list here - other threads might access it right now
1320 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1321 * but that is waaay costly.
1322 * It'd probably be clever to use some kind of RCU here, but I don't know
1323 * anything about that.
1325 g_mutex_lock (&__log_func_mutex);
1326 list = g_slist_copy (__log_functions);
1327 __log_functions = g_slist_prepend (list, entry);
1328 g_mutex_unlock (&__log_func_mutex);
1330 if (gst_is_initialized ())
1331 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1336 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1338 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1340 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1344 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1346 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1348 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1352 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1355 GSList *new, *cleanup = NULL;
1358 g_mutex_lock (&__log_func_mutex);
1359 new = __log_functions;
1361 while ((found = g_slist_find_custom (new, data, func))) {
1362 if (new == __log_functions) {
1363 /* make a copy when we have the first hit, so that we modify the copy and
1364 * make that the new list later */
1365 new = g_slist_copy (new);
1368 cleanup = g_slist_prepend (cleanup, found->data);
1369 new = g_slist_delete_link (new, found);
1372 /* FIXME: We leak the old list here. See _add_log_function for why. */
1373 __log_functions = new;
1374 g_mutex_unlock (&__log_func_mutex);
1377 LogFuncEntry *entry = cleanup->data;
1380 entry->notify (entry->user_data);
1382 g_slice_free (LogFuncEntry, entry);
1383 cleanup = g_slist_delete_link (cleanup, cleanup);
1389 * gst_debug_remove_log_function:
1390 * @func: (scope call) (allow-none): the log function to remove, or %NULL to
1391 * remove the default log function
1393 * Removes all registered instances of the given logging functions.
1395 * Returns: How many instances of the function were removed
1398 gst_debug_remove_log_function (GstLogFunction func)
1403 func = gst_debug_log_default;
1406 gst_debug_remove_with_compare_func
1407 (gst_debug_compare_log_function_by_func, (gpointer) func);
1409 if (gst_is_initialized ()) {
1410 GST_DEBUG ("removed log function %p %d times from log function list", func,
1413 /* If the default log function is removed before gst_init() was called,
1414 * set a flag so we don't add it in gst_init() later */
1415 if (func == gst_debug_log_default) {
1416 add_default_log_func = FALSE;
1425 * gst_debug_remove_log_function_by_data:
1426 * @data: user data of the log function to remove
1428 * Removes all registered instances of log functions with the given user data.
1430 * Returns: How many instances of the function were removed
1433 gst_debug_remove_log_function_by_data (gpointer data)
1438 gst_debug_remove_with_compare_func
1439 (gst_debug_compare_log_function_by_data, data);
1441 if (gst_is_initialized ())
1443 ("removed %d log functions with user data %p from log function list",
1450 * gst_debug_set_colored:
1451 * @colored: Whether to use colored output or not
1453 * Sets or unsets the use of coloured debugging output.
1454 * Same as gst_debug_set_color_mode () with the argument being
1455 * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1457 * This function may be called before gst_init().
1460 gst_debug_set_colored (gboolean colored)
1462 GstDebugColorMode new_mode;
1463 new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1464 g_atomic_int_set (&__use_color, (gint) new_mode);
1468 * gst_debug_set_color_mode:
1469 * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1471 * Changes the coloring mode for debug output.
1473 * This function may be called before gst_init().
1478 gst_debug_set_color_mode (GstDebugColorMode mode)
1480 g_atomic_int_set (&__use_color, mode);
1484 * gst_debug_set_color_mode_from_string:
1485 * @mode: The coloring mode for debug output. One of the following:
1486 * "on", "auto", "off", "disable", "unix".
1488 * Changes the coloring mode for debug output.
1490 * This function may be called before gst_init().
1495 gst_debug_set_color_mode_from_string (const gchar * mode)
1497 if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1498 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1499 else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1500 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1501 else if (strcmp (mode, "unix") == 0)
1502 gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1506 * gst_debug_is_colored:
1508 * Checks if the debugging output should be colored.
1510 * Returns: %TRUE, if the debug output should be colored.
1513 gst_debug_is_colored (void)
1515 GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1516 return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1520 * gst_debug_get_color_mode:
1522 * Changes the coloring mode for debug output.
1524 * Returns: see @GstDebugColorMode for possible values.
1529 gst_debug_get_color_mode (void)
1531 return g_atomic_int_get (&__use_color);
1535 * gst_debug_set_active:
1536 * @active: Whether to use debugging output or not
1538 * If activated, debugging messages are sent to the debugging
1540 * It makes sense to deactivate it for speed issues.
1541 * > This function is not threadsafe. It makes sense to only call it
1542 * during initialization.
1545 gst_debug_set_active (gboolean active)
1547 _gst_debug_enabled = active;
1549 _gst_debug_min = GST_LEVEL_COUNT;
1551 _gst_debug_min = GST_LEVEL_NONE;
1555 * gst_debug_is_active:
1557 * Checks if debugging output is activated.
1559 * Returns: %TRUE, if debugging is activated
1562 gst_debug_is_active (void)
1564 return _gst_debug_enabled;
1568 * gst_debug_set_default_threshold:
1569 * @level: level to set
1571 * Sets the default threshold to the given level and updates all categories to
1572 * use this threshold.
1574 * This function may be called before gst_init().
1577 gst_debug_set_default_threshold (GstDebugLevel level)
1579 g_atomic_int_set (&__default_level, level);
1580 gst_debug_reset_all_thresholds ();
1584 * gst_debug_get_default_threshold:
1586 * Returns the default threshold that is used for new categories.
1588 * Returns: the default threshold level
1591 gst_debug_get_default_threshold (void)
1593 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1597 gst_debug_apply_entry (GstDebugCategory * cat, LevelNameEntry * entry)
1599 if (!g_pattern_match_string (entry->pat, cat->name))
1602 if (gst_is_initialized ())
1603 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1604 cat->name, entry->pat, entry->level);
1606 gst_debug_category_set_threshold (cat, entry->level);
1611 gst_debug_reset_threshold (gpointer category, gpointer unused)
1613 GstDebugCategory *cat = (GstDebugCategory *) category;
1616 g_mutex_lock (&__level_name_mutex);
1618 for (walk = __level_name; walk != NULL; walk = walk->next) {
1619 if (gst_debug_apply_entry (cat, walk->data))
1623 g_mutex_unlock (&__level_name_mutex);
1626 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1630 gst_debug_reset_all_thresholds (void)
1632 g_mutex_lock (&__cat_mutex);
1633 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1634 g_mutex_unlock (&__cat_mutex);
1638 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1640 GstDebugCategory *cat = (GstDebugCategory *) data;
1641 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1643 gst_debug_apply_entry (cat, entry);
1647 * gst_debug_set_threshold_for_name:
1648 * @name: name of the categories to set
1649 * @level: level to set them to
1651 * Sets all categories which match the given glob style pattern to the given
1655 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1658 LevelNameEntry *entry;
1660 g_return_if_fail (name != NULL);
1662 pat = g_pattern_spec_new (name);
1663 entry = g_slice_new (LevelNameEntry);
1665 entry->level = level;
1666 g_mutex_lock (&__level_name_mutex);
1667 __level_name = g_slist_prepend (__level_name, entry);
1668 g_mutex_unlock (&__level_name_mutex);
1669 g_mutex_lock (&__cat_mutex);
1670 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1671 g_mutex_unlock (&__cat_mutex);
1675 * gst_debug_unset_threshold_for_name:
1676 * @name: name of the categories to set
1678 * Resets all categories with the given name back to the default level.
1681 gst_debug_unset_threshold_for_name (const gchar * name)
1686 g_return_if_fail (name != NULL);
1688 pat = g_pattern_spec_new (name);
1689 g_mutex_lock (&__level_name_mutex);
1690 walk = __level_name;
1691 /* improve this if you want, it's mighty slow */
1693 LevelNameEntry *entry = walk->data;
1695 if (g_pattern_spec_equal (entry->pat, pat)) {
1696 __level_name = g_slist_remove_link (__level_name, walk);
1697 g_pattern_spec_free (entry->pat);
1698 g_slice_free (LevelNameEntry, entry);
1699 g_slist_free_1 (walk);
1700 walk = __level_name;
1702 walk = g_slist_next (walk);
1705 g_mutex_unlock (&__level_name_mutex);
1706 g_pattern_spec_free (pat);
1707 gst_debug_reset_all_thresholds ();
1711 _gst_debug_category_new (const gchar * name, guint color,
1712 const gchar * description)
1714 GstDebugCategory *cat, *catfound;
1716 g_return_val_if_fail (name != NULL, NULL);
1718 cat = g_slice_new (GstDebugCategory);
1719 cat->name = g_strdup (name);
1721 if (description != NULL) {
1722 cat->description = g_strdup (description);
1724 cat->description = g_strdup ("no description");
1726 g_atomic_int_set (&cat->threshold, 0);
1727 gst_debug_reset_threshold (cat, NULL);
1729 /* add to category list */
1730 g_mutex_lock (&__cat_mutex);
1731 catfound = _gst_debug_get_category_locked (name);
1733 g_free ((gpointer) cat->name);
1734 g_free ((gpointer) cat->description);
1735 g_slice_free (GstDebugCategory, cat);
1738 __categories = g_slist_prepend (__categories, cat);
1740 g_mutex_unlock (&__cat_mutex);
1746 * gst_debug_category_free:
1747 * @category: #GstDebugCategory to free.
1749 * Removes and frees the category and all associated resources.
1752 gst_debug_category_free (GstDebugCategory * category)
1754 if (category == NULL)
1757 /* remove from category list */
1758 g_mutex_lock (&__cat_mutex);
1759 __categories = g_slist_remove (__categories, category);
1760 g_mutex_unlock (&__cat_mutex);
1762 g_free ((gpointer) category->name);
1763 g_free ((gpointer) category->description);
1764 g_slice_free (GstDebugCategory, category);
1768 * gst_debug_category_set_threshold:
1769 * @category: a #GstDebugCategory to set threshold of.
1770 * @level: the #GstDebugLevel threshold to set.
1772 * Sets the threshold of the category to the given level. Debug information will
1773 * only be output if the threshold is lower or equal to the level of the
1774 * debugging message.
1775 * > Do not use this function in production code, because other functions may
1776 * > change the threshold of categories as side effect. It is however a nice
1777 * > function to use when debugging (even from gdb).
1780 gst_debug_category_set_threshold (GstDebugCategory * category,
1781 GstDebugLevel level)
1783 g_return_if_fail (category != NULL);
1785 if (level > _gst_debug_min) {
1786 _gst_debug_enabled = TRUE;
1787 _gst_debug_min = level;
1790 g_atomic_int_set (&category->threshold, level);
1794 * gst_debug_category_reset_threshold:
1795 * @category: a #GstDebugCategory to reset threshold of.
1797 * Resets the threshold of the category to the default level. Debug information
1798 * will only be output if the threshold is lower or equal to the level of the
1799 * debugging message.
1800 * Use this function to set the threshold back to where it was after using
1801 * gst_debug_category_set_threshold().
1804 gst_debug_category_reset_threshold (GstDebugCategory * category)
1806 gst_debug_reset_threshold (category, NULL);
1810 * gst_debug_category_get_threshold:
1811 * @category: a #GstDebugCategory to get threshold of.
1813 * Returns the threshold of a #GstDebugCategory.
1815 * Returns: the #GstDebugLevel that is used as threshold.
1818 gst_debug_category_get_threshold (GstDebugCategory * category)
1820 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1824 * gst_debug_category_get_name:
1825 * @category: a #GstDebugCategory to get name of.
1827 * Returns the name of a debug category.
1829 * Returns: the name of the category.
1832 gst_debug_category_get_name (GstDebugCategory * category)
1834 return category->name;
1838 * gst_debug_category_get_color:
1839 * @category: a #GstDebugCategory to get the color of.
1841 * Returns the color of a debug category used when printing output in this
1844 * Returns: the color of the category.
1847 gst_debug_category_get_color (GstDebugCategory * category)
1849 return category->color;
1853 * gst_debug_category_get_description:
1854 * @category: a #GstDebugCategory to get the description of.
1856 * Returns the description of a debug category.
1858 * Returns: the description of the category.
1861 gst_debug_category_get_description (GstDebugCategory * category)
1863 return category->description;
1867 * gst_debug_get_all_categories:
1869 * Returns a snapshot of a all categories that are currently in use . This list
1870 * may change anytime.
1871 * The caller has to free the list after use.
1873 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1877 gst_debug_get_all_categories (void)
1881 g_mutex_lock (&__cat_mutex);
1882 ret = g_slist_copy (__categories);
1883 g_mutex_unlock (&__cat_mutex);
1888 static GstDebugCategory *
1889 _gst_debug_get_category_locked (const gchar * name)
1891 GstDebugCategory *ret = NULL;
1894 for (node = __categories; node; node = g_slist_next (node)) {
1895 ret = (GstDebugCategory *) node->data;
1896 if (!strcmp (name, ret->name)) {
1904 _gst_debug_get_category (const gchar * name)
1906 GstDebugCategory *ret;
1908 g_mutex_lock (&__cat_mutex);
1909 ret = _gst_debug_get_category_locked (name);
1910 g_mutex_unlock (&__cat_mutex);
1916 parse_debug_category (gchar * str, const gchar ** category)
1921 /* works in place */
1924 if (str[0] != '\0') {
1933 parse_debug_level (gchar * str, GstDebugLevel * level)
1938 /* works in place */
1941 if (g_ascii_isdigit (str[0])) {
1944 l = strtoul (str, &endptr, 10);
1945 if (endptr > str && endptr[0] == 0) {
1946 *level = (GstDebugLevel) l;
1950 } else if (strcmp (str, "ERROR") == 0) {
1951 *level = GST_LEVEL_ERROR;
1952 } else if (strncmp (str, "WARN", 4) == 0) {
1953 *level = GST_LEVEL_WARNING;
1954 } else if (strcmp (str, "FIXME") == 0) {
1955 *level = GST_LEVEL_FIXME;
1956 } else if (strcmp (str, "INFO") == 0) {
1957 *level = GST_LEVEL_INFO;
1958 } else if (strcmp (str, "DEBUG") == 0) {
1959 *level = GST_LEVEL_DEBUG;
1960 } else if (strcmp (str, "LOG") == 0) {
1961 *level = GST_LEVEL_LOG;
1962 } else if (strcmp (str, "TRACE") == 0) {
1963 *level = GST_LEVEL_TRACE;
1964 } else if (strcmp (str, "MEMDUMP") == 0) {
1965 *level = GST_LEVEL_MEMDUMP;
1973 * gst_debug_set_threshold_from_string:
1974 * @list: comma-separated list of "category:level" pairs to be used
1975 * as debug logging levels
1976 * @reset: %TRUE to clear all previously-set debug levels before setting
1978 * %FALSE if adding the threshold described by @list to the one already set.
1980 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1981 * environment variable. You can use wildcards such as '*', but note that
1982 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1983 * everything to log level 2.
1988 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1996 gst_debug_set_default_threshold (GST_LEVEL_DEFAULT);
1998 split = g_strsplit (list, ",", 0);
2000 for (walk = split; *walk; walk++) {
2001 if (strchr (*walk, ':')) {
2002 gchar **values = g_strsplit (*walk, ":", 2);
2004 if (values[0] && values[1]) {
2005 GstDebugLevel level;
2006 const gchar *category;
2008 if (parse_debug_category (values[0], &category)
2009 && parse_debug_level (values[1], &level)) {
2010 gst_debug_set_threshold_for_name (category, level);
2012 /* bump min-level anyway to allow the category to be registered in the
2014 if (level > _gst_debug_min) {
2015 _gst_debug_min = level;
2020 g_strfreev (values);
2022 GstDebugLevel level;
2024 if (parse_debug_level (*walk, &level))
2025 gst_debug_set_default_threshold (level);
2032 /*** FUNCTION POINTERS ********************************************************/
2034 static GHashTable *__gst_function_pointers; /* NULL */
2035 static GMutex __dbg_functions_mutex;
2037 /* This function MUST NOT return NULL */
2039 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2047 if (G_UNLIKELY (func == NULL))
2050 g_mutex_lock (&__dbg_functions_mutex);
2051 if (G_LIKELY (__gst_function_pointers)) {
2052 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
2053 g_mutex_unlock (&__dbg_functions_mutex);
2054 if (G_LIKELY (ptrname))
2057 g_mutex_unlock (&__dbg_functions_mutex);
2059 /* we need to create an entry in the hash table for this one so we don't leak
2062 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
2063 const gchar *name = g_intern_string (dl_info.dli_sname);
2065 _gst_debug_register_funcptr (func, name);
2070 gchar *name = g_strdup_printf ("%p", (gpointer) func);
2071 const gchar *iname = g_intern_string (name);
2075 _gst_debug_register_funcptr (func, iname);
2081 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2083 gpointer ptr = (gpointer) func;
2085 g_mutex_lock (&__dbg_functions_mutex);
2087 if (!__gst_function_pointers)
2088 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
2089 if (!g_hash_table_lookup (__gst_function_pointers, ptr)) {
2090 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
2093 g_mutex_unlock (&__dbg_functions_mutex);
2097 _priv_gst_debug_cleanup (void)
2099 g_mutex_lock (&__dbg_functions_mutex);
2101 if (__gst_function_pointers) {
2102 g_hash_table_unref (__gst_function_pointers);
2103 __gst_function_pointers = NULL;
2106 g_mutex_unlock (&__dbg_functions_mutex);
2110 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
2111 const guint8 * mem, gsize mem_offset, gsize mem_size)
2113 gchar hexstr[50], ascstr[18], digitstr[4];
2125 while (i < mem_size) {
2126 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
2127 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
2128 g_strlcat (hexstr, digitstr, sizeof (hexstr));
2134 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
2135 (guint) mem_offset, hexstr, ascstr);
2139 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2140 const gchar * func, gint line, GObject * obj, const gchar * msg,
2141 const guint8 * data, guint length)
2145 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2146 "-------------------------------------------------------------------");
2148 if (msg != NULL && *msg != '\0') {
2149 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
2152 while (off < length) {
2155 /* gst_info_dump_mem_line will process 16 bytes at most */
2156 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
2157 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
2161 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
2162 "-------------------------------------------------------------------");
2165 #else /* !GST_DISABLE_GST_DEBUG */
2166 #ifndef GST_REMOVE_DISABLED
2169 _gst_debug_category_new (const gchar * name, guint color,
2170 const gchar * description)
2176 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
2180 /* This function MUST NOT return NULL */
2182 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
2188 _priv_gst_debug_cleanup (void)
2193 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
2194 const gchar * file, const gchar * function, gint line,
2195 GObject * object, const gchar * format, ...)
2200 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
2201 const gchar * file, const gchar * function, gint line,
2202 GObject * object, const gchar * format, va_list args)
2207 gst_debug_message_get (GstDebugMessage * message)
2213 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
2214 const gchar * file, const gchar * function, gint line,
2215 GObject * object, GstDebugMessage * message, gpointer unused)
2220 gst_debug_level_get_name (GstDebugLevel level)
2226 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2227 GDestroyNotify notify)
2232 gst_debug_remove_log_function (GstLogFunction func)
2238 gst_debug_remove_log_function_by_data (gpointer data)
2244 gst_debug_set_active (gboolean active)
2249 gst_debug_is_active (void)
2255 gst_debug_set_colored (gboolean colored)
2260 gst_debug_set_color_mode (GstDebugColorMode mode)
2265 gst_debug_set_color_mode_from_string (const gchar * str)
2270 gst_debug_is_colored (void)
2276 gst_debug_get_color_mode (void)
2278 return GST_DEBUG_COLOR_MODE_OFF;
2282 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2287 gst_debug_set_default_threshold (GstDebugLevel level)
2292 gst_debug_get_default_threshold (void)
2294 return GST_LEVEL_NONE;
2298 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2303 gst_debug_unset_threshold_for_name (const gchar * name)
2308 gst_debug_category_free (GstDebugCategory * category)
2313 gst_debug_category_set_threshold (GstDebugCategory * category,
2314 GstDebugLevel level)
2319 gst_debug_category_reset_threshold (GstDebugCategory * category)
2324 gst_debug_category_get_threshold (GstDebugCategory * category)
2326 return GST_LEVEL_NONE;
2330 gst_debug_category_get_name (GstDebugCategory * category)
2336 gst_debug_category_get_color (GstDebugCategory * category)
2342 gst_debug_category_get_description (GstDebugCategory * category)
2348 gst_debug_get_all_categories (void)
2354 _gst_debug_get_category (const gchar * name)
2360 gst_debug_construct_term_color (guint colorinfo)
2362 return g_strdup ("00");
2366 gst_debug_construct_win_color (guint colorinfo)
2372 _priv_gst_in_valgrind (void)
2378 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2379 const gchar * func, gint line, GObject * obj, const gchar * msg,
2380 const guint8 * data, guint length)
2383 #endif /* GST_REMOVE_DISABLED */
2384 #endif /* GST_DISABLE_GST_DEBUG */
2386 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2387 * fallback function that cleans up the format string and replaces all pointer
2388 * extension formats with plain %p. */
2389 #ifdef GST_DISABLE_GST_DEBUG
2391 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2393 gchar *clean_format, *c;
2399 clean_format = g_strdup (format);
2401 while ((c = strstr (c, "%p\a"))) {
2402 if (c[3] < 'A' || c[3] > 'Z') {
2406 len = strlen (c + 4);
2407 memmove (c + 2, c + 4, len + 1);
2410 while ((c = strstr (clean_format, "%P"))) /* old GST_PTR_FORMAT */
2412 while ((c = strstr (clean_format, "%Q"))) /* old GST_SEGMENT_FORMAT */
2415 len = g_vasprintf (result, clean_format, args);
2417 g_free (clean_format);
2419 if (*result == NULL)
2427 * gst_info_vasprintf:
2428 * @result: (out): the resulting string
2429 * @format: a printf style format string
2430 * @args: the va_list of printf arguments for @format
2432 * Allocates and fills a string large enough (including the terminating null
2433 * byte) to hold the specified printf style @format and @args.
2435 * This function deals with the GStreamer specific printf specifiers
2436 * #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT. If you do not have these specifiers
2437 * in your @format string, you do not need to use this function and can use
2438 * alternatives such as g_vasprintf().
2440 * Free @result with g_free().
2442 * Returns: the length of the string allocated into @result or -1 on any error
2447 gst_info_vasprintf (gchar ** result, const gchar * format, va_list args)
2449 /* This will fallback to __gst_info_fallback_vasprintf() via a #define in
2450 * gst_private.h if the debug system is disabled which will remove the gst
2451 * specific printf format specifiers */
2452 return __gst_vasprintf (result, format, args);
2456 * gst_info_strdup_vprintf:
2457 * @format: a printf style format string
2458 * @args: the va_list of printf arguments for @format
2460 * Allocates, fills and returns a null terminated string from the printf style
2461 * @format string and @args.
2463 * See gst_info_vasprintf() for when this function is required.
2465 * Free with g_free().
2467 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2472 gst_info_strdup_vprintf (const gchar * format, va_list args)
2476 if (gst_info_vasprintf (&ret, format, args) < 0)
2483 * gst_info_strdup_printf:
2484 * @format: a printf style format string
2485 * @...: the printf arguments for @format
2487 * Allocates, fills and returns a 0-terminated string from the printf style
2488 * @format string and corresponding arguments.
2490 * See gst_info_vasprintf() for when this function is required.
2492 * Free with g_free().
2494 * Returns: (nullable): a newly allocated null terminated string or %NULL on any error
2499 gst_info_strdup_printf (const gchar * format, ...)
2504 va_start (args, format);
2505 ret = gst_info_strdup_vprintf (format, args);
2513 * @format: a printf style format string
2514 * @...: the printf arguments for @format
2516 * Outputs a formatted message via the GLib print handler. The default print
2517 * handler simply outputs the message to stdout.
2519 * This function will not append a new-line character at the end, unlike
2520 * gst_println() which will.
2522 * All strings must be in ASCII or UTF-8 encoding.
2524 * This function differs from g_print() in that it supports all the additional
2525 * printf specifiers that are supported by GStreamer's debug logging system,
2526 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2528 * This function is primarily for printing debug output.
2533 gst_print (const gchar * format, ...)
2538 va_start (args, format);
2539 str = gst_info_strdup_vprintf (format, args);
2542 g_print ("%s", str);
2548 * @format: a printf style format string
2549 * @...: the printf arguments for @format
2551 * Outputs a formatted message via the GLib print handler. The default print
2552 * handler simply outputs the message to stdout.
2554 * This function will append a new-line character at the end, unlike
2555 * gst_print() which will not.
2557 * All strings must be in ASCII or UTF-8 encoding.
2559 * This function differs from g_print() in that it supports all the additional
2560 * printf specifiers that are supported by GStreamer's debug logging system,
2561 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2563 * This function is primarily for printing debug output.
2568 gst_println (const gchar * format, ...)
2573 va_start (args, format);
2574 str = gst_info_strdup_vprintf (format, args);
2577 g_print ("%s\n", str);
2583 * @format: a printf style format string
2584 * @...: the printf arguments for @format
2586 * Outputs a formatted message via the GLib error message handler. The default
2587 * handler simply outputs the message to stderr.
2589 * This function will not append a new-line character at the end, unlike
2590 * gst_printerrln() which will.
2592 * All strings must be in ASCII or UTF-8 encoding.
2594 * This function differs from g_printerr() in that it supports the additional
2595 * printf specifiers that are supported by GStreamer's debug logging system,
2596 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2598 * This function is primarily for printing debug output.
2603 gst_printerr (const gchar * format, ...)
2608 va_start (args, format);
2609 str = gst_info_strdup_vprintf (format, args);
2612 g_printerr ("%s", str);
2618 * @format: a printf style format string
2619 * @...: the printf arguments for @format
2621 * Outputs a formatted message via the GLib error message handler. The default
2622 * handler simply outputs the message to stderr.
2624 * This function will append a new-line character at the end, unlike
2625 * gst_printerr() which will not.
2627 * All strings must be in ASCII or UTF-8 encoding.
2629 * This function differs from g_printerr() in that it supports the additional
2630 * printf specifiers that are supported by GStreamer's debug logging system,
2631 * such as #GST_PTR_FORMAT and #GST_SEGMENT_FORMAT.
2633 * This function is primarily for printing debug output.
2638 gst_printerrln (const gchar * format, ...)
2643 va_start (args, format);
2644 str = gst_info_strdup_vprintf (format, args);
2647 g_printerr ("%s\n", str);
2654 append_debug_info (GString * trace, Dwfl * dwfl, const void *ip)
2658 Dwfl_Module *module;
2659 const gchar *function_name;
2661 if (dwfl_linux_proc_report (dwfl, getpid ()) != 0)
2664 if (dwfl_report_end (dwfl, NULL, NULL))
2667 addr = (uintptr_t) ip;
2668 module = dwfl_addrmodule (dwfl, addr);
2669 function_name = dwfl_module_addrname (module, addr);
2671 g_string_append_printf (trace, "%s (", function_name ? function_name : "??");
2673 line = dwfl_getsrc (dwfl, addr);
2677 const gchar *filename = dwfl_lineinfo (line, &addr,
2678 &nline, NULL, NULL, NULL);
2680 g_string_append_printf (trace, "%s:%d", strrchr (filename,
2681 G_DIR_SEPARATOR) + 1, nline);
2683 const gchar *eflfile = NULL;
2685 dwfl_module_info (module, NULL, NULL, NULL, NULL, NULL, &eflfile, NULL);
2686 g_string_append_printf (trace, "%s:%p", eflfile ? eflfile : "??", ip);
2691 #endif /* HAVE_DW */
2694 generate_unwind_trace (GstStackTraceFlags flags)
2698 unw_cursor_t cursor;
2699 gboolean use_libunwind = TRUE;
2700 GString *trace = g_string_new (NULL);
2704 Dwfl_Callbacks callbacks = {
2705 .find_elf = dwfl_linux_proc_find_elf,
2706 .find_debuginfo = dwfl_standard_find_debuginfo,
2709 if ((flags & GST_STACK_TRACE_SHOW_FULL))
2710 dwfl = dwfl_begin (&callbacks);
2711 #endif /* HAVE_DW */
2713 unret = unw_getcontext (&uc);
2715 GST_DEBUG ("Could not get libunwind context (%d)", unret);
2719 unret = unw_init_local (&cursor, &uc);
2721 GST_DEBUG ("Could not init libunwind context (%d)", unret);
2726 while (unw_step (&cursor) > 0) {
2731 unret = unw_get_reg (&cursor, UNW_REG_IP, &ip);
2733 GST_DEBUG ("libunwind could read frame info (%d)", unret);
2738 if (append_debug_info (trace, dwfl, (void *) (ip - 4))) {
2739 use_libunwind = FALSE;
2740 g_string_append (trace, ")\n");
2743 #endif /* HAVE_DW */
2745 if (use_libunwind) {
2748 unw_word_t offset = 0;
2749 unw_get_proc_name (&cursor, name, sizeof (name), &offset);
2750 g_string_append_printf (trace, "%s (0x%" G_GSIZE_FORMAT ")\n", name,
2761 return g_string_free (trace, FALSE);
2764 #endif /* HAVE_UNWIND */
2766 #ifdef HAVE_BACKTRACE
2768 generate_backtrace_trace (void)
2771 void *buffer[BT_BUF_SIZE];
2775 trace = g_string_new (NULL);
2776 nptrs = backtrace (buffer, BT_BUF_SIZE);
2778 strings = backtrace_symbols (buffer, nptrs);
2783 for (j = 0; j < nptrs; j++)
2784 g_string_append_printf (trace, "%s\n", strings[j]);
2788 return g_string_free (trace, FALSE);
2791 #define generate_backtrace_trace() NULL
2792 #endif /* HAVE_BACKTRACE */
2796 dbghelp_initialize_symbols (HANDLE process)
2798 static gsize initialization_value = 0;
2800 if (g_once_init_enter (&initialization_value)) {
2801 GST_INFO ("Initializing Windows symbol handler");
2802 SymSetOptions (SYMOPT_LOAD_LINES);
2803 SymInitialize (process, NULL, TRUE);
2804 GST_INFO ("Initialized Windows symbol handler");
2806 g_once_init_leave (&initialization_value, 1);
2811 generate_dbghelp_trace (void)
2813 HANDLE process = GetCurrentProcess ();
2814 HANDLE thread = GetCurrentThread ();
2815 IMAGEHLP_MODULE64 module_info;
2818 STACKFRAME64 frame = { 0 };
2820 GString *trace = g_string_new (NULL);
2822 dbghelp_initialize_symbols (process);
2824 memset (&context, 0, sizeof (CONTEXT));
2825 context.ContextFlags = CONTEXT_FULL;
2827 RtlCaptureContext (&context);
2829 frame.AddrPC.Mode = AddrModeFlat;
2830 frame.AddrStack.Mode = AddrModeFlat;
2831 frame.AddrFrame.Mode = AddrModeFlat;
2833 #if (defined _M_IX86)
2834 machine = IMAGE_FILE_MACHINE_I386;
2835 frame.AddrFrame.Offset = context.Ebp;
2836 frame.AddrPC.Offset = context.Eip;
2837 frame.AddrStack.Offset = context.Esp;
2838 #elif (defined _M_X64)
2839 machine = IMAGE_FILE_MACHINE_AMD64;
2840 frame.AddrFrame.Offset = context.Rbp;
2841 frame.AddrPC.Offset = context.Rip;
2842 frame.AddrStack.Offset = context.Rsp;
2847 module_info.SizeOfStruct = sizeof (module_info);
2848 save_context = (machine == IMAGE_FILE_MACHINE_I386) ? NULL : &context;
2851 char buffer[sizeof (SYMBOL_INFO) + MAX_SYM_NAME * sizeof (TCHAR)];
2852 PSYMBOL_INFO symbol = (PSYMBOL_INFO) buffer;
2853 IMAGEHLP_LINE64 line;
2854 DWORD displacement = 0;
2856 symbol->SizeOfStruct = sizeof (SYMBOL_INFO);
2857 symbol->MaxNameLen = MAX_SYM_NAME;
2859 line.SizeOfStruct = sizeof (line);
2861 if (!StackWalk64 (machine, process, thread, &frame, save_context, 0,
2862 SymFunctionTableAccess64, SymGetModuleBase64, 0))
2865 if (SymFromAddr (process, frame.AddrPC.Offset, 0, symbol))
2866 g_string_append_printf (trace, "%s ", symbol->Name);
2868 g_string_append (trace, "?? ");
2870 if (SymGetLineFromAddr64 (process, frame.AddrPC.Offset, &displacement,
2872 g_string_append_printf (trace, "(%s:%u)", line.FileName, line.LineNumber);
2873 else if (SymGetModuleInfo64 (process, frame.AddrPC.Offset, &module_info))
2874 g_string_append_printf (trace, "(%s)", module_info.ImageName);
2876 g_string_append_printf (trace, "(%s)", "??");
2878 g_string_append (trace, "\n");
2882 return g_string_free (trace, FALSE);
2884 #endif /* HAVE_DBGHELP */
2887 * gst_debug_get_stack_trace:
2888 * @flags: A set of #GstStackTraceFlags to determine how the stack
2889 * trace should look like. Pass 0 to retrieve a minimal backtrace.
2891 * Returns: (nullable): a stack trace, if libunwind or glibc backtrace are
2892 * present, else %NULL.
2897 gst_debug_get_stack_trace (GstStackTraceFlags flags)
2899 gchar *trace = NULL;
2900 #ifdef HAVE_BACKTRACE
2901 gboolean have_backtrace = TRUE;
2903 gboolean have_backtrace = FALSE;
2907 if ((flags & GST_STACK_TRACE_SHOW_FULL) || !have_backtrace)
2908 trace = generate_unwind_trace (flags);
2909 #endif /* HAVE_UNWIND */
2912 trace = generate_dbghelp_trace ();
2917 else if (have_backtrace)
2918 return generate_backtrace_trace ();
2924 * gst_debug_print_stack_trace:
2926 * If libunwind, glibc backtrace or DbgHelp are present
2927 * a stack trace is printed.
2930 gst_debug_print_stack_trace (void)
2932 gchar *trace = gst_debug_get_stack_trace (GST_STACK_TRACE_SHOW_FULL);
2935 g_print ("%s\n", trace);
2940 #ifndef GST_DISABLE_GST_DEBUG
2943 guint max_size_per_thread;
2944 guint thread_timeout;
2946 GHashTable *thread_index;
2947 } GstRingBufferLogger;
2959 G_LOCK_DEFINE_STATIC (ring_buffer_logger);
2960 static GstRingBufferLogger *ring_buffer_logger = NULL;
2963 gst_ring_buffer_logger_log (GstDebugCategory * category,
2964 GstDebugLevel level,
2966 const gchar * function,
2967 gint line, GObject * object, GstDebugMessage * message, gpointer user_data)
2969 GstRingBufferLogger *logger = user_data;
2972 GstClockTime elapsed;
2977 GstRingBufferLog *log;
2978 gint64 now = g_get_monotonic_time ();
2979 const gchar *message_str = gst_debug_message_get (message);
2981 G_LOCK (ring_buffer_logger);
2983 if (logger->thread_timeout > 0) {
2984 /* Remove all threads that saw no output since thread_timeout seconds.
2985 * By construction these are all at the tail of the queue, and the queue
2986 * is ordered by last use, so we just need to look at the tail.
2988 while (logger->threads.tail) {
2989 log = logger->threads.tail->data;
2990 if (log->last_use + logger->thread_timeout * G_USEC_PER_SEC >= now)
2993 g_hash_table_remove (logger->thread_index, log->thread);
2994 while ((output = g_queue_pop_head (&log->log)))
2997 g_queue_pop_tail (&logger->threads);
3001 /* Get logger for this thread, and put it back at the
3002 * head of the threads queue */
3003 thread = g_thread_self ();
3004 log = g_hash_table_lookup (logger->thread_index, thread);
3006 log = g_new0 (GstRingBufferLog, 1);
3007 g_queue_init (&log->log);
3009 g_queue_push_head (&logger->threads, log);
3010 log->link = logger->threads.head;
3011 log->thread = thread;
3012 g_hash_table_insert (logger->thread_index, thread, log);
3014 g_queue_unlink (&logger->threads, log->link);
3015 g_queue_push_head_link (&logger->threads, log->link);
3017 log->last_use = now;
3019 /* __FILE__ might be a file name or an absolute path or a
3020 * relative path, irrespective of the exact compiler used,
3021 * in which case we want to shorten it to the filename for
3024 if (c == '.' || c == '/' || c == '\\' || (c != '\0' && file[1] == ':')) {
3025 file = gst_path_basename (file);
3031 obj = gst_debug_print_object (object);
3036 elapsed = GST_CLOCK_DIFF (_priv_gst_start_time, gst_util_get_timestamp ());
3038 /* no color, all platforms */
3039 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
3041 g_strdup_printf ("%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
3042 pid, thread, gst_debug_level_get_name (level),
3043 gst_debug_category_get_name (category), file, line, function, obj,
3047 output_len = strlen (output);
3049 if (output_len < logger->max_size_per_thread) {
3052 /* While using a GQueue here is not the most efficient thing to do, we
3053 * have to allocate a string for every output anyway and could just store
3054 * that instead of copying it to an actual ringbuffer.
3055 * Better than GQueue would be GstQueueArray, but that one is in
3056 * libgstbase and we can't use it here. That one allocation will not make
3057 * much of a difference anymore, considering the number of allocations
3058 * needed to get to this point...
3060 while (log->log_size + output_len > logger->max_size_per_thread) {
3061 buf = g_queue_pop_head (&log->log);
3062 log->log_size -= strlen (buf);
3065 g_queue_push_tail (&log->log, output);
3066 log->log_size += output_len;
3070 /* Can't really write anything as the line is bigger than the maximum
3071 * allowed log size already, so just remove everything */
3073 while ((buf = g_queue_pop_head (&log->log)))
3082 G_UNLOCK (ring_buffer_logger);
3086 * gst_debug_ring_buffer_logger_get_logs:
3088 * Fetches the current logs per thread from the ring buffer logger. See
3089 * gst_debug_add_ring_buffer_logger() for details.
3091 * Returns: (transfer full) (array zero-terminated): NULL-terminated array of
3092 * strings with the debug output per thread
3097 gst_debug_ring_buffer_logger_get_logs (void)
3099 gchar **logs, **tmp;
3102 g_return_val_if_fail (ring_buffer_logger != NULL, NULL);
3104 G_LOCK (ring_buffer_logger);
3106 tmp = logs = g_new0 (gchar *, ring_buffer_logger->threads.length + 1);
3107 for (l = ring_buffer_logger->threads.head; l; l = l->next) {
3108 GstRingBufferLog *log = l->data;
3113 *tmp = p = g_new0 (gchar, log->log_size + 1);
3115 for (l = log->log.head; l; l = l->next) {
3116 len = strlen (l->data);
3117 memcpy (p, l->data, len);
3124 G_UNLOCK (ring_buffer_logger);
3130 gst_ring_buffer_logger_free (GstRingBufferLogger * logger)
3132 G_LOCK (ring_buffer_logger);
3133 if (ring_buffer_logger == logger) {
3134 GstRingBufferLog *log;
3136 while ((log = g_queue_pop_head (&logger->threads))) {
3138 while ((buf = g_queue_pop_head (&log->log)))
3143 g_hash_table_unref (logger->thread_index);
3146 ring_buffer_logger = NULL;
3148 G_UNLOCK (ring_buffer_logger);
3152 * gst_debug_add_ring_buffer_logger:
3153 * @max_size_per_thread: Maximum size of log per thread in bytes
3154 * @thread_timeout: Timeout for threads in seconds
3156 * Adds a memory ringbuffer based debug logger that stores up to
3157 * @max_size_per_thread bytes of logs per thread and times out threads after
3158 * @thread_timeout seconds of inactivity.
3160 * Logs can be fetched with gst_debug_ring_buffer_logger_get_logs() and the
3161 * logger can be removed again with gst_debug_remove_ring_buffer_logger().
3162 * Only one logger at a time is possible.
3167 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3168 guint thread_timeout)
3170 GstRingBufferLogger *logger;
3172 G_LOCK (ring_buffer_logger);
3174 if (ring_buffer_logger) {
3175 g_warn_if_reached ();
3176 G_UNLOCK (ring_buffer_logger);
3180 logger = ring_buffer_logger = g_new0 (GstRingBufferLogger, 1);
3182 logger->max_size_per_thread = max_size_per_thread;
3183 logger->thread_timeout = thread_timeout;
3184 logger->thread_index = g_hash_table_new (g_direct_hash, g_direct_equal);
3185 g_queue_init (&logger->threads);
3187 gst_debug_add_log_function (gst_ring_buffer_logger_log, logger,
3188 (GDestroyNotify) gst_ring_buffer_logger_free);
3189 G_UNLOCK (ring_buffer_logger);
3193 * gst_debug_remove_ring_buffer_logger:
3195 * Removes any previously added ring buffer logger with
3196 * gst_debug_add_ring_buffer_logger().
3201 gst_debug_remove_ring_buffer_logger (void)
3203 gst_debug_remove_log_function (gst_ring_buffer_logger_log);
3206 #else /* GST_DISABLE_GST_DEBUG */
3207 #ifndef GST_REMOVE_DISABLED
3210 gst_debug_ring_buffer_logger_get_logs (void)
3216 gst_debug_add_ring_buffer_logger (guint max_size_per_thread,
3217 guint thread_timeout)
3222 gst_debug_remove_ring_buffer_logger (void)
3226 #endif /* GST_REMOVE_DISABLED */
3227 #endif /* GST_DISABLE_GST_DEBUG */