11-11-2019 05:19 AM
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:
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.
Solved! Go to Solution.
11-13-2019 10:02 AM
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.
11-13-2019 11:08 AM
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 v18.104.22.168 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.
11-13-2019 11:52 AM
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.