Logging Messages in Scripts

Log statements allow you to write text and computed results to the model’s Run Log.  This can also be useful for debugging scripts. The content can be a simple message, a warning, or a fatal error.

There are three types of Log statements.  All three can be inserted by selecting the appropriate type from the Script element’s Insert menu (or pressing the appropriate hot-key when in the Script dialog):

 

Log Type

Hot-Key

Behavior

Message

Ctrl+L

Writes a message to the Run Log; however, no warning is displayed after the run.

Warning

Ctrl+N

Writes a message to the Run Log; a warning is displayed after the run to alert the user.

Error

Ctrl+R

Writes a message to the Run Log; treated as a fatal error (the simulation is stopped).

The Log statement is inserted below the statement that was highlighted at the time the new statement was selected for insertion. The following dialog will be displayed:

Note that at the bottom of the dialog you can modify the type of Log statement (Message, Warning or Error).  If you check the Show Message checkbox, the message will also appear in a pop-up window during the simulation whenever the statement is executed.

One of the most powerful features of the Log statement is that you can write the value of a variable, output or expression in the message. To include a scalar value in the log, type {=VALUE}, where VALUE is the output, variable or expression whose value you wish to display.

For example, assume you had a script variable named X, and an external output (an output from outside the Script that was referenced inside the Script) named Y.  A Log message statement like this:

would look like this in the script:

   Note: When referencing script variables in a Log statement, they must be referenced using the ~ (since they are locally available properties).

The message that would be displayed in a pop-up would look like this:

In the Run Log, it would look like this:

If the variables for which you are writing values have units, the units will be displayed.  For example, if X was a length, this would be indicated in the message:

However, you cannot control the units in which they are displayed.  They are always displayed in the base units for the particular dimension (e.g., meters for lengths, seconds for time).

   Note: No links are created by referencing external elements in a script Log statement. Since there are no links, the causality sequence is not affected in any way by the existence of the expression or reference. This means that the previous (rather than current) value of an element’s output could potentially be shown if the referenced element follows the Script element in the causality sequence. Usually, however, this issue will not come up, as element outputs referenced in Log statements will typically have been used elsewhere in the script (and hence would come before the Script element in the causality sequence).

Related Topics…

Learn more about: