Perfect Abstractions
  • Home
  • Contact
  • Blog
  • Home
  • Contact
  • Blog

Debugging and Testing Gateway Scripts in Ignition

7/23/2014

5 Comments

 
Testing and debugging event scripts on components is easy. Open up the Output Console in the Designer and sprinkle your code with print statements to see what is going on.
This approach doesn't work as well with gateway scripts because print statements are not output to the Output Console in the Designer.
A gateway script is any Python script that runs in the Gateway and not in a designer or client. Here are a variety of tools to use for testing and debugging gateway scripts:

Run Your Gateway Code in the Designer

The Designer has a tool called "Script Playground". It can be found here: Tools -> Script Playground. Take the code in your gateway script and paste it into the Script Playground and run it. See if it works. This kind of testing will work for many gateway scripts.
It may be difficult to catch infrequent problems by running gateway script code in the designer. Some of the other methods of testing and debugging are better suited for that.
Note: Some functions such as system.gui.getWindow and other GUI functions work in the designer but do not work in gateway scripts. Code with such functions will run without error in the Script Playground but won't actually work when run in the Gateway. If code runs fine in the Script Playground but not in a gateway script then use one of the other methods of testing and debugging.

Gateway Scripts Status

Check the Status -> Gateway Scripts web page in your Gateway for exceptions. This is an easy way to see if an exception is being raised in your scripts and find out what the exception is. Here is an example of a Gateway Timer Script called "test" that has an error:
Picture

Create Your Own Logger

Ignition 7.6 and earlier has an undocumented scripting function called system.util.logger(). This function is used to create a logger. Once a logger is created messages can be sent to the System Console Gateway webpage. This is a great way to test different parts of gateway scripts. In Ignition 7.7 there is a new documented function called system.util.getLogger() which is used for the same purpose. Here is an example:
 logger = system.util.logger("MyLogger")
value1 = system.tag.read("firstTag").value
value2 = system.tag.read("secondTag").value
message = "firstTag value is %s, secondTag value is %s"% (value1, value2)
logger.info(message)
You can see the result in the first line of logging in the image below.
Picture

Try/Except with Logging

Using Try/Except with Logging gives you the most control for handling and reporting exceptions in gateway scripts. Here is an example:
 logger = system.util.logger("MyLogger")
try:
#code here
value1 = system.tag.read("firstTag").value
value2 = system.tag.read("secondTag").value
system.tag.write("thirdTag", value1+value2)
except:
import sys
from java.lang import Exception
exceptionType, exception, stacktrace = sys.exc_info()
if exceptionType == Exception:
exception = exception.getCause()
exceptionName = exception.__class__.__name__
logger.error(exceptionName+" : "+str(exception))
In the example above code is put between try and except. If any exception occurs it gets logged.
This code is especially useful in Ignition 7.7.0 Tag Event Scripts because exceptions in Tag Event Scripts do not show in the System Console and do not show in the Gateway Status web pages, and they are also not logged to the wrapper.log file. There are plans in Ignition 7.7.1 to add default logging of Tag Event Script errors.

wrapper.log File and Print Statements

Use print statements and the wrapper.log file. This can be a cumbersome way to debug and test gateway scripts if the right tool isn't used. Raised exceptions and the output of print statements are automatically sent to a log file called wrapper.log which exists where Ignition is installed on Windows or /var/log/ignition/ on Linux. A big limitation with this approach is that you have to log in to the server where Ignition is installed to get or see the wrapper.log file.
If Ignition is installed on Linux and you have ssh access to it, the command "tail -F /var/log/ignition/wrapper.log" will show real-time changes to the wrapper.log file. This is a very convenient way to debug and test gateway scripts.
If Ignition is installed on Window, similar tail programs like Tail for Win32 can be used.
Note: In Ignition 7.7.0, exceptions in Tag Event Script are not automatically logged to the wrapper.log file. The "Try/Except with Logging" method must be used.

Leave a Comment

Please leave a comment about this article. Let me know about your experiences with testing and debugging Gateway Event Scripts. Or let me know if this blog post helps you.
Sign up for my mailing list to get notified of future blog posts.
5 Comments
Zack Scriven link
7/24/2014 05:38:21 am

Thank you for the great post Nick. Your contribution to the community is greatly appreciated.

Reply
Nick Mudge
7/24/2014 05:51:48 am

Thank you Zack.

Reply
Jordan Clark
9/23/2014 09:15:33 pm

Definitely bookmarking this one. Thanks, Nick!

Reply
Huck Bales link
10/11/2016 11:58:04 am

what Zack said!

Reply
Janice link
12/30/2020 11:49:00 pm

Lovelyy blog you have here

Reply



Leave a Reply.

    Author

    Nick Mudge
    Ignition Software Consultant
    nick@perfectabstractions.com
    916.234.6521

    See our blogs here http://blog.perfectabstractions.com
    nickmudge.info.

    Links

    Nick Mudge's Weblog
    Computing Without Boundaries

    Archives

    March 2017
    November 2016
    September 2015
    August 2015
    January 2015
    September 2014
    August 2014
    July 2014

    Categories

    All
    Applets
    Chat
    Debugging
    Fonts
    Gateway Scripting
    Ignition
    Ignition 7.7
    Ignition Modules
    Ignition Scripting
    Ignition Templates
    Java
    Jython
    Kepware
    Linux
    Logging
    OPC
    OPC UA
    OPC-UA
    Python
    RSS
    SCADA
    Security
    Software Abstraction
    Support
    Testing
    Water
    Weather
    Weather Radar
    Windows

    RSS Feed

    Join My Mailing List

    • Get email updates about news, blog posts, articles, product offers and other items of interest.

    • subscribed: 0
    • I respect your privacy
    • Email Marketingby GetResponse
© Perfect Abstractions LLC