Can a Python script detect which Perspective view or binding it is running in?

I know how to use ApplicationScope to determine which context a script is running in, and can tell if it's in Perspective, but I'm wondering if there's any other info the script can grab about it's context.

I'm especially looking for info like which Perspective view and which property binding or message handler the script is being run within.

We're adding debug wrapping around some of our most heavily used low-level script functions to try and get a sense whether they're being mis-used or abused by any particular views.

For bindings, you might could leverage the Integration Toolkit's debugging expressions. Debugging - Integration Toolkit for Ignition

Possibly a starting point here:

If you just log (not print!) a message, you should get useful MDC output indicating some information.

Technically you could retrieve that at runtime, via the static method on the MDC class:
org.slf4j.MDC.get("key")

As in:

from org.slf4j import MDC
component = MDC.get("component")
2 Likes

The MDC object gives access to things like view and target that I can see appended to some lines in the logs? That should be most of what I want.

Is there rhyme or reason why that MDC data isn't always present on the log lines? I haven't dug in to confirm, but I feel like I've seen logging output from a Perspective binding that didn't have that info. I'm trying to use this debug wrapping to generate a coverage report among other things and if some calls don't have context then that gets less useful.

I'd have to see a particular log to be sure. In general, MDC works as a 'stacking' context side-channel for information - so we're in charge of manually putting that context onto the stack as well as removing it afterwards (so it doesn't 'leak' onto other things executing on the same thread after). Bugs are definitely possible, though my expectation is that most of them will have shaken out by now in core areas like Perspective components.