2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstinfo.c: INFO, ERROR, and DEBUG systems
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.
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.
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.
24 #include "gst_private.h"
25 #include "gstelement.h"
28 extern gchar *_gst_progname;
31 /***** Categories and colorization *****/
32 static gchar *_gst_info_category_strings[] = {
66 * gst_get_category_name:
67 * @category: the category to return the name of
69 * Returns: string containing the name of the category
72 gst_get_category_name (gint category) {
73 if ((category >= 0) && (category < GST_CAT_MAX_CATEGORY))
74 return _gst_info_category_strings[category];
82 * 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
84 * 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
85 * Background color codes:
86 * 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
88 const gchar *_gst_category_colors[32] = {
89 [GST_CAT_GST_INIT] = "07;37",
90 [GST_CAT_COTHREADS] = "00;32",
91 [GST_CAT_COTHREAD_SWITCH] = "00;37;42",
92 [GST_CAT_AUTOPLUG] = "00;34",
93 [GST_CAT_AUTOPLUG_ATTEMPT] = "00;36;44",
94 [GST_CAT_PARENTAGE] = "01;37;41", // !!
95 [GST_CAT_STATES] = "00;31",
96 [GST_CAT_PLANNING] = "07;35",
97 [GST_CAT_SCHEDULING] = "00;35",
98 [GST_CAT_DATAFLOW] = "00;32",
99 [GST_CAT_BUFFER] = "00;32",
100 [GST_CAT_CAPS] = "04;34",
101 [GST_CAT_CLOCK] = "00;33", // !!
102 [GST_CAT_ELEMENT_PADS] = "01;37;41", // !!
103 [GST_CAT_ELEMENTFACTORY] = "01;37;41", // !!
104 [GST_CAT_PADS] = "01;37;41", // !!
105 [GST_CAT_PIPELINE] = "01;37;41", // !!
106 [GST_CAT_PLUGIN_LOADING] = "00;36",
107 [GST_CAT_PLUGIN_ERRORS] = "05;31",
108 [GST_CAT_PLUGIN_INFO] = "00;36",
109 [GST_CAT_PROPERTIES] = "00;37;44", // !!
110 [GST_CAT_THREAD] = "00;31",
111 [GST_CAT_TYPES] = "01;37;41", // !!
112 [GST_CAT_XML] = "01;37;41", // !!
113 [GST_CAT_NEGOTIATION] = "07;34",
114 [GST_CAT_REFCOUNTING] = "00;34:42",
115 [GST_CAT_EVENT] = "01;37;41", // !!
116 [GST_CAT_PARAMS] = "00;30;43", // !!
118 [GST_CAT_CALL_TRACE] = "",
121 /* colorization hash - DEPRACATED in favor of above */
122 inline gint _gst_debug_stringhash_color(gchar *file) {
124 while (file[0]) filecolor += *(char *)(file++);
125 filecolor = (filecolor % 6) + 31;
131 /***** DEBUG system *****/
132 GstDebugHandler _gst_debug_handler = gst_default_debug_handler;
133 guint32 _gst_debug_categories = 0x00000000;
136 * gst_default_debug_handler:
137 * @category: category of the DEBUG message
138 * @incore: if the debug handler is for core code.
139 * @file: the file the DEBUG occurs in
140 * @function: the function the DEBUG occurs in
141 * @line: the line number in the file
142 * @debug_string: the current debug_string in the function, if any
143 * @element: pointer to the #GstElement in question
144 * @string: the actual DEBUG string
146 * Prints out the DEBUG mesage in a variant of the following form:
148 * DEBUG(pid:cid):gst_function:542(args): [elementname] something neat happened
151 gst_default_debug_handler (gint category, gboolean incore,
152 const gchar *file, const gchar *function,
153 gint line, const gchar *debug_string,
154 void *element, gchar *string)
155 __attribute__ ((no_instrument_function));
158 gst_default_debug_handler (gint category, gboolean incore,
159 const gchar *file, const gchar *function,
160 gint line, const gchar *debug_string,
161 void *element, gchar *string)
164 gchar *elementname = empty,*location = empty;
165 int pthread_id = getpid();
166 int cothread_id = cothread_getcurrent();
167 #ifdef GST_DEBUG_COLOR
168 int pthread_color = pthread_id%6 + 31;
169 int cothread_color = (cothread_id < 0) ? 37 : (cothread_id%6 + 31);
172 if (debug_string == NULL) debug_string = "";
173 // if (category != GST_CAT_GST_INIT)
174 location = g_strdup_printf("%s:%d%s:",function,line,debug_string);
175 if (element && GST_IS_ELEMENT (element))
176 #ifdef GST_DEBUG_COLOR
177 elementname = g_strdup_printf (" \033[04m[%s]\033[00m", GST_OBJECT_NAME (element));
179 elementname = g_strdup_printf (" [%s]", GST_OBJECT_NAME (element));
182 #ifdef GST_DEBUG_COLOR
183 fprintf(stderr,"DEBUG(\033[00;%dm%5d\033[00m:\033[00;%dm%2d\033[00m)\033["
184 "%s;%sm%s%s\033[00m %s",
185 pthread_color,pthread_id,cothread_color,cothread_id,incore?"00":"01",
186 _gst_category_colors[category],location,elementname,string);
188 fprintf(stderr,"DEBUG(%5d:%2d)%s%s %s",
189 pthread_id,cothread_id,location,elementname,string);
190 #endif /* GST_DEBUG_COLOR */
192 if (location != empty) g_free(location);
193 if (elementname != empty) g_free(elementname);
200 * gst_debug_set_categories:
201 * @categories: bitmask of DEBUG categories to enable
203 * Enable the output of DEBUG categories based on the given bitmask.
204 * The bit for any given category is (1 << GST_CAT_...).
207 gst_debug_set_categories (guint32 categories) {
208 _gst_debug_categories = categories;
210 GST_INFO (0, "setting DEBUG categories to 0x%08X",categories);
214 * gst_debug_get_categories:
216 * Returns: the current bitmask of enabled DEBUG categories
217 * The bit for any given category is (1 << GST_CAT_...).
220 gst_debug_get_categories () {
221 return _gst_debug_categories;
225 * gst_debug_enable_category:
226 * @category: the category to enable
228 * Enables the given GST_CAT_... DEBUG category.
231 gst_debug_enable_category (gint category) {
232 _gst_debug_categories |= (1 << category);
233 if (_gst_debug_categories)
234 GST_INFO (0, "setting DEBUG categories to 0x%08X",_gst_debug_categories);
238 * gst_debug_disable_category:
239 * @category: the category to disable
241 * Disables the given GST_CAT_... DEBUG category.
244 gst_debug_disable_category (gint category) {
245 _gst_debug_categories &= ~ (1 << category);
246 if (_gst_debug_categories)
247 GST_INFO (0, "setting DEBUG categories to 0x%08X",_gst_debug_categories);
253 /***** INFO system *****/
254 GstInfoHandler _gst_info_handler = gst_default_info_handler;
255 guint32 _gst_info_categories = 0x00000001;
259 * gst_default_info_handler:
260 * @category: category of the INFO message
261 * @incore: if the info handler is for core code.
262 * @file: the file the INFO occurs in
263 * @function: the function the INFO occurs in
264 * @line: the line number in the file
265 * @debug_string: the current debug_string in the function, if any
266 * @element: pointer to the #GstElement in question
267 * @string: the actual INFO string
269 * Prints out the INFO mesage in a variant of the following form:
271 * INFO:gst_function:542(args): [elementname] something neat happened
274 gst_default_info_handler (gint category, gboolean incore,
275 const gchar *file, const gchar *function,
276 gint line, const gchar *debug_string,
277 void *element, gchar *string)
280 gchar *elementname = empty,*location = empty;
281 int pthread_id = getpid();
282 int cothread_id = cothread_getcurrent();
283 #ifdef GST_DEBUG_COLOR
284 int pthread_color = pthread_id%6 + 31;
285 int cothread_color = (cothread_id < 0) ? 37 : (cothread_id%6 + 31);
288 if (debug_string == NULL) debug_string = "";
289 if (category != GST_CAT_GST_INIT)
290 location = g_strdup_printf("%s:%d%s:",function,line,debug_string);
291 if (element && GST_IS_ELEMENT (element))
292 elementname = g_strdup_printf (" \033[04m[%s]\033[00m", GST_OBJECT_NAME (element));
295 #ifdef GST_DEBUG_ENABLED
297 #ifdef GST_DEBUG_COLOR
298 fprintf(stderr,"\033[01mINFO\033[00m (\033[00;%dm%5d\033[00m:\033[00;%dm%2d\033[00m)\033["
299 GST_DEBUG_CHAR_MODE ";%sm%s%s\033[00m %s\n",
300 pthread_color,pthread_id,cothread_color,cothread_id,
301 _gst_category_colors[category],location,elementname,string);
303 fprintf(stderr,"INFO (%5d:%2d)%s%s %s\n",
304 pthread_id,cothread_id,location,elementname,string);
305 #endif // GST_DEBUG_COLOR
308 #ifdef GST_DEBUG_COLOR
309 fprintf(stderr,"\033[01mINFO\033[00m:\033[" GST_DEBUG_CHAR_MODE ";%sm%s%s\033[00m %s\n",
310 location,elementname,_gst_category_colors[category],string);
312 fprintf(stderr,"INFO:%s%s %s\n",
313 location,elementname,string);
314 #endif // GST_DEBUG_COLOR
318 if (location != empty) g_free(location);
319 if (elementname != empty) g_free(elementname);
325 * gst_info_set_categories:
326 * @categories: bitmask of INFO categories to enable
328 * Enable the output of INFO categories based on the given bitmask.
329 * The bit for any given category is (1 << GST_CAT_...).
332 gst_info_set_categories (guint32 categories) {
333 _gst_info_categories = categories;
335 GST_INFO (0, "setting INFO categories to 0x%08X",categories);
339 * gst_info_get_categories:
341 * Returns: the current bitmask of enabled INFO categories
342 * The bit for any given category is (1 << GST_CAT_...).
345 gst_info_get_categories () {
346 return _gst_info_categories;
350 * gst_info_enable_category:
351 * @category: the category to enable
353 * Enables the given GST_CAT_... INFO category.
356 gst_info_enable_category (gint category) {
357 _gst_info_categories |= (1 << category);
358 if (_gst_info_categories)
359 GST_INFO (0, "setting INFO categories to 0x%08X",_gst_info_categories);
363 * gst_info_disable_category:
364 * @category: the category to disable
366 * Disables the given GST_CAT_... INFO category.
369 gst_info_disable_category (gint category) {
370 _gst_info_categories &= ~ (1 << category);
371 if (_gst_info_categories)
372 GST_INFO (0, "setting INFO categories to 0x%08X",_gst_info_categories);
377 /***** ERROR system *****/
378 GstErrorHandler _gst_error_handler = gst_default_error_handler;
381 * gst_default_error_handler:
382 * @file: the file the ERROR occurs in
383 * @function: the function the INFO occurs in
384 * @line: the line number in the file
385 * @debug_string: the current debug_string in the function, if any
386 * @element: pointer to the #GstElement in question
387 * @object: pointer to a related object
388 * @string: the actual ERROR string
390 * Prints out the given ERROR string in a variant of the following format:
392 * ***** GStreamer ERROR ***** in file gstsomething.c at gst_function:399(arg)
393 * Element: /pipeline/thread/element.src
394 * Error: peer is null!
395 * ***** attempting to stack trace.... *****
397 * At the end, it attempts to print the stack trace via GDB.
400 gst_default_error_handler (gchar *file, gchar *function,
401 gint line, gchar *debug_string,
402 void *element, void *object, gchar *string)
408 // if there are NULL pointers, point them to null strings to clean up output
409 if (!debug_string) debug_string = "";
410 if (!string) string = "";
412 // print out a preamble
413 fprintf(stderr,"***** GStreamer ERROR ***** in file %s at %s:%d%s\n",
414 file,function,line,debug_string);
416 // if there's an element, print out the pertinent information
418 if (GST_IS_OBJECT(element)) {
419 path = gst_object_get_path_string(element);
420 fprintf(stderr,"Element: %s",path);
421 chars = 9 + strlen(path);
424 fprintf(stderr,"Element ptr: %p",element);
425 chars = 15 + sizeof(void*)*2;
429 // if there's an object, print it out as well
431 // attempt to pad the line, or create a new one
433 for (i=0;i<(40-chars)/8+1;i++) fprintf(stderr,"\t");
435 fprintf(stderr,"\n");
437 if (GST_IS_OBJECT(object)) {
438 path = gst_object_get_path_string(object);
439 fprintf(stderr,"Object: %s",path);
442 fprintf(stderr,"Object ptr: %p",object);
446 fprintf(stderr,"\n");
447 fprintf(stderr,"Error: %s\n",string);
451 fprintf(stderr,"***** attempting to stack trace.... *****\n");
453 g_on_error_stack_trace (_gst_progname);
460 /***** DEBUG system *****/
461 GHashTable *__gst_function_pointers = NULL;
462 // FIXME make this thread specific
463 static GSList* stack_trace = NULL;
465 gchar *_gst_debug_nameof_funcptr (void *ptr) __attribute__ ((no_instrument_function));
468 _gst_debug_nameof_funcptr (void *ptr)
472 if (__gst_function_pointers && (ptrname = g_hash_table_lookup(__gst_function_pointers,ptr))) {
473 return g_strdup(ptrname);
474 } else if (dladdr(ptr,&dlinfo) && dlinfo.dli_sname) {
475 return g_strdup(dlinfo.dli_sname);
477 return g_strdup_printf("%p",ptr);
483 #ifdef GST_ENABLE_FUNC_INSTRUMENTATION
484 void __cyg_profile_func_enter(void *this_fn,void *call_site) __attribute__ ((no_instrument_function));
485 void __cyg_profile_func_enter(void *this_fn,void *call_site)
487 gchar *name = _gst_debug_nameof_funcptr (this_fn);
488 gchar *site = _gst_debug_nameof_funcptr (call_site);
490 GST_DEBUG(GST_CAT_CALL_TRACE, "entering function %s from %s\n", name, site);
491 stack_trace = g_slist_prepend (stack_trace, g_strdup_printf ("%8p in %s from %p (%s)", this_fn, name, call_site, site));
497 void __cyg_profile_func_exit(void *this_fn,void *call_site) __attribute__ ((no_instrument_function));
498 void __cyg_profile_func_exit(void *this_fn,void *call_site)
500 gchar *name = _gst_debug_nameof_funcptr (this_fn);
502 GST_DEBUG(GST_CAT_CALL_TRACE, "leaving function %s\n", name);
503 g_free (stack_trace->data);
504 stack_trace = g_slist_delete_link (stack_trace, stack_trace);
510 gst_debug_print_stack_trace (void)
512 GSList *walk = stack_trace;
516 walk = g_slist_next (walk);
519 gchar *name = (gchar *) walk->data;
521 g_print ("#%-2d %s\n", count++, name);
523 walk = g_slist_next (walk);
528 gst_debug_print_stack_trace (void)
532 #endif /* GST_ENABLE_FUNC_INTSTRUMENTATION */