123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- // Licensed Materials - Property of IBM
- //
- // IBM Cognos Products: pps
- //
- // (C) Copyright IBM Corp. 2005, 2017
- //
- // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- // JS Debug Library (optional)
- // The inclusion of debug.js is done based on options in the PPSRoot.cfx file. There are also a few configuration options which are activated by adding settings appropriately. In order to enable the debugger, simply add a sub-tree to the PWQ section of the PPSRoot.cfx.
- //
- // <JavaScript Debugging="IN,,1" DebugFontSize="IN,,3" DebugAppendOutput="IN,Y,0"/>
- //
- // Debugging: Boolean value which enables/disables the debugger.
- // DebugFontSize: Integer value which controls the size of the font in the debugger window.
- // DebugAppendOutput: Boolean value which allows the output of each "run" to be appended to the end of the window.
- //
- // Available functions
- // The debugger is a singleton object represented by the pdb variable. In init.js, there is a function named initDebug() which creates a new Debugger object, if it is available. This function is also a good place to setup function and object traces.
- //
- // pdb.traceFunction(context, functionName);
- // pdb.traceObject(context, objectName);
- // where context is the execution context of the function/object (for global functions, the execution context is "window")
- // functionName/objectName is the name of the function/object
- //
- // These two functions will mark the beginning and end of a function (all member functions for an object), the return value of the function and the time (in ms) it took for the function to execute. These functions should be called within the initDebug() function in order to setup the traces at the beginning of an execution run.
- //
- // pdb.dumpArguments(arguments);
- // pdb.dumpObject(obj);
- // pdb.out("string");
- //
- // The first two methods dump either the arguments of a function (identified by the keyword arguments), or the member variables of an object. The third is the equivalent of a printf, simply dumping the string to the output window.
- function Debugger () {
- //Member assignment...
- this.debugWindowString = 'open("", "debugWindow", "height=400,left=10,top=10,width=600,location=no,menubar=no,scrollbars=yes,resizable=yes,status=no,toolbar=no")';
- this.debugWin = eval(this.debugWindowString);
- this.maxDebugLength = 100;
- this.startTime = new Date();
- this.stack = new Array();
- this.execprofile = new Array();
- this.action = "";
- this.lastCaller = "";
- this.dumpObjects = true;
- this.appendOutput = false;
- var dbgAppend = topparent.getGlobal("jsDebugAppendOutput");
- if (typeof dbgAppend != "undefined")
- this.appendOutput = dbgAppend;
- this.fontSize = 1;
- var dbgFont = topparent.getGlobal("jsDebugFontSize");
- if (typeof dbgFont != "undefined")
- this.fontSize = dbgFont;
- if (!this.appendOutput)
- {
- this.debugWin.document.close();
- this.debugWin.document.open();
- }
- if (!this.debugWin.document.title || this.debugWin.document.closed)
- {
- this.debugWin.document.write("<HTML><HEAD><TITLE>PPWeb Debug</TITLE></HEAD><BODY>");
- this.debugWin.document.write("<FONT SIZE=" + this.fontSize + "><B>*** Start ***</B><BR>");
- }
- else
- {
- this.debugWin.document.write("<BR><B>*** Start ***</B><BR>");
- }
- }
- Debugger.prototype.out = function () {
- // this is a variable argument function used to write to the debug window
- // suitable calls:
- //
- // pdb.out("This is a message") >> simply prints the message...
- // pdb.out("Test", action /*push, pop*/) >> prints the message and verifies stack for indentation
- var msg = "";
- var numArgs = arguments.length;
- if (numArgs <= 0) {
- return;
- }
- if (arguments.length == 2)
- {
- //must be from a callback function wrapper...
- //if it's not nested, then print it on the same line...
- var caller = arguments[1];
- var action = this.action;
- if (action == "push")
- msg += "<br>";
- else if (action == "pop" && caller != this.lastCaller)
- msg += "<br>";
- this.lastCaller = caller;
- }
- else
- {
- //not from a function/object wrapper...simply a message dump
- msg += "<br>";
- this.lastCaller = "";
- this.action = "msg";
- }
- if (msg.indexOf("<br>") == 0)
- {
- for (var j = 0; j < this.stack.length; j++)
- msg += " ";
- }
- msg += arguments[0];
- if (msg) {
- this.debugWin.document.write(msg);
- }
- }
- Debugger.prototype.dumpArguments = function (obj) {
- this.out(this.dumpArgs(obj));
- }
- Debugger.prototype.dumpArgs = function (args) {
- var result = "";
- for (var i = 0; i < args.length; i++)
- {
- var arg = args[i];
- var argType = typeof arg;
-
- if (argType == "string" || argType == "number")
- {
- result += arg;
- }
- else if (argType == "object")
- {
- if (this.dumpObjects)
- {
- result += this.dumpObj(arg);
- }
- else
- result += arg;
- }
- else
- {
- result += "<unknown function parameter>";
- }
- if (i+1 < args.length)
- result += ", ";
- }
- return result;
- }
- Debugger.prototype.dumpObject = function (obj) {
- this.out(this.dumpObj(obj));
- }
- Debugger.prototype.dumpObj = function (obj) {
- var result = this.getObjectName( obj ) + "<br> { <br>";
- for (var prop in obj)
- {
- if (typeof prop != "function")
- result += prop + "=" + obj[prop] + "<br>";
- }
- result += " }; <br>";
- return result;
- }
- Debugger.prototype.getObjectName = function (obj) {
- if (typeof obj.constructor == "undefined")
- return "Object";
- return this.getFunctionName( obj.constructor );
- }
- Debugger.prototype.getFunctionName = function (funcref) {
- if (!funcref)
- return '';
- if (funcref.name)
- return funcref.name;
- var name = funcref + '';
- name = name.substring(name.indexOf(' ') + 1, name.indexOf('('));
- funcref.name = name;
- return name;
- }
- Debugger.prototype.createFunctionWrapper = function (scopename, funcname, precall, postcall)
- {
- var wrappedfunc;
- var scopeobject = eval(scopename);
- var funcref = scopeobject[funcname];
- scopeobject['xbDebug_orig_' + funcname] = funcref;
- wrappedfunc = function ()
- {
- var rv;
- precall(scopename, funcname, arguments);
- rv = funcref.apply(scopeobject, arguments);
- postcall(scopename, funcname, arguments, rv);
- return rv;
- };
- if (typeof(funcref.constructor) != 'undefined')
- wrappedfunc.constructor = funcref.constuctor;
- if (typeof(funcref.prototype) != 'undefined')
- wrappedfunc.prototype = funcref.prototype;
- scopeobject[funcname] = wrappedfunc;
- }
- Debugger.prototype.createMethodWrapper = function (contextname, classname, methodname, precall, postcall)
- {
- var context = eval(contextname);
- var methodref = context[classname].prototype[methodname];
- context[classname].prototype['xbDebug_orig_' + methodname] = methodref;
- var wrappedmethod = function ()
- {
- var rv;
- // eval 'this' at method run time to pick up reference to the object's instance
- var thisref = eval('this');
- // eval 'arguments' at method run time to pick up method's arguments
- var argsref = arguments;
- precall(contextname + '.' + classname, methodname, argsref);
- rv = methodref.apply(thisref, argsref);
- postcall(contextname + '.' + classname, methodname, argsref, rv);
- return rv;
- };
- return wrappedmethod;
- }
- Debugger.prototype.traceBefore = function (scopename, funcname, funcarguments)
- {
- var i;
- var s = '';
- var name = scopename + '.' + funcname;
- var profile = pdb.execprofile[name];
- if (!profile)
- profile = pdb.execprofile[name] = { started: 0, time: 0, count: 0 };
-
- var s = name + '(' + pdb.storeAsString(funcarguments) + ')';
- pdb.action = "push";
- pdb.out(s, name);
- pdb.stack.push(name);
- profile.started = (new Date()).getTime();
- }
- Debugger.prototype.traceAfter = function (scopename, funcname, funcarguments, rv)
- {
- var i;
- var s = '';
- var name = scopename + '.' + funcname;
- var execprofile = pdb.execprofile[name];
- if (!execprofile)
- pdb.out('TraceAfter: execprofile not created for ' + name);
- else if (execprofile.started == 0)
- pdb.out('TraceAfter: execprofile.started == 0 for ' + name);
- else
- {
- execprofile.time = (new Date()).getTime() - execprofile.started;
- execprofile.count++;
- execprofile.started = 0;
- }
- pdb.stack.pop();
- pdb.action = "pop";
- var s = " returned: " + rv + ", execution time: " + execprofile.time;
- pdb.out(s, name);
- }
- Debugger.prototype.traceFunction = function (scopename, funcname)
- {
- this.createFunctionWrapper(scopename, funcname, this.traceBefore, this.traceAfter);
- }
- Debugger.prototype.traceObject = function (contextname, classname)
- {
- var classref = eval(contextname + '.' + classname);
- var p;
- var sp;
- if (!classref || !classref.prototype)
- return;
- for (p in classref.prototype)
- {
- sp = p;
- if (typeof(classref.prototype[sp]) == 'function' && (sp).indexOf('xbDebug_orig') == -1)
- {
- classref.prototype[sp] = this.createMethodWrapper(contextname, classname, sp, this.traceBefore, this.traceAfter);
- }
- }
- }
-
- Debugger.prototype.dumpProfile = function ()
- {
- var p;
- var execprofile;
- var avg;
- for (p in this.execprofile)
- {
- execprofile = this.execprofile[p];
- avg = Math.round ( 100 * execprofile.time/execprofile.count) /100;
- this.out('Execution profile ' + p + ' called ' + execprofile.count + ' times. Total time=' + execprofile.time + 'ms. Avg Time=' + avg + 'ms.');
- }
- }
- Debugger.prototype.storeAsString = function (obj)
- {
- var argType = typeof obj;
- var msg = "";
-
- if (argType == "string")
- {
- msg = obj;
- }
-
- else if (argType == "object")
- {
- if (obj.constructor == "Array" || typeof obj.length != undefined)
- {
- for (var i = 0; i < obj.length; i++)
- {
- msg += obj[i]
- if (i+1 < obj.length)
- msg += ", ";
- }
- }
- else
- {
- msg = pdb.dumpObj(obj);
- }
- }
- return msg;
- }
|