docs: convert NULL, TRUE, and FALSE to %NULL, %TRUE, and %FALSE
[platform/upstream/gstreamer.git] / gst / gstinfo.c
1 /* GStreamer
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>
6  *
7  * gstinfo.c: debugging functions
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /**
26  * SECTION:gstinfo
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.
30  *
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.
34  *
35  * The debugging subsystem works only after GStreamer has been initialized
36  * - for example by calling gst_init().
37  *
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
44  * messages to stderr.
45  *
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,
51  * declare
52  * the variables and set the default category.
53  * |[
54  *   GST_DEBUG_CATEGORY_STATIC (my_category);  // define category (statically)
55  *   #define GST_CAT_DEFAULT my_category       // set as default
56  * ]|
57  * After that you only need to initialize the category.
58  * |[
59  *   GST_DEBUG_CATEGORY_INIT (my_category, "my category",
60  *                            0, "This is my very own");
61  * ]|
62  * Initialization must be done before the category is used first.
63  * Plugins do this
64  * in their plugin_init function, libraries and applications should do that
65  * during their initialization.
66  *
67  * The whole debugging subsystem can be disabled at build time with passing the
68  * --disable-gst-debug switch to configure. If this is done, every function,
69  * macro and even structs described in this file evaluate to default values or
70  * nothing at all.
71  * So don't take addresses of these functions or use other tricks.
72  * If you must do that for some reason, there is still an option.
73  * If the debugging
74  * subsystem was compiled out, #GST_DISABLE_GST_DEBUG is defined in
75  * &lt;gst/gst.h&gt;,
76  * so you can check that before doing your trick.
77  * Disabling the debugging subsystem will give you a slight (read: unnoticeable)
78  * speed increase and will reduce the size of your compiled code. The GStreamer
79  * library itself becomes around 10% smaller.
80  *
81  * Please note that there are naming conventions for the names of debugging
82  * categories. These are explained at GST_DEBUG_CATEGORY_INIT().
83  */
84
85 #define GST_INFO_C
86 #include "gst_private.h"
87 #include "gstinfo.h"
88
89 #undef gst_debug_remove_log_function
90 #undef gst_debug_add_log_function
91
92 #ifndef GST_DISABLE_GST_DEBUG
93
94 #ifdef HAVE_DLFCN_H
95 #  include <dlfcn.h>
96 #endif
97 #include <stdio.h>              /* fprintf */
98 #include <glib/gstdio.h>
99 #include <errno.h>
100 #ifdef HAVE_UNISTD_H
101 #  include <unistd.h>           /* getpid on UNIX */
102 #endif
103 #ifdef HAVE_PROCESS_H
104 #  include <process.h>          /* getpid on win32 */
105 #endif
106 #include <string.h>             /* G_VA_COPY */
107 #ifdef G_OS_WIN32
108 #  define WIN32_LEAN_AND_MEAN   /* prevents from including too many things */
109 #  include <windows.h>          /* GetStdHandle, windows console */
110 #endif
111
112 #include "gst_private.h"
113 #include "gstutils.h"
114 #include "gstquark.h"
115 #include "gstsegment.h"
116 #include "gstvalue.h"
117 #include "gstcapsfeatures.h"
118
119 #ifdef HAVE_VALGRIND_VALGRIND_H
120 #  include <valgrind/valgrind.h>
121 #endif
122 #include <glib/gprintf.h>       /* g_sprintf */
123
124 /* our own printf implementation with custom extensions to %p for caps etc. */
125 #include "printf/printf.h"
126 #include "printf/printf-extension.h"
127
128 static char *gst_info_printf_pointer_extension_func (const char *format,
129     void *ptr);
130
131 #endif /* !GST_DISABLE_GST_DEBUG */
132
133 extern gboolean gst_is_initialized (void);
134
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)
138
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;
143
144 GstDebugCategory *GST_CAT_DEFAULT = NULL;
145
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;
151
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;
179
180
181 #endif /* !defined(GST_DISABLE_GST_DEBUG) || !defined(GST_REMOVE_DISABLED) */
182
183 #ifndef GST_DISABLE_GST_DEBUG
184
185 /* underscore is to prevent conflict with GST_CAT_DEBUG define */
186 GST_DEBUG_CATEGORY_STATIC (_GST_CAT_DEBUG);
187
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
191  */
192 GstClockTime _priv_gst_info_start_time;
193
194 #if 0
195 #if defined __sgi__
196 #include <rld_interface.h>
197 typedef struct DL_INFO
198 {
199   const char *dli_fname;
200   void *dli_fbase;
201   const char *dli_sname;
202   void *dli_saddr;
203   int dli_version;
204   int dli_reserved1;
205   long dli_reserved[4];
206 }
207 Dl_info;
208
209 #define _RLD_DLADDR             14
210 int dladdr (void *address, Dl_info * dl);
211
212 int
213 dladdr (void *address, Dl_info * dl)
214 {
215   void *v;
216
217   v = _rld_new_interface (_RLD_DLADDR, address, dl);
218   return (int) v;
219 }
220 #endif /* __sgi__ */
221 #endif
222
223 static void gst_debug_reset_threshold (gpointer category, gpointer unused);
224 static void gst_debug_reset_all_thresholds (void);
225
226 struct _GstDebugMessage
227 {
228   gchar *message;
229   const gchar *format;
230   va_list arguments;
231 };
232
233 /* list of all name/level pairs from --gst-debug and GST_DEBUG */
234 static GMutex __level_name_mutex;
235 static GSList *__level_name = NULL;
236 typedef struct
237 {
238   GPatternSpec *pat;
239   GstDebugLevel level;
240 }
241 LevelNameEntry;
242
243 /* list of all categories */
244 static GMutex __cat_mutex;
245 static GSList *__categories = NULL;
246
247 static GstDebugCategory *_gst_debug_get_category_locked (const gchar * name);
248
249
250 /* all registered debug handlers */
251 typedef struct
252 {
253   GstLogFunction func;
254   gpointer user_data;
255   GDestroyNotify notify;
256 }
257 LogFuncEntry;
258 static GMutex __log_func_mutex;
259 static GSList *__log_functions = NULL;
260
261 #define PRETTY_TAGS_DEFAULT  TRUE
262 static gboolean pretty_tags = PRETTY_TAGS_DEFAULT;
263
264 static volatile gint G_GNUC_MAY_ALIAS __default_level = GST_LEVEL_DEFAULT;
265 static volatile gint G_GNUC_MAY_ALIAS __use_color = GST_DEBUG_COLOR_MODE_ON;
266
267 static FILE *log_file;
268
269 /* FIXME: export this? */
270 gboolean
271 _priv_gst_in_valgrind (void)
272 {
273   static enum
274   {
275     GST_VG_UNCHECKED,
276     GST_VG_NO_VALGRIND,
277     GST_VG_INSIDE
278   }
279   in_valgrind = GST_VG_UNCHECKED;
280
281   if (in_valgrind == GST_VG_UNCHECKED) {
282 #ifdef HAVE_VALGRIND_VALGRIND_H
283     if (RUNNING_ON_VALGRIND) {
284       GST_CAT_INFO (GST_CAT_GST_INIT, "we're running inside valgrind");
285       printf ("GStreamer has detected that it is running inside valgrind.\n");
286       printf ("It might now take different code paths to ease debugging.\n");
287       printf ("Of course, this may also lead to different bugs.\n");
288       in_valgrind = GST_VG_INSIDE;
289     } else {
290       GST_CAT_LOG (GST_CAT_GST_INIT, "not doing extra valgrind stuff");
291       in_valgrind = GST_VG_NO_VALGRIND;
292     }
293 #else
294     in_valgrind = GST_VG_NO_VALGRIND;
295 #endif
296     g_assert (in_valgrind == GST_VG_NO_VALGRIND ||
297         in_valgrind == GST_VG_INSIDE);
298   }
299   return (in_valgrind == GST_VG_INSIDE);
300 }
301
302 /* Initialize the debugging system */
303 void
304 _priv_gst_debug_init (void)
305 {
306   const gchar *env;
307
308   env = g_getenv ("GST_DEBUG_FILE");
309   if (env != NULL && *env != '\0') {
310     if (strcmp (env, "-") == 0) {
311       log_file = stdout;
312     } else {
313       log_file = g_fopen (env, "w");
314       if (log_file == NULL) {
315         g_printerr ("Could not open log file '%s' for writing: %s\n", env,
316             g_strerror (errno));
317         log_file = stderr;
318       }
319     }
320   } else {
321     log_file = stderr;
322   }
323
324   /* get time we started for debugging messages */
325   _priv_gst_info_start_time = gst_util_get_timestamp ();
326
327   __gst_printf_pointer_extension_set_func
328       (gst_info_printf_pointer_extension_func);
329
330   /* do NOT use a single debug function before this line has been run */
331   GST_CAT_DEFAULT = _gst_debug_category_new ("default",
332       GST_DEBUG_UNDERLINE, NULL);
333   _GST_CAT_DEBUG = _gst_debug_category_new ("GST_DEBUG",
334       GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, "debugging subsystem");
335
336   gst_debug_add_log_function (gst_debug_log_default, NULL, NULL);
337
338   /* FIXME: add descriptions here */
339   GST_CAT_GST_INIT = _gst_debug_category_new ("GST_INIT",
340       GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
341   GST_CAT_MEMORY = _gst_debug_category_new ("GST_MEMORY",
342       GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, "memory");
343   GST_CAT_PARENTAGE = _gst_debug_category_new ("GST_PARENTAGE",
344       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
345   GST_CAT_STATES = _gst_debug_category_new ("GST_STATES",
346       GST_DEBUG_BOLD | GST_DEBUG_FG_RED, NULL);
347   GST_CAT_SCHEDULING = _gst_debug_category_new ("GST_SCHEDULING",
348       GST_DEBUG_BOLD | GST_DEBUG_FG_MAGENTA, NULL);
349   GST_CAT_BUFFER = _gst_debug_category_new ("GST_BUFFER",
350       GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
351   GST_CAT_BUFFER_LIST = _gst_debug_category_new ("GST_BUFFER_LIST",
352       GST_DEBUG_BOLD | GST_DEBUG_BG_GREEN, NULL);
353   GST_CAT_BUS = _gst_debug_category_new ("GST_BUS", GST_DEBUG_BG_YELLOW, NULL);
354   GST_CAT_CAPS = _gst_debug_category_new ("GST_CAPS",
355       GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
356   GST_CAT_CLOCK = _gst_debug_category_new ("GST_CLOCK",
357       GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW, NULL);
358   GST_CAT_ELEMENT_PADS = _gst_debug_category_new ("GST_ELEMENT_PADS",
359       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
360   GST_CAT_PADS = _gst_debug_category_new ("GST_PADS",
361       GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_RED, NULL);
362   GST_CAT_PERFORMANCE = _gst_debug_category_new ("GST_PERFORMANCE",
363       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
364   GST_CAT_PIPELINE = _gst_debug_category_new ("GST_PIPELINE",
365       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
366   GST_CAT_PLUGIN_LOADING = _gst_debug_category_new ("GST_PLUGIN_LOADING",
367       GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
368   GST_CAT_PLUGIN_INFO = _gst_debug_category_new ("GST_PLUGIN_INFO",
369       GST_DEBUG_BOLD | GST_DEBUG_FG_CYAN, NULL);
370   GST_CAT_PROPERTIES = _gst_debug_category_new ("GST_PROPERTIES",
371       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_BLUE, NULL);
372   GST_CAT_NEGOTIATION = _gst_debug_category_new ("GST_NEGOTIATION",
373       GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
374   GST_CAT_REFCOUNTING = _gst_debug_category_new ("GST_REFCOUNTING",
375       GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_BLUE, NULL);
376   GST_CAT_ERROR_SYSTEM = _gst_debug_category_new ("GST_ERROR_SYSTEM",
377       GST_DEBUG_BOLD | GST_DEBUG_FG_RED | GST_DEBUG_BG_WHITE, NULL);
378
379   GST_CAT_EVENT = _gst_debug_category_new ("GST_EVENT",
380       GST_DEBUG_BOLD | GST_DEBUG_FG_BLUE, NULL);
381   GST_CAT_MESSAGE = _gst_debug_category_new ("GST_MESSAGE",
382       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
383   GST_CAT_PARAMS = _gst_debug_category_new ("GST_PARAMS",
384       GST_DEBUG_BOLD | GST_DEBUG_FG_BLACK | GST_DEBUG_BG_YELLOW, NULL);
385   GST_CAT_CALL_TRACE = _gst_debug_category_new ("GST_CALL_TRACE",
386       GST_DEBUG_BOLD, NULL);
387   GST_CAT_SIGNAL = _gst_debug_category_new ("GST_SIGNAL",
388       GST_DEBUG_BOLD | GST_DEBUG_FG_WHITE | GST_DEBUG_BG_RED, NULL);
389   GST_CAT_PROBE = _gst_debug_category_new ("GST_PROBE",
390       GST_DEBUG_BOLD | GST_DEBUG_FG_GREEN, "pad probes");
391   GST_CAT_REGISTRY = _gst_debug_category_new ("GST_REGISTRY", 0, "registry");
392   GST_CAT_QOS = _gst_debug_category_new ("GST_QOS", 0, "QoS");
393   _priv_GST_CAT_POLL = _gst_debug_category_new ("GST_POLL", 0, "poll");
394   GST_CAT_META = _gst_debug_category_new ("GST_META", 0, "meta");
395   GST_CAT_LOCKING = _gst_debug_category_new ("GST_LOCKING", 0, "locking");
396   GST_CAT_CONTEXT = _gst_debug_category_new ("GST_CONTEXT", 0, NULL);
397
398   /* print out the valgrind message if we're in valgrind */
399   _priv_gst_in_valgrind ();
400
401   env = g_getenv ("GST_DEBUG_OPTIONS");
402   if (env != NULL) {
403     if (strstr (env, "full_tags") || strstr (env, "full-tags"))
404       pretty_tags = FALSE;
405     else if (strstr (env, "pretty_tags") || strstr (env, "pretty-tags"))
406       pretty_tags = TRUE;
407   }
408
409   if (g_getenv ("GST_DEBUG_NO_COLOR") != NULL)
410     gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
411   env = g_getenv ("GST_DEBUG_COLOR_MODE");
412   if (env)
413     gst_debug_set_color_mode_from_string (env);
414
415   env = g_getenv ("GST_DEBUG");
416   if (env) {
417     gst_debug_set_threshold_from_string (env, FALSE);
418   }
419 }
420
421 /* we can't do this further above, because we initialize the GST_CAT_DEFAULT struct */
422 #define GST_CAT_DEFAULT _GST_CAT_DEBUG
423
424 /**
425  * gst_debug_log:
426  * @category: category to log
427  * @level: level of the message is in
428  * @file: the file that emitted the message, usually the __FILE__ identifier
429  * @function: the function that emitted the message
430  * @line: the line from that the message was emitted, usually __LINE__
431  * @object: (transfer none) (allow-none): the object this message relates to,
432  *     or %NULL if none
433  * @format: a printf style format string
434  * @...: optional arguments for the format
435  *
436  * Logs the given message using the currently registered debugging handlers.
437  */
438 void
439 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
440     const gchar * file, const gchar * function, gint line,
441     GObject * object, const gchar * format, ...)
442 {
443   va_list var_args;
444
445   va_start (var_args, format);
446   gst_debug_log_valist (category, level, file, function, line, object, format,
447       var_args);
448   va_end (var_args);
449 }
450
451 #ifdef G_OS_WIN32
452 /* based on g_basename(), which we can't use because it was deprecated */
453 static inline const gchar *
454 gst_path_basename (const gchar * file_name)
455 {
456   register const gchar *base;
457
458   base = strrchr (file_name, G_DIR_SEPARATOR);
459
460   {
461     const gchar *q = strrchr (file_name, '/');
462     if (base == NULL || (q != NULL && q > base))
463       base = q;
464   }
465
466   if (base)
467     return base + 1;
468
469   if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
470     return file_name + 2;
471
472   return file_name;
473 }
474 #endif
475
476 /**
477  * gst_debug_log_valist:
478  * @category: category to log
479  * @level: level of the message is in
480  * @file: the file that emitted the message, usually the __FILE__ identifier
481  * @function: the function that emitted the message
482  * @line: the line from that the message was emitted, usually __LINE__
483  * @object: (transfer none) (allow-none): the object this message relates to,
484  *     or %NULL if none
485  * @format: a printf style format string
486  * @args: optional arguments for the format
487  *
488  * Logs the given message using the currently registered debugging handlers.
489  */
490 void
491 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
492     const gchar * file, const gchar * function, gint line,
493     GObject * object, const gchar * format, va_list args)
494 {
495   GstDebugMessage message;
496   LogFuncEntry *entry;
497   GSList *handler;
498
499   g_return_if_fail (category != NULL);
500   g_return_if_fail (file != NULL);
501   g_return_if_fail (function != NULL);
502   g_return_if_fail (format != NULL);
503
504   /* The predefined macro __FILE__ is always the exact path given to the
505    * compiler with MSVC, which may or may not be the basename.  We work
506    * around it at runtime to improve the readability. */
507 #ifdef G_OS_WIN32
508   file = gst_path_basename (file);
509 #endif
510
511   message.message = NULL;
512   message.format = format;
513   G_VA_COPY (message.arguments, args);
514
515   handler = __log_functions;
516   while (handler) {
517     entry = handler->data;
518     handler = g_slist_next (handler);
519     entry->func (category, level, file, function, line, object, &message,
520         entry->user_data);
521   }
522   g_free (message.message);
523   va_end (message.arguments);
524 }
525
526 /**
527  * gst_debug_message_get:
528  * @message: a debug message
529  *
530  * Gets the string representation of a #GstDebugMessage. This function is used
531  * in debug handlers to extract the message.
532  *
533  * Returns: the string representation of a #GstDebugMessage.
534  */
535 const gchar *
536 gst_debug_message_get (GstDebugMessage * message)
537 {
538   if (message->message == NULL) {
539     int len;
540
541     len = __gst_vasprintf (&message->message, message->format,
542         message->arguments);
543
544     if (len < 0)
545       message->message = NULL;
546   }
547   return message->message;
548 }
549
550 #define MAX_BUFFER_DUMP_STRING_LEN  100
551
552 /* structure_to_pretty_string:
553  * @str: a serialized #GstStructure
554  *
555  * If the serialized structure contains large buffers such as images the hex
556  * representation of those buffers will be shortened so that the string remains
557  * readable.
558  *
559  * Returns: the filtered string
560  */
561 static gchar *
562 prettify_structure_string (gchar * str)
563 {
564   gchar *pos = str, *end;
565
566   while ((pos = strstr (pos, "(buffer)"))) {
567     guint count = 0;
568
569     pos += strlen ("(buffer)");
570     for (end = pos; *end != '\0' && *end != ';' && *end != ' '; ++end)
571       ++count;
572     if (count > MAX_BUFFER_DUMP_STRING_LEN) {
573       memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 6, "..", 2);
574       memcpy (pos + MAX_BUFFER_DUMP_STRING_LEN - 4, pos + count - 4, 4);
575       memmove (pos + MAX_BUFFER_DUMP_STRING_LEN, pos + count,
576           strlen (pos + count) + 1);
577       pos += MAX_BUFFER_DUMP_STRING_LEN;
578     }
579   }
580
581   return str;
582 }
583
584 static inline gchar *
585 gst_info_structure_to_string (const GstStructure * s)
586 {
587   if (G_LIKELY (s)) {
588     gchar *str = gst_structure_to_string (s);;
589     if (G_UNLIKELY (pretty_tags && s->name == GST_QUARK (TAGLIST)))
590       return prettify_structure_string (str);
591     else
592       return str;
593   }
594   return NULL;
595 }
596
597 static inline gchar *
598 gst_info_describe_buffer (GstBuffer * buffer)
599 {
600   return g_strdup_printf ("buffer: %p, pts %" GST_TIME_FORMAT ", dts %"
601       GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT
602       ", offset %" G_GUINT64_FORMAT ", offset_end %" G_GUINT64_FORMAT
603       ", flags 0x%x", buffer, GST_TIME_ARGS (GST_BUFFER_PTS (buffer)),
604       GST_TIME_ARGS (GST_BUFFER_DTS (buffer)),
605       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
606       gst_buffer_get_size (buffer), GST_BUFFER_OFFSET (buffer),
607       GST_BUFFER_OFFSET_END (buffer), GST_BUFFER_FLAGS (buffer));
608 }
609
610 static inline gchar *
611 gst_info_describe_event (GstEvent * event)
612 {
613   gchar *s, *ret;
614
615   s = gst_info_structure_to_string (gst_event_get_structure (event));
616   ret = g_strdup_printf ("%s event: %p, time %" GST_TIME_FORMAT
617       ", seq-num %d, %s", GST_EVENT_TYPE_NAME (event), event,
618       GST_TIME_ARGS (GST_EVENT_TIMESTAMP (event)), GST_EVENT_SEQNUM (event),
619       (s ? s : "(NULL)"));
620   g_free (s);
621   return ret;
622 }
623
624 static inline gchar *
625 gst_info_describe_message (GstMessage * message)
626 {
627   gchar *s, *ret;
628
629   s = gst_info_structure_to_string (gst_message_get_structure (message));
630   ret = g_strdup_printf ("%s message: %p, time %" GST_TIME_FORMAT
631       ", seq-num %d, element '%s', %s", GST_MESSAGE_TYPE_NAME (message),
632       message, GST_TIME_ARGS (GST_MESSAGE_TIMESTAMP (message)),
633       GST_MESSAGE_SEQNUM (message),
634       ((message->src) ? GST_ELEMENT_NAME (message->src) : "(NULL)"),
635       (s ? s : "(NULL)"));
636   g_free (s);
637   return ret;
638 }
639
640 static inline gchar *
641 gst_info_describe_query (GstQuery * query)
642 {
643   gchar *s, *ret;
644
645   s = gst_info_structure_to_string (gst_query_get_structure (query));
646   ret = g_strdup_printf ("%s query: %p, %s", GST_QUERY_TYPE_NAME (query),
647       query, (s ? s : "(NULL)"));
648   g_free (s);
649   return ret;
650 }
651
652 static gchar *
653 gst_debug_print_object (gpointer ptr)
654 {
655   GObject *object = (GObject *) ptr;
656
657 #ifdef unused
658   /* This is a cute trick to detect unmapped memory, but is unportable,
659    * slow, screws around with madvise, and not actually that useful. */
660   {
661     int ret;
662
663     ret = madvise ((void *) ((unsigned long) ptr & (~0xfff)), 4096, 0);
664     if (ret == -1 && errno == ENOMEM) {
665       buffer = g_strdup_printf ("%p (unmapped memory)", ptr);
666     }
667   }
668 #endif
669
670   /* nicely printed object */
671   if (object == NULL) {
672     return g_strdup ("(NULL)");
673   }
674   if (*(GType *) ptr == GST_TYPE_CAPS) {
675     return gst_caps_to_string ((const GstCaps *) ptr);
676   }
677   if (*(GType *) ptr == GST_TYPE_STRUCTURE) {
678     return gst_info_structure_to_string ((const GstStructure *) ptr);
679   }
680   if (*(GType *) ptr == GST_TYPE_CAPS_FEATURES) {
681     return gst_caps_features_to_string ((const GstCapsFeatures *) ptr);
682   }
683   if (*(GType *) ptr == GST_TYPE_TAG_LIST) {
684     gchar *str = gst_tag_list_to_string ((GstTagList *) ptr);
685     if (G_UNLIKELY (pretty_tags))
686       return prettify_structure_string (str);
687     else
688       return str;
689   }
690   if (*(GType *) ptr == GST_TYPE_DATE_TIME) {
691     return __gst_date_time_serialize ((GstDateTime *) ptr, TRUE);
692   }
693   if (GST_IS_BUFFER (ptr)) {
694     return gst_info_describe_buffer (GST_BUFFER_CAST (ptr));
695   }
696 #ifdef USE_POISONING
697   if (*(guint32 *) ptr == 0xffffffff) {
698     return g_strdup_printf ("<poisoned@%p>", ptr);
699   }
700 #endif
701   if (GST_IS_PAD (object) && GST_OBJECT_NAME (object)) {
702     return g_strdup_printf ("<%s:%s>", GST_DEBUG_PAD_NAME (object));
703   }
704   if (GST_IS_OBJECT (object) && GST_OBJECT_NAME (object)) {
705     return g_strdup_printf ("<%s>", GST_OBJECT_NAME (object));
706   }
707   if (G_IS_OBJECT (object)) {
708     return g_strdup_printf ("<%s@%p>", G_OBJECT_TYPE_NAME (object), object);
709   }
710   if (GST_IS_MESSAGE (object)) {
711     return gst_info_describe_message (GST_MESSAGE_CAST (object));
712   }
713   if (GST_IS_QUERY (object)) {
714     return gst_info_describe_query (GST_QUERY_CAST (object));
715   }
716   if (GST_IS_EVENT (object)) {
717     return gst_info_describe_event (GST_EVENT_CAST (object));
718   }
719   if (GST_IS_CONTEXT (object)) {
720     GstContext *context = GST_CONTEXT_CAST (object);
721     gchar *s, *ret;
722     const gchar *type;
723     const GstStructure *structure;
724
725     type = gst_context_get_context_type (context);
726     structure = gst_context_get_structure (context);
727
728     s = gst_info_structure_to_string (structure);
729
730     ret = g_strdup_printf ("context '%s'='%s'", type, s);
731     g_free (s);
732     return ret;
733   }
734
735   return g_strdup_printf ("%p", ptr);
736 }
737
738 static gchar *
739 gst_debug_print_segment (gpointer ptr)
740 {
741   GstSegment *segment = (GstSegment *) ptr;
742
743   /* nicely printed segment */
744   if (segment == NULL) {
745     return g_strdup ("(NULL)");
746   }
747
748   switch (segment->format) {
749     case GST_FORMAT_UNDEFINED:{
750       return g_strdup_printf ("UNDEFINED segment");
751     }
752     case GST_FORMAT_TIME:{
753       return g_strdup_printf ("time segment start=%" GST_TIME_FORMAT
754           ", offset=%" GST_TIME_FORMAT ", stop=%" GST_TIME_FORMAT
755           ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" GST_TIME_FORMAT
756           ", base=%" GST_TIME_FORMAT ", position %" GST_TIME_FORMAT
757           ", duration %" GST_TIME_FORMAT, GST_TIME_ARGS (segment->start),
758           GST_TIME_ARGS (segment->offset), GST_TIME_ARGS (segment->stop),
759           segment->rate, segment->applied_rate, (guint) segment->flags,
760           GST_TIME_ARGS (segment->time), GST_TIME_ARGS (segment->base),
761           GST_TIME_ARGS (segment->position), GST_TIME_ARGS (segment->duration));
762     }
763     default:{
764       const gchar *format_name;
765
766       format_name = gst_format_get_name (segment->format);
767       if (G_UNLIKELY (format_name == NULL))
768         format_name = "(UNKNOWN FORMAT)";
769       return g_strdup_printf ("%s segment start=%" G_GINT64_FORMAT
770           ", offset=%" G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT
771           ", rate=%f, applied_rate=%f" ", flags=0x%02x, time=%" G_GINT64_FORMAT
772           ", base=%" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT
773           ", duration %" G_GINT64_FORMAT, format_name, segment->start,
774           segment->offset, segment->stop, segment->rate, segment->applied_rate,
775           (guint) segment->flags, segment->time, segment->base,
776           segment->position, segment->duration);
777     }
778   }
779 }
780
781 static char *
782 gst_info_printf_pointer_extension_func (const char *format, void *ptr)
783 {
784   char *s = NULL;
785
786   if (format[0] == 'p' && format[1] == '\a') {
787     switch (format[2]) {
788       case 'A':                /* GST_PTR_FORMAT     */
789         s = gst_debug_print_object (ptr);
790         break;
791       case 'B':                /* GST_SEGMENT_FORMAT */
792         s = gst_debug_print_segment (ptr);
793         break;
794       default:
795         /* must have been compiled against a newer version with an extension
796          * we don't known about yet - just ignore and fallback to %p below */
797         break;
798     }
799   }
800   if (s == NULL)
801     s = g_strdup_printf ("%p", ptr);
802
803   return s;
804 }
805
806 /**
807  * gst_debug_construct_term_color:
808  * @colorinfo: the color info
809  *
810  * Constructs a string that can be used for getting the desired color in color
811  * terminals.
812  * You need to free the string after use.
813  *
814  * Returns: (transfer full) (type gchar*): a string containing the color
815  *     definition
816  */
817 gchar *
818 gst_debug_construct_term_color (guint colorinfo)
819 {
820   GString *color;
821
822   color = g_string_new ("\033[00");
823
824   if (colorinfo & GST_DEBUG_BOLD) {
825     g_string_append_len (color, ";01", 3);
826   }
827   if (colorinfo & GST_DEBUG_UNDERLINE) {
828     g_string_append_len (color, ";04", 3);
829   }
830   if (colorinfo & GST_DEBUG_FG_MASK) {
831     g_string_append_printf (color, ";3%1d", colorinfo & GST_DEBUG_FG_MASK);
832   }
833   if (colorinfo & GST_DEBUG_BG_MASK) {
834     g_string_append_printf (color, ";4%1d",
835         (colorinfo & GST_DEBUG_BG_MASK) >> 4);
836   }
837   g_string_append_c (color, 'm');
838
839   return g_string_free (color, FALSE);
840 }
841
842 /**
843  * gst_debug_construct_win_color:
844  * @colorinfo: the color info
845  *
846  * Constructs an integer that can be used for getting the desired color in
847  * windows' terminals (cmd.exe). As there is no mean to underline, we simply
848  * ignore this attribute.
849  *
850  * This function returns 0 on non-windows machines.
851  *
852  * Returns: an integer containing the color definition
853  */
854 gint
855 gst_debug_construct_win_color (guint colorinfo)
856 {
857   gint color = 0;
858 #ifdef G_OS_WIN32
859   static const guchar ansi_to_win_fg[8] = {
860     0,                          /* black   */
861     FOREGROUND_RED,             /* red     */
862     FOREGROUND_GREEN,           /* green   */
863     FOREGROUND_RED | FOREGROUND_GREEN,  /* yellow  */
864     FOREGROUND_BLUE,            /* blue    */
865     FOREGROUND_RED | FOREGROUND_BLUE,   /* magenta */
866     FOREGROUND_GREEN | FOREGROUND_BLUE, /* cyan    */
867     FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* white   */
868   };
869   static const guchar ansi_to_win_bg[8] = {
870     0,
871     BACKGROUND_RED,
872     BACKGROUND_GREEN,
873     BACKGROUND_RED | BACKGROUND_GREEN,
874     BACKGROUND_BLUE,
875     BACKGROUND_RED | BACKGROUND_BLUE,
876     BACKGROUND_GREEN | FOREGROUND_BLUE,
877     BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
878   };
879
880   /* we draw black as white, as cmd.exe can only have black bg */
881   if ((colorinfo & (GST_DEBUG_FG_MASK | GST_DEBUG_BG_MASK)) == 0) {
882     color = ansi_to_win_fg[7];
883   }
884   if (colorinfo & GST_DEBUG_UNDERLINE) {
885     color |= BACKGROUND_INTENSITY;
886   }
887   if (colorinfo & GST_DEBUG_BOLD) {
888     color |= FOREGROUND_INTENSITY;
889   }
890   if (colorinfo & GST_DEBUG_FG_MASK) {
891     color |= ansi_to_win_fg[colorinfo & GST_DEBUG_FG_MASK];
892   }
893   if (colorinfo & GST_DEBUG_BG_MASK) {
894     color |= ansi_to_win_bg[(colorinfo & GST_DEBUG_BG_MASK) >> 4];
895   }
896 #endif
897   return color;
898 }
899
900 /* width of %p varies depending on actual value of pointer, which can make
901  * output unevenly aligned if multiple threads are involved, hence the %14p
902  * (should really be %18p, but %14p seems a good compromise between too many
903  * white spaces and likely unalignment on my system) */
904 #if defined (GLIB_SIZEOF_VOID_P) && GLIB_SIZEOF_VOID_P == 8
905 #define PTR_FMT "%14p"
906 #else
907 #define PTR_FMT "%10p"
908 #endif
909 #define PID_FMT "%5d"
910 #define CAT_FMT "%20s %s:%d:%s:%s"
911
912 #ifdef G_OS_WIN32
913 static const guchar levelcolormap_w32[GST_LEVEL_COUNT] = {
914   /* GST_LEVEL_NONE */
915   FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
916   /* GST_LEVEL_ERROR */
917   FOREGROUND_RED | FOREGROUND_INTENSITY,
918   /* GST_LEVEL_WARNING */
919   FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY,
920   /* GST_LEVEL_INFO */
921   FOREGROUND_GREEN | FOREGROUND_INTENSITY,
922   /* GST_LEVEL_DEBUG */
923   FOREGROUND_GREEN | FOREGROUND_BLUE,
924   /* GST_LEVEL_LOG */
925   FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
926   /* GST_LEVEL_FIXME */
927   FOREGROUND_RED | FOREGROUND_GREEN,
928   /* GST_LEVEL_TRACE */
929   FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
930   /* placeholder for log level 8 */
931   0,
932   /* GST_LEVEL_MEMDUMP */
933   FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
934 };
935
936 static const guchar available_colors[] = {
937   FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_RED | FOREGROUND_GREEN,
938   FOREGROUND_BLUE, FOREGROUND_RED | FOREGROUND_BLUE,
939   FOREGROUND_GREEN | FOREGROUND_BLUE,
940 };
941 #endif /* G_OS_WIN32 */
942 static const gchar *levelcolormap[GST_LEVEL_COUNT] = {
943   "\033[37m",                   /* GST_LEVEL_NONE */
944   "\033[31;01m",                /* GST_LEVEL_ERROR */
945   "\033[33;01m",                /* GST_LEVEL_WARNING */
946   "\033[32;01m",                /* GST_LEVEL_INFO */
947   "\033[36m",                   /* GST_LEVEL_DEBUG */
948   "\033[37m",                   /* GST_LEVEL_LOG */
949   "\033[33;01m",                /* GST_LEVEL_FIXME */
950   "\033[37m",                   /* GST_LEVEL_TRACE */
951   "\033[37m",                   /* placeholder for log level 8 */
952   "\033[37m"                    /* GST_LEVEL_MEMDUMP */
953 };
954
955 /**
956  * gst_debug_log_default:
957  * @category: category to log
958  * @level: level of the message
959  * @file: the file that emitted the message, usually the __FILE__ identifier
960  * @function: the function that emitted the message
961  * @line: the line from that the message was emitted, usually __LINE__
962  * @message: the actual message
963  * @object: (transfer none) (allow-none): the object this message relates to,
964  *     or %NULL if none
965  * @unused: an unused variable, reserved for some user_data.
966  *
967  * The default logging handler used by GStreamer. Logging functions get called
968  * whenever a macro like GST_DEBUG or similar is used. This function outputs the
969  * message and additional info to stderr (or the log file specified via the
970  * GST_DEBUG_FILE environment variable).
971  *
972  * You can add other handlers by using gst_debug_add_log_function().
973  * And you can remove this handler by calling
974  * gst_debug_remove_log_function(gst_debug_log_default);
975  */
976 void
977 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
978     const gchar * file, const gchar * function, gint line,
979     GObject * object, GstDebugMessage * message, gpointer unused)
980 {
981   gint pid;
982   GstClockTime elapsed;
983   gchar *obj = NULL;
984   GstDebugColorMode color_mode;
985
986   if (level > gst_debug_category_get_threshold (category))
987     return;
988
989   pid = getpid ();
990   color_mode = gst_debug_get_color_mode ();
991
992   if (object) {
993     obj = gst_debug_print_object (object);
994   } else {
995     obj = g_strdup ("");
996   }
997
998   elapsed = GST_CLOCK_DIFF (_priv_gst_info_start_time,
999       gst_util_get_timestamp ());
1000
1001   if (color_mode != GST_DEBUG_COLOR_MODE_OFF) {
1002 #ifdef G_OS_WIN32
1003     /* We take a lock to keep colors and content together.
1004      * Maybe there is a better way but for now this will do the right
1005      * thing. */
1006     static GMutex win_print_mutex;
1007     g_mutex_lock (&win_print_mutex);
1008     if (color_mode == GST_DEBUG_COLOR_MODE_UNIX) {
1009 #endif
1010       /* colors, non-windows */
1011       gchar *color = NULL;
1012       const gchar *clear;
1013       gchar pidcolor[10];
1014       const gchar *levelcolor;
1015
1016       color = gst_debug_construct_term_color (gst_debug_category_get_color
1017           (category));
1018       clear = "\033[00m";
1019       g_sprintf (pidcolor, "\033[3%1dm", pid % 6 + 31);
1020       levelcolor = levelcolormap[level];
1021
1022 #define PRINT_FMT " %s"PID_FMT"%s "PTR_FMT" %s%s%s %s"CAT_FMT"%s %s\n"
1023       fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1024           pidcolor, pid, clear, g_thread_self (), levelcolor,
1025           gst_debug_level_get_name (level), clear, color,
1026           gst_debug_category_get_name (category), file, line, function, obj,
1027           clear, gst_debug_message_get (message));
1028       fflush (log_file);
1029 #undef PRINT_FMT
1030       g_free (color);
1031 #ifdef G_OS_WIN32
1032     } else {
1033       /* colors, windows. */
1034       const gint clear = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1035 #define SET_COLOR(c) G_STMT_START { \
1036   if (log_file == stderr) \
1037     SetConsoleTextAttribute (GetStdHandle (STD_ERROR_HANDLE), (c)); \
1038   } G_STMT_END
1039       /* timestamp */
1040       fprintf (log_file, "%" GST_TIME_FORMAT " ", GST_TIME_ARGS (elapsed));
1041       fflush (log_file);
1042       /* pid */
1043       SET_COLOR (available_colors[pid % G_N_ELEMENTS (available_colors)]);
1044       fprintf (log_file, PID_FMT, pid);
1045       fflush (log_file);
1046       /* thread */
1047       SET_COLOR (clear);
1048       fprintf (log_file, " " PTR_FMT " ", g_thread_self ());
1049       fflush (log_file);
1050       /* level */
1051       SET_COLOR (levelcolormap_w32[level]);
1052       fprintf (log_file, "%s ", gst_debug_level_get_name (level));
1053       fflush (log_file);
1054       /* category */
1055       SET_COLOR (gst_debug_construct_win_color (gst_debug_category_get_color
1056               (category)));
1057       fprintf (log_file, CAT_FMT, gst_debug_category_get_name (category),
1058           file, line, function, obj);
1059       fflush (log_file);
1060       /* message */
1061       SET_COLOR (clear);
1062       fprintf (log_file, " %s\n", gst_debug_message_get (message));
1063       fflush (log_file);
1064     }
1065     g_mutex_unlock (&win_print_mutex);
1066 #endif
1067   } else {
1068     /* no color, all platforms */
1069 #define PRINT_FMT " "PID_FMT" "PTR_FMT" %s "CAT_FMT" %s\n"
1070     fprintf (log_file, "%" GST_TIME_FORMAT PRINT_FMT, GST_TIME_ARGS (elapsed),
1071         pid, g_thread_self (), gst_debug_level_get_name (level),
1072         gst_debug_category_get_name (category), file, line, function, obj,
1073         gst_debug_message_get (message));
1074     fflush (log_file);
1075 #undef PRINT_FMT
1076   }
1077
1078   g_free (obj);
1079 }
1080
1081 /**
1082  * gst_debug_level_get_name:
1083  * @level: the level to get the name for
1084  *
1085  * Get the string representation of a debugging level
1086  *
1087  * Returns: the name
1088  */
1089 const gchar *
1090 gst_debug_level_get_name (GstDebugLevel level)
1091 {
1092   switch (level) {
1093     case GST_LEVEL_NONE:
1094       return "";
1095     case GST_LEVEL_ERROR:
1096       return "ERROR  ";
1097     case GST_LEVEL_WARNING:
1098       return "WARN   ";
1099     case GST_LEVEL_INFO:
1100       return "INFO   ";
1101     case GST_LEVEL_DEBUG:
1102       return "DEBUG  ";
1103     case GST_LEVEL_LOG:
1104       return "LOG    ";
1105     case GST_LEVEL_FIXME:
1106       return "FIXME  ";
1107     case GST_LEVEL_TRACE:
1108       return "TRACE  ";
1109     case GST_LEVEL_MEMDUMP:
1110       return "MEMDUMP";
1111     default:
1112       g_warning ("invalid level specified for gst_debug_level_get_name");
1113       return "";
1114   }
1115 }
1116
1117 /**
1118  * gst_debug_add_log_function:
1119  * @func: the function to use
1120  * @user_data: user data
1121  * @notify: called when @user_data is not used anymore
1122  *
1123  * Adds the logging function to the list of logging functions.
1124  * Be sure to use #G_GNUC_NO_INSTRUMENT on that function, it is needed.
1125  */
1126 void
1127 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
1128     GDestroyNotify notify)
1129 {
1130   LogFuncEntry *entry;
1131   GSList *list;
1132
1133   if (func == NULL)
1134     func = gst_debug_log_default;
1135
1136   entry = g_slice_new (LogFuncEntry);
1137   entry->func = func;
1138   entry->user_data = user_data;
1139   entry->notify = notify;
1140   /* FIXME: we leak the old list here - other threads might access it right now
1141    * in gst_debug_logv. Another solution is to lock the mutex in gst_debug_logv,
1142    * but that is waaay costly.
1143    * It'd probably be clever to use some kind of RCU here, but I don't know
1144    * anything about that.
1145    */
1146   g_mutex_lock (&__log_func_mutex);
1147   list = g_slist_copy (__log_functions);
1148   __log_functions = g_slist_prepend (list, entry);
1149   g_mutex_unlock (&__log_func_mutex);
1150
1151   if (gst_is_initialized ())
1152     GST_DEBUG ("prepended log function %p (user data %p) to log functions",
1153         func, user_data);
1154 }
1155
1156 static gint
1157 gst_debug_compare_log_function_by_func (gconstpointer entry, gconstpointer func)
1158 {
1159   gpointer entryfunc = (gpointer) (((LogFuncEntry *) entry)->func);
1160
1161   return (entryfunc < func) ? -1 : (entryfunc > func) ? 1 : 0;
1162 }
1163
1164 static gint
1165 gst_debug_compare_log_function_by_data (gconstpointer entry, gconstpointer data)
1166 {
1167   gpointer entrydata = ((LogFuncEntry *) entry)->user_data;
1168
1169   return (entrydata < data) ? -1 : (entrydata > data) ? 1 : 0;
1170 }
1171
1172 static guint
1173 gst_debug_remove_with_compare_func (GCompareFunc func, gpointer data)
1174 {
1175   GSList *found;
1176   GSList *new, *cleanup = NULL;
1177   guint removals = 0;
1178
1179   g_mutex_lock (&__log_func_mutex);
1180   new = __log_functions;
1181   cleanup = NULL;
1182   while ((found = g_slist_find_custom (new, data, func))) {
1183     if (new == __log_functions) {
1184       /* make a copy when we have the first hit, so that we modify the copy and
1185        * make that the new list later */
1186       new = g_slist_copy (new);
1187       continue;
1188     }
1189     cleanup = g_slist_prepend (cleanup, found->data);
1190     new = g_slist_delete_link (new, found);
1191     removals++;
1192   }
1193   /* FIXME: We leak the old list here. See _add_log_function for why. */
1194   __log_functions = new;
1195   g_mutex_unlock (&__log_func_mutex);
1196
1197   while (cleanup) {
1198     LogFuncEntry *entry = cleanup->data;
1199
1200     if (entry->notify)
1201       entry->notify (entry->user_data);
1202
1203     g_slice_free (LogFuncEntry, entry);
1204     cleanup = g_slist_delete_link (cleanup, cleanup);
1205   }
1206   return removals;
1207 }
1208
1209 /**
1210  * gst_debug_remove_log_function:
1211  * @func: (scope call): the log function to remove
1212  *
1213  * Removes all registered instances of the given logging functions.
1214  *
1215  * Returns: How many instances of the function were removed
1216  */
1217 guint
1218 gst_debug_remove_log_function (GstLogFunction func)
1219 {
1220   guint removals;
1221
1222   if (func == NULL)
1223     func = gst_debug_log_default;
1224
1225   removals =
1226       gst_debug_remove_with_compare_func
1227       (gst_debug_compare_log_function_by_func, (gpointer) func);
1228   if (gst_is_initialized ())
1229     GST_DEBUG ("removed log function %p %d times from log function list", func,
1230         removals);
1231
1232   return removals;
1233 }
1234
1235 /**
1236  * gst_debug_remove_log_function_by_data:
1237  * @data: user data of the log function to remove
1238  *
1239  * Removes all registered instances of log functions with the given user data.
1240  *
1241  * Returns: How many instances of the function were removed
1242  */
1243 guint
1244 gst_debug_remove_log_function_by_data (gpointer data)
1245 {
1246   guint removals;
1247
1248   removals =
1249       gst_debug_remove_with_compare_func
1250       (gst_debug_compare_log_function_by_data, data);
1251
1252   if (gst_is_initialized ())
1253     GST_DEBUG
1254         ("removed %d log functions with user data %p from log function list",
1255         removals, data);
1256
1257   return removals;
1258 }
1259
1260 /**
1261  * gst_debug_set_colored:
1262  * @colored: Whether to use colored output or not
1263  *
1264  * Sets or unsets the use of coloured debugging output.
1265  * Same as gst_debug_set_color_mode () with the argument being
1266  * being GST_DEBUG_COLOR_MODE_ON or GST_DEBUG_COLOR_MODE_OFF.
1267  *
1268  * This function may be called before gst_init().
1269  */
1270 void
1271 gst_debug_set_colored (gboolean colored)
1272 {
1273   GstDebugColorMode new_mode;
1274   new_mode = colored ? GST_DEBUG_COLOR_MODE_ON : GST_DEBUG_COLOR_MODE_OFF;
1275   g_atomic_int_set (&__use_color, (gint) new_mode);
1276 }
1277
1278 /**
1279  * gst_debug_set_color_mode:
1280  * @mode: The coloring mode for debug output. See @GstDebugColorMode.
1281  *
1282  * Changes the coloring mode for debug output.
1283  *
1284  * This function may be called before gst_init().
1285  *
1286  * Since: 1.2
1287  */
1288 void
1289 gst_debug_set_color_mode (GstDebugColorMode mode)
1290 {
1291   g_atomic_int_set (&__use_color, mode);
1292 }
1293
1294 /**
1295  * gst_debug_set_color_mode_from_string:
1296  * @mode: The coloring mode for debug output. One of the following:
1297  * "on", "auto", "off", "disable", "unix".
1298  *
1299  * Changes the coloring mode for debug output.
1300  *
1301  * This function may be called before gst_init().
1302  *
1303  * Since: 1.2
1304  */
1305 void
1306 gst_debug_set_color_mode_from_string (const gchar * mode)
1307 {
1308   if ((strcmp (mode, "on") == 0) || (strcmp (mode, "auto") == 0))
1309     gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_ON);
1310   else if ((strcmp (mode, "off") == 0) || (strcmp (mode, "disable") == 0))
1311     gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_OFF);
1312   else if (strcmp (mode, "unix") == 0)
1313     gst_debug_set_color_mode (GST_DEBUG_COLOR_MODE_UNIX);
1314 }
1315
1316 /**
1317  * gst_debug_is_colored:
1318  *
1319  * Checks if the debugging output should be colored.
1320  *
1321  * Returns: %TRUE, if the debug output should be colored.
1322  */
1323 gboolean
1324 gst_debug_is_colored (void)
1325 {
1326   GstDebugColorMode mode = g_atomic_int_get (&__use_color);
1327   return (mode == GST_DEBUG_COLOR_MODE_UNIX || mode == GST_DEBUG_COLOR_MODE_ON);
1328 }
1329
1330 /**
1331  * gst_debug_get_color_mode:
1332  *
1333  * Changes the coloring mode for debug output.
1334  *
1335  * Returns: see @GstDebugColorMode for possible values.
1336  *
1337  * Since: 1.2
1338  */
1339 GstDebugColorMode
1340 gst_debug_get_color_mode (void)
1341 {
1342   return g_atomic_int_get (&__use_color);
1343 }
1344
1345 /**
1346  * gst_debug_set_active:
1347  * @active: Whether to use debugging output or not
1348  *
1349  * If activated, debugging messages are sent to the debugging
1350  * handlers.
1351  * It makes sense to deactivate it for speed issues.
1352  * <note><para>This function is not threadsafe. It makes sense to only call it
1353  * during initialization.</para></note>
1354  */
1355 void
1356 gst_debug_set_active (gboolean active)
1357 {
1358   _gst_debug_enabled = active;
1359   if (active)
1360     _gst_debug_min = GST_LEVEL_COUNT;
1361   else
1362     _gst_debug_min = GST_LEVEL_NONE;
1363 }
1364
1365 /**
1366  * gst_debug_is_active:
1367  *
1368  * Checks if debugging output is activated.
1369  *
1370  * Returns: %TRUE, if debugging is activated
1371  */
1372 gboolean
1373 gst_debug_is_active (void)
1374 {
1375   return _gst_debug_enabled;
1376 }
1377
1378 /**
1379  * gst_debug_set_default_threshold:
1380  * @level: level to set
1381  *
1382  * Sets the default threshold to the given level and updates all categories to
1383  * use this threshold.
1384  *
1385  * This function may be called before gst_init().
1386  */
1387 void
1388 gst_debug_set_default_threshold (GstDebugLevel level)
1389 {
1390   g_atomic_int_set (&__default_level, level);
1391   gst_debug_reset_all_thresholds ();
1392 }
1393
1394 /**
1395  * gst_debug_get_default_threshold:
1396  *
1397  * Returns the default threshold that is used for new categories.
1398  *
1399  * Returns: the default threshold level
1400  */
1401 GstDebugLevel
1402 gst_debug_get_default_threshold (void)
1403 {
1404   return (GstDebugLevel) g_atomic_int_get (&__default_level);
1405 }
1406
1407 static void
1408 gst_debug_reset_threshold (gpointer category, gpointer unused)
1409 {
1410   GstDebugCategory *cat = (GstDebugCategory *) category;
1411   GSList *walk;
1412
1413   g_mutex_lock (&__level_name_mutex);
1414   walk = __level_name;
1415   while (walk) {
1416     LevelNameEntry *entry = walk->data;
1417
1418     walk = g_slist_next (walk);
1419     if (g_pattern_match_string (entry->pat, cat->name)) {
1420       if (gst_is_initialized ())
1421         GST_LOG ("category %s matches pattern %p - gets set to level %d",
1422             cat->name, entry->pat, entry->level);
1423       gst_debug_category_set_threshold (cat, entry->level);
1424       goto exit;
1425     }
1426   }
1427   gst_debug_category_set_threshold (cat, gst_debug_get_default_threshold ());
1428
1429 exit:
1430   g_mutex_unlock (&__level_name_mutex);
1431 }
1432
1433 static void
1434 gst_debug_reset_all_thresholds (void)
1435 {
1436   g_mutex_lock (&__cat_mutex);
1437   g_slist_foreach (__categories, gst_debug_reset_threshold, NULL);
1438   g_mutex_unlock (&__cat_mutex);
1439 }
1440
1441 static void
1442 for_each_threshold_by_entry (gpointer data, gpointer user_data)
1443 {
1444   GstDebugCategory *cat = (GstDebugCategory *) data;
1445   LevelNameEntry *entry = (LevelNameEntry *) user_data;
1446
1447   if (g_pattern_match_string (entry->pat, cat->name)) {
1448     if (gst_is_initialized ())
1449       GST_LOG ("category %s matches pattern %p - gets set to level %d",
1450           cat->name, entry->pat, entry->level);
1451     gst_debug_category_set_threshold (cat, entry->level);
1452   }
1453 }
1454
1455 /**
1456  * gst_debug_set_threshold_for_name:
1457  * @name: name of the categories to set
1458  * @level: level to set them to
1459  *
1460  * Sets all categories which match the given glob style pattern to the given
1461  * level.
1462  */
1463 void
1464 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
1465 {
1466   GPatternSpec *pat;
1467   LevelNameEntry *entry;
1468
1469   g_return_if_fail (name != NULL);
1470
1471   pat = g_pattern_spec_new (name);
1472   entry = g_slice_new (LevelNameEntry);
1473   entry->pat = pat;
1474   entry->level = level;
1475   g_mutex_lock (&__level_name_mutex);
1476   __level_name = g_slist_prepend (__level_name, entry);
1477   g_mutex_unlock (&__level_name_mutex);
1478   g_mutex_lock (&__cat_mutex);
1479   g_slist_foreach (__categories, for_each_threshold_by_entry, entry);
1480   g_mutex_unlock (&__cat_mutex);
1481 }
1482
1483 /**
1484  * gst_debug_unset_threshold_for_name:
1485  * @name: name of the categories to set
1486  *
1487  * Resets all categories with the given name back to the default level.
1488  */
1489 void
1490 gst_debug_unset_threshold_for_name (const gchar * name)
1491 {
1492   GSList *walk;
1493   GPatternSpec *pat;
1494
1495   g_return_if_fail (name != NULL);
1496
1497   pat = g_pattern_spec_new (name);
1498   g_mutex_lock (&__level_name_mutex);
1499   walk = __level_name;
1500   /* improve this if you want, it's mighty slow */
1501   while (walk) {
1502     LevelNameEntry *entry = walk->data;
1503
1504     if (g_pattern_spec_equal (entry->pat, pat)) {
1505       __level_name = g_slist_remove_link (__level_name, walk);
1506       g_pattern_spec_free (entry->pat);
1507       g_slice_free (LevelNameEntry, entry);
1508       g_slist_free_1 (walk);
1509       walk = __level_name;
1510     }
1511   }
1512   g_mutex_unlock (&__level_name_mutex);
1513   g_pattern_spec_free (pat);
1514   gst_debug_reset_all_thresholds ();
1515 }
1516
1517 GstDebugCategory *
1518 _gst_debug_category_new (const gchar * name, guint color,
1519     const gchar * description)
1520 {
1521   GstDebugCategory *cat, *catfound;
1522
1523   g_return_val_if_fail (name != NULL, NULL);
1524
1525   cat = g_slice_new (GstDebugCategory);
1526   cat->name = g_strdup (name);
1527   cat->color = color;
1528   if (description != NULL) {
1529     cat->description = g_strdup (description);
1530   } else {
1531     cat->description = g_strdup ("no description");
1532   }
1533   g_atomic_int_set (&cat->threshold, 0);
1534   gst_debug_reset_threshold (cat, NULL);
1535
1536   /* add to category list */
1537   g_mutex_lock (&__cat_mutex);
1538   catfound = _gst_debug_get_category_locked (name);
1539   if (catfound) {
1540     g_free ((gpointer) cat->name);
1541     g_free ((gpointer) cat->description);
1542     g_slice_free (GstDebugCategory, cat);
1543     cat = catfound;
1544   } else {
1545     __categories = g_slist_prepend (__categories, cat);
1546   }
1547   g_mutex_unlock (&__cat_mutex);
1548
1549   return cat;
1550 }
1551
1552 /**
1553  * gst_debug_category_free:
1554  * @category: #GstDebugCategory to free.
1555  *
1556  * Removes and frees the category and all associated resources.
1557  */
1558 void
1559 gst_debug_category_free (GstDebugCategory * category)
1560 {
1561   if (category == NULL)
1562     return;
1563
1564   /* remove from category list */
1565   g_mutex_lock (&__cat_mutex);
1566   __categories = g_slist_remove (__categories, category);
1567   g_mutex_unlock (&__cat_mutex);
1568
1569   g_free ((gpointer) category->name);
1570   g_free ((gpointer) category->description);
1571   g_slice_free (GstDebugCategory, category);
1572 }
1573
1574 /**
1575  * gst_debug_category_set_threshold:
1576  * @category: a #GstDebugCategory to set threshold of.
1577  * @level: the #GstDebugLevel threshold to set.
1578  *
1579  * Sets the threshold of the category to the given level. Debug information will
1580  * only be output if the threshold is lower or equal to the level of the
1581  * debugging message.
1582  * <note><para>
1583  * Do not use this function in production code, because other functions may
1584  * change the threshold of categories as side effect. It is however a nice
1585  * function to use when debugging (even from gdb).
1586  * </para></note>
1587  */
1588 void
1589 gst_debug_category_set_threshold (GstDebugCategory * category,
1590     GstDebugLevel level)
1591 {
1592   g_return_if_fail (category != NULL);
1593
1594   if (level > _gst_debug_min) {
1595     _gst_debug_enabled = TRUE;
1596     _gst_debug_min = level;
1597   }
1598
1599   g_atomic_int_set (&category->threshold, level);
1600 }
1601
1602 /**
1603  * gst_debug_category_reset_threshold:
1604  * @category: a #GstDebugCategory to reset threshold of.
1605  *
1606  * Resets the threshold of the category to the default level. Debug information
1607  * will only be output if the threshold is lower or equal to the level of the
1608  * debugging message.
1609  * Use this function to set the threshold back to where it was after using
1610  * gst_debug_category_set_threshold().
1611  */
1612 void
1613 gst_debug_category_reset_threshold (GstDebugCategory * category)
1614 {
1615   gst_debug_reset_threshold (category, NULL);
1616 }
1617
1618 /**
1619  * gst_debug_category_get_threshold:
1620  * @category: a #GstDebugCategory to get threshold of.
1621  *
1622  * Returns the threshold of a #GstDebugCategory.
1623  *
1624  * Returns: the #GstDebugLevel that is used as threshold.
1625  */
1626 GstDebugLevel
1627 gst_debug_category_get_threshold (GstDebugCategory * category)
1628 {
1629   return (GstDebugLevel) g_atomic_int_get (&category->threshold);
1630 }
1631
1632 /**
1633  * gst_debug_category_get_name:
1634  * @category: a #GstDebugCategory to get name of.
1635  *
1636  * Returns the name of a debug category.
1637  *
1638  * Returns: the name of the category.
1639  */
1640 const gchar *
1641 gst_debug_category_get_name (GstDebugCategory * category)
1642 {
1643   return category->name;
1644 }
1645
1646 /**
1647  * gst_debug_category_get_color:
1648  * @category: a #GstDebugCategory to get the color of.
1649  *
1650  * Returns the color of a debug category used when printing output in this
1651  * category.
1652  *
1653  * Returns: the color of the category.
1654  */
1655 guint
1656 gst_debug_category_get_color (GstDebugCategory * category)
1657 {
1658   return category->color;
1659 }
1660
1661 /**
1662  * gst_debug_category_get_description:
1663  * @category: a #GstDebugCategory to get the description of.
1664  *
1665  * Returns the description of a debug category.
1666  *
1667  * Returns: the description of the category.
1668  */
1669 const gchar *
1670 gst_debug_category_get_description (GstDebugCategory * category)
1671 {
1672   return category->description;
1673 }
1674
1675 /**
1676  * gst_debug_get_all_categories:
1677  *
1678  * Returns a snapshot of a all categories that are currently in use . This list
1679  * may change anytime.
1680  * The caller has to free the list after use.
1681  *
1682  * Returns: (transfer container) (element-type Gst.DebugCategory): the list of
1683  *     debug categories
1684  */
1685 GSList *
1686 gst_debug_get_all_categories (void)
1687 {
1688   GSList *ret;
1689
1690   g_mutex_lock (&__cat_mutex);
1691   ret = g_slist_copy (__categories);
1692   g_mutex_unlock (&__cat_mutex);
1693
1694   return ret;
1695 }
1696
1697 static GstDebugCategory *
1698 _gst_debug_get_category_locked (const gchar * name)
1699 {
1700   GstDebugCategory *ret = NULL;
1701   GSList *node;
1702
1703   for (node = __categories; node; node = g_slist_next (node)) {
1704     ret = (GstDebugCategory *) node->data;
1705     if (!strcmp (name, ret->name)) {
1706       return ret;
1707     }
1708   }
1709   return NULL;
1710 }
1711
1712 GstDebugCategory *
1713 _gst_debug_get_category (const gchar * name)
1714 {
1715   GstDebugCategory *ret;
1716
1717   g_mutex_lock (&__cat_mutex);
1718   ret = _gst_debug_get_category_locked (name);
1719   g_mutex_unlock (&__cat_mutex);
1720
1721   return ret;
1722 }
1723
1724 static gboolean
1725 parse_debug_category (gchar * str, const gchar ** category)
1726 {
1727   if (!str)
1728     return FALSE;
1729
1730   /* works in place */
1731   g_strstrip (str);
1732
1733   if (str[0] != '\0') {
1734     *category = str;
1735     return TRUE;
1736   }
1737
1738   return FALSE;
1739 }
1740
1741 static gboolean
1742 parse_debug_level (gchar * str, GstDebugLevel * level)
1743 {
1744   if (!str)
1745     return FALSE;
1746
1747   /* works in place */
1748   g_strstrip (str);
1749
1750   if (g_ascii_isdigit (str[0])) {
1751     unsigned long l;
1752     char *endptr;
1753     l = strtoul (str, &endptr, 10);
1754     if (endptr > str && endptr[0] == 0) {
1755       *level = (GstDebugLevel) l;
1756     } else {
1757       return FALSE;
1758     }
1759   } else if (strcmp (str, "ERROR") == 0) {
1760     *level = GST_LEVEL_ERROR;
1761   } else if (strncmp (str, "WARN", 4) == 0) {
1762     *level = GST_LEVEL_WARNING;
1763   } else if (strcmp (str, "FIXME") == 0) {
1764     *level = GST_LEVEL_FIXME;
1765   } else if (strcmp (str, "INFO") == 0) {
1766     *level = GST_LEVEL_INFO;
1767   } else if (strcmp (str, "DEBUG") == 0) {
1768     *level = GST_LEVEL_DEBUG;
1769   } else if (strcmp (str, "LOG") == 0) {
1770     *level = GST_LEVEL_LOG;
1771   } else if (strcmp (str, "TRACE") == 0) {
1772     *level = GST_LEVEL_TRACE;
1773   } else if (strcmp (str, "MEMDUMP") == 0) {
1774     *level = GST_LEVEL_MEMDUMP;
1775   } else
1776     return FALSE;
1777
1778   return TRUE;
1779 }
1780
1781 /**
1782  * gst_debug_set_threshold_from_string:
1783  * @list: comma-separated list of "category:level" pairs to be used
1784  *     as debug logging levels
1785  * @reset: %TRUE to clear all previously-set debug levels before setting
1786  *     new thresholds
1787  * %FALSE if adding the threshold described by @list to the one already set.
1788  *
1789  * Sets the debug logging wanted in the same form as with the GST_DEBUG
1790  * environment variable. You can use wildcards such as '*', but note that
1791  * the order matters when you use wild cards, e.g. "foosrc:6,*src:3,*:2" sets
1792  * everything to log level 2.
1793  *
1794  * Since: 1.2
1795  */
1796 void
1797 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
1798 {
1799   gchar **split;
1800   gchar **walk;
1801
1802   g_assert (list);
1803
1804   if (reset == TRUE)
1805     gst_debug_set_default_threshold (0);
1806
1807   split = g_strsplit (list, ",", 0);
1808
1809   for (walk = split; *walk; walk++) {
1810     if (strchr (*walk, ':')) {
1811       gchar **values = g_strsplit (*walk, ":", 2);
1812
1813       if (values[0] && values[1]) {
1814         GstDebugLevel level;
1815         const gchar *category;
1816
1817         if (parse_debug_category (values[0], &category)
1818             && parse_debug_level (values[1], &level))
1819           gst_debug_set_threshold_for_name (category, level);
1820       }
1821
1822       g_strfreev (values);
1823     } else {
1824       GstDebugLevel level;
1825
1826       if (parse_debug_level (*walk, &level))
1827         gst_debug_set_default_threshold (level);
1828     }
1829   }
1830
1831   g_strfreev (split);
1832 }
1833
1834 /*** FUNCTION POINTERS ********************************************************/
1835
1836 static GHashTable *__gst_function_pointers;     /* NULL */
1837 static GMutex __dbg_functions_mutex;
1838
1839 /* This function MUST NOT return NULL */
1840 const gchar *
1841 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1842 {
1843   gchar *ptrname;
1844
1845 #ifdef HAVE_DLADDR
1846   Dl_info dl_info;
1847 #endif
1848
1849   if (G_UNLIKELY (func == NULL))
1850     return "(NULL)";
1851
1852   g_mutex_lock (&__dbg_functions_mutex);
1853   if (G_LIKELY (__gst_function_pointers)) {
1854     ptrname = g_hash_table_lookup (__gst_function_pointers, (gpointer) func);
1855     g_mutex_unlock (&__dbg_functions_mutex);
1856     if (G_LIKELY (ptrname))
1857       return ptrname;
1858   } else {
1859     g_mutex_unlock (&__dbg_functions_mutex);
1860   }
1861   /* we need to create an entry in the hash table for this one so we don't leak
1862    * the name */
1863 #ifdef HAVE_DLADDR
1864   if (dladdr ((gpointer) func, &dl_info) && dl_info.dli_sname) {
1865     gchar *name = g_strdup (dl_info.dli_sname);
1866
1867     _gst_debug_register_funcptr (func, name);
1868     return name;
1869   } else
1870 #endif
1871   {
1872     gchar *name = g_strdup_printf ("%p", (gpointer) func);
1873
1874     _gst_debug_register_funcptr (func, name);
1875     return name;
1876   }
1877 }
1878
1879 void
1880 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1881 {
1882   gpointer ptr = (gpointer) func;
1883
1884   g_mutex_lock (&__dbg_functions_mutex);
1885
1886   if (!__gst_function_pointers)
1887     __gst_function_pointers = g_hash_table_new (g_direct_hash, g_direct_equal);
1888   if (!g_hash_table_lookup (__gst_function_pointers, ptr))
1889     g_hash_table_insert (__gst_function_pointers, ptr, (gpointer) ptrname);
1890
1891   g_mutex_unlock (&__dbg_functions_mutex);
1892 }
1893
1894 static void
1895 gst_info_dump_mem_line (gchar * linebuf, gsize linebuf_size,
1896     const guint8 * mem, gsize mem_offset, gsize mem_size)
1897 {
1898   gchar hexstr[50], ascstr[18], digitstr[4];
1899
1900   if (mem_size > 16)
1901     mem_size = 16;
1902
1903   hexstr[0] = '\0';
1904   ascstr[0] = '\0';
1905
1906   if (mem != NULL) {
1907     guint i = 0;
1908
1909     mem += mem_offset;
1910     while (i < mem_size) {
1911       ascstr[i] = (g_ascii_isprint (mem[i])) ? mem[i] : '.';
1912       g_snprintf (digitstr, sizeof (digitstr), "%02x ", mem[i]);
1913       g_strlcat (hexstr, digitstr, sizeof (hexstr));
1914       ++i;
1915     }
1916     ascstr[i] = '\0';
1917   }
1918
1919   g_snprintf (linebuf, linebuf_size, "%08x: %-48.48s %-16.16s",
1920       (guint) mem_offset, hexstr, ascstr);
1921 }
1922
1923 void
1924 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
1925     const gchar * func, gint line, GObject * obj, const gchar * msg,
1926     const guint8 * data, guint length)
1927 {
1928   guint off = 0;
1929
1930   gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1931       "-------------------------------------------------------------------");
1932
1933   if (msg != NULL && *msg != '\0') {
1934     gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", msg);
1935   }
1936
1937   while (off < length) {
1938     gchar buf[128];
1939
1940     /* gst_info_dump_mem_line will process 16 bytes at most */
1941     gst_info_dump_mem_line (buf, sizeof (buf), data, off, length - off);
1942     gst_debug_log (cat, GST_LEVEL_MEMDUMP, file, func, line, obj, "%s", buf);
1943     off += 16;
1944   }
1945
1946   gst_debug_log ((cat), GST_LEVEL_MEMDUMP, file, func, line, obj, "--------"
1947       "-------------------------------------------------------------------");
1948 }
1949
1950 #else /* !GST_DISABLE_GST_DEBUG */
1951 #ifndef GST_REMOVE_DISABLED
1952
1953 GstDebugCategory *
1954 _gst_debug_category_new (const gchar * name, guint color,
1955     const gchar * description)
1956 {
1957   return NULL;
1958 }
1959
1960 void
1961 _gst_debug_register_funcptr (GstDebugFuncPtr func, const gchar * ptrname)
1962 {
1963 }
1964
1965 /* This function MUST NOT return NULL */
1966 const gchar *
1967 _gst_debug_nameof_funcptr (GstDebugFuncPtr func)
1968 {
1969   return "(NULL)";
1970 }
1971
1972 void
1973 gst_debug_log (GstDebugCategory * category, GstDebugLevel level,
1974     const gchar * file, const gchar * function, gint line,
1975     GObject * object, const gchar * format, ...)
1976 {
1977 }
1978
1979 void
1980 gst_debug_log_valist (GstDebugCategory * category, GstDebugLevel level,
1981     const gchar * file, const gchar * function, gint line,
1982     GObject * object, const gchar * format, va_list args)
1983 {
1984 }
1985
1986 const gchar *
1987 gst_debug_message_get (GstDebugMessage * message)
1988 {
1989   return "";
1990 }
1991
1992 void
1993 gst_debug_log_default (GstDebugCategory * category, GstDebugLevel level,
1994     const gchar * file, const gchar * function, gint line,
1995     GObject * object, GstDebugMessage * message, gpointer unused)
1996 {
1997 }
1998
1999 const gchar *
2000 gst_debug_level_get_name (GstDebugLevel level)
2001 {
2002   return "NONE";
2003 }
2004
2005 void
2006 gst_debug_add_log_function (GstLogFunction func, gpointer user_data,
2007     GDestroyNotify notify)
2008 {
2009 }
2010
2011 guint
2012 gst_debug_remove_log_function (GstLogFunction func)
2013 {
2014   return 0;
2015 }
2016
2017 guint
2018 gst_debug_remove_log_function_by_data (gpointer data)
2019 {
2020   return 0;
2021 }
2022
2023 void
2024 gst_debug_set_active (gboolean active)
2025 {
2026 }
2027
2028 gboolean
2029 gst_debug_is_active (void)
2030 {
2031   return FALSE;
2032 }
2033
2034 void
2035 gst_debug_set_colored (gboolean colored)
2036 {
2037 }
2038
2039 void
2040 gst_debug_set_color_mode (GstDebugColorMode mode)
2041 {
2042 }
2043
2044 void
2045 gst_debug_set_color_mode_from_string (const gchar * str)
2046 {
2047 }
2048
2049 gboolean
2050 gst_debug_is_colored (void)
2051 {
2052   return FALSE;
2053 }
2054
2055 GstDebugColorMode
2056 gst_debug_get_color_mode (void)
2057 {
2058   return GST_DEBUG_COLOR_MODE_OFF;
2059 }
2060
2061 void
2062 gst_debug_set_threshold_from_string (const gchar * list, gboolean reset)
2063 {
2064 }
2065
2066 void
2067 gst_debug_set_default_threshold (GstDebugLevel level)
2068 {
2069 }
2070
2071 GstDebugLevel
2072 gst_debug_get_default_threshold (void)
2073 {
2074   return GST_LEVEL_NONE;
2075 }
2076
2077 void
2078 gst_debug_set_threshold_for_name (const gchar * name, GstDebugLevel level)
2079 {
2080 }
2081
2082 void
2083 gst_debug_unset_threshold_for_name (const gchar * name)
2084 {
2085 }
2086
2087 void
2088 gst_debug_category_free (GstDebugCategory * category)
2089 {
2090 }
2091
2092 void
2093 gst_debug_category_set_threshold (GstDebugCategory * category,
2094     GstDebugLevel level)
2095 {
2096 }
2097
2098 void
2099 gst_debug_category_reset_threshold (GstDebugCategory * category)
2100 {
2101 }
2102
2103 GstDebugLevel
2104 gst_debug_category_get_threshold (GstDebugCategory * category)
2105 {
2106   return GST_LEVEL_NONE;
2107 }
2108
2109 const gchar *
2110 gst_debug_category_get_name (GstDebugCategory * category)
2111 {
2112   return "";
2113 }
2114
2115 guint
2116 gst_debug_category_get_color (GstDebugCategory * category)
2117 {
2118   return 0;
2119 }
2120
2121 const gchar *
2122 gst_debug_category_get_description (GstDebugCategory * category)
2123 {
2124   return "";
2125 }
2126
2127 GSList *
2128 gst_debug_get_all_categories (void)
2129 {
2130   return NULL;
2131 }
2132
2133 GstDebugCategory *
2134 _gst_debug_get_category (const gchar * name)
2135 {
2136   return NULL;
2137 }
2138
2139 gchar *
2140 gst_debug_construct_term_color (guint colorinfo)
2141 {
2142   return g_strdup ("00");
2143 }
2144
2145 gint
2146 gst_debug_construct_win_color (guint colorinfo)
2147 {
2148   return 0;
2149 }
2150
2151 gboolean
2152 _priv_gst_in_valgrind (void)
2153 {
2154   return FALSE;
2155 }
2156
2157 void
2158 _gst_debug_dump_mem (GstDebugCategory * cat, const gchar * file,
2159     const gchar * func, gint line, GObject * obj, const gchar * msg,
2160     const guint8 * data, guint length)
2161 {
2162 }
2163 #endif /* GST_REMOVE_DISABLED */
2164 #endif /* GST_DISABLE_GST_DEBUG */
2165
2166 /* Need this for _gst_element_error_printf even if GST_REMOVE_DISABLED is set:
2167  * fallback function that cleans up the format string and replaces all pointer
2168  * extension formats with plain %p. */
2169 #ifdef GST_DISABLE_GST_DEBUG
2170 #include <glib/gprintf.h>
2171 int
2172 __gst_info_fallback_vasprintf (char **result, char const *format, va_list args)
2173 {
2174   gchar *clean_format, *c;
2175   gsize len;
2176
2177   if (format == NULL)
2178     return -1;
2179
2180   clean_format = g_strdup (format);
2181   c = clean_format;
2182   while ((c = strstr (c, "%p\a"))) {
2183     if (c[3] < 'A' || c[3] > 'Z') {
2184       c += 3;
2185       continue;
2186     }
2187     len = strlen (c + 4);
2188     memmove (c + 2, c + 4, len + 1);
2189     c += 2;
2190   }
2191   while ((c = strstr (clean_format, "%P")))     /* old GST_PTR_FORMAT */
2192     c[1] = 'p';
2193   while ((c = strstr (clean_format, "%Q")))     /* old GST_SEGMENT_FORMAT */
2194     c[1] = 'p';
2195
2196   len = g_vasprintf (result, clean_format, args);
2197
2198   g_free (clean_format);
2199
2200   if (*result == NULL)
2201     return -1;
2202
2203   return len;
2204 }
2205 #endif
2206
2207 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
2208 /* FIXME make this thread specific */
2209 static GSList *stack_trace = NULL;
2210
2211 void
2212 __cyg_profile_func_enter (void *this_fn, void *call_site)
2213     G_GNUC_NO_INSTRUMENT;
2214      void __cyg_profile_func_enter (void *this_fn, void *call_site)
2215 {
2216   gchar *name = _gst_debug_nameof_funcptr (this_fn);
2217   gchar *site = _gst_debug_nameof_funcptr (call_site);
2218
2219   GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "entering function %s from %s", name,
2220       site);
2221   stack_trace =
2222       g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)",
2223           this_fn, name, call_site, site));
2224
2225   g_free (name);
2226   g_free (site);
2227 }
2228
2229 void
2230 __cyg_profile_func_exit (void *this_fn, void *call_site)
2231     G_GNUC_NO_INSTRUMENT;
2232      void __cyg_profile_func_exit (void *this_fn, void *call_site)
2233 {
2234   gchar *name = _gst_debug_nameof_funcptr (this_fn);
2235
2236   GST_CAT_DEBUG (GST_CAT_CALL_TRACE, "leaving function %s", name);
2237   g_free (stack_trace->data);
2238   stack_trace = g_slist_delete_link (stack_trace, stack_trace);
2239
2240   g_free (name);
2241 }
2242
2243 /**
2244  * gst_debug_print_stack_trace:
2245  *
2246  * If GST_ENABLE_FUNC_INSTRUMENTATION is defined a stacktrace is available for
2247  * gstreamer code, which can be printed with this function.
2248  */
2249 void
2250 gst_debug_print_stack_trace (void)
2251 {
2252   GSList *walk = stack_trace;
2253   gint count = 0;
2254
2255   if (walk)
2256     walk = g_slist_next (walk);
2257
2258   while (walk) {
2259     gchar *name = (gchar *) walk->data;
2260
2261     g_print ("#%-2d %s\n", count++, name);
2262
2263     walk = g_slist_next (walk);
2264   }
2265 }
2266 #else
2267 void
2268 gst_debug_print_stack_trace (void)
2269 {
2270   /* nothing because it's compiled out */
2271 }
2272
2273 #endif /* GST_ENABLE_FUNC_INSTRUMENTATION */