Added GST_INFO_ENABLED flags
[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 #include "cothreads.h"
35
36
37 /**********************************************************************
38  * DEBUG system
39  **********************************************************************/
40
41 /* for include files that make too much noise normally */
42 #ifdef GST_DEBUG_FORCE_DISABLE
43 #undef GST_DEBUG_ENABLED
44 #endif
45 /* for applications that really really want all the noise */
46 #ifdef GST_DEBUG_FORCE_ENABLE
47 #define GST_DEBUG_ENABLED
48 #endif
49
50 /* fallback, this should probably be a 'weak' symbol or something */
51 G_GNUC_UNUSED static gchar *_debug_string = NULL;
52
53 #define GST_DEBUG_PREFIX(format,args...) \
54 "DEBUG(%d:%d)" __PRETTY_FUNCTION__ ":%d" format , getpid() , cothread_getcurrent() , __LINE__ , ## args
55
56 #ifdef GST_DEBUG_ENABLED
57 #define DEBUG(format,args...) \
58   (_debug_string != NULL) ? \
59     fprintf(stderr,GST_DEBUG_PREFIX("%s: "format , _debug_string , ## args )) : \
60     fprintf(stderr,GST_DEBUG_PREFIX(": "format , ## args ))
61 #define DEBUG_NOPREFIX(format,args...) fprintf(stderr,format , ## args )
62 #define DEBUG_ENTER(format, args...) \
63   fprintf(stderr,GST_DEBUG_PREFIX(format": entering\n" , ## args ))
64 #define DEBUG_SET_STRING(format, args...) \
65   gchar *_debug_string = g_strdup_printf(format , ## args )
66 #define DEBUG_ENTER_STRING DEBUG_ENTER("%s",_debug_string)
67 #define DEBUG_LEAVE(format, args...) \
68   if (_debug_string != NULL) g_free(_debug_string),\
69     fprintf(stderr,GST_DEBUG_PREFIX(format": leaving\n" , ## args ))
70 #define DEBUG_LEAVE_STRING DEBUG_LEAVE("%s",_debug_string)
71 #else
72 #define DEBUG(format, args...)
73 #define DEBUG_NOPREFIX(format, args...)
74 #define DEBUG_ENTER(format, args...)
75 #define DEBUG_LEAVE(format, args...)
76 #define DEBUG_SET_STRING(format, args...)
77 #define DEBUG_ENTER_STRING
78 #endif
79
80
81
82 /********** some convenience macros for debugging **********/
83 #define GST_DEBUG_PAD_NAME(pad) \
84   ((pad)->parent != NULL) ? gst_element_get_name(GST_ELEMENT((pad)->parent)) : "''", gst_pad_get_name(pad)
85
86
87
88 /********** function pointer stuff **********/
89 extern GHashTable *__gst_function_pointers;
90
91 #ifdef GST_DEBUG_ENABLED
92 #define GST_DEBUG_FUNCPTR(ptr) _gst_debug_register_funcptr((void *)(ptr), #ptr)
93 #define GST_DEBUG_FUNCPTR_NAME(ptr) _gst_debug_nameof_funcptr((void *)ptr)
94 #else
95 #define GST_DEBUG_FUNCPTR(ptr) (ptr)
96 #define GST_DEBUG_FUNCPTR_NAME(ptr) ""
97 #endif
98
99 static inline void *
100 _gst_debug_register_funcptr (void *ptr, gchar *ptrname) 
101 {
102   if (!__gst_function_pointers) __gst_function_pointers = g_hash_table_new(g_direct_hash,g_direct_equal);
103   if (!g_hash_table_lookup(__gst_function_pointers,ptr))
104     g_hash_table_insert(__gst_function_pointers,ptr,ptrname);
105   return ptr;
106 }
107
108 static inline gchar *
109 _gst_debug_nameof_funcptr (void *ptr) 
110 {
111   gchar *ptrname = __gst_function_pointers ? g_hash_table_lookup(__gst_function_pointers,ptr) : NULL;
112 // FIXME this must go away, it's a major leak
113   if (!ptrname) return g_strdup_printf("%p",ptr);
114   else return ptrname;
115 }
116
117
118 /**********************************************************************
119  * The following is a DEBUG_ENTER implementation that will wrap the
120  * function it sits at the head of.  It removes the need for a
121  * DEBUG_LEAVE call.  However, it segfaults whenever it gets anywhere
122  * near cothreads.  We will not use it for the moment.
123  *
124 typedef void (*_debug_function_f)();
125 G_GNUC_UNUSED static gchar *_debug_string_pointer = NULL;
126 G_GNUC_UNUSED static GModule *_debug_self_module = NULL;
127
128 #define _DEBUG_ENTER_BUILTIN(format,args...)                                            \
129   static int _debug_in_wrapper = 0;                                                     \
130   gchar *_debug_string = ({                                                             \
131     if (!_debug_in_wrapper) {                                                           \
132       void *_return_value;                                                              \
133       gchar *_debug_string;                                                             \
134       _debug_function_f function;                                                       \
135       void *_function_args = __builtin_apply_args();                                    \
136       _debug_in_wrapper = 1;                                                            \
137       _debug_string = g_strdup_printf(GST_DEBUG_PREFIX(""));                            \
138       _debug_string_pointer = _debug_string;                                            \
139       fprintf(stderr,"%s: entered " __PRETTY_FUNCTION__ format "\n" , _debug_string , ## args ); \
140       if (_debug_self_module == NULL) _debug_self_module = g_module_open(NULL,0);       \
141       g_module_symbol(_debug_self_module,__FUNCTION__,(gpointer *)&function);           \
142       _return_value = __builtin_apply(function,_function_args,64);                      \
143       fprintf(stderr,"%s: left " __PRETTY_FUNCTION__ format "\n" , _debug_string , ## args ); \
144       g_free(_debug_string);                                                            \
145       __builtin_return(_return_value);                                                  \
146     } else {                                                                            \
147       _debug_in_wrapper = 0;                                                            \
148     }                                                                                   \
149     _debug_string_pointer;                                                              \
150   });
151
152 * WARNING: there's a gcc CPP bug lurking in here.  The extra space before the ##args    *
153  * somehow make the preprocessor leave the _debug_string. If it's removed, the          *
154  * _debug_string somehow gets stripped along with the ##args, and that's all she wrote. *
155 #define _DEBUG_BUILTIN(format,args...)                          \
156   if (_debug_string != (void *)-1) {                            \
157     if (_debug_string)                                          \
158       fprintf(stderr,"%s: " format , _debug_string , ## args);  \
159     else                                                        \
160       fprintf(stderr,GST_DEBUG_PREFIX(": " format , ## args));  \
161   }
162
163 */
164
165
166
167 /**********************************************************************
168  * INFO system
169  **********************************************************************/
170
171 typedef void (*GstInfoHandler) (gint category,gchar *file,gchar *function,
172                                 gint line,gchar *debug_string,
173                                 void *element,gchar *string);
174
175 void gst_default_info_handler (gint category,gchar *file,gchar *function,
176                                gint line,gchar *debug_string,
177                                void *element,gchar *string);
178
179 extern GstInfoHandler _gst_info_handler;
180 extern guint32 _gst_info_categories;
181
182 /* for include files that make too much noise normally */
183 #ifdef GST_INFO_FORCE_DISABLE
184 #undef GST_INFO_ENABLED
185 #endif
186 /* for applications that really really want all the noise */
187 #ifdef GST_INFO_FORCE_ENABLE
188 #define GST_INFO_ENABLED
189 #endif
190
191 #ifdef GST_INFO_ENABLED
192 #define INFO(cat,format,args...) G_STMT_START{ \
193   if ((1<<cat) & _gst_info_categories) \
194     _gst_info_handler(cat,__FILE__,__PRETTY_FUNCTION__,__LINE__,_debug_string, \
195                       NULL,g_strdup_printf( format , ## args )); \
196 }G_STMT_END
197
198 #define INFO_ELEMENT(cat,element,format,args...) G_STMT_START{ \
199   if ((1<<cat) & _gst_info_categories) \
200     _gst_info_handler(cat,__FILE__,__PRETTY_FUNCTION__,__LINE__,_debug_string, \
201                       element,g_strdup_printf( format , ## args )); \
202 }G_STMT_END
203
204 #else
205 #define INFO(cat,format,args...) 
206 #define INFO_ELEMENT(cat,element,format,args...)
207 #endif
208
209 void            gst_info_set_categories         (guint32 categories);
210 guint32         gst_info_get_categories         (void);
211 const gchar *   gst_info_get_category_name      (gint category);
212 void            gst_info_enable_category        (gint category);
213 void            gst_info_disable_category       (gint category);
214
215 enum {
216   GST_INFO_GST_INIT = 0,        // Library initialization
217   GST_INFO_COTHREADS,           // Cothread creation, etc.
218   GST_INFO_COTHREAD_SWITCH,     // Cothread switching
219   GST_INFO_AUTOPLUG,            // Successful autoplug results
220   GST_INFO_AUTOPLUG_ATTEMPT,    // Attempted autoplug operations
221   GST_INFO_PARENTAGE,           // GstBin parentage issues
222   GST_INFO_STATES,              // State changes and such
223   GST_INFO_PLANNING,            // Plan generation
224   GST_INFO_SCHEDULING,          // Schedule construction
225   GST_INFO_OPERATION,           // Events during actual data movement
226   GST_INFO_BUFFER,              // Buffer creation/destruction
227   GST_INFO_CAPS,                // Capabilities matching
228   GST_INFO_CLOCK,               // Clocking
229   GST_INFO_ELEMENT_PADS,        // Element pad management
230   GST_INFO_ELEMENTFACTORY,      // Elementfactory stuff
231   GST_INFO_PADS,                // Pad creation/connection
232   GST_INFO_PIPELINE,            // Pipeline stuff
233   GST_INFO_PLUGIN_LOADING,      // Plugin loading
234   GST_INFO_PLUGIN_ERRORS,       // Errors during plugin loading
235   GST_INFO_PROPERTIES,          // Properties
236   GST_INFO_THREAD,              // Thread creation/management
237   GST_INFO_TYPES,               // Typing
238   GST_INFO_XML,                 // XML load/save of everything
239 };
240
241
242
243
244 /**********************************************************************
245  * ERROR system
246  **********************************************************************/
247
248 typedef void (*GstErrorHandler) (gchar *file,gchar *function,
249                                  gint line,gchar *debug_string,
250                                  void *element,void *object,gchar *string);
251
252 void gst_default_error_handler (gchar *file,gchar *function,
253                                 gint line,gchar *debug_string,
254                                 void *element,void *object,gchar *string);
255
256 extern GstErrorHandler _gst_error_handler;
257
258 #define ERROR(element,format,args...) \
259   _gst_error_handler(__FILE__,__PRETTY_FUNCTION__,__LINE__,_debug_string, \
260                      element,NULL,g_strdup_printf( format , ## args ))
261
262 #define ERROR_OBJECT(element,object,format,args...) \
263   _gst_error_handler(__FILE__,__PRETTY_FUNCTION__,__LINE__,_debug_string, \
264                      element,object,g_strdup_printf( format , ## args ))
265
266
267
268 #endif /* __GSTINFO_H__ */