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)
try:
system.perspective.print(message)
except IllegalArgumentException:
system.util.getLogger("utils.print").info(message)
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.