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