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