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