Logging line number of Script Includes

MGanon
Tera Guru

Is it possible to log the line number of a script includes for debugging?

I have a very long Script Includes called during an import transform map. It would be very helpful for debugging to identify some of the specific lines processed. I added several gs.log entries into the Script Includes but it is cumbersome to accurately maintain the gs.log("line number X") when adding lines to or removing lines from the Script Includes. Is there a dynamic function that can more dynamically record the Script Includes line?

6 REPLIES 6

Ian-Fusion3
Tera Guru

I like to use a variable to store my log then post it as a single entry.

var logStr = " --- Transform Log --- \n\n";

logStr += "Line 1 finished \n";

logStr += "Line 2 errored \n";

gs.log(logStr);

 

This will show up in 1 entry in your logs. It helps to keep everything organized. Just make sure you use \n at the end of the string so it prints a new line.

Giles Lewis
Giga Guru

I do not believe there is a function to return the Script Include line number.  Here are some techniques I have found to be helpful for debugging large script includes.

  1. Split the Script Include into lots of small functions. If you do this, then you will no longer be concerned about line number: Instead you will be concerned about the name of the function.
  2. During initialization, add: "this.debug = true;"
  3. Prefix each gs.log statement with "if (this.debug) gs.log(...". Now you can easily turn debugging on or off.
  4. The first parameter of gs.log should begin with the name of the function (or method) from where it is being called.
  5. Set the second parameter (source) of gs.log to this.type.  Now in sys_log you can easily filter all messages which came from this script include. Another option is to set the source to this.type + "." + functionname.

Now your debug statements look like this

if (this.debug) gs.log("functionname arg1=" + arg1 + " arg2=" + arg2, this.type);

You may be working with a legacy script include, in which case it is not viable to split up the script include into a lot of small functions.  That's okay.  Instead of functionname use some other literal (e.g. section name) which will tell you from where the log statement was called.

Sorry for the delay. At first it appeared that "this.debug = true;" with if (this.debug) gs.log(... worked, except that an error appeared about 1,000 down in the script includes that there were too many errors to check.

Yusuf5
Tera Expert

It depends on the browser you are using.

You can use lineNumber for Mozilla compatible browsers such as FireFox, Chrome, MS Edge and Opera.

try {
decodeURI("%%%"); // You cannot URI decode percent signs throws URIError exception
}
catch (err) {
gs.print(err.lineNumber);
}

But it doesn't work for MS IE.

Here are some other properties that you can use in catch block.

fileName
lineNumber
columnNumber
stack
name
message

 

Hope it helps,

Regards,

Yusuf