I have a C++ function that is being called from many Python functions via Boost::Python. When the C++ function detects bad arguments I want to write a log message and continue processing. In that log message I would like to note the Python module and line number that called into C++. How can I do this?
I am able to throw an exception from C++ which is translated into a Python exception that I can catch, but that aborts the C++ function which I cannot have.
For example, let's say I want to log a warning message if factorial() receives a number less than one. Ignore the fact that it can easily handle the case (and does)--my boss wants a warning anyway. ;)
This code can be called from Python directly or from other C++ functions that get called from Python so the C++ stack trace itself isn't very helpful.
int factorial(int n) {
if (n < 1) {
logMsg("invalid n passed to factorial() at %s, line %d",
<python-module>, <python-line-number>);
}
return n <= 1 ? 1 : n * factorial(n - 1);
}
I'm hoping that the Boost::Python library provides this ability from C++.