1

I have a class in my C++ code similar to the following:

class myClass
{
public:
  void method1(int a, int b, std::string str);
};

Other classes can instantiate an object from myClass and call method1.

void caller()
{
    obj1->method1(12, 4, "sample");
}

I want to log all the callers of myClass (function name, file name, line number). One possible solution is this:

class myClass
{
public:
  method1(int a, int b, std::string str, const char *_function = __builtin_FUNCTION(), const char *_file = __builtin_FILE(), int _line = __builtin_LINE());
};

which is using __builtin_xxx as default arguments. This solution has multiple shortcomings:

  1. It is an ugly solution
  2. __builtin_xxx is only available in gcc version > 4.8
  3. We have to add three default parameters to method1
  4. IDE shows the default parameters on auto-completion that are not meant to be provided by the user!

Another solution is using __LINE__, __FILE__ and __func__ that is basically very similar to the previous solution. They are not defined outside of function scope, and they should be used like this:

void caller()
{
    obj1->method1(12, 4, "sample", __func__, __FILE__, __LINE__);
}

Here is a working example for both solutions.

Is there any better solution to log the caller when the user calls method1 on myClass object. By a better solution I specifically mean not to change the method1's declaration by adding three more parameters!

ManiAm
  • 1,759
  • 5
  • 24
  • 43
  • Possible duplicate of [C/C++ need a clever way to track function calls](https://stackoverflow.com/questions/3315248/c-c-need-a-clever-way-to-track-function-calls) – user835611 Oct 01 '17 at 06:01
  • You probably want to clarify better...as the macro solution does not sacrifice performance...The problem with the IDE is really an IDE problem and not a C++ problem. What do you say? – Jay Rajput Oct 01 '17 at 07:17
  • If you don't care about performances, you can use the callstack: http://stackoverflow.com/questions/3899870/print-call-stack-in-c-or-c – Colin Pitrat Oct 01 '17 at 08:06

2 Answers2

0

Another ugly solution, but I'm using...

Use macros to automatically add __LINE__ __FILE__ ...etc. things into parameters.

For example

#define Method(param0,param1) Method(param0,param1,__LINE__)

It has a lot of problem, if you want macros work as normal function, you has to do a lot of things, and it still may not works.

I use it to help me log errors.

  • I don't consider this answer as a better solution! I have updated my question to clarify more. – ManiAm Oct 01 '17 at 07:08
0

Looks like a duplicate of Print the file name, line number and function name of a calling function - C Prog

I'd pass the data to the function through parameters (maybe get the help of a macro)

int info(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
int debug(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
int error(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
And to call them

info(__FILE__, __LINE__, __func__, ...);
debug(__FILE__, __LINE__, __func__, ...);
error(__FILE__, __LINE__, __func__, ...);
Note: __func__ is C99; gcc, in mode C89 has __FUNCTION__
Jay Rajput
  • 1,813
  • 17
  • 23
  • I don't consider this answer as a better solution! I have updated my question to clarify more. – ManiAm Oct 01 '17 at 07:08