Emit debug messages from nopython mode

During development it is often useful to emit debug messages from our code. However, sacrificing nopython mode can lead to significant slowdowns. It is not completely clear to me what kind of output functions are supported in nopython mode. Based on the documentation, I assume that print is supported (only to stdout? are the optional args supported?). This could be useful assuming that the file argument is supported: my code runs njitted functions many times per minute and I would prefer to send debug messages to a logfile.

Alternatively, I’m wondering what would be the best way to use methods of an existing logging.Logger instance in nopython mode, or if that is at all possible without extreme performance penalties. From another thread (I can’t link it) it seems that emitting messages at compile time is quite simple. However, it would be convenient to use logging methods inside compiled code. I can see two ways to go about this:

  1. Use the objmode context manager to evaluate the logging calls with the Python interpreter. I have tried this and it seems to have a fairly high performance impact.
  2. Reimplement or wrap logging.Logger in numba, maybe using @jitclass or some other extension mechanism. I wonder if anyone has tried this or if it sounds like a good approach. I’m not particularly attached to cpythons logging implementation and would be happy with something much slimmer if necessary. The main features I use are logging at one level to console and at a lower level to a file, and it’s nice to control the handlers from a central location rather than giong through and commenting out print statements all over the place.

Cheers

hi @adigitoleo , one trick I’ve used in the past is to capture stdout before executing the jitted function and re-directing it to a file, that’s possible in pure python.

Luk