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 #ifdef HAVE_PRINTF_EXTENSION
104 #include <stdio.h> /* fprintf */
105 #include <glib/gstdio.h>
108 # include <unistd.h> /* getpid on UNIX */
110 #ifdef HAVE_PROCESS_H
111 # include <process.h> /* getpid on win32 */
113 #include <string.h> /* G_VA_COPY */
115 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
116 # include <windows.h> /* GetStdHandle, windows console */
119 #include "gst_private.h"
120 #include "gstutils.h"
121 #include "gstquark.h"
122 #include "gstsegment.h"
123 #include "gstvalue.h"
125 #ifdef HAVE_VALGRIND_VALGRIND_H
126 # include <valgrind/valgrind.h>
128 #include <glib/gprintf.h> /* g_sprintf */
130 #endif /* !GST_DISABLE_GST_DEBUG */
132 extern gboolean gst_is_initialized (void);
134 /* we want these symbols exported even if debug is disabled, to maintain
135 * ABI compatibility. Unless GST_REMOVE_DISABLED is defined. */
136 #if !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED)
138 /* disabled by default, as soon as some threshold is set > NONE,
139 * it becomes enabled. */
140 gboolean _gst_debug_enabled = FALSE;
141 GstDebugLevel _gst_debug_min = GST_LEVEL_NONE;
143 GstDebugCategory *GST_CAT_DEFAULT = NULL;
145 GstDebugCategory *GST_CAT_GST_INIT = NULL;
146 GstDebugCategory *GST_CAT_MEMORY = NULL;
147 GstDebugCategory *GST_CAT_PARENTAGE = NULL;
148 GstDebugCategory *GST_CAT_STATES = NULL;
149 GstDebugCategory *GST_CAT_SCHEDULING = NULL;
151 GstDebugCategory *GST_CAT_BUFFER = NULL;
152 GstDebugCategory *GST_CAT_BUFFER_LIST = NULL;
153 GstDebugCategory *GST_CAT_BUS = NULL;
154 GstDebugCategory *GST_CAT_CAPS = NULL;
155 GstDebugCategory *GST_CAT_CLOCK = NULL;
156 GstDebugCategory *GST_CAT_ELEMENT_PADS = NULL;
157 GstDebugCategory *GST_CAT_PADS = NULL;
158 GstDebugCategory *GST_CAT_PERFORMANCE = NULL;
159 GstDebugCategory *GST_CAT_PIPELINE = NULL;
160 GstDebugCategory *GST_CAT_PLUGIN_LOADING = NULL;
161 GstDebugCategory *GST_CAT_PLUGIN_INFO = NULL;
162 GstDebugCategory *GST_CAT_PROPERTIES = NULL;
163 GstDebugCategory *GST_CAT_NEGOTIATION = NULL;
164 GstDebugCategory *GST_CAT_REFCOUNTING = NULL;
165 GstDebugCategory *GST_CAT_ERROR_SYSTEM = NULL;
166 GstDebugCategory *GST_CAT_EVENT = NULL;
167 GstDebugCategory *GST_CAT_MESSAGE = NULL;
168 GstDebugCategory *GST_CAT_PARAMS = NULL;
169 GstDebugCategory *GST_CAT_CALL_TRACE = NULL;
170 GstDebugCategory *GST_CAT_SIGNAL = NULL;
171 GstDebugCategory *GST_CAT_PROBE = NULL;
172 GstDebugCategory *GST_CAT_REGISTRY = NULL;
173 GstDebugCategory *GST_CAT_QOS = NULL;
174 GstDebugCategory *_priv_GST_CAT_POLL = NULL;
175 GstDebugCategory *GST_CAT_META = NULL;
176 GstDebugCategory *GST_CAT_LOCKING = NULL;
177 GstDebugCategory *GST_CAT_CONTEXT = NULL;
180 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
182 #ifndef GST_DISABLE_GST_DEBUG
184 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
185 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
187 /* time of initialization, so we get useful debugging output times
188 * FIXME: we use this in gstdebugutils.c, what about a function + macro to
189 * get the running time: GST_DEBUG_RUNNING_TIME
191 GstClockTime _priv_gst_info_start_time;
195 #include <rld_interface.h>
196 typedef struct DL_INFO
198 const char *dli_fname;
200 const char *dli_sname;
204 long dli_reserved[4];
208 #define _RLD_DLADDR 14
209 int dladdr (void *address, Dl_info * dl);
212 dladdr (void *address, Dl_info * dl)
216 v = _rld_new_interface (_RLD_DLADDR, address, dl);
222 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
223 static void gst_debug_reset_all_thresholds (void);
225 #ifdef GST_USING_PRINTF_EXTENSION
226 static int _gst_info_printf_extension_ptr (FILE * stream,
227 const struct printf_info *info, const void *const *args);
228 static int _gst_info_printf_extension_segment (FILE * stream,
229 const struct printf_info *info, const void *const *args);
230 #ifdef HAVE_REGISTER_PRINTF_SPECIFIER
231 static int _gst_info_printf_extension_arginfo (const struct printf_info *info,
232 size_t n, int *argtypes, int *size);
234 static int _gst_info_printf_extension_arginfo (const struct printf_info *info,
235 size_t n, int *argtypes);
239 struct _GstDebugMessage
246 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
247 static GMutex __level_name_mutex;
248 static GSList *__level_name = NULL;
256 /* list of all categories */
257 static GMutex __cat_mutex;
258 static GSList *__categories = NULL;
260 /* all registered debug handlers */
265 GDestroyNotify notify;
268 static GMutex __log_func_mutex;
269 static GSList *__log_functions = NULL;
271 #define PRETTY_TAGS_DEFAULT TRUE
272 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
274 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
275 static volatile gint G_GNUC_MAY_ALIAS __use_color = 1;
277 static FILE *log_file;
279 /* FIXME: export this? */
281 _priv_gst_in_valgrind (void)
289 in_valgrind = GST_VG_UNCHECKED;
291 if (in_valgrind == GST_VG_UNCHECKED) {
292 #ifdef HAVE_VALGRIND_VALGRIND_H
293 if (RUNNING_ON_VALGRIND) {
294 GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
295 printf ("GStreamer has detected that it is running inside valgrind.\n");
296 printf ("It might now take different code paths to ease debugging.\n");
297 printf ("Of course, this may also lead to different bugs.\n");
298 in_valgrind = GST_VG_INSIDE;
300 GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
301 in_valgrind = GST_VG_NO_VALGRIND;
304 in_valgrind = GST_VG_NO_VALGRIND;
306 g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
307 in_valgrind == GST_VG_INSIDE);
309 return (in_valgrind == GST_VG_INSIDE);
312 /* Initialize the debugging system */
314 _priv_gst_debug_init (void)
318 env = g_getenv ("GST_DEBUG_FILE");
319 if (env != NULL && *env != '\0') {
320 if (strcmp (env, "-") == 0) {
323 log_file = g_fopen (env, "w");
324 if (log_file == NULL) {
325 g_printerr ("Could not open log file '%s' for writing: %s\n", env,
334 /* get time we started for debugging messages */
335 _priv_gst_info_start_time = gst_util_get_timestamp ();
337 #ifdef GST_USING_PRINTF_EXTENSION
338 #ifdef HAVE_REGISTER_PRINTF_SPECIFIER
339 register_printf_specifier (GST_PTR_FORMAT[0], _gst_info_printf_extension_ptr,
340 _gst_info_printf_extension_arginfo);
341 register_printf_specifier (GST_SEGMENT_FORMAT[0],
342 _gst_info_printf_extension_segment, _gst_info_printf_extension_arginfo);
344 register_printf_function (GST_PTR_FORMAT[0], _gst_info_printf_extension_ptr,
345 _gst_info_printf_extension_arginfo);
346 register_printf_function (GST_SEGMENT_FORMAT[0],
347 _gst_info_printf_extension_segment, _gst_info_printf_extension_arginfo);
351 /* do NOT use a single debug function before this line has been run */
352 GST_CAT_DEFAULT = _gst_debug_category_new ("default",
353 GST_DEBUG_UNDERLINE, NULL);
354 _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
355 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
357 gst_debug_add_log_function (gst_debug_log_default, NULL, NULL);
359 /* FIXME: add descriptions here */
360 GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
361 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
362 GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
363 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
364 GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
365 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
366 GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
367 GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
368 GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
369 GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
370 GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
371 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
372 GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
373 GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
374 GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
375 GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
376 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
377 GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
378 GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
379 GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
380 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
381 GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
382 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
383 GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
384 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
385 GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
386 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
387 GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
388 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
389 GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
390 GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
391 GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
392 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
393 GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
394 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
395 GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
396 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
397 GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
398 GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
400 GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
401 GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
402 GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
403 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
404 GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
405 GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
406 GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
407 GST_DEBUG_BOLD, NULL);
408 GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
409 GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
410 GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
411 GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
412 GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
413 GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
414 _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
415 GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
416 GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
417 GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
419 /* print out the valgrind message if we're in valgrind */
420 _priv_gst_in_valgrind ();
422 env = g_getenv ("GST_DEBUG_OPTIONS");
424 if (strstr (env, "full_tags") || strstr (env, "full-tags"))
426 else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
431 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
432 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
436 * @category: category to log
437 * @level: level of the message is in
438 * @file: the file that emitted the message, usually the __FILE__ identifier
439 * @function: the function that emitted the message
440 * @line: the line from that the message was emitted, usually __LINE__
441 * @object: (transfer none) (allow-none): the object this message relates to,
443 * @format: a printf style format string
444 * @...: optional arguments for the format
446 * Logs the given message using the currently registered debugging handlers.
449 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
450 const gchar * file, const gchar * function, gint line,
451 GObject * object, const gchar * format, ...)
455 va_start (var_args, format);
456 gst_debug_log_valist (category, level, file, function, line, object, format,
462 /* based on g_basename(), which we can't use because it was deprecated */
463 static inline const gchar *
464 gst_path_basename (const gchar * file_name)
466 register const gchar *base;
468 base = strrchr (file_name, G_DIR_SEPARATOR);
471 const gchar *q = strrchr (file_name, '/');
472 if (base == NULL || (q != NULL && q > base))
479 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
480 return file_name + 2;
487 * gst_debug_log_valist:
488 * @category: category to log
489 * @level: level of the message is in
490 * @file: the file that emitted the message, usually the __FILE__ identifier
491 * @function: the function that emitted the message
492 * @line: the line from that the message was emitted, usually __LINE__
493 * @object: (transfer none) (allow-none): the object this message relates to,
495 * @format: a printf style format string
496 * @args: optional arguments for the format
498 * Logs the given message using the currently registered debugging handlers.
501 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
502 const gchar * file, const gchar * function, gint line,
503 GObject * object, const gchar * format, va_list args)
505 GstDebugMessage message;
509 g_return_if_fail (category != NULL);
510 g_return_if_fail (file != NULL);
511 g_return_if_fail (function != NULL);
512 g_return_if_fail (format != NULL);
514 /* The predefined macro __FILE__ is always the exact path given to the
515 * compiler with MSVC, which may or may not be the basename. We work
516 * around it at runtime to improve the readability. */
518 file = gst_path_basename (file);
521 message.message = NULL;
522 message.format = format;
523 G_VA_COPY (message.arguments, args);
525 handler = __log_functions;
527 entry = handler->data;
528 handler = g_slist_next (handler);
529 entry->func (category, level, file, function, line, object, &message,
532 g_free (message.message);
533 va_end (message.arguments);
537 * gst_debug_message_get:
538 * @message: a debug message
540 * Gets the string representation of a #GstDebugMessage. This function is used
541 * in debug handlers to extract the message.
543 * Returns: the string representation of a #GstDebugMessage.
546 gst_debug_message_get (GstDebugMessage * message)
548 if (message->message == NULL) {
549 message->message = g_strdup_vprintf (message->format, message->arguments);
551 return message->message;
554 #define MAX_BUFFER_DUMP_STRING_LEN 100
556 /* structure_to_pretty_string:
557 * @structure: a #GstStructure
559 * Converts @structure to a human-readable string representation. Basically
560 * the same as gst_structure_to_string(), but if the structure contains large
561 * buffers such as images the hex representation of those buffers will be
562 * shortened so that the string remains readable.
564 * Returns: a newly-allocated string. g_free() when no longer needed.
567 structure_to_pretty_string (const GstStructure * s)
569 gchar *str, *pos, *end;
571 str = gst_structure_to_string (s);
576 while ((pos = strstr (pos, "(buffer)"))) {
579 pos += strlen ("(buffer)");
580 for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
582 if (count > MAX_BUFFER_DUMP_STRING_LEN) {
583 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
584 memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
585 g_memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
586 strlen (pos + count) + 1);
587 pos += MAX_BUFFER_DUMP_STRING_LEN;
594 static inline gchar *
595 gst_info_structure_to_string (const GstStructure * s)
597 if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
598 return structure_to_pretty_string (s);
600 return gst_structure_to_string (s);
604 gst_debug_print_object (gpointer ptr)
606 GObject *object = (GObject *) ptr;
609 /* This is a cute trick to detect unmapped memory, but is unportable,
610 * slow, screws around with madvise, and not actually that useful. */
614 ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
615 if (ret == -1 && errno == ENOMEM) {
616 buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
621 /* nicely printed object */
622 if (object == NULL) {
623 return g_strdup ("(NULL)");
625 if (*(GType *) ptr == GST_TYPE_CAPS) {
626 return gst_caps_to_string ((const GstCaps *) ptr);
628 if (*(GType *) ptr == GST_TYPE_STRUCTURE) {
629 return gst_info_structure_to_string ((const GstStructure *) ptr);
631 if (*(GType *) ptr == GST_TYPE_TAG_LIST) {
632 /* FIXME: want pretty tag list with long byte dumps removed.. */
633 return gst_tag_list_to_string ((GstTagList *) ptr);
635 if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
636 return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
638 if (GST_IS_BUFFER (ptr)) {
639 GstBuffer *buf = (GstBuffer *) ptr;
643 g_strdup_printf ("%p, pts %" GST_TIME_FORMAT ", dts %" GST_TIME_FORMAT
644 ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", offset %"
645 G_GUINT64_FORMAT ", offset_end %" G_GUINT64_FORMAT, buf,
646 GST_TIME_ARGS (GST_BUFFER_PTS (buf)),
647 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
648 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), gst_buffer_get_size (buf),
649 GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf));
653 if (*(guint32 *) ptr == 0xffffffff) {
654 return g_strdup_printf ("<poisoned@%p>", ptr);
657 if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
658 return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
660 if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
661 return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
663 if (G_IS_OBJECT (object)) {
664 return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
666 if (GST_IS_MESSAGE (object)) {
667 GstMessage *msg = GST_MESSAGE_CAST (object);
669 const GstStructure *structure;
671 structure = gst_message_get_structure (msg);
674 s = gst_info_structure_to_string (structure);
676 s = g_strdup ("(NULL)");
679 ret = g_strdup_printf ("%s message from element '%s': %s",
680 GST_MESSAGE_TYPE_NAME (msg), (msg->src != NULL) ?
681 GST_ELEMENT_NAME (msg->src) : "(NULL)", s);
685 if (GST_IS_QUERY (object)) {
686 GstQuery *query = GST_QUERY_CAST (object);
687 const GstStructure *structure;
689 structure = gst_query_get_structure (query);
692 return gst_info_structure_to_string (structure);
694 const gchar *query_type_name;
696 query_type_name = gst_query_type_get_name (query->type);
697 if (G_LIKELY (query_type_name != NULL)) {
698 return g_strdup_printf ("%s query", query_type_name);
700 return g_strdup_printf ("query of unknown type %d", query->type);
704 if (GST_IS_EVENT (object)) {
705 GstEvent *event = GST_EVENT_CAST (object);
707 GstStructure *structure;
709 structure = (GstStructure *) gst_event_get_structure (event);
711 s = gst_info_structure_to_string (structure);
713 s = g_strdup ("(NULL)");
716 ret = g_strdup_printf ("%s event at time %"
717 GST_TIME_FORMAT ": %s",
718 GST_EVENT_TYPE_NAME (event), GST_TIME_ARGS (event->timestamp), s);
722 if (GST_IS_CONTEXT (object)) {
723 GstContext *context = GST_CONTEXT_CAST (object);
725 const GstStructure *structure;
727 structure = gst_context_get_structure (context);
730 s = gst_info_structure_to_string (structure);
732 s = g_strdup ("(NULL)");
735 ret = g_strdup_printf ("context '%s'", s);
740 return g_strdup_printf ("%p", ptr);
743 #ifdef GST_USING_PRINTF_EXTENSION
746 gst_debug_print_segment (gpointer ptr)
748 GstSegment *segment = (GstSegment *) ptr;
750 /* nicely printed segment */
751 if (segment == NULL) {
752 return g_strdup ("(NULL)");
755 switch (segment->format) {
756 case GST_FORMAT_UNDEFINED:{
757 return g_strdup_printf ("UNDEFINED segment");
759 case GST_FORMAT_TIME:{
760 return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
761 ", stop=%" GST_TIME_FORMAT ", rate=%f, applied_rate=%f"
762 ", flags=0x%02x, time=%" GST_TIME_FORMAT ", base=%" GST_TIME_FORMAT
763 ", position %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
764 GST_TIME_ARGS (segment->start), GST_TIME_ARGS (segment->stop),
765 segment->rate, segment->applied_rate, (guint) segment->flags,
766 GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
767 GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
770 const gchar *format_name;
772 format_name = gst_format_get_name (segment->format);
773 if (G_UNLIKELY (format_name == NULL))
774 format_name = "(UNKNOWN FORMAT)";
775 return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
776 ", stop=%" G_GINT64_FORMAT ", rate=%f, applied_rate=%f"
777 ", flags=0x%02x, time=%" G_GINT64_FORMAT ", base=%" G_GINT64_FORMAT
778 ", position %" G_GINT64_FORMAT ", duration %" G_GINT64_FORMAT,
779 format_name, segment->start, segment->stop, segment->rate,
780 segment->applied_rate, (guint) segment->flags,
781 segment->time, segment->base, segment->position, segment->duration);
786 #endif /* GST_USING_PRINTF_EXTENSION */
789 * gst_debug_construct_term_color:
790 * @colorinfo: the color info
792 * Constructs a string that can be used for getting the desired color in color
794 * You need to free the string after use.
796 * Returns: (transfer full) (type gchar*): a string containing the color
800 gst_debug_construct_term_color (guint colorinfo)
804 color = g_string_new ("\033[00");
806 if (colorinfo & GST_DEBUG_BOLD) {
807 g_string_append_len (color, ";01", 3);
809 if (colorinfo & GST_DEBUG_UNDERLINE) {
810 g_string_append_len (color, ";04", 3);
812 if (colorinfo & GST_DEBUG_FG_MASK) {
813 g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
815 if (colorinfo & GST_DEBUG_BG_MASK) {
816 g_string_append_printf (color, ";4%1d",
817 (colorinfo & GST_DEBUG_BG_MASK) >> 4);
819 g_string_append_c (color, 'm');
821 return g_string_free (color, FALSE);
825 * gst_debug_construct_win_color:
826 * @colorinfo: the color info
828 * Constructs an integer that can be used for getting the desired color in
829 * windows' terminals (cmd.exe). As there is no mean to underline, we simply
830 * ignore this attribute.
832 * This function returns 0 on non-windows machines.
834 * Returns: an integer containing the color definition
837 gst_debug_construct_win_color (guint colorinfo)
841 static const guchar ansi_to_win_fg[8] = {
843 FOREGROUND_RED, /* red */
844 FOREGROUND_GREEN, /* green */
845 FOREGROUND_RED | FOREGROUND_GREEN, /* yellow */
846 FOREGROUND_BLUE, /* blue */
847 FOREGROUND_RED | FOREGROUND_BLUE, /* magenta */
848 FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan */
849 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white */
851 static const guchar ansi_to_win_bg[8] = {
855 BACKGROUND_RED | BACKGROUND_GREEN,
857 BACKGROUND_RED | BACKGROUND_BLUE,
858 BACKGROUND_GREEN | FOREGROUND_BLUE,
859 BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
862 /* we draw black as white, as cmd.exe can only have black bg */
863 if (colorinfo == 0) {
864 return ansi_to_win_fg[7];
867 if (colorinfo & GST_DEBUG_BOLD) {
868 color |= FOREGROUND_INTENSITY;
870 if (colorinfo & GST_DEBUG_FG_MASK) {
871 color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
873 if (colorinfo & GST_DEBUG_BG_MASK) {
874 color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
880 /* width of %p varies depending on actual value of pointer, which can make
881 * output unevenly aligned if multiple threads are involved, hence the %14p
882 * (should really be %18p, but %14p seems a good compromise between too many
883 * white spaces and likely unalignment on my system) */
884 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
885 #define PTR_FMT "%14p"
887 #define PTR_FMT "%10p"
889 #define PID_FMT "%5d"
890 #define CAT_FMT "%20s %s:%d:%s:%s"
893 static const guchar levelcolormap[GST_LEVEL_COUNT] = {
895 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
896 /* GST_LEVEL_ERROR */
897 FOREGROUND_RED | FOREGROUND_INTENSITY,
898 /* GST_LEVEL_WARNING */
899 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
901 FOREGROUND_GREEN | FOREGROUND_INTENSITY,
902 /* GST_LEVEL_DEBUG */
903 FOREGROUND_GREEN | FOREGROUND_BLUE,
905 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
906 /* GST_LEVEL_FIXME */
907 FOREGROUND_RED | FOREGROUND_GREEN,
908 /* GST_LEVEL_TRACE */
909 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
910 /* placeholder for log level 8 */
912 /* GST_LEVEL_MEMDUMP */
913 FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
916 static const guchar available_colors[] = {
917 FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
918 FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
919 FOREGROUND_GREEN | FOREGROUND_BLUE,
922 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
923 "\033[37m", /* GST_LEVEL_NONE */
924 "\033[31;01m", /* GST_LEVEL_ERROR */
925 "\033[33;01m", /* GST_LEVEL_WARNING */
926 "\033[32;01m", /* GST_LEVEL_INFO */
927 "\033[36m", /* GST_LEVEL_DEBUG */
928 "\033[37m", /* GST_LEVEL_LOG */
929 "\033[33;01m", /* GST_LEVEL_FIXME */
930 "\033[37m", /* GST_LEVEL_TRACE */
931 "\033[37m", /* placeholder for log level 8 */
932 "\033[37m" /* GST_LEVEL_MEMDUMP */
937 * gst_debug_log_default:
938 * @category: category to log
939 * @level: level of the message
940 * @file: the file that emitted the message, usually the __FILE__ identifier
941 * @function: the function that emitted the message
942 * @line: the line from that the message was emitted, usually __LINE__
943 * @message: the actual message
944 * @object: (transfer none) (allow-none): the object this message relates to,
946 * @unused: an unused variable, reserved for some user_data.
948 * The default logging handler used by GStreamer. Logging functions get called
949 * whenever a macro like GST_DEBUG or similar is used. This function outputs the
950 * message and additional info to stderr (or the log file specified via the
951 * GST_DEBUG_FILE environment variable).
953 * You can add other handlers by using gst_debug_add_log_function().
954 * And you can remove this handler by calling
955 * gst_debug_remove_log_function(gst_debug_log_default);
958 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
959 const gchar * file, const gchar * function, gint line,
960 GObject * object, GstDebugMessage * message, gpointer unused)
963 GstClockTime elapsed;
967 if (level > gst_debug_category_get_threshold (category))
971 is_colored = gst_debug_is_colored ();
974 obj = gst_debug_print_object (object);
979 elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
980 gst_util_get_timestamp ());
984 /* colors, non-windows */
988 const gchar *levelcolor;
990 color = gst_debug_construct_term_color (gst_debug_category_get_color
993 g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
994 levelcolor = levelcolormap[level];
996 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
997 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
998 pidcolor, pid, clear, g_thread_self (), levelcolor,
999 gst_debug_level_get_name (level), clear, color,
1000 gst_debug_category_get_name (category), file, line, function, obj,
1001 clear, gst_debug_message_get (message));
1006 /* colors, windows. We take a lock to keep colors and content together.
1007 * Maybe there is a better way but for now this will do the right
1009 static GMutex win_print_mutex;
1010 const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1011 #define SET_COLOR(c) G_STMT_START { \
1012 if (log_file == stderr) \
1013 SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1015 g_mutex_lock (&win_print_mutex);
1017 fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1020 SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1021 fprintf (log_file, PID_FMT, pid);
1025 fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1028 SET_COLOR (levelcolormap[level]);
1029 fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1032 SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1034 fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1035 file, line, function, obj);
1039 fprintf (log_file, " %s\n", gst_debug_message_get (message));
1041 g_mutex_unlock (&win_print_mutex);
1044 /* no color, all platforms */
1045 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1046 fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1047 pid, g_thread_self (), gst_debug_level_get_name (level),
1048 gst_debug_category_get_name (category), file, line, function, obj,
1049 gst_debug_message_get (message));
1058 * gst_debug_level_get_name:
1059 * @level: the level to get the name for
1061 * Get the string representation of a debugging level
1066 gst_debug_level_get_name (GstDebugLevel level)
1069 case GST_LEVEL_NONE:
1071 case GST_LEVEL_ERROR:
1073 case GST_LEVEL_WARNING:
1075 case GST_LEVEL_INFO:
1077 case GST_LEVEL_DEBUG:
1081 case GST_LEVEL_FIXME:
1083 case GST_LEVEL_TRACE:
1085 case GST_LEVEL_MEMDUMP:
1088 g_warning ("invalid level specified for gst_debug_level_get_name");
1094 * gst_debug_add_log_function:
1095 * @func: the function to use
1096 * @user_data: user data
1097 * @notify: called when @user_data is not used anymore
1099 * Adds the logging function to the list of logging functions.
1100 * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1103 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1104 GDestroyNotify notify)
1106 LogFuncEntry *entry;
1110 func = gst_debug_log_default;
1112 entry = g_slice_new (LogFuncEntry);
1114 entry->user_data = user_data;
1115 entry->notify = notify;
1116 /* FIXME: we leak the old list here - other threads might access it right now
1117 * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1118 * but that is waaay costly.
1119 * It'd probably be clever to use some kind of RCU here, but I don't know
1120 * anything about that.
1122 g_mutex_lock (&__log_func_mutex);
1123 list = g_slist_copy (__log_functions);
1124 __log_functions = g_slist_prepend (list, entry);
1125 g_mutex_unlock (&__log_func_mutex);
1127 if (gst_is_initialized ())
1128 GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1133 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1135 gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1137 return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1141 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1143 gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1145 return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1149 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1152 GSList *new, *cleanup = NULL;
1155 g_mutex_lock (&__log_func_mutex);
1156 new = __log_functions;
1158 while ((found = g_slist_find_custom (new, data, func))) {
1159 if (new == __log_functions) {
1160 /* make a copy when we have the first hit, so that we modify the copy and
1161 * make that the new list later */
1162 new = g_slist_copy (new);
1165 cleanup = g_slist_prepend (cleanup, found->data);
1166 new = g_slist_delete_link (new, found);
1169 /* FIXME: We leak the old list here. See _add_log_function for why. */
1170 __log_functions = new;
1171 g_mutex_unlock (&__log_func_mutex);
1174 LogFuncEntry *entry = cleanup->data;
1177 entry->notify (entry->user_data);
1179 g_slice_free (LogFuncEntry, entry);
1180 cleanup = g_slist_delete_link (cleanup, cleanup);
1186 * gst_debug_remove_log_function:
1187 * @func: (scope call): the log function to remove
1189 * Removes all registered instances of the given logging functions.
1191 * Returns: How many instances of the function were removed
1194 gst_debug_remove_log_function (GstLogFunction func)
1199 func = gst_debug_log_default;
1202 gst_debug_remove_with_compare_func
1203 (gst_debug_compare_log_function_by_func, (gpointer) func);
1204 if (gst_is_initialized ())
1205 GST_DEBUG ("removed log function %p %d times from log function list", func,
1212 * gst_debug_remove_log_function_by_data:
1213 * @data: user data of the log function to remove
1215 * Removes all registered instances of log functions with the given user data.
1217 * Returns: How many instances of the function were removed
1220 gst_debug_remove_log_function_by_data (gpointer data)
1225 gst_debug_remove_with_compare_func
1226 (gst_debug_compare_log_function_by_data, data);
1228 if (gst_is_initialized ())
1230 ("removed %d log functions with user data %p from log function list",
1237 * gst_debug_set_colored:
1238 * @colored: Whether to use colored output or not
1240 * Sets or unsets the use of coloured debugging output.
1242 * This function may be called before gst_init().
1245 gst_debug_set_colored (gboolean colored)
1247 g_atomic_int_set (&__use_color, (gint) colored);
1251 * gst_debug_is_colored:
1253 * Checks if the debugging output should be colored.
1255 * Returns: TRUE, if the debug output should be colored.
1258 gst_debug_is_colored (void)
1260 return (gboolean) g_atomic_int_get (&__use_color);
1264 * gst_debug_set_active:
1265 * @active: Whether to use debugging output or not
1267 * If activated, debugging messages are sent to the debugging
1269 * It makes sense to deactivate it for speed issues.
1270 * <note><para>This function is not threadsafe. It makes sense to only call it
1271 * during initialization.</para></note>
1274 gst_debug_set_active (gboolean active)
1276 _gst_debug_enabled = active;
1278 _gst_debug_min = GST_LEVEL_COUNT;
1280 _gst_debug_min = GST_LEVEL_NONE;
1284 * gst_debug_is_active:
1286 * Checks if debugging output is activated.
1288 * Returns: TRUE, if debugging is activated
1291 gst_debug_is_active (void)
1293 return _gst_debug_enabled;
1297 * gst_debug_set_default_threshold:
1298 * @level: level to set
1300 * Sets the default threshold to the given level and updates all categories to
1301 * use this threshold.
1303 * This function may be called before gst_init().
1306 gst_debug_set_default_threshold (GstDebugLevel level)
1308 g_atomic_int_set (&__default_level, level);
1309 gst_debug_reset_all_thresholds ();
1313 * gst_debug_get_default_threshold:
1315 * Returns the default threshold that is used for new categories.
1317 * Returns: the default threshold level
1320 gst_debug_get_default_threshold (void)
1322 return (GstDebugLevel) g_atomic_int_get (&__default_level);
1326 gst_debug_reset_threshold (gpointer category, gpointer unused)
1328 GstDebugCategory *cat = (GstDebugCategory *) category;
1331 g_mutex_lock (&__level_name_mutex);
1332 walk = __level_name;
1334 LevelNameEntry *entry = walk->data;
1336 walk = g_slist_next (walk);
1337 if (g_pattern_match_string (entry->pat, cat->name)) {
1338 if (gst_is_initialized ())
1339 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1340 cat->name, entry->pat, entry->level);
1341 gst_debug_category_set_threshold (cat, entry->level);
1345 gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1348 g_mutex_unlock (&__level_name_mutex);
1352 gst_debug_reset_all_thresholds (void)
1354 g_mutex_lock (&__cat_mutex);
1355 g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1356 g_mutex_unlock (&__cat_mutex);
1360 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1362 GstDebugCategory *cat = (GstDebugCategory *) data;
1363 LevelNameEntry *entry = (LevelNameEntry *) user_data;
1365 if (g_pattern_match_string (entry->pat, cat->name)) {
1366 if (gst_is_initialized ())
1367 GST_LOG ("category %s matches pattern %p - gets set to level %d",
1368 cat->name, entry->pat, entry->level);
1369 gst_debug_category_set_threshold (cat, entry->level);
1374 * gst_debug_set_threshold_for_name:
1375 * @name: name of the categories to set
1376 * @level: level to set them to
1378 * Sets all categories which match the given glob style pattern to the given
1382 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1385 LevelNameEntry *entry;
1387 g_return_if_fail (name != NULL);
1389 pat = g_pattern_spec_new (name);
1390 entry = g_slice_new (LevelNameEntry);
1392 entry->level = level;
1393 g_mutex_lock (&__level_name_mutex);
1394 __level_name = g_slist_prepend (__level_name, entry);
1395 g_mutex_unlock (&__level_name_mutex);
1396 g_mutex_lock (&__cat_mutex);
1397 g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1398 g_mutex_unlock (&__cat_mutex);
1402 * gst_debug_unset_threshold_for_name:
1403 * @name: name of the categories to set
1405 * Resets all categories with the given name back to the default level.
1408 gst_debug_unset_threshold_for_name (const gchar * name)
1413 g_return_if_fail (name != NULL);
1415 pat = g_pattern_spec_new (name);
1416 g_mutex_lock (&__level_name_mutex);
1417 walk = __level_name;
1418 /* improve this if you want, it's mighty slow */
1420 LevelNameEntry *entry = walk->data;
1422 if (g_pattern_spec_equal (entry->pat, pat)) {
1423 __level_name = g_slist_remove_link (__level_name, walk);
1424 g_pattern_spec_free (entry->pat);
1425 g_slice_free (LevelNameEntry, entry);
1426 g_slist_free_1 (walk);
1427 walk = __level_name;
1430 g_mutex_unlock (&__level_name_mutex);
1431 g_pattern_spec_free (pat);
1432 gst_debug_reset_all_thresholds ();
1436 _gst_debug_category_new (const gchar * name, guint color,
1437 const gchar * description)
1439 GstDebugCategory *cat;
1441 g_return_val_if_fail (name != NULL, NULL);
1443 cat = g_slice_new (GstDebugCategory);
1444 cat->name = g_strdup (name);
1446 if (description != NULL) {
1447 cat->description = g_strdup (description);
1449 cat->description = g_strdup ("no description");
1451 g_atomic_int_set (&cat->threshold, 0);
1452 gst_debug_reset_threshold (cat, NULL);
1454 /* add to category list */
1455 g_mutex_lock (&__cat_mutex);
1456 __categories = g_slist_prepend (__categories, cat);
1457 g_mutex_unlock (&__cat_mutex);
1463 * gst_debug_category_free:
1464 * @category: #GstDebugCategory to free.
1466 * Removes and frees the category and all associated resources.
1469 gst_debug_category_free (GstDebugCategory * category)
1471 if (category == NULL)
1474 /* remove from category list */
1475 g_mutex_lock (&__cat_mutex);
1476 __categories = g_slist_remove (__categories, category);
1477 g_mutex_unlock (&__cat_mutex);
1479 g_free ((gpointer) category->name);
1480 g_free ((gpointer) category->description);
1481 g_slice_free (GstDebugCategory, category);
1485 * gst_debug_category_set_threshold:
1486 * @category: a #GstDebugCategory to set threshold of.
1487 * @level: the #GstDebugLevel threshold to set.
1489 * Sets the threshold of the category to the given level. Debug information will
1490 * only be output if the threshold is lower or equal to the level of the
1491 * debugging message.
1493 * Do not use this function in production code, because other functions may
1494 * change the threshold of categories as side effect. It is however a nice
1495 * function to use when debugging (even from gdb).
1499 gst_debug_category_set_threshold (GstDebugCategory * category,
1500 GstDebugLevel level)
1502 g_return_if_fail (category != NULL);
1504 if (level > _gst_debug_min) {
1505 _gst_debug_enabled = TRUE;
1506 _gst_debug_min = level;
1509 g_atomic_int_set (&category->threshold, level);
1513 * gst_debug_category_reset_threshold:
1514 * @category: a #GstDebugCategory to reset threshold of.
1516 * Resets the threshold of the category to the default level. Debug information
1517 * will only be output if the threshold is lower or equal to the level of the
1518 * debugging message.
1519 * Use this function to set the threshold back to where it was after using
1520 * gst_debug_category_set_threshold().
1523 gst_debug_category_reset_threshold (GstDebugCategory * category)
1525 gst_debug_reset_threshold (category, NULL);
1529 * gst_debug_category_get_threshold:
1530 * @category: a #GstDebugCategory to get threshold of.
1532 * Returns the threshold of a #GstDebugCategory.
1534 * Returns: the #GstDebugLevel that is used as threshold.
1537 gst_debug_category_get_threshold (GstDebugCategory * category)
1539 return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1543 * gst_debug_category_get_name:
1544 * @category: a #GstDebugCategory to get name of.
1546 * Returns the name of a debug category.
1548 * Returns: the name of the category.
1551 gst_debug_category_get_name (GstDebugCategory * category)
1553 return category->name;
1557 * gst_debug_category_get_color:
1558 * @category: a #GstDebugCategory to get the color of.
1560 * Returns the color of a debug category used when printing output in this
1563 * Returns: the color of the category.
1566 gst_debug_category_get_color (GstDebugCategory * category)
1568 return category->color;
1572 * gst_debug_category_get_description:
1573 * @category: a #GstDebugCategory to get the description of.
1575 * Returns the description of a debug category.
1577 * Returns: the description of the category.
1580 gst_debug_category_get_description (GstDebugCategory * category)
1582 return category->description;
1586 * gst_debug_get_all_categories:
1588 * Returns a snapshot of a all categories that are currently in use . This list
1589 * may change anytime.
1590 * The caller has to free the list after use.
1592 * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1596 gst_debug_get_all_categories (void)
1600 g_mutex_lock (&__cat_mutex);
1601 ret = g_slist_copy (__categories);
1602 g_mutex_unlock (&__cat_mutex);
1608 _gst_debug_get_category (const gchar * name)
1610 GstDebugCategory *ret = NULL;
1613 for (node = __categories; node; node = g_slist_next (node)) {
1614 ret = (GstDebugCategory *) node->data;
1615 if (!strcmp (name, ret->name)) {
1623 parse_debug_category (gchar * str, const gchar ** category)
1628 /* works in place */
1631 if (str[0] != '\0') {
1640 parse_debug_level (gchar * str, GstDebugLevel * level)
1645 /* works in place */
1648 if (str[0] != '\0' && str[1] == '\0'
1649 && str[0] >= '0' && str[0] < '0' + GST_LEVEL_COUNT) {
1650 *level = (GstDebugLevel) (str[0] - '0');
1651 } else if (strcmp (str, "ERROR") == 0) {
1652 *level = GST_LEVEL_ERROR;
1653 } else if (strncmp (str, "WARN", 4) == 0) {
1654 *level = GST_LEVEL_WARNING;
1655 } else if (strcmp (str, "FIXME") == 0) {
1656 *level = GST_LEVEL_FIXME;
1657 } else if (strcmp (str, "INFO") == 0) {
1658 *level = GST_LEVEL_INFO;
1659 } else if (strcmp (str, "DEBUG") == 0) {
1660 *level = GST_LEVEL_DEBUG;
1661 } else if (strcmp (str, "LOG") == 0) {
1662 *level = GST_LEVEL_LOG;
1663 } else if (strcmp (str, "TRACE") == 0) {
1664 *level = GST_LEVEL_TRACE;
1665 } else if (strcmp (str, "MEMDUMP") == 0) {
1666 *level = GST_LEVEL_MEMDUMP;
1674 * gst_debug_set_threshold_from_string:
1675 * @list: comma-separated list of "category:level" pairs to be used
1676 * as debug logging levels
1677 * @reset: %TRUE to clear all previously-set debug levels before setting
1679 * %FALSE if adding the threshold described by @list to the one already set.
1681 * Sets the debug logging wanted in the same form as with the GST_DEBUG
1682 * environment variable. You can use wildcards such as '*', but note that
1683 * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1684 * everything to log level 2.
1689 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1697 gst_debug_set_default_threshold (0);
1699 split = g_strsplit (list, ",", 0);
1701 for (walk = split; *walk; walk++) {
1702 if (strchr (*walk, ':')) {
1703 gchar **values = g_strsplit (*walk, ":", 2);
1705 if (values[0] && values[1]) {
1706 GstDebugLevel level;
1707 const gchar *category;
1709 if (parse_debug_category (values[0], &category)
1710 && parse_debug_level (values[1], &level))
1711 gst_debug_set_threshold_for_name (category, level);
1714 g_strfreev (values);
1716 GstDebugLevel level;
1718 if (parse_debug_level (*walk, &level))
1719 gst_debug_set_default_threshold (level);
1726 /*** FUNCTION POINTERS ********************************************************/
1728 static GHashTable *__gst_function_pointers; /* NULL */
1729 static GMutex __dbg_functions_mutex;
1731 /* This function MUST NOT return NULL */
1733 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1741 if (G_UNLIKELY (func == NULL))
1744 g_mutex_lock (&__dbg_functions_mutex);
1745 if (G_LIKELY (__gst_function_pointers)) {
1746 ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1747 g_mutex_unlock (&__dbg_functions_mutex);
1748 if (G_LIKELY (ptrname))
1751 g_mutex_unlock (&__dbg_functions_mutex);
1753 /* we need to create an entry in the hash table for this one so we don't leak
1756 if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1757 gchar *name = g_strdup (dl_info.dli_sname);
1759 _gst_debug_register_funcptr (func, name);
1764 gchar *name = g_strdup_printf ("%p", (gpointer) func);
1766 _gst_debug_register_funcptr (func, name);
1772 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1774 gpointer ptr = (gpointer) func;
1776 g_mutex_lock (&__dbg_functions_mutex);
1778 if (!__gst_function_pointers)
1779 __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1780 if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1781 g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1783 g_mutex_unlock (&__dbg_functions_mutex);
1786 /*** PRINTF EXTENSIONS ********************************************************/
1788 #ifdef GST_USING_PRINTF_EXTENSION
1790 _gst_info_printf_extension_ptr (FILE * stream, const struct printf_info *info,
1791 const void *const *args)
1798 ptr = *(void **) args[0];
1800 buffer = gst_debug_print_object (ptr);
1801 len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
1809 _gst_info_printf_extension_segment (FILE * stream,
1810 const struct printf_info *info, const void *const *args)
1817 ptr = *(void **) args[0];
1819 buffer = gst_debug_print_segment (ptr);
1820 len = fprintf (stream, "%*s", (info->left ? -info->width : info->width),
1827 #ifdef HAVE_REGISTER_PRINTF_SPECIFIER
1829 _gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
1830 int *argtypes, int *size)
1833 _gst_info_printf_extension_arginfo (const struct printf_info *info, size_t n,
1838 argtypes[0] = PA_POINTER;
1839 #ifdef HAVE_REGISTER_PRINTF_SPECIFIER
1840 *size = sizeof (gpointer);
1845 #endif /* GST_USING_PRINTF_EXTENSION */
1848 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1849 const guint8 * mem, gsize mem_offset, gsize mem_size)
1851 gchar hexstr[50], ascstr[18], digitstr[4];
1863 while (i < mem_size) {
1864 ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1865 g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1866 g_strlcat (hexstr, digitstr, sizeof (hexstr));
1872 g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1873 (guint) mem_offset, hexstr, ascstr);
1877 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1878 const gchar * func, gint line, GObject * obj, const gchar * msg,
1879 const guint8 * data, guint length)
1883 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1884 "-------------------------------------------------------------------");
1886 if (msg != NULL && *msg != '\0') {
1887 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1890 while (off < length) {
1893 /* gst_info_dump_mem_line will process 16 bytes at most */
1894 gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1895 gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
1899 gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1900 "-------------------------------------------------------------------");
1903 #else /* !GST_DISABLE_GST_DEBUG */
1904 #ifndef GST_REMOVE_DISABLED
1907 _gst_debug_category_new (const gchar * name, guint color,
1908 const gchar * description)
1914 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1918 /* This function MUST NOT return NULL */
1920 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1926 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
1927 const gchar * file, const gchar * function, gint line,
1928 GObject * object, const gchar * format, ...)
1933 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
1934 const gchar * file, const gchar * function, gint line,
1935 GObject * object, const gchar * format, va_list args)
1940 gst_debug_message_get (GstDebugMessage * message)
1946 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1947 const gchar * file, const gchar * function, gint line,
1948 GObject * object, GstDebugMessage * message, gpointer unused)
1953 gst_debug_level_get_name (GstDebugLevel level)
1959 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1960 GDestroyNotify notify)
1965 gst_debug_remove_log_function (GstLogFunction func)
1971 gst_debug_remove_log_function_by_data (gpointer data)
1977 gst_debug_set_active (gboolean active)
1982 gst_debug_is_active (void)
1988 gst_debug_set_colored (gboolean colored)
1993 gst_debug_is_colored (void)
1999 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2004 gst_debug_set_default_threshold (GstDebugLevel level)
2009 gst_debug_get_default_threshold (void)
2011 return GST_LEVEL_NONE;
2015 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2020 gst_debug_unset_threshold_for_name (const gchar * name)
2025 gst_debug_category_free (GstDebugCategory * category)
2030 gst_debug_category_set_threshold (GstDebugCategory * category,
2031 GstDebugLevel level)
2036 gst_debug_category_reset_threshold (GstDebugCategory * category)
2041 gst_debug_category_get_threshold (GstDebugCategory * category)
2043 return GST_LEVEL_NONE;
2047 gst_debug_category_get_name (GstDebugCategory * category)
2053 gst_debug_category_get_color (GstDebugCategory * category)
2059 gst_debug_category_get_description (GstDebugCategory * category)
2065 gst_debug_get_all_categories (void)
2071 _gst_debug_get_category (const gchar * name)
2077 gst_debug_construct_term_color (guint colorinfo)
2079 return g_strdup ("00");
2083 gst_debug_construct_win_color (guint colorinfo)
2089 _priv_gst_in_valgrind (void)
2095 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2096 const gchar * func, gint line, GObject * obj, const gchar * msg,
2097 const guint8 * data, guint length)
2100 #endif /* GST_REMOVE_DISABLED */
2101 #endif /* GST_DISABLE_GST_DEBUG */
2104 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2105 /* FIXME make this thread specific */
2106 static GSList *stack_trace = NULL;
2109 __cyg_profile_func_enter (void *this_fn, void *call_site)
2110 G_GNUC_NO_INSTRUMENT;
2111 void __cyg_profile_func_enter (void *this_fn, void *call_site)
2113 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2114 gchar *site = _gst_debug_nameof_funcptr (call_site);
2116 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2119 g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2120 this_fn, name, call_site, site));
2127 __cyg_profile_func_exit (void *this_fn, void *call_site)
2128 G_GNUC_NO_INSTRUMENT;
2129 void __cyg_profile_func_exit (void *this_fn, void *call_site)
2131 gchar *name = _gst_debug_nameof_funcptr (this_fn);
2133 GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2134 g_free (stack_trace->data);
2135 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2141 * gst_debug_print_stack_trace:
2143 * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2144 * gstreamer code, which can be printed with this function.
2147 gst_debug_print_stack_trace (void)
2149 GSList *walk = stack_trace;
2153 walk = g_slist_next (walk);
2156 gchar *name = (gchar *) walk->data;
2158 g_print ("#%-2d %s\n", count++, name);
2160 walk = g_slist_next (walk);
2165 gst_debug_print_stack_trace (void)
2167 /* nothing because it's compiled out */
2170 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */