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