Determining line number and perl file name from C ++

I work with Perl embedded in our application. We have installed quite a few C ++ functions that are called from Perl. One of them is the logging function. I would like to add the file name and line number of the Perl file that called this function to the log message.
I know that on the Perl side I can use the "caller ()" function to get this information, but this function is already used in hundreds of places, so I would prefer to change the C ++ side whether this information is passed to C ++ XSUB functions, and if so, how can I get from it?

Thanks.

+4
source share
2 answers

This should work:

char *file; I32 line; file = OutCopFILE(PL_curcop); line = CopLINE(PL_curcop); 

Controls (cops) are one of two options OP_NEXTSTATE and op_DBSTATE, which (loosely speaking) are separate statements. They contain important information for lexical status and error messages. At run time, PL_curcop is set to the most recently executed command, and therefore can be used to determine our current state.

- cop.h

+2
source

Is it possible to call perl inline functions from XS? I admit, I do not know.

If not, you can always do something like this:

 sub logger { _real_logger(caller, @_) } 

Assuming logger is what is called your function (and you rename your C ++ XS function to _real_logger ). You can also do this, presumably if you need to hide in the call tree:

 sub logger { unshift @_, caller; goto &_real_logger; } 

which, of course, is the normal goto form used in AUTOLOAD .

This, of course, will add overhead, but it probably won't be a big problem for the logging function.

+1
source

Source: https://habr.com/ru/post/1385998/


All Articles