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