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