merge from release branch
[platform/upstream/gstreamer.git] / gst / gstinfo.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstinfo.h: 
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifndef __GSTINFO_H__
24 #define __GSTINFO_H__
25
26 #include <stdio.h>
27 #include <gmodule.h>
28 #include <unistd.h>
29 #include <glib/gmacros.h>
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 /* FIXME: convert to using G_STRLOC all the way if we can ! */
36
37 #ifndef FUNCTION
38 #ifdef G_GNUC_PRETTY_FUNCTION
39 #define FUNCTION G_GNUC_PRETTY_FUNCTION
40 #elif HAVE_FUNC
41 #define FUNCTION __func__
42 #elif HAVE_PRETTY_FUNCTION
43 #define FUNCTION __PRETTY_FUNCTION__
44 #elif HAVE_FUNCTION
45 #define FUNCTION __FUNCTION__
46 #else
47 #define FUNCTION ""
48 #endif
49 #endif /* ifndef FUNCTION */
50
51 /***** are we in the core or not? *****/
52 #ifdef __GST_PRIVATE_H__
53   #define _GST_DEBUG_INCORE TRUE
54 #else
55   #define _GST_DEBUG_INCORE FALSE
56 #endif
57
58
59 /* colorization stuff */
60 #ifdef GST_DEBUG_COLOR
61   #ifdef __GST_PRIVATE_H__   /* FIXME this should be some libgst.la -specific thing */
62     #define GST_DEBUG_CHAR_MODE "00"
63   #else
64     #define GST_DEBUG_CHAR_MODE "01"
65   #endif
66 #endif
67
68 gint _gst_debug_stringhash_color(gchar *file);
69
70
71
72 /**********************************************************************
73  * Categories
74  **********************************************************************/
75
76 const gchar *   gst_get_category_name   (gint category);
77
78 enum {
79   GST_CAT_GST_INIT = 0,         /* Library initialization */
80   GST_CAT_COTHREADS,            /* Cothread creation, etc. */
81   GST_CAT_COTHREAD_SWITCH,      /* Cothread switching */
82   GST_CAT_AUTOPLUG,             /* Successful autoplug results */
83   GST_CAT_AUTOPLUG_ATTEMPT,     /* Attempted autoplug operations */
84   GST_CAT_PARENTAGE,            /* GstBin parentage issues */
85   GST_CAT_STATES,               /* State changes and such */
86   GST_CAT_PLANNING,             /* Plan generation */
87   GST_CAT_SCHEDULING,           /* Schedule construction */
88   GST_CAT_DATAFLOW,             /* Events during actual data movement */
89   GST_CAT_BUFFER,               /* Buffer creation/destruction */
90   GST_CAT_CAPS,                 /* Capabilities matching */
91   GST_CAT_CLOCK,                /* Clocking */
92   GST_CAT_ELEMENT_PADS,         /* Element pad management */
93   GST_CAT_ELEMENT_FACTORY,      /* Elementfactory stuff */
94   GST_CAT_PADS,                 /* Pad creation/connection */
95   GST_CAT_PIPELINE,             /* Pipeline stuff */
96   GST_CAT_PLUGIN_LOADING,       /* Plugin loading */
97   GST_CAT_PLUGIN_ERRORS,        /* Errors during plugin loading */
98   GST_CAT_PLUGIN_INFO,          /* Plugin state information */
99   GST_CAT_PROPERTIES,           /* Properties */
100   GST_CAT_THREAD,               /* Thread creation/management */
101   GST_CAT_TYPES,                /* Typing */
102   GST_CAT_XML,                  /* XML load/save of everything */
103   GST_CAT_NEGOTIATION,          /* Caps Negotiation stuff */
104   GST_CAT_REFCOUNTING,          /* Ref Counting stuff */
105   GST_CAT_EVENT,                /* Event system */
106   GST_CAT_PARAMS,               /* Dynamic parameters */
107
108   GST_CAT_CALL_TRACE = 30,      /* Call tracing */
109
110   GST_CAT_MAX_CATEGORY = 31
111 };
112
113 extern const gchar *_gst_category_colors[32];
114
115
116
117 /**********************************************************************
118  * DEBUG system
119  **********************************************************************/
120
121 /* for include files that make too much noise normally */
122 #ifdef GST_DEBUG_FORCE_DISABLE
123 #undef GST_DEBUG_ENABLED
124 #endif
125 /* for applications that really really want all the noise */
126 #ifdef GST_DEBUG_FORCE_ENABLE
127 #define GST_DEBUG_ENABLED
128 #endif
129
130 /*#ifdef GST_DEBUG_ENABLED */
131 #define GST_DEBUG_ENABLE_CATEGORIES 0xffffffff
132 /*#else */
133 /*#define GST_DEBUG_ENABLE_CATEGORIES 0x00000000 */
134 /*#endif */
135
136
137 typedef void (*GstDebugHandler) (gint category,gboolean core,
138                                  const gchar *file,const gchar *function,
139                                  gint line,const gchar *debug_string,
140                                  void *element,gchar *string);
141
142 void gst_default_debug_handler (gint category,gboolean incore,
143                                 const gchar *file, const gchar *function,
144                                 gint line,const gchar *debug_string,
145                                 void *element,gchar *string);
146
147 extern guint32 _gst_debug_categories;
148 extern GstDebugHandler _gst_debug_handler;
149
150 /* fallback, this should probably be a 'weak' symbol or something */
151 G_GNUC_UNUSED static gchar *_debug_string = NULL;
152
153
154
155 #ifdef G_HAVE_ISO_VARARGS
156
157 #ifdef GST_DEBUG_ENABLED
158 #define GST_DEBUG(cat, ...) G_STMT_START{ \
159   if ((1<<cat) & _gst_debug_categories) \
160     _gst_debug_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
161                        NULL,g_strdup_printf( __VA_ARGS__ )); \
162 }G_STMT_END
163
164 #define GST_DEBUG_ELEMENT(cat, element, ...) G_STMT_START{ \
165   if ((1<<cat) & _gst_debug_categories) \
166     _gst_debug_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
167                        element,g_strdup_printf( __VA_ARGS__ )); \
168 }G_STMT_END
169
170 #else
171 #define GST_DEBUG(cat, ...)
172 #define GST_DEBUG_ELEMENT(cat,element, ...)
173 #endif
174
175 #elif defined(G_HAVE_GNUC_VARARGS)
176
177 #ifdef GST_DEBUG_ENABLED
178 #define GST_DEBUG(cat,format,args...) G_STMT_START{ \
179   if ((1<<cat) & _gst_debug_categories) \
180     _gst_debug_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
181                        NULL,g_strdup_printf( format , ## args )); \
182 }G_STMT_END
183
184 #define GST_DEBUG_ELEMENT(cat,element,format,args...) G_STMT_START{ \
185   if ((1<<cat) & _gst_debug_categories) \
186     _gst_debug_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
187                        element,g_strdup_printf( format , ## args )); \
188 }G_STMT_END
189
190 #else
191 #define GST_DEBUG(cat,format,args...)
192 #define GST_DEBUG_ELEMENT(cat,element,format,args...)
193 #endif
194
195 #endif
196
197
198
199
200 /********** some convenience macros for debugging **********/
201 #define GST_DEBUG_PAD_NAME(pad) \
202   (GST_OBJECT_PARENT(pad) != NULL) ? \
203   GST_OBJECT_NAME (GST_OBJECT_PARENT(pad)) : \
204   "''", GST_OBJECT_NAME (pad)
205
206 #ifdef G_HAVE_ISO_VARARGS
207
208 #ifdef GST_DEBUG_COLOR
209   #define GST_DEBUG_ENTER(...) GST_DEBUG( 31 , "\033[00;37mentering\033[00m :" __VA_ARGS__ )
210   #define GST_DEBUG_LEAVE(...) GST_DEBUG( 31 , "\033[00;37mleaving\033[00m :"  __VA_ARGS__ )
211 #else
212   #define GST_DEBUG_ENTER(...) GST_DEBUG( 31 , "entering :" __VA_ARGS__ )
213   #define GST_DEBUG_LEAVE(...) GST_DEBUG( 31 , "leaving :" __VA_ARGS__ )
214 #endif
215
216 #elif defined(G_HAVE_GNUC_VARARGS)
217
218 #ifdef GST_DEBUG_COLOR
219   #define GST_DEBUG_ENTER(format, args...) GST_DEBUG( 31 , format ": \033[00;37mentering\033[00m" , ##args )
220   #define GST_DEBUG_LEAVE(format, args...) GST_DEBUG( 31 , format ": \033[00;37mleaving\033[00m" , ##args )
221 #else
222   #define GST_DEBUG_ENTER(format, args...) GST_DEBUG( 31 , format ": entering" , ##args )
223   #define GST_DEBUG_LEAVE(format, args...) GST_DEBUG( 31 , format ": leaving" , ##args )
224 #endif
225
226 #endif
227
228
229 /***** Colorized debug for thread ids *****/
230 #ifdef GST_DEBUG_COLOR
231   #define GST_DEBUG_THREAD_FORMAT "\033[00;%dm%d\033[00m"
232   #define GST_DEBUG_THREAD_ARGS(id) ( ((id) < 0) ? 37 : ((id) % 6 + 31) ), (id)
233 #else
234   #define GST_DEBUG_THREAD_FORMAT "%d"
235   #define GST_DEBUG_THREAD_ARGS(id) (id)
236 #endif
237
238
239
240 /**********************************************************************
241  * The following is a DEBUG_ENTER implementation that will wrap the
242  * function it sits at the head of.  It removes the need for a
243  * DEBUG_LEAVE call.  However, it segfaults whenever it gets anywhere
244  * near cothreads.  We will not use it for the moment.
245  *
246 typedef void (*_debug_function_f)();
247 G_GNUC_UNUSED static gchar *_debug_string_pointer = NULL;
248 G_GNUC_UNUSED static GModule *_debug_self_module = NULL;
249
250 #ifdef G_HAVE_ISO_VARARGS
251
252 #define _DEBUG_ENTER_BUILTIN(...)                                                       \
253   static int _debug_in_wrapper = 0;                                                     \
254   gchar *_debug_string = ({                                                             \
255     if (!_debug_in_wrapper) {                                                           \
256       void *_return_value;                                                              \
257       gchar *_debug_string;                                                             \
258       _debug_function_f function;                                                       \
259       void *_function_args = __builtin_apply_args();                                    \
260       _debug_in_wrapper = 1;                                                            \
261       _debug_string = g_strdup_printf(GST_DEBUG_PREFIX(""));                            \
262       _debug_string_pointer = _debug_string;                                            \
263       fprintf(stderr,"%s: entered " FUNCTION, _debug_string);                           \ 
264       fprintf(stderr, __VA_ARGS__ );                                                    \
265       fprintf(stderr,"\n");                                                             \
266       if (_debug_self_module == NULL) _debug_self_module = g_module_open(NULL,0);       \
267       g_module_symbol(_debug_self_module,FUNCTION,(gpointer *)&function);               \
268       _return_value = __builtin_apply(function,_function_args,64);                      \
269       fprintf(stderr,"%s: left " FUNCTION, _debug_string);                              \
270       fprintf(stderr, __VA_ARGS__);                                                     \
271       fprintf(stderr,"\n");                                                             \
272       g_free(_debug_string);                                                            \
273       __builtin_return(_return_value);                                                  \
274     } else {                                                                            \
275       _debug_in_wrapper = 0;                                                            \
276     }                                                                                   \
277     _debug_string_pointer;                                                              \
278   });
279
280 #elif defined(G_HAVE_GNUC_VARARGS)
281
282 #define _DEBUG_ENTER_BUILTIN(format,args...)                                            \
283   static int _debug_in_wrapper = 0;                                                     \
284   gchar *_debug_string = ({                                                             \
285     if (!_debug_in_wrapper) {                                                           \
286       void *_return_value;                                                              \
287       gchar *_debug_string;                                                             \
288       _debug_function_f function;                                                       \
289       void *_function_args = __builtin_apply_args();                                    \
290       _debug_in_wrapper = 1;                                                            \
291       _debug_string = g_strdup_printf(GST_DEBUG_PREFIX(""));                            \
292       _debug_string_pointer = _debug_string;                                            \
293       fprintf(stderr,"%s: entered " FUNCTION format "\n" , _debug_string , ## args );   \
294       if (_debug_self_module == NULL) _debug_self_module = g_module_open(NULL,0);       \
295       g_module_symbol(_debug_self_module,FUNCTION,(gpointer *)&function);               \
296       _return_value = __builtin_apply(function,_function_args,64);                      \
297       fprintf(stderr,"%s: left " FUNCTION format "\n" , _debug_string , ## args ); \
298       g_free(_debug_string);                                                            \
299       __builtin_return(_return_value);                                                  \
300     } else {                                                                            \
301       _debug_in_wrapper = 0;                                                            \
302     }                                                                                   \
303     _debug_string_pointer;                                                              \
304   });
305
306 #endif
307
308 * WARNING: there's a gcc CPP bug lurking in here.  The extra space before the ##args    *
309  * somehow make the preprocessor leave the _debug_string. If it's removed, the          *
310  * _debug_string somehow gets stripped along with the ##args, and that's all she wrote. *
311
312 #ifdef G_HAVE_ISO_VARARGS
313
314 #define _DEBUG_BUILTIN(...)                                     \
315   if (_debug_string != (void *)-1) {                            \
316     if (_debug_string) {                                        \
317       fprintf(stderr, "%s: " _debug_string);                    \
318       fprintf(stderr, __VA_ARGS__);                             \
319     } else {                                                    \
320       fprintf(stderr,GST_DEBUG_PREFIX(": " __VA_ARGS__));       \
321     }                                                           \
322   }
323
324 #elif defined(G_HAVE_GNUC_VARARGS)
325
326 #define _DEBUG_BUILTIN(format,args...)                          \
327   if (_debug_string != (void *)-1) {                            \
328     if (_debug_string)                                          \
329       fprintf(stderr,"%s: " format , _debug_string , ## args);  \
330     else                                                        \
331       fprintf(stderr,GST_DEBUG_PREFIX(": " format , ## args));  \
332   }
333
334 #endif
335
336 */
337
338
339
340 /**********************************************************************
341  * INFO system
342  **********************************************************************/
343
344 typedef void (*GstInfoHandler) (gint category,gboolean incore,
345                                 const gchar *file,const gchar *function,
346                                 gint line,const gchar *debug_string,
347                                 void *element,gchar *string);
348
349 void gst_default_info_handler (gint category,gboolean incore,
350                                const gchar *file,const gchar *function,
351                                gint line,const gchar *debug_string,
352                                void *element,gchar *string);
353
354 extern GstInfoHandler _gst_info_handler;
355 extern guint32 _gst_info_categories;
356
357 /* for include files that make too much noise normally */
358 #ifdef GST_INFO_FORCE_DISABLE
359 #undef GST_INFO_ENABLED
360 #endif
361 /* for applications that really really want all the noise */
362 #ifdef GST_INFO_FORCE_ENABLE
363 #define GST_INFO_ENABLED
364 #endif
365
366 #ifdef G_HAVE_ISO_VARARGS
367
368 #ifdef GST_INFO_ENABLED
369 #define GST_INFO(cat,...) G_STMT_START{ \
370   if ((1<<cat) & _gst_info_categories) \
371     _gst_info_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
372                       NULL,g_strdup_printf( __VA_ARGS__ )); \
373 }G_STMT_END
374
375 #define GST_INFO_ELEMENT(cat,element,...) G_STMT_START{ \
376   if ((1<<cat) & _gst_info_categories) \
377     _gst_info_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
378                       element,g_strdup_printf( __VA_ARGS__ )); \
379 }G_STMT_END
380
381 #else
382 #define GST_INFO(cat,...) 
383 #define GST_INFO_ELEMENT(cat,element,...)
384 #endif
385
386 #elif defined(G_HAVE_GNUC_VARARGS)
387
388 #ifdef GST_INFO_ENABLED
389 #define GST_INFO(cat,format,args...) G_STMT_START{ \
390   if ((1<<cat) & _gst_info_categories) \
391     _gst_info_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
392                       NULL,g_strdup_printf( format , ## args )); \
393 }G_STMT_END
394
395 #define GST_INFO_ELEMENT(cat,element,format,args...) G_STMT_START{ \
396   if ((1<<cat) & _gst_info_categories) \
397     _gst_info_handler(cat,_GST_DEBUG_INCORE,__FILE__,FUNCTION,__LINE__,_debug_string, \
398                       element,g_strdup_printf( format , ## args )); \
399 }G_STMT_END
400
401 #else
402 #define GST_INFO(cat,format,args...) 
403 #define GST_INFO_ELEMENT(cat,element,format,args...)
404 #endif
405
406 #endif
407
408
409 void            gst_info_set_categories         (guint32 categories);
410 guint32         gst_info_get_categories         (void);
411 void            gst_info_enable_category        (gint category);
412 void            gst_info_disable_category       (gint category);
413
414 void            gst_debug_set_categories        (guint32 categories);
415 guint32         gst_debug_get_categories        (void);
416 void            gst_debug_enable_category       (gint category);
417 void            gst_debug_disable_category      (gint category);
418
419
420
421
422 /**********************************************************************
423  * ERROR system
424  **********************************************************************/
425
426 typedef void (*GstErrorHandler) (gchar *file,gchar *function,
427                                  gint line,gchar *debug_string,
428                                  void *element,void *object,gchar *string);
429
430 void gst_default_error_handler (gchar *file,gchar *function,
431                                 gint line,gchar *debug_string,
432                                 void *element,void *object,gchar *string);
433
434 extern GstErrorHandler _gst_error_handler;
435
436 #ifdef G_HAVE_ISO_VARARGS
437
438 #define GST_ERROR(element,...) \
439   _gst_error_handler(__FILE__,FUNCTION,__LINE__,_debug_string, \
440                      element,NULL,g_strdup_printf( __VA_ARGS__ ))
441
442 #define GST_ERROR_OBJECT(element,object,...) \
443   _gst_error_handler(__FILE__,FUNCTION,__LINE__,_debug_string, \
444                      element,object,g_strdup_printf( __VA_ARGS__ ))
445
446 #elif defined(G_HAVE_GNUC_VARARGS)
447
448 #define GST_ERROR(element,format,args...) \
449   _gst_error_handler(__FILE__,FUNCTION,__LINE__,_debug_string, \
450                      element,NULL,g_strdup_printf( format , ## args ))
451
452 #define GST_ERROR_OBJECT(element,object,format,args...) \
453   _gst_error_handler(__FILE__,FUNCTION,__LINE__,_debug_string, \
454                      element,object,g_strdup_printf( format , ## args ))
455
456 #endif
457
458
459
460
461 /********** function pointer stuff **********/
462 extern GHashTable *__gst_function_pointers;
463
464
465 #if GST_DEBUG_ENABLED
466 #define GST_DEBUG_FUNCPTR(ptr) _gst_debug_register_funcptr((void *)(ptr), #ptr)
467 #define GST_DEBUG_FUNCPTR_NAME(ptr) _gst_debug_nameof_funcptr((void *)ptr)
468 #else
469 #define GST_DEBUG_FUNCPTR(ptr) (ptr)
470 #define GST_DEBUG_FUNCPTR_NAME(ptr) ""
471 #endif
472
473 static inline void *
474 _gst_debug_register_funcptr (void *ptr, gchar *ptrname) 
475 {
476   if (!__gst_function_pointers) __gst_function_pointers = g_hash_table_new(g_direct_hash,g_direct_equal);
477   if (!g_hash_table_lookup(__gst_function_pointers,ptr))
478     g_hash_table_insert(__gst_function_pointers,ptr,ptrname);
479   return ptr;
480 }
481
482 gchar *_gst_debug_nameof_funcptr (void *ptr);
483
484 void gst_debug_print_stack_trace (void);
485
486
487
488 #endif /* __GSTINFO_H__ */