debugging. spawn copy editor in second tabbed interface
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / util / error.js
1 sdump('D_TRACE',"Loading error.js\n");
2
3 var consoleService = Components.classes['@mozilla.org/consoleservice;1']
4         .getService(Components.interfaces.nsIConsoleService);
5
6 var consoleDump = true;
7 var debugDump = true;
8 var arg_dump_full = false;
9
10 var sdump_levels = {
11         'D_NONE' : false,
12         'D_ALL' : true,
13         'D_ERROR' : true,
14         'D_TRACE' :  false,
15         'D_TRACE_ENTER' :  false,
16         'D_TRACE_EXIT' :  false,
17         'D_TIMEOUT' :  false,
18         'D_FILTER' : false,
19         'D_CONSTRUCTOR' : false,
20         'D_FIREFOX' : false,
21         'D_LEGACY' : false,
22
23         'D_CLAM' : false,
24         'D_PAGED_TREE' : false,
25         'D_GRID_LIST' : false,
26         'D_HTML_TABLE' : false,
27         'D_TAB' : false,
28
29         'D_AUTH' : false,
30
31         'D_OPAC' : true,
32
33         'D_PATRON_SEARCH' : false,
34         'D_PATRON_SEARCH_FORM' : false,
35         'D_PATRON_SEARCH_RESULTS' : false,
36
37         'D_PATRON_DISPLAY' : false,
38         'D_PATRON_DISPLAY_STATUS' : false,
39         'D_PATRON_DISPLAY_CONTACT' : false,
40
41         'D_PATRON_ITEMS' : false,
42         'D_PATRON_CHECKOUT_ITEMS' : false,
43         'D_PATRON_HOLDS' : true,
44         'D_PATRON_BILLS' : false,
45         'D_PATRON_EDIT' : false,
46
47         'D_CHECKIN' : true,
48         'D_CHECKIN_ITEMS' : false,
49
50         'D_HOLD_CAPTURE' : true,
51         'D_HOLD_CAPTURE_ITEMS' : false,
52
53         'D_CAT' : false,
54
55         'D_PATRON_UTILS' : false,
56         'D_CIRC_UTILS' : false,
57
58         'D_EXPLODE' : false,
59         'D_FM_UTILS' : false,
60         'D_PRINT' : true,
61         'D_SES' : true,
62         'D_SES_FUNC' : false,
63         'D_SES_RESULT' : false,
64         'D_SPAWN' : false,
65         'D_STRING' : false,
66         'D_UTIL' : false,
67         'D_WIN' : false,
68         'D_WIDGETS' : false
69
70 };
71
72 var sdump_last_time = new Date();
73
74 function filter_console_init(p) {
75         sdump('D_FILTER',arg_dump(arguments,{0:true}));
76
77         var filterConsoleListener = {
78                 observe: function( msg ) {
79                         try {
80                                 p.observe_msg( msg );
81                         } catch(E) {
82                                 alert(E);
83                         }
84                 },
85                 QueryInterface: function (iid) {
86                         if (!iid.equals(Components.interfaces.nsIConsoleListener) &&
87                                 !iid.equals(Components.interfaces.nsISupports)) {
88                                         throw Components.results.NS_ERROR_NO_INTERFACE;
89                         }
90                         return this;
91                 }
92         };
93         try {
94                 consoleService.registerListener(filterConsoleListener); 
95         } catch(E) {
96                 alert(E);
97         }
98
99         sdump('D_TRACE_EXIT',arg_dump(arguments));
100 }
101
102 function sdump(level,msg) {
103         try {
104                 var now = new Date();
105                 var message = now.valueOf() + '\tdelta = ' + (now.valueOf() - sdump_last_time.valueOf()) + '\n' + level + '\n' + msg;
106                 if (sdump_levels['D_NONE']) return null;
107                 if (sdump_levels[level]||sdump_levels['D_ALL']) {
108                         sdump_last_time = now;
109                         if (debugDump)
110                                 debug(message);
111                         if (consoleDump)
112                                 consoleService.logStringMessage(message);
113                 }
114         } catch(E) {
115                 dump('Calling sdump but ' + E + '\n');
116         }
117 }
118
119 function arg_dump(args,dump_these) {
120         var s = '*>*>*> Called function ';
121         try {
122                 if (!dump_these)
123                         dump_these = {};
124                 s += args.callee.toString().match(/\w+/g)[1] + ' : ';
125                 for (var i = 0; i < args.length; i++)
126                         s += typeof(args[i]) + ' ';
127                 s += '\n';
128                 for (var i = 0; i < args.length; i++)
129                         if (dump_these[i]) {
130
131                                 var arg = args[i];
132                                 //dump('dump_these[i] = ' + dump_these[i] + '  arg = ' + arg + '\n');
133
134                                 if (typeof(dump_these[i])=='string') {
135
136                                         if (dump_these[i].slice(0,1) == '.') {
137                                                 var cmd = 'arg' + dump_these[i];
138                                                 var result;
139                                                 try {
140                                                         result = eval( cmd );
141                                                 } catch(E) {
142                                                         result = cmd + ' ==> ' + E;
143                                                 }
144                                                 s += '\targ #' + i + ': ' + cmd + ' = ' + result;
145                                         } else {
146                                                 var result;
147                                                 try {
148                                                         result = eval( dump_these[i] );
149                                                 } catch(E) {
150                                                         result = dump_these[i] + ' ==> ' + E;
151                                                 }
152                                                 s += '\targ #' + i + ': ' + result;
153                                         }
154
155                                 } else {
156                                         s += '\targ #' + i + ' = ';
157                                         try {
158                                                 //s += js2JSON( arg );
159                                                 s += arg;
160                                         } catch(E) {
161                                                 s += arg;
162                                         }
163                                 }
164
165                                 s += '\n';
166                                 if (arg_dump_full)
167                                         s += 'Definition: ' + args.callee.toString() + '\n';
168
169                         }
170                 return s;
171         } catch(E) {
172                 return s + '\nDEBUG ME: ' + js2JSON(E) + '\n';
173         }
174 }
175
176 function handle_error(E,annoy) {
177         var s = '';
178         if (instanceOf(E,ex)) {
179                 s += E.err_msg();
180                 //s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
181                 //s += 'This error was anticipated.\n\n';
182                 //s += js2JSON(E).substr(0,200) + '...\n\n';
183                 if (snd_bad) snd_bad();
184         } else {
185                 s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
186                 s += 'This is a bug that we will fix later.\n\n';
187                 try {
188                         s += mw.js2JSON(E).substr(0,1024) + '\n\n';
189                 } catch(E2) {
190                         try {
191                                 s += E.substr(0,1024) + '\n\n';
192                         } catch(E3) {
193                                 s += E + '\n\n';
194                         }
195                 }
196                 if (snd_really_bad) snd_really_bad();
197         }
198         sdump('D_ERROR',s);
199         if (annoy)
200                 s_alert(s);
201         else
202                 alert(s);
203 }
204