2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
5 * Copyright (C) 2008-2009 Tim-Philipp Müller <tim centricular net>
7 * gstinfo.c: debugging functions
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
27 * @short_description: Debugging and logging facilities
28 * @see_also: #gstreamer-gstconfig, #gstreamer-Gst for command line parameters
29 * and environment variables that affect the debugging output.
31 * GStreamer's debugging subsystem is an easy way to get information about what
32 * the application is doing. It is not meant for programming errors. Use GLib
33 * methods (g_warning and friends) for that.
35 * The debugging subsystem works only after GStreamer has been initialized
36 * - for example by calling gst_init().
38 * The debugging subsystem is used to log informational messages while the
39 * application runs. Each messages has some properties attached to it. Among
40 * these properties are the debugging category, the severity (called "level"
41 * here) and an optional #GObject it belongs to. Each of these messages is sent
42 * to all registered debugging handlers, which then handle the messages.
43 * GStreamer attaches a default handler on startup, which outputs requested
46 * Messages are output by using shortcut macros like #GST_DEBUG,
47 * #GST_CAT_ERROR_OBJECT or similar. These all expand to calling gst_debug_log()
48 * with the right parameters.
49 * The only thing a developer will probably want to do is define his own
50 * categories. This is easily done with 3 lines. At the top of your code,
52 * the variables and set the default category.
55 * GST_DEBUG_CATEGORY_STATIC (my_category); // define category (statically)
56 * &hash;define GST_CAT_DEFAULT my_category // set as default
59 * After that you only need to initialize the category.
62 * GST_DEBUG_CATEGORY_INIT (my_category, "my category",
63 * 0, "This is my very own");
66 * Initialization must be done before the category is used first.
68 * in their plugin_init function, libraries and applications should do that
69 * during their initialization.
71 * The whole debugging subsystem can be disabled at build time with passing the
72 * --disable-gst-debug switch to configure. If this is done, every function,
73 * macro and even structs described in this file evaluate to default values or
75 * So don't take addresses of these functions or use other tricks.
76 * If you must do that for some reason, there is still an option.
78 * subsystem was compiled out, #GST_DISABLE_GST_DEBUG is defined in
80 * so you can check that before doing your trick.
81 * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
82 * speed increase and will reduce the size of your compiled code. The GStreamer
83 * library itself becomes around 10% smaller.
85 * Please note that there are naming conventions for the names of debugging
86 * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
90 #include "gst_private.h"
93 #undef gst_debug_remove_log_function
94 #undef gst_debug_add_log_function
96 #ifndef GST_DISABLE_GST_DEBUG
101 #include <stdio.h> /* fprintf */
102 #include <glib/gstdio.h>
105 # include <unistd.h> /* getpid on UNIX */
107 #ifdef HAVE_PROCESS_H
108 # include <process.h> /* getpid on win32 */
110 #include <string.h> /* G_VA_COPY */
112 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
113 # include <windows.h> /* GetStdHandle, windows console */
116 #include "gst_private.h"
117 #include "gstutils.h"
118 #include "gstquark.h"
119 #include "gstsegment.h"
120 #include "gstvalue.h"
121 #include "gstcapsfeatures.h"
123 #ifdef HAVE_VALGRIND_VALGRIND_H
124 # include <valgrind/valgrind.h>
126 #include <glib/gprintf.h> /* g_sprintf */
128 /* our own printf implementation with custom extensions to %p for caps etc. */
129 #include "printf/printf.h"
131 #endif /* !GST_DISABLE_GST_DEBUG */
133 extern gboolean gst_is_initialized (void);
135 /* we want these symbols exported even if debug is disabled, to maintain
136 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
137 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
139 /* disabled by default, as soon as some threshold is set > NONE,
140 * it becomes enabled. */
141 gboolean _gst_debug_enabled = FALSE;
142 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
144 GstDebugCategory *GST_CAT_DEFAULT = NULL;
146 GstDebugCategory *GST_CAT_GST_INIT = NULL;
147 GstDebugCategory *GST_CAT_MEMORY = NULL;
148 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
149 GstDebugCategory *GST_CAT_STATES = NULL;
150 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
152 GstDebugCategory *GST_CAT_BUFFER = NULL;
153 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
154 GstDebugCategory *GST_CAT_BUS = NULL;
155 GstDebugCategory *GST_CAT_CAPS = NULL;
156 GstDebugCategory *GST_CAT_CLOCK = NULL;
157 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
158 GstDebugCategory *GST_CAT_PADS = NULL;
159 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
160 GstDebugCategory *GST_CAT_PIPELINE = NULL;
161 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
162 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
163 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
164 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
165 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
166 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
167 GstDebugCategory *GST_CAT_EVENT = NULL;
168 GstDebugCategory *GST_CAT_MESSAGE = NULL;
169 GstDebugCategory *GST_CAT_PARAMS = NULL;
170 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
171 GstDebugCategory *GST_CAT_SIGNAL = NULL;
172 GstDebugCategory *GST_CAT_PROBE = NULL;
173 GstDebugCategory *GST_CAT_REGISTRY = NULL;
174 GstDebugCategory *GST_CAT_QOS = NULL;
175 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
176 GstDebugCategory *GST_CAT_META = NULL;
177 GstDebugCategory *GST_CAT_LOCKING = NULL;
178 GstDebugCategory *GST_CAT_CONTEXT = NULL;
181 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
183 #ifndef GST_DISABLE_GST_DEBUG
185 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
186 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
188 /* time of initialization, so we get useful debugging output times
189 * FIXME: we use this in gstdebugutils.c, what about a function + macro to
190 * get the running time: GST_DEBUG_RUNNING_TIME
192 GstClockTime _priv_gst_info_start_time;
196 #include <rld_interface.h>
197 typedef struct DL_INFO
199 const char *dli_fname;
201 const char *dli_sname;
205 long dli_reserved[4];
209 #define _RLD_DLADDR 14
210 int dladdr (void *address, Dl_info * dl);
213 dladdr (void *address, Dl_info * dl)
217 v = _rld_new_interface (_RLD_DLADDR, address, dl);
223 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
224 static void gst_debug_reset_all_thresholds (void);
227 static int _gst_info_printf_extension_ptr (FILE * stream,
228 const struct printf_info *info, const void *const *args);
229 static int _gst_info_printf_extension_segment (FILE * stream,
230 const struct printf_info *info, const void *const *args);
233 struct _GstDebugMessage
240 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
241 static GMutex __level_name_mutex;
242 static GSList *__level_name = NULL;
250 /* list of all categories */
251 static GMutex __cat_mutex;
252 static GSList *__categories = NULL;
254 /* all registered debug handlers */
259 GDestroyNotify notify;
262 static GMutex __log_func_mutex;
263 static GSList *__log_functions = NULL;
265 #define PRETTY_TAGS_DEFAULT TRUE
266 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
268 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
269 static volatile gint G_GNUC_MAY_ALIAS __use_color = 1;
271 static FILE *log_file;
273 /* FIXME: export this? */
275 _priv_gst_in_valgrind (void)
283 in_valgrind = GST_VG_UNCHECKED;
285 if (in_valgrind == GST_VG_UNCHECKED) {
286 #ifdef HAVE_VALGRIND_VALGRIND_H
287 if (RUNNING_ON_VALGRIND) {
288 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
289 printf ("GStreamer has detected that it is running inside valgrind.\n");
290 printf ("It might now take different code paths to ease debugging.\n");
291 printf ("Of course, this may also lead to different bugs.\n");
292 in_valgrind = GST_VG_INSIDE;
294 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
295 in_valgrind = GST_VG_NO_VALGRIND;
298 in_valgrind = GST_VG_NO_VALGRIND;
300 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
301 in_valgrind == GST_VG_INSIDE);
303 return (in_valgrind == GST_VG_INSIDE);
306 /* Initialize the debugging system */
308 _priv_gst_debug_init (void)
312 env = g_getenv ("GST_DEBUG_FILE");
313 if (env != NULL && *env != '\0') {
314 if (strcmp (env, "-") == 0) {
317 log_file = g_fopen (env, "w");
318 if (log_file == NULL) {
319 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
328 /* get time we started for debugging messages */
329 _priv_gst_info_start_time = gst_util_get_timestamp ();
332 register_printf_specifier (GST_PTR_FORMAT[0], _gst_info_printf_extension_ptr,
334 register_printf_specifier (GST_SEGMENT_FORMAT[0],
335 _gst_info_printf_extension_segment, NULL);
338 /* do NOT use a single debug function before this line has been run */
339 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
340 GST_DEBUG_UNDERLINE, NULL);
341 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
342 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
344 gst_debug_add_log_function (gst_debug_log_default, NULL, NULL);
346 /* FIXME: add descriptions here */
347 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
348 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
349 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
350 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
351 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
352 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
353 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
354 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
355 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
356 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
357 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
358 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
359 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
360 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
361 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
362 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
363 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
364 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
365 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
366 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
367 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
368 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
369 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
370 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
371 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
372 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
373 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
374 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
375 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
376 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
377 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
378 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
379 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
380 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
381 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
382 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
383 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
384 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
385 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
387 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
388 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
389 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
391 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
392 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
393 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
394 GST_DEBUG_BOLD, NULL);
395 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
396 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
397 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
398 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
399 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
400 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
401 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
402 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
403 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
404 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
406 /* print out the valgrind message if we're in valgrind */
407 _priv_gst_in_valgrind ();
409 env = g_getenv ("GST_DEBUG_OPTIONS");
411 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
413 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
418 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
419 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
423 * @category: category to log
424 * @level: level of the message is in
425 * @file: the file that emitted the message, usually the __FILE__ identifier
426 * @function: the function that emitted the message
427 * @line: the line from that the message was emitted, usually __LINE__
428 * @object: (transfer none) (allow-none): the object this message relates to,
430 * @format: a printf style format string
431 * @...: optional arguments for the format
433 * Logs the given message using the currently registered debugging handlers.
436 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
437 const gchar * file, const gchar * function, gint line,
438 GObject * object, const gchar * format, ...)
442 va_start (var_args, format);
443 gst_debug_log_valist (category, level, file, function, line, object, format,
449 /* based on g_basename(), which we can't use because it was deprecated */
450 static inline const gchar *
451 gst_path_basename (const gchar * file_name)
453 register const gchar *base;
455 base = strrchr (file_name, G_DIR_SEPARATOR);
458 const gchar *q = strrchr (file_name, '/');
459 if (base == NULL || (q != NULL && q > base))
466 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
467 return file_name + 2;
474 * gst_debug_log_valist:
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 * @args: optional arguments for the format
485 * Logs the given message using the currently registered debugging handlers.
488 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
489 const gchar * file, const gchar * function, gint line,
490 GObject * object, const gchar * format, va_list args)
492 GstDebugMessage message;
496 g_return_if_fail (category != NULL);
497 g_return_if_fail (file != NULL);
498 g_return_if_fail (function != NULL);
499 g_return_if_fail (format != NULL);
501 /* The predefined macro __FILE__ is always the exact path given to the
502 * compiler with MSVC, which may or may not be the basename. We work
503 * around it at runtime to improve the readability. */
505 file = gst_path_basename (file);
508 message.message = NULL;
509 message.format = format;
510 G_VA_COPY (message.arguments, args);
512 handler = __log_functions;
514 entry = handler->data;
515 handler = g_slist_next (handler);
516 entry->func (category, level, file, function, line, object, &message,
519 g_free (message.message);
520 va_end (message.arguments);
524 * gst_debug_message_get:
525 * @message: a debug message
527 * Gets the string representation of a #GstDebugMessage. This function is used
528 * in debug handlers to extract the message.
530 * Returns: the string representation of a #GstDebugMessage.
533 gst_debug_message_get (GstDebugMessage * message)
535 if (message->message == NULL) {
538 len = __gst_vasprintf (&message->message, message->format,
542 message->message = NULL;
544 return message->message;
547 #define MAX_BUFFER_DUMP_STRING_LEN 100
549 /* structure_to_pretty_string:
550 * @str: a serialized #GstStructure
552 * If the serialized structure contains large buffers such as images the hex
553 * representation of those buffers will be shortened so that the string remains
556 * Returns: the filtered string
559 prettify_structure_string (gchar * str)
561 gchar *pos = str, *end;
563 while ((pos = strstr (pos, "(buffer)"))) {
566 pos += strlen ("(buffer)");
567 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
569 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
570 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
571 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
572 g_memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
573 strlen (pos + count) + 1);
574 pos += MAX_BUFFER_DUMP_STRING_LEN;
581 static inline gchar *
582 gst_info_structure_to_string (const GstStructure * s)
585 gchar *str = gst_structure_to_string (s);;
586 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
587 return prettify_structure_string (str);
594 static inline gchar *
595 gst_info_describe_buffer (GstBuffer * buffer)
597 return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
598 GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
599 ", offset %" G_GUINT64_FORMAT ", offset_end %" G_GUINT64_FORMAT
600 ", flags 0x%x", buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
601 GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
602 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
603 gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer),
604 GST_BUFFER_OFFSET_END (buffer), GST_BUFFER_FLAGS (buffer));
607 static inline gchar *
608 gst_info_describe_event (GstEvent * event)
612 s = gst_info_structure_to_string (gst_event_get_structure (event));
613 ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
614 ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
615 GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
621 static inline gchar *
622 gst_info_describe_message (GstMessage * message)
626 s = gst_info_structure_to_string (gst_message_get_structure (message));
627 ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
628 ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
629 message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
630 GST_MESSAGE_SEQNUM (message),
631 ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
637 static inline gchar *
638 gst_info_describe_query (GstQuery * query)
642 s = gst_info_structure_to_string (gst_query_get_structure (query));
643 ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
644 query, (s ? s : "(NULL)"));
650 gst_debug_print_object (gpointer ptr)
652 GObject *object = (GObject *) ptr;
655 /* This is a cute trick to detect unmapped memory, but is unportable,
656 * slow, screws around with madvise, and not actually that useful. */
660 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
661 if (ret == -1 && errno == ENOMEM) {
662 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
667 /* nicely printed object */
668 if (object == NULL) {
669 return g_strdup ("(NULL)");
671 if (*(GType *) ptr == GST_TYPE_CAPS) {
672 return gst_caps_to_string ((const GstCaps *) ptr);
674 if (*(GType *) ptr == GST_TYPE_STRUCTURE) {
675 return gst_info_structure_to_string ((const GstStructure *) ptr);
677 if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
678 return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
680 if (*(GType *) ptr == GST_TYPE_TAG_LIST) {
681 gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
682 if (G_UNLIKELY (pretty_tags))
683 return prettify_structure_string (str);
687 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
688 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
690 if (GST_IS_BUFFER (ptr)) {
691 return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
694 if (*(guint32 *) ptr == 0xffffffff) {
695 return g_strdup_printf ("<poisoned@%p>", ptr);
698 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
699 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
701 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
702 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
704 if (G_IS_OBJECT (object)) {
705 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
707 if (GST_IS_MESSAGE (object)) {
708 return gst_info_describe_message (GST_MESSAGE_CAST (object));
710 if (GST_IS_QUERY (object)) {
711 return gst_info_describe_query (GST_QUERY_CAST (object));
713 if (GST_IS_EVENT (object)) {
714 return gst_info_describe_event (GST_EVENT_CAST (object));
716 if (GST_IS_CONTEXT (object)) {
717 GstContext *context = GST_CONTEXT_CAST (object);
719 const GstStructure *structure;
721 structure = gst_context_get_structure (context);
724 s = gst_info_structure_to_string (structure);
726 s = g_strdup ("(NULL)");
729 ret = g_strdup_printf ("context '%s'", s);
734 return g_strdup_printf ("%p", ptr);
739 gst_debug_print_segment (gpointer ptr)
741 GstSegment *segment = (GstSegment *) ptr;
743 /* nicely printed segment */
744 if (segment == NULL) {
745 return g_strdup ("(NULL)");
748 switch (segment->format) {
749 case GST_FORMAT_UNDEFINED:{
750 return g_strdup_printf ("UNDEFINED segment");
752 case GST_FORMAT_TIME:{
753 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
754 ", stop=%" GST_TIME_FORMAT ", rate=%f, applied_rate=%f"
755 ", flags=0x%02x, time=%" GST_TIME_FORMAT ", base=%" GST_TIME_FORMAT
756 ", position %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
757 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
758 segment->rate, segment->applied_rate, (guint) segment->flags,
759 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
760 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
763 const gchar *format_name;
765 format_name = gst_format_get_name (segment->format);
766 if (G_UNLIKELY (format_name == NULL))
767 format_name = "(UNKNOWN FORMAT)";
768 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
769 ", stop=%" G_GINT64_FORMAT ", rate=%f, applied_rate=%f"
770 ", flags=0x%02x, time=%" G_GINT64_FORMAT ", base=%" G_GINT64_FORMAT
771 ", position %" G_GINT64_FORMAT ", duration %" G_GINT64_FORMAT,
772 format_name, segment->start, segment->stop, segment->rate,
773 segment->applied_rate, (guint) segment->flags,
774 segment->time, segment->base, segment->position, segment->duration);
781 * gst_debug_construct_term_color:
782 * @colorinfo: the color info
784 * Constructs a string that can be used for getting the desired color in color
786 * You need to free the string after use.
788 * Returns: (transfer full) (type gchar*): a string containing the color
792 gst_debug_construct_term_color (guint colorinfo)
796 color = g_string_new ("\033[00");
798 if (colorinfo & GST_DEBUG_BOLD) {
799 g_string_append_len (color, ";01", 3);
801 if (colorinfo & GST_DEBUG_UNDERLINE) {
802 g_string_append_len (color, ";04", 3);
804 if (colorinfo & GST_DEBUG_FG_MASK) {
805 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
807 if (colorinfo & GST_DEBUG_BG_MASK) {
808 g_string_append_printf (color, ";4%1d",
809 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
811 g_string_append_c (color, 'm');
813 return g_string_free (color, FALSE);
817 * gst_debug_construct_win_color:
818 * @colorinfo: the color info
820 * Constructs an integer that can be used for getting the desired color in
821 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
822 * ignore this attribute.
824 * This function returns 0 on non-windows machines.
826 * Returns: an integer containing the color definition
829 gst_debug_construct_win_color (guint colorinfo)
833 static const guchar ansi_to_win_fg[8] = {
835 FOREGROUND_RED, /* red */
836 FOREGROUND_GREEN, /* green */
837 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
838 FOREGROUND_BLUE, /* blue */
839 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
840 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
841 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
843 static const guchar ansi_to_win_bg[8] = {
847 BACKGROUND_RED | BACKGROUND_GREEN,
849 BACKGROUND_RED | BACKGROUND_BLUE,
850 BACKGROUND_GREEN | FOREGROUND_BLUE,
851 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
854 /* we draw black as white, as cmd.exe can only have black bg */
855 if (colorinfo == 0) {
856 return ansi_to_win_fg[7];
859 if (colorinfo & GST_DEBUG_BOLD) {
860 color |= FOREGROUND_INTENSITY;
862 if (colorinfo & GST_DEBUG_FG_MASK) {
863 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
865 if (colorinfo & GST_DEBUG_BG_MASK) {
866 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
872 /* width of %p varies depending on actual value of pointer, which can make
873 * output unevenly aligned if multiple threads are involved, hence the %14p
874 * (should really be %18p, but %14p seems a good compromise between too many
875 * white spaces and likely unalignment on my system) */
876 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
877 #define PTR_FMT "%14p"
879 #define PTR_FMT "%10p"
881 #define PID_FMT "%5d"
882 #define CAT_FMT "%20s %s:%d:%s:%s"
885 static const guchar levelcolormap[GST_LEVEL_COUNT] = {
887 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
888 /* GST_LEVEL_ERROR */
889 FOREGROUND_RED | FOREGROUND_INTENSITY,
890 /* GST_LEVEL_WARNING */
891 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
893 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
894 /* GST_LEVEL_DEBUG */
895 FOREGROUND_GREEN | FOREGROUND_BLUE,
897 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
898 /* GST_LEVEL_FIXME */
899 FOREGROUND_RED | FOREGROUND_GREEN,
900 /* GST_LEVEL_TRACE */
901 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
902 /* placeholder for log level 8 */
904 /* GST_LEVEL_MEMDUMP */
905 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
908 static const guchar available_colors[] = {
909 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
910 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
911 FOREGROUND_GREEN | FOREGROUND_BLUE,
914 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
915 "\033[37m", /* GST_LEVEL_NONE */
916 "\033[31;01m", /* GST_LEVEL_ERROR */
917 "\033[33;01m", /* GST_LEVEL_WARNING */
918 "\033[32;01m", /* GST_LEVEL_INFO */
919 "\033[36m", /* GST_LEVEL_DEBUG */
920 "\033[37m", /* GST_LEVEL_LOG */
921 "\033[33;01m", /* GST_LEVEL_FIXME */
922 "\033[37m", /* GST_LEVEL_TRACE */
923 "\033[37m", /* placeholder for log level 8 */
924 "\033[37m" /* GST_LEVEL_MEMDUMP */
929 * gst_debug_log_default:
930 * @category: category to log
931 * @level: level of the message
932 * @file: the file that emitted the message, usually the __FILE__ identifier
933 * @function: the function that emitted the message
934 * @line: the line from that the message was emitted, usually __LINE__
935 * @message: the actual message
936 * @object: (transfer none) (allow-none): the object this message relates to,
938 * @unused: an unused variable, reserved for some user_data.
940 * The default logging handler used by GStreamer. Logging functions get called
941 * whenever a macro like GST_DEBUG or similar is used. This function outputs the
942 * message and additional info to stderr (or the log file specified via the
943 * GST_DEBUG_FILE environment variable).
945 * You can add other handlers by using gst_debug_add_log_function().
946 * And you can remove this handler by calling
947 * gst_debug_remove_log_function(gst_debug_log_default);
950 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
951 const gchar * file, const gchar * function, gint line,
952 GObject * object, GstDebugMessage * message, gpointer unused)
955 GstClockTime elapsed;
959 if (level > gst_debug_category_get_threshold (category))
963 is_colored = gst_debug_is_colored ();
966 obj = gst_debug_print_object (object);
971 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
972 gst_util_get_timestamp ());
976 /* colors, non-windows */
980 const gchar *levelcolor;
982 color = gst_debug_construct_term_color (gst_debug_category_get_color
985 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
986 levelcolor = levelcolormap[level];
988 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
989 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
990 pidcolor, pid, clear, g_thread_self (), levelcolor,
991 gst_debug_level_get_name (level), clear, color,
992 gst_debug_category_get_name (category), file, line, function, obj,
993 clear, gst_debug_message_get (message));
998 /* colors, windows. We take a lock to keep colors and content together.
999 * Maybe there is a better way but for now this will do the right
1001 static GMutex win_print_mutex;
1002 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1003 #define SET_COLOR(c) G_STMT_START { \
1004 if (log_file == stderr) \
1005 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1007 g_mutex_lock (&win_print_mutex);
1009 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1012 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1013 fprintf (log_file, PID_FMT, pid);
1017 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1020 SET_COLOR (levelcolormap[level]);
1021 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1024 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1026 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1027 file, line, function, obj);
1031 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1033 g_mutex_unlock (&win_print_mutex);
1036 /* no color, all platforms */
1037 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1038 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1039 pid, g_thread_self (), gst_debug_level_get_name (level),
1040 gst_debug_category_get_name (category), file, line, function, obj,
1041 gst_debug_message_get (message));
1050 * gst_debug_level_get_name:
1051 * @level: the level to get the name for
1053 * Get the string representation of a debugging level
1058 gst_debug_level_get_name (GstDebugLevel level)
1061 case GST_LEVEL_NONE:
1063 case GST_LEVEL_ERROR:
1065 case GST_LEVEL_WARNING:
1067 case GST_LEVEL_INFO:
1069 case GST_LEVEL_DEBUG:
1073 case GST_LEVEL_FIXME:
1075 case GST_LEVEL_TRACE:
1077 case GST_LEVEL_MEMDUMP:
1080 g_warning ("invalid level specified for gst_debug_level_get_name");
1086 * gst_debug_add_log_function:
1087 * @func: the function to use
1088 * @user_data: user data
1089 * @notify: called when @user_data is not used anymore
1091 * Adds the logging function to the list of logging functions.
1092 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1095 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1096 GDestroyNotify notify)
1098 LogFuncEntry *entry;
1102 func = gst_debug_log_default;
1104 entry = g_slice_new (LogFuncEntry);
1106 entry->user_data = user_data;
1107 entry->notify = notify;
1108 /* FIXME: we leak the old list here - other threads might access it right now
1109 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1110 * but that is waaay costly.
1111 * It'd probably be clever to use some kind of RCU here, but I don't know
1112 * anything about that.
1114 g_mutex_lock (&__log_func_mutex);
1115 list = g_slist_copy (__log_functions);
1116 __log_functions = g_slist_prepend (list, entry);
1117 g_mutex_unlock (&__log_func_mutex);
1119 if (gst_is_initialized ())
1120 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1125 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1127 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1129 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1133 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1135 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1137 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1141 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1144 GSList *new, *cleanup = NULL;
1147 g_mutex_lock (&__log_func_mutex);
1148 new = __log_functions;
1150 while ((found = g_slist_find_custom (new, data, func))) {
1151 if (new == __log_functions) {
1152 /* make a copy when we have the first hit, so that we modify the copy and
1153 * make that the new list later */
1154 new = g_slist_copy (new);
1157 cleanup = g_slist_prepend (cleanup, found->data);
1158 new = g_slist_delete_link (new, found);
1161 /* FIXME: We leak the old list here. See _add_log_function for why. */
1162 __log_functions = new;
1163 g_mutex_unlock (&__log_func_mutex);
1166 LogFuncEntry *entry = cleanup->data;
1169 entry->notify (entry->user_data);
1171 g_slice_free (LogFuncEntry, entry);
1172 cleanup = g_slist_delete_link (cleanup, cleanup);
1178 * gst_debug_remove_log_function:
1179 * @func: (scope call): the log function to remove
1181 * Removes all registered instances of the given logging functions.
1183 * Returns: How many instances of the function were removed
1186 gst_debug_remove_log_function (GstLogFunction func)
1191 func = gst_debug_log_default;
1194 gst_debug_remove_with_compare_func
1195 (gst_debug_compare_log_function_by_func, (gpointer) func);
1196 if (gst_is_initialized ())
1197 GST_DEBUG ("removed log function %p %d times from log function list", func,
1204 * gst_debug_remove_log_function_by_data:
1205 * @data: user data of the log function to remove
1207 * Removes all registered instances of log functions with the given user data.
1209 * Returns: How many instances of the function were removed
1212 gst_debug_remove_log_function_by_data (gpointer data)
1217 gst_debug_remove_with_compare_func
1218 (gst_debug_compare_log_function_by_data, data);
1220 if (gst_is_initialized ())
1222 ("removed %d log functions with user data %p from log function list",
1229 * gst_debug_set_colored:
1230 * @colored: Whether to use colored output or not
1232 * Sets or unsets the use of coloured debugging output.
1234 * This function may be called before gst_init().
1237 gst_debug_set_colored (gboolean colored)
1239 g_atomic_int_set (&__use_color, (gint) colored);
1243 * gst_debug_is_colored:
1245 * Checks if the debugging output should be colored.
1247 * Returns: TRUE, if the debug output should be colored.
1250 gst_debug_is_colored (void)
1252 return (gboolean) g_atomic_int_get (&__use_color);
1256 * gst_debug_set_active:
1257 * @active: Whether to use debugging output or not
1259 * If activated, debugging messages are sent to the debugging
1261 * It makes sense to deactivate it for speed issues.
1262 * <note><para>This function is not threadsafe. It makes sense to only call it
1263 * during initialization.</para></note>
1266 gst_debug_set_active (gboolean active)
1268 _gst_debug_enabled = active;
1270 _gst_debug_min = GST_LEVEL_COUNT;
1272 _gst_debug_min = GST_LEVEL_NONE;
1276 * gst_debug_is_active:
1278 * Checks if debugging output is activated.
1280 * Returns: TRUE, if debugging is activated
1283 gst_debug_is_active (void)
1285 return _gst_debug_enabled;
1289 * gst_debug_set_default_threshold:
1290 * @level: level to set
1292 * Sets the default threshold to the given level and updates all categories to
1293 * use this threshold.
1295 * This function may be called before gst_init().
1298 gst_debug_set_default_threshold (GstDebugLevel level)
1300 g_atomic_int_set (&__default_level, level);
1301 gst_debug_reset_all_thresholds ();
1305 * gst_debug_get_default_threshold:
1307 * Returns the default threshold that is used for new categories.
1309 * Returns: the default threshold level
1312 gst_debug_get_default_threshold (void)
1314 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1318 gst_debug_reset_threshold (gpointer category, gpointer unused)
1320 GstDebugCategory *cat = (GstDebugCategory *) category;
1323 g_mutex_lock (&__level_name_mutex);
1324 walk = __level_name;
1326 LevelNameEntry *entry = walk->data;
1328 walk = g_slist_next (walk);
1329 if (g_pattern_match_string (entry->pat, cat->name)) {
1330 if (gst_is_initialized ())
1331 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1332 cat->name, entry->pat, entry->level);
1333 gst_debug_category_set_threshold (cat, entry->level);
1337 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1340 g_mutex_unlock (&__level_name_mutex);
1344 gst_debug_reset_all_thresholds (void)
1346 g_mutex_lock (&__cat_mutex);
1347 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1348 g_mutex_unlock (&__cat_mutex);
1352 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1354 GstDebugCategory *cat = (GstDebugCategory *) data;
1355 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1357 if (g_pattern_match_string (entry->pat, cat->name)) {
1358 if (gst_is_initialized ())
1359 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1360 cat->name, entry->pat, entry->level);
1361 gst_debug_category_set_threshold (cat, entry->level);
1366 * gst_debug_set_threshold_for_name:
1367 * @name: name of the categories to set
1368 * @level: level to set them to
1370 * Sets all categories which match the given glob style pattern to the given
1374 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1377 LevelNameEntry *entry;
1379 g_return_if_fail (name != NULL);
1381 pat = g_pattern_spec_new (name);
1382 entry = g_slice_new (LevelNameEntry);
1384 entry->level = level;
1385 g_mutex_lock (&__level_name_mutex);
1386 __level_name = g_slist_prepend (__level_name, entry);
1387 g_mutex_unlock (&__level_name_mutex);
1388 g_mutex_lock (&__cat_mutex);
1389 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1390 g_mutex_unlock (&__cat_mutex);
1394 * gst_debug_unset_threshold_for_name:
1395 * @name: name of the categories to set
1397 * Resets all categories with the given name back to the default level.
1400 gst_debug_unset_threshold_for_name (const gchar * name)
1405 g_return_if_fail (name != NULL);
1407 pat = g_pattern_spec_new (name);
1408 g_mutex_lock (&__level_name_mutex);
1409 walk = __level_name;
1410 /* improve this if you want, it's mighty slow */
1412 LevelNameEntry *entry = walk->data;
1414 if (g_pattern_spec_equal (entry->pat, pat)) {
1415 __level_name = g_slist_remove_link (__level_name, walk);
1416 g_pattern_spec_free (entry->pat);
1417 g_slice_free (LevelNameEntry, entry);
1418 g_slist_free_1 (walk);
1419 walk = __level_name;
1422 g_mutex_unlock (&__level_name_mutex);
1423 g_pattern_spec_free (pat);
1424 gst_debug_reset_all_thresholds ();
1428 _gst_debug_category_new (const gchar * name, guint color,
1429 const gchar * description)
1431 GstDebugCategory *cat;
1433 g_return_val_if_fail (name != NULL, NULL);
1435 cat = g_slice_new (GstDebugCategory);
1436 cat->name = g_strdup (name);
1438 if (description != NULL) {
1439 cat->description = g_strdup (description);
1441 cat->description = g_strdup ("no description");
1443 g_atomic_int_set (&cat->threshold, 0);
1444 gst_debug_reset_threshold (cat, NULL);
1446 /* add to category list */
1447 g_mutex_lock (&__cat_mutex);
1448 __categories = g_slist_prepend (__categories, cat);
1449 g_mutex_unlock (&__cat_mutex);
1455 * gst_debug_category_free:
1456 * @category: #GstDebugCategory to free.
1458 * Removes and frees the category and all associated resources.
1461 gst_debug_category_free (GstDebugCategory * category)
1463 if (category == NULL)
1466 /* remove from category list */
1467 g_mutex_lock (&__cat_mutex);
1468 __categories = g_slist_remove (__categories, category);
1469 g_mutex_unlock (&__cat_mutex);
1471 g_free ((gpointer) category->name);
1472 g_free ((gpointer) category->description);
1473 g_slice_free (GstDebugCategory, category);
1477 * gst_debug_category_set_threshold:
1478 * @category: a #GstDebugCategory to set threshold of.
1479 * @level: the #GstDebugLevel threshold to set.
1481 * Sets the threshold of the category to the given level. Debug information will
1482 * only be output if the threshold is lower or equal to the level of the
1483 * debugging message.
1485 * Do not use this function in production code, because other functions may
1486 * change the threshold of categories as side effect. It is however a nice
1487 * function to use when debugging (even from gdb).
1491 gst_debug_category_set_threshold (GstDebugCategory * category,
1492 GstDebugLevel level)
1494 g_return_if_fail (category != NULL);
1496 if (level > _gst_debug_min) {
1497 _gst_debug_enabled = TRUE;
1498 _gst_debug_min = level;
1501 g_atomic_int_set (&category->threshold, level);
1505 * gst_debug_category_reset_threshold:
1506 * @category: a #GstDebugCategory to reset threshold of.
1508 * Resets the threshold of the category to the default level. Debug information
1509 * will only be output if the threshold is lower or equal to the level of the
1510 * debugging message.
1511 * Use this function to set the threshold back to where it was after using
1512 * gst_debug_category_set_threshold().
1515 gst_debug_category_reset_threshold (GstDebugCategory * category)
1517 gst_debug_reset_threshold (category, NULL);
1521 * gst_debug_category_get_threshold:
1522 * @category: a #GstDebugCategory to get threshold of.
1524 * Returns the threshold of a #GstDebugCategory.
1526 * Returns: the #GstDebugLevel that is used as threshold.
1529 gst_debug_category_get_threshold (GstDebugCategory * category)
1531 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1535 * gst_debug_category_get_name:
1536 * @category: a #GstDebugCategory to get name of.
1538 * Returns the name of a debug category.
1540 * Returns: the name of the category.
1543 gst_debug_category_get_name (GstDebugCategory * category)
1545 return category->name;
1549 * gst_debug_category_get_color:
1550 * @category: a #GstDebugCategory to get the color of.
1552 * Returns the color of a debug category used when printing output in this
1555 * Returns: the color of the category.
1558 gst_debug_category_get_color (GstDebugCategory * category)
1560 return category->color;
1564 * gst_debug_category_get_description:
1565 * @category: a #GstDebugCategory to get the description of.
1567 * Returns the description of a debug category.
1569 * Returns: the description of the category.
1572 gst_debug_category_get_description (GstDebugCategory * category)
1574 return category->description;
1578 * gst_debug_get_all_categories:
1580 * Returns a snapshot of a all categories that are currently in use . This list
1581 * may change anytime.
1582 * The caller has to free the list after use.
1584 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1588 gst_debug_get_all_categories (void)
1592 g_mutex_lock (&__cat_mutex);
1593 ret = g_slist_copy (__categories);
1594 g_mutex_unlock (&__cat_mutex);
1600 _gst_debug_get_category (const gchar * name)
1602 GstDebugCategory *ret = NULL;
1605 for (node = __categories; node; node = g_slist_next (node)) {
1606 ret = (GstDebugCategory *) node->data;
1607 if (!strcmp (name, ret->name)) {
1615 parse_debug_category (gchar * str, const gchar ** category)
1620 /* works in place */
1623 if (str[0] != '\0') {
1632 parse_debug_level (gchar * str, GstDebugLevel * level)
1637 /* works in place */
1640 if (str[0] != '\0' && str[1] == '\0'
1641 && str[0] >= '0' && str[0] < '0' + GST_LEVEL_COUNT) {
1642 *level = (GstDebugLevel) (str[0] - '0');
1643 } else if (strcmp (str, "ERROR") == 0) {
1644 *level = GST_LEVEL_ERROR;
1645 } else if (strncmp (str, "WARN", 4) == 0) {
1646 *level = GST_LEVEL_WARNING;
1647 } else if (strcmp (str, "FIXME") == 0) {
1648 *level = GST_LEVEL_FIXME;
1649 } else if (strcmp (str, "INFO") == 0) {
1650 *level = GST_LEVEL_INFO;
1651 } else if (strcmp (str, "DEBUG") == 0) {
1652 *level = GST_LEVEL_DEBUG;
1653 } else if (strcmp (str, "LOG") == 0) {
1654 *level = GST_LEVEL_LOG;
1655 } else if (strcmp (str, "TRACE") == 0) {
1656 *level = GST_LEVEL_TRACE;
1657 } else if (strcmp (str, "MEMDUMP") == 0) {
1658 *level = GST_LEVEL_MEMDUMP;
1666 * gst_debug_set_threshold_from_string:
1667 * @list: comma-separated list of "category:level" pairs to be used
1668 * as debug logging levels
1669 * @reset: %TRUE to clear all previously-set debug levels before setting
1671 * %FALSE if adding the threshold described by @list to the one already set.
1673 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1674 * environment variable. You can use wildcards such as '*', but note that
1675 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1676 * everything to log level 2.
1681 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1689 gst_debug_set_default_threshold (0);
1691 split = g_strsplit (list, ",", 0);
1693 for (walk = split; *walk; walk++) {
1694 if (strchr (*walk, ':')) {
1695 gchar **values = g_strsplit (*walk, ":", 2);
1697 if (values[0] && values[1]) {
1698 GstDebugLevel level;
1699 const gchar *category;
1701 if (parse_debug_category (values[0], &category)
1702 && parse_debug_level (values[1], &level))
1703 gst_debug_set_threshold_for_name (category, level);
1706 g_strfreev (values);
1708 GstDebugLevel level;
1710 if (parse_debug_level (*walk, &level))
1711 gst_debug_set_default_threshold (level);
1718 /*** FUNCTION POINTERS ********************************************************/
1720 static GHashTable *__gst_function_pointers; /* NULL */
1721 static GMutex __dbg_functions_mutex;
1723 /* This function MUST NOT return NULL */
1725 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1733 if (G_UNLIKELY (func == NULL))
1736 g_mutex_lock (&__dbg_functions_mutex);
1737 if (G_LIKELY (__gst_function_pointers)) {
1738 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1739 g_mutex_unlock (&__dbg_functions_mutex);
1740 if (G_LIKELY (ptrname))
1743 g_mutex_unlock (&__dbg_functions_mutex);
1745 /* we need to create an entry in the hash table for this one so we don't leak
1748 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1749 gchar *name = g_strdup (dl_info.dli_sname);
1751 _gst_debug_register_funcptr (func, name);
1756 gchar *name = g_strdup_printf ("%p", (gpointer) func);
1758 _gst_debug_register_funcptr (func, name);
1764 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1766 gpointer ptr = (gpointer) func;
1768 g_mutex_lock (&__dbg_functions_mutex);
1770 if (!__gst_function_pointers)
1771 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1772 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1773 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1775 g_mutex_unlock (&__dbg_functions_mutex);
1778 /*** PRINTF EXTENSIONS ********************************************************/
1782 _gst_info_printf_extension_ptr (FILE * stream, const struct printf_info *info,
1783 const void *const *args)
1790 ptr = *(void **) args[0];
1792 buffer = gst_debug_print_object (ptr);
1793 len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
1801 _gst_info_printf_extension_segment (FILE * stream,
1802 const struct printf_info *info, const void *const *args)
1809 ptr = *(void **) args[0];
1811 buffer = gst_debug_print_segment (ptr);
1812 len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
1821 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1822 const guint8 * mem, gsize mem_offset, gsize mem_size)
1824 gchar hexstr[50], ascstr[18], digitstr[4];
1836 while (i < mem_size) {
1837 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1838 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1839 g_strlcat (hexstr, digitstr, sizeof (hexstr));
1845 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1846 (guint) mem_offset, hexstr, ascstr);
1850 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1851 const gchar * func, gint line, GObject * obj, const gchar * msg,
1852 const guint8 * data, guint length)
1856 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1857 "-------------------------------------------------------------------");
1859 if (msg != NULL && *msg != '\0') {
1860 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1863 while (off < length) {
1866 /* gst_info_dump_mem_line will process 16 bytes at most */
1867 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1868 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
1872 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1873 "-------------------------------------------------------------------");
1876 #else /* !GST_DISABLE_GST_DEBUG */
1877 #ifndef GST_REMOVE_DISABLED
1880 _gst_debug_category_new (const gchar * name, guint color,
1881 const gchar * description)
1887 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1891 /* This function MUST NOT return NULL */
1893 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1899 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
1900 const gchar * file, const gchar * function, gint line,
1901 GObject * object, const gchar * format, ...)
1906 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
1907 const gchar * file, const gchar * function, gint line,
1908 GObject * object, const gchar * format, va_list args)
1913 gst_debug_message_get (GstDebugMessage * message)
1919 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1920 const gchar * file, const gchar * function, gint line,
1921 GObject * object, GstDebugMessage * message, gpointer unused)
1926 gst_debug_level_get_name (GstDebugLevel level)
1932 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1933 GDestroyNotify notify)
1938 gst_debug_remove_log_function (GstLogFunction func)
1944 gst_debug_remove_log_function_by_data (gpointer data)
1950 gst_debug_set_active (gboolean active)
1955 gst_debug_is_active (void)
1961 gst_debug_set_colored (gboolean colored)
1966 gst_debug_is_colored (void)
1972 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1977 gst_debug_set_default_threshold (GstDebugLevel level)
1982 gst_debug_get_default_threshold (void)
1984 return GST_LEVEL_NONE;
1988 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1993 gst_debug_unset_threshold_for_name (const gchar * name)
1998 gst_debug_category_free (GstDebugCategory * category)
2003 gst_debug_category_set_threshold (GstDebugCategory * category,
2004 GstDebugLevel level)
2009 gst_debug_category_reset_threshold (GstDebugCategory * category)
2014 gst_debug_category_get_threshold (GstDebugCategory * category)
2016 return GST_LEVEL_NONE;
2020 gst_debug_category_get_name (GstDebugCategory * category)
2026 gst_debug_category_get_color (GstDebugCategory * category)
2032 gst_debug_category_get_description (GstDebugCategory * category)
2038 gst_debug_get_all_categories (void)
2044 _gst_debug_get_category (const gchar * name)
2050 gst_debug_construct_term_color (guint colorinfo)
2052 return g_strdup ("00");
2056 gst_debug_construct_win_color (guint colorinfo)
2062 _priv_gst_in_valgrind (void)
2068 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2069 const gchar * func, gint line, GObject * obj, const gchar * msg,
2070 const guint8 * data, guint length)
2073 #endif /* GST_REMOVE_DISABLED */
2074 #endif /* GST_DISABLE_GST_DEBUG */
2077 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2078 /* FIXME make this thread specific */
2079 static GSList *stack_trace = NULL;
2082 __cyg_profile_func_enter (void *this_fn, void *call_site)
2083 G_GNUC_NO_INSTRUMENT;
2084 void __cyg_profile_func_enter (void *this_fn, void *call_site)
2086 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2087 gchar *site = _gst_debug_nameof_funcptr (call_site);
2089 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2092 g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2093 this_fn, name, call_site, site));
2100 __cyg_profile_func_exit (void *this_fn, void *call_site)
2101 G_GNUC_NO_INSTRUMENT;
2102 void __cyg_profile_func_exit (void *this_fn, void *call_site)
2104 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2106 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2107 g_free (stack_trace->data);
2108 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2114 * gst_debug_print_stack_trace:
2116 * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2117 * gstreamer code, which can be printed with this function.
2120 gst_debug_print_stack_trace (void)
2122 GSList *walk = stack_trace;
2126 walk = g_slist_next (walk);
2129 gchar *name = (gchar *) walk->data;
2131 g_print ("#%-2d %s\n", count++, name);
2133 walk = g_slist_next (walk);
2138 gst_debug_print_stack_trace (void)
2140 /* nothing because it's compiled out */
2143 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */