1 | // =========================================================== // |
---|
2 | // // |
---|
3 | // File : AW_debug.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in May 2009 // |
---|
7 | // Institute of Microbiology (Technical University Munich) // |
---|
8 | // http://www.arb-home.de/ // |
---|
9 | // // |
---|
10 | // =========================================================== // |
---|
11 | |
---|
12 | |
---|
13 | #include <Xm/Xm.h> |
---|
14 | |
---|
15 | #include "aw_root.hxx" |
---|
16 | #include "aw_window.hxx" |
---|
17 | #include "aw_Xm.hxx" |
---|
18 | #include "aw_window_Xm.hxx" |
---|
19 | #include <arbdbt.h> |
---|
20 | |
---|
21 | #include <vector> |
---|
22 | #include <iterator> |
---|
23 | #include <string> |
---|
24 | #include <algorithm> |
---|
25 | |
---|
26 | // do includes above (otherwise depends depend on DEBUG) |
---|
27 | #if defined(DEBUG) |
---|
28 | // -------------------------------------------------------------------------------- |
---|
29 | |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | typedef vector<string> CallbackArray; |
---|
33 | typedef CallbackArray::const_iterator CallbackIter; |
---|
34 | |
---|
35 | static GB_HASH *dontCallHash = 0; |
---|
36 | static GB_HASH *alreadyCalledHash = 0; |
---|
37 | |
---|
38 | static void forgetCalledCallbacks() { |
---|
39 | if (alreadyCalledHash) GBS_free_hash(alreadyCalledHash); |
---|
40 | alreadyCalledHash = GBS_create_hash(5000, GB_MIND_CASE); |
---|
41 | } |
---|
42 | |
---|
43 | static long auto_dontcall1(const char *key, long value, void *cl_hash) { |
---|
44 | if (strncmp(key, "ARB_NT/", 7) == 0) { |
---|
45 | GB_HASH *autodontCallHash = (GB_HASH*)cl_hash; |
---|
46 | GBS_write_hash(autodontCallHash, GBS_global_string("ARB_NT_1/%s", key+7), value); |
---|
47 | } |
---|
48 | return value; |
---|
49 | } |
---|
50 | static long auto_dontcall2(const char *key, long value, void *) { |
---|
51 | GBS_write_hash(dontCallHash, key, value); |
---|
52 | return value; |
---|
53 | } |
---|
54 | |
---|
55 | static void build_dontCallHash() { |
---|
56 | aw_assert(!dontCallHash); |
---|
57 | dontCallHash = GBS_create_hash(30, GB_MIND_CASE); |
---|
58 | forgetCalledCallbacks(); |
---|
59 | |
---|
60 | GBS_write_hash(dontCallHash, "ARB_NT/QUIT", 1); |
---|
61 | GBS_write_hash(dontCallHash, "quit", 1); |
---|
62 | GBS_write_hash(dontCallHash, "ARB_EDIT4/QUIT", 1); |
---|
63 | GBS_write_hash(dontCallHash, "ARB_INTRO/CANCEL", 1); |
---|
64 | |
---|
65 | // avoid start of some external programs: |
---|
66 | #if 1 |
---|
67 | GBS_write_hash(dontCallHash, "ARB_NT/EDIT_SEQUENCES", 2); |
---|
68 | GBS_write_hash(dontCallHash, "CPR_MAIN/HELP", 2); |
---|
69 | GBS_write_hash(dontCallHash, "GDE__user__Start_a_slave_ARB_on_a_foreign_host_/GO", 2); |
---|
70 | GBS_write_hash(dontCallHash, "HELP/BROWSE", 2); |
---|
71 | GBS_write_hash(dontCallHash, "HELP/EDIT", 2); |
---|
72 | GBS_write_hash(dontCallHash, "MACROS/EDIT", 2); |
---|
73 | GBS_write_hash(dontCallHash, "MACROS/EXECUTE", 2); |
---|
74 | GBS_write_hash(dontCallHash, "NAME_SERVER_ADMIN/EDIT_NAMES_FILE", 2); |
---|
75 | GBS_write_hash(dontCallHash, "arb_dist", 2); |
---|
76 | GBS_write_hash(dontCallHash, "arb_edit", 2); |
---|
77 | GBS_write_hash(dontCallHash, "arb_pars", 2); |
---|
78 | GBS_write_hash(dontCallHash, "arb_pars_quick", 2); |
---|
79 | GBS_write_hash(dontCallHash, "arb_phyl", 2); |
---|
80 | GBS_write_hash(dontCallHash, "count_different_chars", 2); |
---|
81 | GBS_write_hash(dontCallHash, "export_to_ARB", 2); |
---|
82 | GBS_write_hash(dontCallHash, "new2_arb_edit4", 2); |
---|
83 | GBS_write_hash(dontCallHash, "new_arb_edit4", 2); |
---|
84 | GBS_write_hash(dontCallHash, "primer_design", 2); |
---|
85 | GBS_write_hash(dontCallHash, "xterm", 2); |
---|
86 | GBS_write_hash(dontCallHash, "SUBMIT_REG/SEND", 2); |
---|
87 | GBS_write_hash(dontCallHash, "SUBMIT_BUG/SEND", 2); |
---|
88 | GBS_write_hash(dontCallHash, "NAME_SERVER_ADMIN/REMOVE_SUPERFLUOUS_ENTRIES_IN_NAMES_FILE", 2); |
---|
89 | GBS_write_hash(dontCallHash, "PRINT_CANVAS/PRINT", 2); |
---|
90 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/CREATE_TEMPLATE", 2); |
---|
91 | GBS_write_hash(dontCallHash, "SELECT_CONFIFURATION/START", 2); |
---|
92 | #endif |
---|
93 | |
---|
94 | // avoid saving |
---|
95 | GBS_write_hash(dontCallHash, "save_changes", 3); |
---|
96 | GBS_write_hash(dontCallHash, "save_props", 3); |
---|
97 | |
---|
98 | #if 1 |
---|
99 | #warning crashing - fix later |
---|
100 | GBS_write_hash(dontCallHash, "ARB_NT/mark_duplicates", 4); |
---|
101 | GBS_write_hash(dontCallHash, "ARB_NT/view_probe_group_result", 4); |
---|
102 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/CHECK_SERVER", 4); |
---|
103 | #endif |
---|
104 | |
---|
105 | #if 1 |
---|
106 | #warning test callbacks asking questions again later |
---|
107 | GBS_write_hash(dontCallHash, "ARB_NT/mark_deep_branches", 5); |
---|
108 | GBS_write_hash(dontCallHash, "ARB_NT/mark_degen_branches", 5); |
---|
109 | GBS_write_hash(dontCallHash, "ARB_NT/mark_long_branches", 5); |
---|
110 | GBS_write_hash(dontCallHash, "ARB_NT/tree_scale_lengths", 5); |
---|
111 | GBS_write_hash(dontCallHash, "CREATE_USER_MASK/CREATE", 5); |
---|
112 | GBS_write_hash(dontCallHash, "GDE__import__Import_sequences_using_Readseq_slow_/GO", 5); |
---|
113 | GBS_write_hash(dontCallHash, "INFO_OF_ALIGNMENT/DELETE", 5); |
---|
114 | GBS_write_hash(dontCallHash, "LOAD_SELECTION_BOX/LOAD", 5); |
---|
115 | GBS_write_hash(dontCallHash, "MULTI_PROBE/CREATE_NEW_SEQUENCE", 5); |
---|
116 | GBS_write_hash(dontCallHash, "NDS_PROPS/SAVELOAD_CONFIG", 5); |
---|
117 | GBS_write_hash(dontCallHash, "PRIMER_DESIGN/SAVELOAD_CONFIG", 5); |
---|
118 | GBS_write_hash(dontCallHash, "PROBE_DESIGN/SAVELOAD_CONFIG", 5); |
---|
119 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/KILL_ALL_SERVERS", 5); |
---|
120 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/KILL_SERVER", 5); |
---|
121 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/UPDATE_SERVER", 5); |
---|
122 | GBS_write_hash(dontCallHash, "SPECIES_QUERY/DELETE_LISTED", 5); |
---|
123 | GBS_write_hash(dontCallHash, "SPECIES_QUERY/SAVELOAD_CONFIG", 5); |
---|
124 | GBS_write_hash(dontCallHash, "SPECIES_SELECTIONS/RENAME", 5); |
---|
125 | GBS_write_hash(dontCallHash, "SPECIES_SELECTIONS/STORE", 5); |
---|
126 | GBS_write_hash(dontCallHash, "del_marked", 5); |
---|
127 | GBS_write_hash(dontCallHash, "REALIGN_DNA/REALIGN", 5); |
---|
128 | GBS_write_hash(dontCallHash, "TREE_PROPS/SAVELOAD_CONFIG", 5); |
---|
129 | GBS_write_hash(dontCallHash, "WWW_PROPS/SAVELOAD_CONFIG", 5); |
---|
130 | #endif |
---|
131 | |
---|
132 | GB_HASH *autodontCallHash = GBS_create_hash(30, GB_MIND_CASE); |
---|
133 | GBS_hash_do_loop(dontCallHash, auto_dontcall1, autodontCallHash); |
---|
134 | GBS_hash_do_loop(autodontCallHash, auto_dontcall2, dontCallHash); |
---|
135 | GBS_free_hash(autodontCallHash); |
---|
136 | } |
---|
137 | |
---|
138 | static long collect_callbacks(const char *key, long value, void *cl_callbacks) { |
---|
139 | if (GBS_read_hash(alreadyCalledHash, key) == 0) { // don't call twice |
---|
140 | CallbackArray *callbacks = reinterpret_cast<CallbackArray*>(cl_callbacks); |
---|
141 | callbacks->push_back(string(key)); |
---|
142 | } |
---|
143 | return value; |
---|
144 | } |
---|
145 | |
---|
146 | int sortedByCallbackLocation(const char *k0, long v0, const char *k1, long v1) { |
---|
147 | AW_cb_struct *cbs0 = reinterpret_cast<AW_cb_struct*>(v0); |
---|
148 | AW_cb_struct *cbs1 = reinterpret_cast<AW_cb_struct*>(v1); |
---|
149 | |
---|
150 | int cmp = (AW_CL)(cbs1->f) - (AW_CL)cbs0->f; // compare address of function |
---|
151 | if (!cmp) { |
---|
152 | cmp = cbs1->get_cd1() - cbs0->get_cd1(); |
---|
153 | if (!cmp) { |
---|
154 | cmp = cbs1->get_cd2() - cbs0->get_cd2(); |
---|
155 | if (!cmp) cmp = strcmp(k0, k1); |
---|
156 | } |
---|
157 | } |
---|
158 | return cmp; |
---|
159 | } |
---|
160 | |
---|
161 | size_t AW_root::callallcallbacks(int mode) { |
---|
162 | // mode == -2 -> mark all as called |
---|
163 | // mode == -1 -> forget called |
---|
164 | // mode == 0 -> call all in alpha-order |
---|
165 | // mode == 1 -> call all in reverse alpha-order |
---|
166 | // mode == 2 -> call all in code-order |
---|
167 | // mode == 3 -> call all in reverse code-order |
---|
168 | // mode == 10 -> call all in random order |
---|
169 | // mode == 11-> call all in random order (repeated until no uncalled callbacks left) |
---|
170 | |
---|
171 | size_t count = GBS_hash_count_elems(prvt->action_hash); |
---|
172 | size_t callCount = 0; |
---|
173 | |
---|
174 | aw_message(GBS_global_string("Found %zi callbacks", count)); |
---|
175 | |
---|
176 | if (!dontCallHash) build_dontCallHash(); |
---|
177 | |
---|
178 | if (mode == 11) { |
---|
179 | aw_message("Calling callbacks iterated"); |
---|
180 | for (int iter = 1; ; ++iter) { // forever |
---|
181 | size_t thisCount = callallcallbacks(10); // call all in random order |
---|
182 | aw_message(GBS_global_string("%zu callbacks were called (iteration %i)", thisCount, iter)); |
---|
183 | if (!thisCount) { |
---|
184 | aw_message("No uncalled callbacks left"); |
---|
185 | break; |
---|
186 | } |
---|
187 | |
---|
188 | callCount += thisCount; |
---|
189 | } |
---|
190 | } |
---|
191 | else if (mode == -1) { |
---|
192 | forgetCalledCallbacks(); |
---|
193 | } |
---|
194 | else { |
---|
195 | CallbackArray callbacks; |
---|
196 | switch (mode) { |
---|
197 | case 0: |
---|
198 | case 1: |
---|
199 | GBS_hash_do_sorted_loop(prvt->action_hash, collect_callbacks, GBS_HCF_sortedByKey, &callbacks); |
---|
200 | break; |
---|
201 | case 2: |
---|
202 | case 3: |
---|
203 | GBS_hash_do_sorted_loop(prvt->action_hash, collect_callbacks, sortedByCallbackLocation, &callbacks); |
---|
204 | break; |
---|
205 | default: |
---|
206 | GBS_hash_do_loop(prvt->action_hash, collect_callbacks, &callbacks); |
---|
207 | break; |
---|
208 | } |
---|
209 | |
---|
210 | switch (mode) { |
---|
211 | case -2: |
---|
212 | case 0: |
---|
213 | case 2: break; // use this order |
---|
214 | case 1: |
---|
215 | case 3: reverse(callbacks.begin(), callbacks.end()); break; // use reverse order |
---|
216 | case 10: random_shuffle(callbacks.begin(), callbacks.end()); break; // use random order |
---|
217 | default : gb_assert(0); break; // unknown mode |
---|
218 | } |
---|
219 | |
---|
220 | count = callbacks.size(); |
---|
221 | aw_message(GBS_global_string("%zu callbacks were not called yet", count)); |
---|
222 | |
---|
223 | CallbackIter end = callbacks.end(); |
---|
224 | |
---|
225 | for (int pass = 1; pass <= 2; ++pass) { |
---|
226 | size_t curr = 1; |
---|
227 | CallbackIter cb = callbacks.begin(); |
---|
228 | |
---|
229 | for (; cb != end; ++cb) { |
---|
230 | const char *remote_command = cb->c_str(); |
---|
231 | bool this_pass = remote_command[0] == '-' ? (pass == 2) : (pass == 1); |
---|
232 | |
---|
233 | if (this_pass) { |
---|
234 | GBS_write_hash(alreadyCalledHash, remote_command, 1); // don't call twice |
---|
235 | |
---|
236 | if (mode != -2) { // -2 means "only mark as called" |
---|
237 | AW_cb_struct *cbs = (AW_cb_struct *)GBS_read_hash(prvt->action_hash, remote_command); |
---|
238 | bool skipcb = remote_command[0] == '!' || GBS_read_hash(dontCallHash, remote_command); |
---|
239 | if (!skipcb) { |
---|
240 | if (cbs->f == (AW_CB)AW_help_entry_pressed) skipcb = true; |
---|
241 | } |
---|
242 | if (skipcb) { |
---|
243 | fprintf(stderr, "Skipped callback %zu/%zu (%s)\n", curr, count, remote_command); |
---|
244 | } |
---|
245 | else { |
---|
246 | fprintf(stderr, "Calling back %zu/%zu (%s)\n", curr, count, remote_command); |
---|
247 | |
---|
248 | GB_clear_error(); |
---|
249 | |
---|
250 | cbs->run_callback(); |
---|
251 | callCount++; |
---|
252 | process_pending_events(); |
---|
253 | |
---|
254 | if (GB_have_error()) { |
---|
255 | fprintf(stderr, "Unhandled error in '%s': %s\n", remote_command, GB_await_error()); |
---|
256 | } |
---|
257 | |
---|
258 | if (cbs->f == AW_POPUP) { |
---|
259 | AW_window *awp = cbs->pop_up_window; |
---|
260 | if (awp) { |
---|
261 | awp->force_expose(); |
---|
262 | process_pending_events(); |
---|
263 | |
---|
264 | fprintf(stderr, "Popping down window '%s'\n", awp->get_window_id()); |
---|
265 | awp->hide(); |
---|
266 | process_pending_events(); |
---|
267 | } |
---|
268 | } |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|
272 | else { |
---|
273 | if (pass == 1) { |
---|
274 | fprintf(stderr, "Delayed callback %zu/%zu (%s)", curr, count, remote_command); |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | curr++; |
---|
279 | } |
---|
280 | |
---|
281 | if (pass == 1) fprintf(stderr, "Executing delayed callbacks:\n"); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | return callCount; |
---|
286 | } |
---|
287 | |
---|
288 | // -------------------------------------------------------------------------------- |
---|
289 | #endif // DEBUG |
---|
290 | |
---|