Way to print for both perspective and vision projects?

I have a Ignition project that has both a vision and perspective aspect to them. They share some business logic functions. I would like it if I could craft a print function that would handle printing if it was in vision or perspective, given one I have to use print and the other I have to use system.perspective.print. Has anyone figured out a way to do this?

Basically I want something like

def print(message, ???):
    if ???:
        print message

but I can’t think of a way for this print function to “know” if things are being called from perspective or vision, except for passing some parameter, which I would then have to include for every single function that wants to print. Any ideas how I can figure out the scope of what is calling what function so that I can print regardless?

from __future__ import print_function
import __builtin__
from java.lang import IllegalArgumentException 

def print(*objects, **kwargs):
	sep = kwargs.pop("sep", ", ")
	end = kwargs.pop("end", "\n")
	message = sep.join(str(object) for object in objects)
	except IllegalArgumentException:
	except AttributeError:
		__builtin__.print(message, end=end) 

Dropped in a utils script module. I would definitely recommend not naming a function print (notice the from __future__ import that was required), but it’s technically possible.

It’ll try to use system.perspective.print; if that fails due to the IllegalArgumentException (meaning you’re on the gateway, but not inside Perspective) then it’ll bail out to a logging statement. But, if system.perspective is an AttributeError, meaning you’re not on the gateway at all, it just uses the builtin print function.

Note that performance on the non-happy path is going to be dramatically worse than the native print statement; throwing and catching exceptions is expensive. There’s probably a better way to only run the same logic once; @pturmel may have some ideas, but this works to at least prove the concept.


Would checking for the existence of a client-scoped tag work?

def universalPrint(message):
    if system.tag.exists('[System]Client/User/Username'):
        print message
1 Like