0

Context

I'm writting a debug class in Qt for a project and I want to know in which class I am so now I write:

MyClass::function1()
{
   DEBUG_IN("[MyClass] Enters function1()")
   ....do something
   DEBUG_OUT("[MyClass] Exits function1()");
}

EDIT: DEBUG_IN() and DEBUG_OUT() are global functions. They are not part of the class MyClass

The question

Is it possible to get the name of the class where DEBUG() is called?

(and the bame of the function where DEBUG() is called?)

so that I could just do:

MyClass::function1()
{
   DEBUG_IN("function1()")
   ....do something
   DEBUG_OUT("function1()");
}

and

DEBUG_IN(QString text)
{
   qDebug() << qPrintable("[")
            << qPrintable(getClassName())
            << qPrintable("] Enters ")
            << qPrintable(text);
}

Thx a lot

ochurlaud
  • 410
  • 5
  • 14
  • Try looking at http://stackoverflow.com/questions/1666802/is-there-a-class-macro-in-c. If you aren't familiar with how macro expansion works I can try to clarify that for you. – Qartar Apr 12 '15 at 17:39
  • @qartar: You gave me the same answer as the 2 other propositions, however I DEBUG() is not part of MyClass (I just edited the question because it wasn't clear enough) – ochurlaud Apr 12 '15 at 17:47
  • You can pass the expanded macro directly to the function. e.g. `DEBUG_IN(__PRETTY_FUNCTION__, ...)` – Qartar Apr 12 '15 at 18:09

2 Answers2

2

The closest to what you want would be __PRETTY_FUNCTION__. This includes fully qualified function name and signature.

Edit:

You should use it like this:

#define DEBUG_IN_PRETTY(...) DEBUG_IN(__PRETTY_FUNCTION__, __VA_ARGS__)

then call DEBUG_IN_PRETTY (obviously DEBUG_IN needs to take the extra argument). This will expand the macro at the call site.

Puppy
  • 144,682
  • 38
  • 256
  • 465
2

Indeed there is.

typeid(*this).name()

Will give you a readable classname. It is not specified how this name is constructed so your compiler might not give the exact class name, but it should be good for debugging purposes.

As DEBUG_IN and DEBUG_OUT are free functions you need to pass the class type as an argument. A simple template should do the trick

template<typename CLASSNAME>
void DEBUG_IN(QString msg, CLASSNAME* that)
{
   ...  typeid(*that).name()

}
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • The problem is that it gives me only that i'm in DEBUG_IN but not the class from where DEBUG_IN is called. I just edited my question because it wasn't clear, it seems – ochurlaud Apr 12 '15 at 17:46
  • how would you use DEBUG_IN() ? You would always have to add `this` in parameter aren't you? – ochurlaud Apr 12 '15 at 17:54
  • I don't believe that it's required to give you a readable classname at all. You could get a mangled name, which I'm pretty sure is *mandatory* for Itanium. – Puppy Apr 12 '15 at 18:45
  • @Puppy I'd be surprised if a compiler gave a name that would not allow you to discern the class. – Captain Giraffe Apr 12 '15 at 20:28
  • I don't get the discussion between Puppy and Captain Giraffe – ochurlaud Apr 12 '15 at 22:40