How to get the file name and a line number of a function call?

Qualphey

I have two functions in different source files:

a.cpp

void A()
{
    B();
}

b.cpp

void B()
{
    std::cout << "B() called from file: " << ??? << " line: " << ??? << std::endl;
}

How can I get the file name and line number of the call?

JaredPar

In general you can do this automatically by hiding your function behind a macro call which passes allong the __FILE__ and __LINE__ values

void _B(const char* file, int line) { ... } 
#define B() _B(__FILE__, __LINE__)

This is by no means a foolproof solution though. It's possible for developers to call _B directly or for _B to be called from generated code, assembly, etc .... where there may be no meaningful file / line number

OP asked for an example with arguments

void _C(int p1, char p2, const char* file, int line) { ... } 
#define C(p1, p2) _C(p1, p2, __FILE__, __LINE__)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get name of calling function, line number and file name in c++

From Dev

How to find line number and file name of error

From Dev

How to get exact line number and file name from command 'info frame' in tcl if the type is proc or eval?

From Dev

How to get current file and line number in Ruby?

From Dev

Is there a way to get line number and function name in Swift language?

From Dev

Get the line number of file

From Dev

How to call a function if the function's file name is known and is a string?

From Dev

How to find encapsulating function name from line number

From Dev

How to Get Uploaded File Name with glob() Function?

From Dev

How to print out the file name and line number of the test in python nose?

From Dev

How to include line number and file name Console.WriteLine output?

From Dev

how to find "File name and line number of Exception" in runtime

From Dev

How to return both file name and line number with find ... -exec grep?

From Dev

How to call the callback function with class name defined in another javascript file

From Dev

Get name of file for command line

From Dev

How to get filename and line number of where a function is called in Node?

From Dev

How can I get the line number that a function was called from?

From Dev

How can I get the line number that a function was called from?

From Dev

How to read a line from text file, call function on that line and move to second line and so on?

From Dev

How to get mocha with chai assert to report file/line number?

From Dev

How to get source line number from .ll file LLVM

From Dev

How to get the number of bytes in just one line of a file?

From Dev

How to get the first non comment line number in a file?

From Dev

How to get source line number from .ll file LLVM

From Dev

How to get line number from a text file in vb.net

From Dev

How get random number value and attach it to file name

From Dev

Trace without file name and line number in Haxe

From Dev

Retrieve the current javascript file name and line number

From Dev

C++ How to call a function from another file with the same name as a function in another file when both are included?

Related Related

  1. 1

    Get name of calling function, line number and file name in c++

  2. 2

    How to find line number and file name of error

  3. 3

    How to get exact line number and file name from command 'info frame' in tcl if the type is proc or eval?

  4. 4

    How to get current file and line number in Ruby?

  5. 5

    Is there a way to get line number and function name in Swift language?

  6. 6

    Get the line number of file

  7. 7

    How to call a function if the function's file name is known and is a string?

  8. 8

    How to find encapsulating function name from line number

  9. 9

    How to Get Uploaded File Name with glob() Function?

  10. 10

    How to print out the file name and line number of the test in python nose?

  11. 11

    How to include line number and file name Console.WriteLine output?

  12. 12

    how to find "File name and line number of Exception" in runtime

  13. 13

    How to return both file name and line number with find ... -exec grep?

  14. 14

    How to call the callback function with class name defined in another javascript file

  15. 15

    Get name of file for command line

  16. 16

    How to get filename and line number of where a function is called in Node?

  17. 17

    How can I get the line number that a function was called from?

  18. 18

    How can I get the line number that a function was called from?

  19. 19

    How to read a line from text file, call function on that line and move to second line and so on?

  20. 20

    How to get mocha with chai assert to report file/line number?

  21. 21

    How to get source line number from .ll file LLVM

  22. 22

    How to get the number of bytes in just one line of a file?

  23. 23

    How to get the first non comment line number in a file?

  24. 24

    How to get source line number from .ll file LLVM

  25. 25

    How to get line number from a text file in vb.net

  26. 26

    How get random number value and attach it to file name

  27. 27

    Trace without file name and line number in Haxe

  28. 28

    Retrieve the current javascript file name and line number

  29. 29

    C++ How to call a function from another file with the same name as a function in another file when both are included?

HotTag

Archive