INTO-CPS-Association / pyfmu

https://into-cps-application.readthedocs.io/en/latest/submodules/pyfmu/docs/index.html
7 stars 2 forks source link

Add logging to FMI slave #12

Closed clegaard closed 4 years ago

clegaard commented 4 years ago

Currently, there is no way to log from Python to the FMI interface. A method, say log(category, msg) could be added to the fmislave base class.

Proposed implementation

One way to do this is:

How to pass messages from Python to C

  1. Store unread log messages in a queue in Python.
  2. Implement method in baseclass for reading these.
  3. Call method from C code and log using logger callback

Adding log categories to model description

Manual approach

Add a method to declare categories to baseclass:

declare_log_category('autograd error')

Then to use:

self.log('autograd error', 'derivative is undefined')

Automatic discovery of log categories by code inspection

Python standard library has methods to grab source code and parse.

inspect.getsource(...)
ast.parse(...)

As such it would be possible to inspect the code for log statements and their categories. This would also be useful to warn if a category is missing.

Questions

Use of Python logging lib

Investigate if it makes sense to use Pythons built in logging for this. The benefit of this would familiarity and the possibility of passing log messages from third party tools through FMI interface.

import logging
log = logging.getLogger("my-logger")
log.info("Hello, world")

https://docs.python.org/3/library/logging.html

stdout and stderr

What should happen to print statements?

print("something bad has happened due to ....")