Search code examples
c++perlxs

Determing line number and file name of the perl file from within C++


I am working with Perl embedded in our application. We have installed quite a few C++ functions that are called from within Perl. One of them is a 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 on the Perl side I can use the "caller()" function to get this information, but this function is already used in hundreds of locations, so I would prefer to modify the C++ side, is this information passed to the C++ XSUB functions and if so how would I get at it?

Thanks.


Solution

  • This should work:

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

    Control ops (cops) are one of the two ops OP_NEXTSTATE and op_DBSTATE, that (loosely speaking) are separate statements. They hold information important for lexical state and error reporting. At run time, PL_curcop is set to point to the most recently executed cop, and thus can be used to determine our current state.

    — cop.h