some print fixes. Need to shove defaults into org_settings, and hook up every list
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / error.js
1 dump('entering util/error.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.error = function () {
5
6         try {
7                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8                 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
9                         .getService(Components.interfaces.nsIConsoleService);
10         } catch(E) {
11                 this.consoleDump = false;
12                 dump('util.error constructor: ' + E + '\n');
13         }
14
15         this.sdump_last_time = new Date();
16
17         this.OpenILS = {};
18
19         return this;
20 };
21
22 util.error.prototype = {
23
24         'printDebug' : true,
25         'consoleDump' : true,
26         'debugDump' : true,
27         'arg_dump_full' : false,
28
29         'debug' : function(e){
30                 dump('-----------------------------------------\n' 
31                         + e + '\n-----------------------------------------\n' );
32         },
33
34         'sdump_levels' : {
35
36                 'D_NONE' : false, 'D_ALL' : false, 'D_ERROR' : true, 'D_DEBUG' : true, 'D_TRACE' :  true,
37                 'D_WARN' : false,
38                 'D_TRACE_ENTER' :  false, 'D_TRACE_EXIT' :  false, 'D_TIMEOUT' :  false, 'D_FILTER' : false,
39                 'D_CONSTRUCTOR' : false, 'D_FIREFOX' : false, 'D_LEGACY' : false, 'D_DATA' : false,
40
41                 'D_CLAM' : false, 'D_PAGED_TREE' : false, 'D_GRID_LIST' : false, 'D_HTML_TABLE' : false,
42                 'D_TAB' : false, 'D_LIST' : false,
43
44                 'D_AUTH' : true, 'D_OPAC' : true, 'D_CAT' : true,
45
46                 'D_PATRON_SEARCH' : false, 'D_PATRON_SEARCH_FORM' : false, 'D_PATRON_SEARCH_RESULTS' : false,
47
48                 'D_PATRON_DISPLAY' : false, 'D_PATRON_DISPLAY_STATUS' : false, 'D_PATRON_DISPLAY_CONTACT' : false,
49
50                 'D_PATRON_ITEMS' : false, 'D_PATRON_CHECKOUT_ITEMS' : false, 'D_PATRON_HOLDS' : false,
51                 'D_PATRON_BILLS' : false, 'D_PATRON_EDIT' : false,
52
53                 'D_CHECKIN' : false, 'D_CHECKIN_ITEMS' : false,
54
55                 'D_HOLD_CAPTURE' : false, 'D_HOLD_CAPTURE_ITEMS' : false,
56
57                 'D_PATRON_UTILS' : false, 'D_CIRC_UTILS' : false,
58
59                 'D_FILE' : true, 'D_EXPLODE' : false, 'D_FM_UTILS' : false, 'D_PRINT' : true, 'D_SES' : true,
60                 'D_SES_FUNC' : false, 'D_SES_RESULT' : true, 'D_SPAWN' : false, 'D_STRING' : false,
61                 'D_UTIL' : false, 'D_WIN' : false, 'D_WIDGETS' : false
62         },
63
64         'filter_console_init' : function (p) {
65                 this.sdump('D_FILTER',this.arg_dump(arguments,{0:true}));
66
67                 var filterConsoleListener = {
68                         observe: function( msg ) {
69                                 try {
70                                         p.observe_msg( msg );
71                                 } catch(E) {
72                                         alert(E);
73                                 }
74                         },
75                         QueryInterface: function (iid) {
76                                 if (!iid.equals(Components.interfaces.nsIConsoleListener) &&
77                                         !iid.equals(Components.interfaces.nsISupports)) {
78                                                 throw Components.results.NS_ERROR_NO_INTERFACE;
79                                 }
80                                 return this;
81                         }
82                 };
83                 try {
84                         this.consoleService.registerListener(filterConsoleListener);    
85                 } catch(E) {
86                         alert(E);
87                 }
88
89                 this.sdump('D_TRACE_EXIT',this.arg_dump(arguments));
90         },
91
92         'sdump' : function (level,msg) {
93                 try {
94                         var now = new Date();
95                         var message = now.valueOf() + '\tdelta = ' + (now.valueOf() - this.sdump_last_time.valueOf()) + '\t' + level + '\n' + msg;
96                         if (this.sdump_levels['D_NONE']) return null;
97                         if (this.sdump_levels[level]||this.sdump_levels['D_ALL']) {
98                                 this.sdump_last_time = now;
99                                 if (this.debugDump)
100                                         this.debug(message);
101                                 if (this.consoleDump) {
102                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
103                                         this.consoleService.logStringMessage(message);
104                                 }
105                         }
106                 } catch(E) {
107                         dump('Calling sdump but ' + E + '\n');
108                 }
109         },
110
111         'arg_dump' : function (args,dump_these) {
112                 var s = '*>*>*> Called function ';
113                 try {
114                         if (!dump_these)
115                                 dump_these = {};
116                         s += args.callee.toString().match(/\w+/g)[1] + ' : ';
117                         for (var i = 0; i < args.length; i++)
118                                 s += typeof(args[i]) + ' ';
119                         s += '\n';
120                         for (var i = 0; i < args.length; i++)
121                                 if (dump_these[i]) {
122
123                                         var arg = args[i];
124                                         //dump('dump_these[i] = ' + dump_these[i] + '  arg = ' + arg + '\n');
125
126                                         if (typeof(dump_these[i])=='string') {
127
128                                                 if (dump_these[i].slice(0,1) == '.') {
129                                                         var cmd = 'arg' + dump_these[i];
130                                                         var result;
131                                                         try {
132                                                                 result = eval( cmd );
133                                                         } catch(E) {
134                                                                 result = cmd + ' ==> ' + E;
135                                                         }
136                                                         s += '\targ #' + i + ': ' + cmd + ' = ' + result;
137                                                 } else {
138                                                         var result;
139                                                         try {
140                                                                 result = eval( dump_these[i] );
141                                                         } catch(E) {
142                                                                 result = dump_these[i] + ' ==> ' + E;
143                                                         }
144                                                         s += '\targ #' + i + ': ' + result;
145                                                 }
146         
147                                         } else {
148                                                 s += '\targ #' + i + ' = ';
149                                                 try {
150                                                         //s += js2JSON( arg );
151                                                         s += arg;
152                                                 } catch(E) {
153                                                         s += arg;
154                                                 }
155                                         }
156         
157                                         s += '\n';
158                                         if (this.arg_dump_full)
159                                                 s += 'Definition: ' + args.callee.toString() + '\n';
160         
161                                 }
162                         return s;
163                 } catch(E) {
164                         return s + '\nDEBUG ME: ' + js2JSON(E) + '\n';
165                 }
166         },
167
168         'handle_error' : function (E,annoy) {
169                 var s = '';
170                 if (instanceOf(E,ex)) {
171                         s += E.err_msg();
172                         //s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
173                         //s += 'This error was anticipated.\n\n';
174                         //s += js2JSON(E).substr(0,200) + '...\n\n';
175                         if (snd_bad) snd_bad();
176                 } else {
177                         s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
178                         s += 'This is a bug that we will fix later.\n\n';
179                         try {
180                                 s += js2JSON(E).substr(0,1024) + '\n\n';
181                         } catch(E2) {
182                                 try {
183                                         s += E.substr(0,1024) + '\n\n';
184                                 } catch(E3) {
185                                         s += E + '\n\n';
186                                 }
187                         }
188                         if (snd_really_bad) snd_really_bad();
189                 }
190                 sdump('D_ERROR',s);
191                 if (annoy)
192                         this.s_alert(s);
193                 else
194                         alert(s);
195         },
196
197         's_alert' : function (s) { alert(s); },
198
199         'get_ilsevent' : function(status) {
200                 JSAN.use('OpenILS.data'); 
201                 this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
202                 return this.OpenILS.data.entities['ilsevent.'+status];
203         },
204
205         'yns_alert' : function (s,title,b1,b2,b3,c) {
206
207                 /*
208                         s       = Message to display
209                         title   = Text in Title Bar
210                         b1      = Text for button 1
211                         b2      = Text for button 2
212                         b3      = Text for button 3
213                         c       = Text for confirmation checkbox.  null for no confirm
214                 */
215
216                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
217
218                 // get a reference to the prompt service component.
219                 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
220                         .getService(Components.interfaces.nsIPromptService);
221
222                 // set the buttons that will appear on the dialog. It should be
223                 // a set of constants multiplied by button position constants. In this case,
224                 // three buttons appear, Save, Cancel and a custom button.
225                 //var flags=promptService.BUTTON_TITLE_OK * promptService.BUTTON_POS_0 +
226                 //      promptService.BUTTON_TITLE_CANCEL * promptService.BUTTON_POS_1 +
227                 //      promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_2;
228                 var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
229                         promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
230                         promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_2; 
231
232                 // display the dialog box. The flags set above are passed
233                 // as the fourth argument. The next three arguments are custom labels used for
234                 // the buttons, which are used if BUTTON_TITLE_IS_STRING is assigned to a
235                 // particular button. The last two arguments are for an optional check box.
236                 var check = {};
237                 var rv = promptService.confirmEx(window,title, s, flags, b1, b2, b3, c, check);
238                 if (c && !check.value) {
239                         return this.yns_alert(s,title,b1,b2,b3,c);
240                 }
241                 return rv;
242         },
243 }
244
245 dump('exiting util/error.js\n');