-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_channels.h
318 lines (277 loc) · 7.54 KB
/
debug_channels.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#ifndef _INTERPRETER_DEBUG_CHANNELS_H_
#define _INTERPRETER_DEBUG_CHANNELS_H_
#include <glibmm.h>
#include <stdarg.h>
#include "util.h"
namespace Interpreter
{
class DebugChannel
{
public:
DebugChannel( const gchar * channel_name, DebugChannel * _parent = 0 )
{
parent = _parent;
explicitly_enabled = false;
name = channel_name;
}
~DebugChannel()
{
}
void setEnabled( bool v )
{
// printf("%s::setEnabled(%s)\n",name.c_str(),(v?"true":"false"));
explicitly_enabled = v;
}
void print( const gchar * format , ... )
{
va_list L;
va_start( L,format );
if( parent && ! explicitly_enabled ) parent->print_owned( name.c_str(), format, L );
else if( explicitly_enabled )
{
gchar fnew[1024];
gchar nbuf[256];
snprintf( nbuf,256,"%s ",name.c_str());
snprintf( fnew, 1024, "%s%s", prefix.empty() ? nbuf : prefix.c_str(), format );
g_vprintf( fnew , L );
}
va_end( L );
}
void setPrefix( Glib::ustring _prefix ) { prefix = _prefix; }
protected:
bool explicitly_enabled;
Glib::ustring name;
DebugChannel * parent;
Glib::ustring prefix;
void print_owned( const gchar * owned , const gchar * format , ... )
{
va_list L;
va_start( L,format );
if( parent && ! explicitly_enabled ) parent->print_owned( owned, format, L );
else if( explicitly_enabled )
{
gchar fnew[1024];
snprintf( fnew, 1024, "%s:%s", name.c_str(), format );
g_vprintf( fnew , L );
}
va_end( L );
}
};
class Debug
{
static Debug * _globalDebugInstance;
public:
Debug()
{
channel_hierarchy = g_hash_table_new( g_str_hash, g_str_equal );
}
~Debug()
{
}
// can be a single name like "library_load"
// or "library_load.library", the second shows the hierarchy,
// this means that you can disable "library" and it will disable library_load
// or you can enable library, and it will enable all subchannels,
// though enabling library_load will not enable all library.
//
// other reserved characters are : , # ; and '
// reasoning: , is to split channels. ( x , y , x )
// reasoning: # is used, below.
// reasoining: ' : ; are reserved for future use.
//
// to implement, shornames: #input lineinput.root
// this means "make a link name input, which refers to lineinput.root
//
static DebugChannel * addChannel( Glib::ustring name )
{
initIfMissing();
return _globalDebugInstance->_globalAddDebugChannel( name );
}
static void setEnabledChannels( const char * list )
{
initIfMissing();
_globalDebugInstance->_globalSetEnabledChannels( list );
}
static void print( Glib::ustring channel, Glib::ustring message )
{
initIfMissing();
}
protected:
// List channels
GHashTable * channel_hierarchy;
typedef struct _internal_pair
{
DebugChannel * c;
GHashTable * t;
} InternalPair;
void _globalSetEnabledChannels( Glib::ustring list )
{
// TODO: split list.
GSList * in_order_l = split_top( list );
GSList * l = g_slist_reverse( in_order_l );
DebugChannel * to_enable = _getChannel( l );
if( to_enable)
{
to_enable->setEnabled( true );
}
}
DebugChannel * _getChannel( GSList * id )
{
DebugChannel * r = NULL;
GHashTable * curTable = channel_hierarchy;
int pos = 0;
while( pos <g_slist_length( id ))
{
Glib::ustring * c = (Glib::ustring*) g_slist_nth_data( id, pos );
const gchar * key = c->c_str();
gpointer v;
if( ( v = g_hash_table_lookup( curTable, key ) ) != NULL )
{
InternalPair *p = (InternalPair*)v;
if( pos + 1 < g_slist_length( id ))
{
if( p->t == NULL )
{
g_print("Debug::_getChannel( %s ): %s is not a channel group.\n", print_slist( id ) , key );
break;
}
curTable = p->t;
}
else // end
r = p->c;
}
else
{
g_print("Debug::_getChannel( %s ): %s doesn't exist.\n",
print_slist( id ) , key );
break;
}
pos++;
}
return r;
}
const gchar * print_slist( GSList * id )
{
int pos = 0;
GSList * tmp = NULL;
while( pos <g_slist_length( id ))
{
Glib::ustring * c = (Glib::ustring*) g_slist_nth_data( id, pos );
const gchar * name = c->c_str();
tmp = g_slist_append( tmp , strdup(name));
if( pos + 1 < g_slist_length( id ))
tmp = g_slist_append( tmp , strdup("." ));
pos++;
}
const char * ret = sprint_gchar_slist( tmp );
// free tmp list.
pos = 0;
while( pos <g_slist_length( tmp ))
{
gchar * c = (gchar*) g_slist_nth_data( tmp, pos );
free( c );
pos++;
}
g_slist_free( tmp );
return ret;
}
DebugChannel * _globalAddDebugChannel( Glib::ustring name )
{
DebugChannel * final = NULL;
DebugChannel * lastChannel = NULL;
GSList * in_order_l = split_top( name );
GSList * l = g_slist_reverse( in_order_l );
GHashTable * curTable = channel_hierarchy;
// printf("_globalAddDebugChannel(%s)\n",name.c_str());
while( g_slist_length( l ) != 0 )
{
Glib::ustring * c = (Glib::ustring*) g_slist_nth_data( l, 0 );
const gchar * key = c->c_str();
gpointer v;
// if key exists
if( ( v = g_hash_table_lookup( curTable, key ) ) != NULL )
{
InternalPair *p = (InternalPair*)v;
// last item, and key is a group
if( g_slist_length( l ) == 1 && p->t != NULL )
{
final = p->c;
g_printf("Redefinition of already existing channel group: %s\n", c->c_str());
}
else if( g_slist_length( l ) == 1 )
{
final = p->c;
g_printf("Redefinition of already existing channel: %s\n", c->c_str());
}
else // just traversing the tree.
{
if( p->t == NULL ) // change channel to group
{
p->t = g_hash_table_new( g_str_hash, g_str_equal );
// g_printf("*Created new channel, %s\n",key);
}
lastChannel = p->c;
curTable = p->t;
}
}
else // doesn't exist.
{
gchar * new_key = strdup( key );
InternalPair * new_pair = (InternalPair*)malloc( sizeof(InternalPair));
memset( new_pair,0,sizeof( InternalPair));
new_pair->t = NULL;
new_pair->c = new DebugChannel( key,lastChannel );
g_hash_table_insert( curTable, new_key, new_pair );
if( g_slist_length( l ) == 1 )
{
// g_printf("*Created new channel, %s\n",key);
final = new_pair->c;
}
else
{
// g_printf("*Created new group, %s\n",key);
GHashTable *nh = g_hash_table_new( g_str_hash, g_str_equal );
new_pair->t = nh;
curTable = new_pair->t;
lastChannel = new_pair->c;
}
}
// g_printf("List size: %i\n",g_slist_length( l ));
l = g_slist_remove( l, c );
delete c;
//g_printf("List size: %i\n",g_slist_length( l ));
}
return final;
}
static void initIfMissing()
{
if( _globalDebugInstance == NULL )
{
_globalDebugInstance = new Debug();
}
}
inline GSList * split_top( Glib::ustring in )
{
GSList * s = NULL; //g_slist_alloc();
// printf("Begin: size %i\n",g_slist_length( s ));
Glib::ustring::size_type p = 0;
int c = 0;
Glib::ustring::size_type l = -1;
// printf("Searching in [%s]\n",in.c_str());
for( ;(l = in.find( ".", p )) > 0 && c < 10; c++ )
{
// printf("%i:%i,%i\n",c,l,p);
Glib::ustring *small = new Glib::ustring(in.substr( p,l-p));
// printf("%i:%i,%i [%s]\n",c,l,p, small->c_str());
s = g_slist_append( s , small );
if( l != Glib::ustring::npos ) p = l+1;
else
break;
}
// printf("End: size %i\n",g_slist_length( s ));
return s;
}
};
Debug *Debug::_globalDebugInstance = NULL;
}
#endif // _INTERPRETER_DEBUG_CHANNELS_H_