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