radeon: Add common debugging functions.
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_debug.h
1 /*
2 * Copyright © 2009 Pauli Nieminen
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 */
25 /*
26 * Authors:
27 * Pauli Nieminen <suokkos@gmail.com>
28 */
29
30 #ifndef RADEON_DEBUG_H_INCLUDED
31 #define RADEON_DEBUG_H_INCLUDED
32
33 #include <stdarg.h>
34 #include <stdio.h>
35
36 typedef enum radeon_debug_levels {
37 RADEON_CRITICAL = 0, /* Only errors */
38 RADEON_IMPORTANT = 1, /* Important warnings and messages */
39 RADEON_NORMAL = 2, /* Normal log messages usefull for debugging */
40 RADEON_VERBOSE = 3, /* Extra details to debugging */
41 RADEON_TRACE = 4 /* Log about everything that happens */
42 } radeon_debug_level_t;
43
44 /**
45 * Compile time option to change level of debugging compiled to dri driver.
46 */
47 #ifndef RADEON_DEBUG_LEVEL
48 #define RADEON_DEBUG_LEVEL RADEON_NORMAL
49 #endif
50
51 typedef enum radeon_debug_types {
52 RADEON_TEXTURE = 0x0001,
53 RADEON_STATE = 0x0002,
54 RADEON_IOCTL = 0x0004,
55 RADEON_RENDER = 0x0008,
56 RADEON_SWRENDER = 0x0010,
57 RADEON_FALLBACKS = 0x0020,
58 RADEON_VFMT = 0x0040,
59 RADEON_SHADER = 0x0080,
60 RADEON_CS = 0x0100,
61 RADEON_DRI = 0x0200,
62 RADEON_DMA = 0x0400,
63 RADEON_SANITY = 0x0800,
64 RADEON_SYNC = 0x1000,
65 RADEON_PIXEL = 0x2000,
66 RADEON_MEMORY = 0x4000,
67 RADEON_GENERAL = 0x8000 /* Used for errors and warnings */
68 } radeon_debug_type_t;
69
70 extern radeon_debug_type_t radeon_enabled_debug_types;
71
72 /**
73 * Compabibility layer for old debug code
74 **/
75 #define RADEON_DEBUG radeon_enabled_debug_types
76
77 static inline int radeon_is_debug_enabled(const radeon_debug_type_t type,
78 const radeon_debug_level_t level)
79 {
80 return RADEON_DEBUG_LEVEL <= level
81 && (type & radeon_enabled_debug_types);
82 }
83
84 /**
85 * Print out debug message if channel specified by type is enabled
86 * and compile time debugging level is at least as high as level parameter
87 */
88 static inline void radeon_print(const radeon_debug_type_t type,
89 const radeon_debug_level_t level,
90 const char* message,
91 ...)
92 {
93 /* Compile out if level of message is too high */
94 if (radeon_is_debug_enabled(type, level)) {
95
96 va_list values;
97 va_start( values, message );
98 vfprintf(stderr, message, values);
99 va_end( values );
100 }
101 }
102
103 /**
104 * printf style function for writing error messages.
105 */
106 static inline void radeon_error(const char* message, ...)
107 {
108 va_list values;
109 va_start( values, message );
110 radeon_print(RADEON_GENERAL, RADEON_CRITICAL, message, values);
111 va_end( values );
112 }
113
114 /**
115 * printf style function for writing warnings.
116 */
117 static inline void radeon_warning(const char* message, ...)
118 {
119 va_list values;
120 va_start( values, message );
121 radeon_print(RADEON_GENERAL, RADEON_IMPORTANT, message, values);
122 va_end( values );
123 }
124
125
126 extern void radeon_init_debug(void);
127
128 /* From http://gcc. gnu.org/onlinedocs/gcc-3.2.3/gcc/Variadic-Macros.html .
129 I suppose we could inline this and use macro to fetch out __LINE__ and stuff in case we run into trouble
130 with other compilers ... GLUE!
131 */
132 #define WARN_ONCE(a, ...) { \
133 static int warn##__LINE__=1; \
134 if(warn##__LINE__){ \
135 radeon_warning("*********************************WARN_ONCE*********************************\n"); \
136 radeon_warning("File %s function %s line %d\n", \
137 __FILE__, __FUNCTION__, __LINE__); \
138 radeon_warning( (a), ## __VA_ARGS__);\
139 radeon_warning("***************************************************************************\n"); \
140 warn##__LINE__=0;\
141 } \
142 }
143
144
145 #endif