Spatial Modeler Discussions

Gain, exchange, and share knowledge, or discuss topics with other Hexagon Geospatial Product pioneers and experts to get the most out of Hexagon Geospatial products.
Showing results for 
Search instead for 
Do you mean 
Reply
Highlighted
Occasional Contributor
Posts: 9
Registered: ‎11-02-2015
Accepted Solution

Logging from custom Operators from within Python script

Hi there,

 

In our custom Oerators for SpatialModeler we are logging plenty of usefull information into the IMAGINE Session log (using esmg_LogMessage()).

When those Operators are called from within a Python script, each line we are 'writing' to the Session log actually gets displayed as a popup window. Execution only continues when the user confirm the dialog. 

This would be an example:

logging_as_dialog.png

 

 

Is there a way to redirect the messages to the Session log so that they are not displayed as dialogs?

 

We are using IMAGINE 2020.

 

Best regards,

Thomas

 

Staff
Posts: 14
Registered: ‎04-26-2018

Re: Logging from custom Operators from within Python script

esmg_LogMessage() is a function in the core libraries intended to support interaction of applications with the ERDAS IMAGINE session manager. Applications which wish to interact with the session manager must also call one of the functions like esmg_JobInit() function, however it is not recommended that such calls be made from within Operators as that makes an assumption about where they will be run.

 

Because python.exe is not connected to the session the esmg_LogMessage() function falls back to the dialog. So at present there is no API in the tookit to send output to a log file, in this scenario.

 

 

Technical Evangelist
Posts: 39
Registered: ‎09-10-2015

Re: Logging from custom Operators from within Python script

It is true that it is not recommended to use esmg_LogMessage in a Spatial Modeler Operator implementation. There are, however, a couple of remedies, depending on your needs.

 

A substitution of eerr_MessageLog for esmg_LogMessage may give you the desired behavior. The messages will continue to go to the session log when running your operator in the Spatial Modeler Editor, but will be suppressed when running in python. You will also need to substitute the appropriate Eerr_MessageClass for the Esmg_LogMessageType and eliminate the Eerr_ErrorReport ** argument.

 

If you require finer control (e.g., you still need the messages to go somewhere when running under Python), you can use the logging macros available in Log.h. This will give you the full control of log4cplus (ERDAS IMAGINE 2020 uses v1.1.1.2 of log4cplus).

 

The typical approach in this case is to define a static logger for your operator using the ERDAS_LOG_GETLOGGER(<logger identifier>) macro and use the various ERDAS_LOG_<loglevel> macros to log messages. When running under SM Editor these messages will still go to the session log (but you may have to adjust the default configuration in etc/SessionLogging.cfg since messages below WARN are suppressed by default). See the log4cplus documentation for additional information.

Technical Evangelist
Posts: 39
Registered: ‎09-10-2015

Re: Logging from custom Operators from within Python script

One more tidbit worth mentioning...the reason you might be getting the default behavior for esmg_LogMessage in your python script is that you haven't actually joined/started an ERDAS IMAGINE session from python. This will not happen by default (similar to how a C++ program using the defunct IMAGINE Developers Toolkit functions will not join an ERDAS IMAGINE session unless you call the right functions).

 

If you put

 

from imagine import job

job.init()

into your python script (often not desirable), your script will run as a job, joining any already running ERDAS IMAGINE session and then your custom operator's esmg_LogMessage calls will simply be emitted to that session's session log.

Occasional Contributor
Posts: 9
Registered: ‎11-02-2015

Re: Logging from custom Operators from within Python script

Thanks for your detailed answer. eerr_MessageLog is doing to job for now. I will explore later the log4cplus options.

Thomas