Closed ischoegl closed 2 years ago
@speth ... while I know how C++ outputs logging messages (fairly apparent in src/base/application.h
) and have a hunch for how information flows (re-routed to wrappers.h
and - I believe - captured with _logger
in utils.pyx
?), I'm not sure that I am following how those are actually caught and displayed in Python? It would be great if you could point me in the right direction.
Right, the lines
cdef CxxPythonLogger* _logger = new CxxPythonLogger()
CxxSetLogger(_logger)
in utils.pyx
create a new instance of PythonLogger
(defined in wrappers.h
) and assign that as the logwriter
member of the global Application
object. That PythonLogger
object is then used for all calls to writelog()
.
I think for this purpose, what you would want is an additional method for the Logger
class that's used to handle calls to warn_user
instead of having that just call writelog
. The implementation of this method in PythonLogger
would then need to call something like PyErr_WarnEx
(see https://docs.python.org/3/c-api/exceptions.html#issuing-warnings). For the base Logger
class, I guess you would probably want to direct the warning to std::cerr
.
:tada: Ha, that comment made all the difference! Thanks!! I guess the magic sauce in the current implementation is in PySys_WriteStdout
and PyRun_SimpleString
. This was hiding in plain sight! Exact same thing (i.e. PyRun_SimpleString
) can be used for triggering warnings.
While exceptions raised in the C++ core are already captured and thrown by the Python API, the same is not true for the warning system. I had originally added this as a discussion topic, but it may be more appropriate to discuss this as an issue (feature request).
Discussed in https://github.com/Cantera/enhancements/discussions/112