2.5.2.2 Debugging Tools

Code Builder (Origin feature)

Code Builder is Origin's integrated development environment to debug LabTalk scripts, Origin C code, X-Function code and fitting functions coded in Origin C. In Code Builder, use various convenient debugging tools like setting up break points, step-by-step execution and inspection of variable values. Code Builder can be opened by the ed.open() method.

To learn how to use the Code Builder, see the Code Builder User's Guide in the Programming help file.

Here is an example showing how to debug LabTalk script in Code Builder.

1. Open an OGS file by running the following script.
2. // Open an ogs file in Code Builder
file$= system.path.program$ + "Samples\LabTalk Script Examples\ave_traces.ogs";
ed.open(%(file$)); 3. Set a break point on line 22 in the open file, by clicking on the margin to the left of this line: 4. fname$ = system.path.program$+ "Samples\Data Manipulation\not_monotonic_multicurve.dat"; The break point will look like this: 5. Place the cursor on line 12 - the [Main] section - then select menu Debug: Execute Current Section. The [Main] section code will run and stop at the line with the break point. 6. Now press F10 to execute the remaining script line by line. Code Builder provides the Watch window to view the value of a variable during debugging. For example, after pressing F10 once, open the Watch window by menu item View: Watch if it is not opened yet. Then type fname$ in the left cell of the table in this window, the value will show in the right cell of the same row.
7. To execute the remaining script, press F5. It will complete unless encountering another break point.

Ed (object)

The Ed (object) provides script access to Code Builder, a dedicated editor for LabTalk script and Origin C code.

The ed object methods are:

Method Brief Description
ed.open() Open the Code Builder window.
ed.open(fileName) Open the specified file in the Code Builder window.
ed.open(fileName, sectionName) Open the specified OGS file at the specified section in the Code Builder window. (Defaults to file beginning if section not found.)

Open the Code Builder

ed.open()

Open a Specific File in Code Builder

The following command opens the file myscript.ogs

ed.open(E:\myfolder\myscript.ogs)

Open a File on a Pre-Saved Path

Use the cd X-Function to first switch to the particular folder:

cd 2;
ed.open(autofit.ogs);

LabTalk Variables and Functions Dialog

The list command with no options as well as the ed command (different than the ed object) opens the LabTalk Variables dialog, which is a table of attributes for all variables in the current project. The attributes are variable name, value, type, subtype, property, plot information, and description.

This is a useful tool for script programmers as the current values and properties of variables can be viewed in real time. Additionally, variables can be sorted by any of their attributes, alphabetically in the case of text descriptors, numerically in the case of numeric values.

Check boxes exist on the right-hand side of the dialog that allow you to see any subset of the entire variable list.

Echo (system variable)

To debug and trace, this system variable, Echo prints scripts or error messages to the Command window (or Script window when entered there). To enable echo, type:

echo = Number

in the Script window (where Number is one of the following):

Number Description
1 Display commands that generate an error;
2 Display scripts that have been sent to the queue for delayed execution;
4 Display scripts that involve commands;
8 Display scripts that involve assignments;
16 Display macros.

These values are bits that can be combined to produce cumulative effects. For example, echo = 12 displays both command and assignment scripts. Echo = 7 (includes echo = 1, echo = 2, and echo = 4) is useful for following script execution during menu command selection. To disable echo, type echo = 0 in the Script window

#!script (special syntax)

Embed debugging statements in your script using this notation. The # character tells the LabTalk interpreter to ignore the text until the end of the line. However, when followed by the ! character, the script is executed if the @B system variable (or System.Debug object property) is set to 1. The following example illustrates this option:

@B = 1;
range rr = [Book1]Sheet1!col(A);  // Range to column A
for (ii=1; ii<=10; ii+=1) {
#!ii=; rr[ii]=;  // Embedded debugging script
rr[ii]+=ii*10;
}
@B = 0;
#!type -a This line will not execute

The script sets @B equal to 1 to allow #! lines to execute. By setting @B to 0 , the last line will not execute.

{script} (special syntax)

An error in your LabTalk code will cause the code to stop at the point of the error and not execute any statements after the error. In cases where you would like the script to continue executing in such cases, you can use curly braces to define where error handling should begin and resume. For instance, in the following script,

type Start;
type End;

the word Start will print to the Script Window, but if BadFileName cannot be found, the script will stop executing at that point, and the word End will not print.

If, however, you surround the line in question with curly braces (i.e., {}), as in,

type Start;
{
}
type End;

then End will print whether or not BadFileName is properly imported.

You can catch this condition with a variable:

@LT = 3; // use in 2017 SR2 and later, remove this line for earlier versions
flag = 1;
{
impasc fname:=MyFile;
flag = 0;
}
if(flag)
type Error ocurred;
else
type OK;

A similar situation occurs when a section in an OGS file fails. Code will silently return to the calling context. Use the above variable method to identify that code failed. In this case, the brackets are not needed:

[Called Section]
flag = 1;
BadCommand; // This line errors and silently returns
flag = 0;  // flag (which must be global variable) is 1, then above code failed.

@B(system variable), System.Debug (object property)

@B system variable controls the Debug mode to execute the LabTalk statements that begin with #! :

1 = enable
0 = disable

It is equivalent to the System.Debug object property.

@OC (system variable)

@OC system variable controls whether or not you can call Origin C functions from LabTalk.

Value Description
@OC = 1 (default) Origin C functions CAN be called
@OC = 0 Origin C functions CANNOT be called

@V(system variable), System.Version(object property)

@V indicates the Origin version number. @V and System.Version object property are equivalent.

@VDF (system variable)

If you set @VDF = 1, when you open a project file (.OPJ), Origin will report the Origin version in which the file was saved.

VarName= (command)

This command examines the value of any variable. Embed this in your script to display intermediate variable values in the Script window during script execution.

Example 1

The following command prints out the value of myHight variable:

myHight=

LabTalk:List (command)

The list command is used to examine your system environment. For example, the list s command displays all datasets (including temporary datasets) in the project.

ErrorProc (macro)

The ErrorProc macro is useful for error trapping.

The ErrorProc macro is triggered ...

• when the LabTalk interpreter detects a #Command Error.
• when you click the Cancel button in any dialog box.
• when you click the No button in dialog boxes that do not have a Cancel button.

The ErrorProc macro is deleted immediately after it is triggered and executed.

This macro displays the message "This operation is still under development..." in a dialog box with an OK button.

Type <ogsFileName> (command)

This variant of the type command prints out the contents of a specified script file (.OGS) in the current directory to the Command (or Script) window. Note that the file extension .OGS in ogsFileName may be omitted. The file name cannot include a path and must be in the working directory.

Examples:

The following script prints the contents of D: \temp\mytemp1.ogs and C:\myogs\hello.ogs.

cd D:\Temp;
type mytemp1.ogs; // Extension included
cd C:\temp;
type hello; // Extension omitted

Log to a File

To output the log information to a file, the type command is available. type -gb will specify the log file to output to, and begin the output routine. Then type -ge will end the routine and stop logging to the file. For example:

type -gb %Ylog.txt;  // Start typing text to a file, log.txt, if not exist, create it
type aa;  // Write aa
type bb;  // Write bb
type cc;  // Write cc
type -ge;  // End writing

This can be particularly useful when your script is creating a large volume of output to the Script Window since it has only a 30000 byte buffer.