1 #ifndef __GEGL_DEBUG_H__
2 #define __GEGL_DEBUG_H__
3 
4 #include <glib.h>
5 
6 G_BEGIN_DECLS
7 
8 extern guint gegl_debug_flags;
9 
10 typedef enum {
11   GEGL_DEBUG_PROCESS         = 1 << 0,
12   GEGL_DEBUG_BUFFER_LOAD     = 1 << 1,
13   GEGL_DEBUG_BUFFER_SAVE     = 1 << 2,
14   GEGL_DEBUG_TILE_BACKEND    = 1 << 3,
15   GEGL_DEBUG_PROCESSOR       = 1 << 4,
16   GEGL_DEBUG_CACHE           = 1 << 5,
17   GEGL_DEBUG_MISC            = 1 << 6,
18   GEGL_DEBUG_INVALIDATION    = 1 << 7,
19   GEGL_DEBUG_OPENCL          = 1 << 8,
20   GEGL_DEBUG_BUFFER_ALLOC    = 1 << 9,
21   GEGL_DEBUG_LICENSE         = 1 << 10
22 } GeglDebugFlag;
23 
24 /* only compiled in from gegl-init.c but kept here to
25  * make it easier to update and keep in sync with the
26  * flags
27  */
28 #ifdef __GEGL_INIT_C
29 const GDebugKey gegl_debug_keys[] = {
30   { "process",       GEGL_DEBUG_PROCESS},
31   { "cache",         GEGL_DEBUG_CACHE},
32   { "buffer-load",   GEGL_DEBUG_BUFFER_LOAD},
33   { "buffer-save",   GEGL_DEBUG_BUFFER_SAVE},
34   { "tile-backend",  GEGL_DEBUG_TILE_BACKEND},
35   { "processor",     GEGL_DEBUG_PROCESSOR},
36   { "invalidation",  GEGL_DEBUG_INVALIDATION},
37   { "opencl",        GEGL_DEBUG_OPENCL},
38   { "buffer-alloc",  GEGL_DEBUG_BUFFER_ALLOC},
39   { "license",       GEGL_DEBUG_LICENSE},
40   { "all",           GEGL_DEBUG_PROCESS|
41                      GEGL_DEBUG_BUFFER_LOAD|
42                      GEGL_DEBUG_BUFFER_SAVE|
43                      GEGL_DEBUG_TILE_BACKEND|
44                      GEGL_DEBUG_PROCESSOR|
45                      GEGL_DEBUG_CACHE|
46                      GEGL_DEBUG_OPENCL|
47                      GEGL_DEBUG_BUFFER_ALLOC|
48                      GEGL_DEBUG_LICENSE},
49 };
50 #else
51 extern GDebugKey gegl_debug_keys[];
52 #endif /* __GEGL_INIT_C */
53 
54 #if defined(__cplusplus) && defined(GEGL_ISO_CXX_VARIADIC_MACROS)
55 #  define GEGL_ISO_VARIADIC_MACROS 1
56 #endif
57 
58 #ifdef GEGL_ENABLE_DEBUG
59 
60 static inline
gegl_get_timestamp(void)61 gint64 gegl_get_timestamp(void)
62 {
63 	return g_get_monotonic_time();
64 }
65 
66 #if defined(GEGL_ISO_VARIADIC_MACROS)
67 
68 /* Use the C99 version; unfortunately, this does not allow us to pass
69  * empty arguments to the macro, which means we have to
70  * do an intemediate printf.
71  */
72 #define GEGL_NOTE(type,...)               G_STMT_START {        \
73         if (gegl_debug_flags & type)                            \
74 	{                                                       \
75 	  gchar * _fmt = g_strdup_printf (__VA_ARGS__);         \
76           g_message ("[" #type "] " G_STRLOC ": %s",_fmt);      \
77           g_free (_fmt);                                        \
78 	}                                                       \
79                                                 } G_STMT_END
80 
81 #define GEGL_TIMESTAMP(type,...)             G_STMT_START {     \
82         if (gegl_debug_flags & type)                            \
83 	{                                                       \
84 	  gchar * _fmt = g_strdup_printf (__VA_ARGS__);         \
85           g_message ("[" #type "]" " %"G_GINT64_FORMAT":"  G_STRLOC ": %s",    \
86                        gegl_get_timestamp(), _fmt);             \
87           g_free (_fmt);                                        \
88 	}                                                       \
89                                                    } G_STMT_END
90 
91 #elif defined(GEGL_GNUC_VARIADIC_MACROS)
92 
93 #define GEGL_NOTE(type,format,a...)          G_STMT_START {     \
94         if (gegl_debug_flags & type)                            \
95           { g_message ("[" #type "] " G_STRLOC ": "             \
96                        format, ##a); }                          \
97                                                 } G_STMT_END
98 
99 #define GEGL_TIMESTAMP(type,format,a...)        G_STMT_START {  \
100         if (gegl_debug_flags & type)                            \
101           { g_message ("[" #type "]" " %"G_GINT64_FORMAT":"  G_STRLOC ": "     \
102                        format, gegl_get_timestamp(), ##a); }    \
103                                                    } G_STMT_END
104 
105 #else
106 
107 #include <stdarg.h>
108 
109 static const gchar *
gegl_lookup_debug_string(guint type)110 gegl_lookup_debug_string  (guint type)
111 {
112   const gchar *key = "!INVALID!";
113   guint i = 0;
114 
115   if (type == GEGL_DEBUG_MISC)
116     {
117       key = "misc";
118     }
119   else
120     {
121       while (g_strcmp0 (gegl_debug_keys[i].key, "all") != 0)
122         {
123           if (gegl_debug_keys[i].value == type)
124             {
125               key = gegl_debug_keys[i].key;
126               break;
127             }
128           i++;
129         }
130     }
131 
132   return key;
133 }
134 
135 static inline void
GEGL_NOTE(guint type,const char * format,...)136 GEGL_NOTE (guint type, const char *format, ...)
137 {
138   va_list args;
139 
140   if (gegl_debug_flags & type)
141   {
142     gchar *formatted;
143     va_start (args, format);
144     formatted = g_strdup_printf (format, args);
145     g_message ("[ %s ] " G_STRLOC ": %s",
146                gegl_lookup_debug_string (type), formatted);
147     g_free (formatted);
148     va_end (args);
149   }
150 }
151 
152 static inline void
GEGL_TIMESTAMP(guint type,const char * format,...)153 GEGL_TIMESTAMP (guint type, const char *format, ...)
154 {
155   va_list args;
156 
157   if (gegl_debug_flags & type)
158   {
159     gchar *formatted;
160     va_start (args, format);
161     formatted = g_strdup_printf (format, args);
162     g_message ("[ %s ] %"G_GINT64_FORMAT": " G_STRLOC ": %s",
163                gegl_lookup_debug_string (type), gegl_get_timestamp(),
164                formatted);
165     g_free (formatted);
166     va_end (args);
167   }
168 }
169 
170 #endif
171 
172 #define GEGL_MARK()      GEGL_NOTE(GEGL_DEBUG_MISC, "== mark ==")
173 #define GEGL_DBG(x) { a }
174 
175 #else /* !GEGL_ENABLE_DEBUG */
176 
177 #if defined(GEGL_ISO_VARIADIC_MACROS)
178 
179 #define GEGL_NOTE(type,...)
180 #define GEGL_TIMESTAMP(type,...)
181 
182 #elif defined(GEGL_GNUC_VARIADIC_MACROS)
183 
184 #define GEGL_NOTE(type,format,a...)
185 #define GEGL_TIMESTAMP(type,format,a...)
186 
187 #else
188 
189 static inline void
GEGL_NOTE(guint type,const char * format,...)190 GEGL_NOTE (guint type, const char *format, ...)
191 {
192   return;
193 }
194 
195 static inline void
GEGL_TIMESTAMP(guint type,const char * format,...)196 GEGL_TIMESTAMP (guint type, const char *format, ...)
197 {
198   return;
199 }
200 
201 #endif
202 
203 #define GEGL_MARK()
204 #define GEGL_DBG(x)
205 
206 #endif /* GEGL_ENABLE_DEBUG */
207 
208 G_END_DECLS
209 
210 #endif /* __GEGL_DEBUG_H__  */
211