0

I just started with Qt and discovered the QMetaObject wich is very nice/handy in my opinion.

Now I wondered if it is possible to get information like current function name, calling function name or current stack with Qt/standard C++ or any other C++ library.

I need this for debugging/logging purposes.

In C# for example you can call MethodBase.GetCurrentMethod() to get the current method. Something like this would be really convenient.

Karsten
  • 1,814
  • 2
  • 17
  • 32

4 Answers4

3

There is a macro PRETTY_FUNCTION that returns a nice formatted function name. It is available in GCC and maybe some other compilers.

Jens Luedicke
  • 964
  • 2
  • 8
  • 20
  • I figured out that `__func__` is the C99 standard to get the function name. `__FUNCTION__` should work on most compiler, too. `__PRETTY_FUNCTION__` and `Q_FUNC_INFO` both work with Qt and return the function signature, but that might not be the case on all OS. – Karsten Mar 31 '11 at 11:57
1

Don't think you can do that in C++. I think what you want to do is to create a logging macro which uses the predefined macros:

__FUNCTION__ __LINE__ __FILE__
ronag
  • 49,529
  • 25
  • 126
  • 221
1

There is no platform independent way of doing this. Sadly (or fortunately depending on your point of view) standard C/C++ does not include those sort of introspective capabilities.

There's discussion of various methods of obtaining a stack trace below and elsewhere on SO.

How can one grab a stack trace in C?

You will likely require debug symbol information to get any meaningful stack trace which you may not want to install on a customer's system.

If you really want to know the stack for a log entry on Windows without having symbolic information installed then you could write a small mini dump file (MiniDumpWriteDump). This could be saved on a customer's system without symbols and then analysed on a developer's system with the .pdb symbol for that build. This will also show local variable values and optionally you can include all process data (which makes the mini dump files huge). More information below and elsewhere on the interweb.

http://blogs.msdn.com/b/joshpoley/archive/2008/05/19/prolific-usage-of-minidumpwritedump-automating-crash-dump-analysis-part-0.aspx

Community
  • 1
  • 1
persiflage
  • 1,154
  • 12
  • 22
  • StackWalker looks very promising, but is it possible to use it with Qt? – Karsten Mar 31 '11 at 12:01
  • StackWalker should work for Qt but on Windows only. Stack walking is specific to OS and compiler rather than any libraries you happen to be using (like Qt). – persiflage Mar 31 '11 at 12:03
0

You can enable RTTI in your compiler if you absolutely want to do this kind of stuff (and your compiler supports it). But in the original design of the C++ standard (pre 98) does not define any "meta" data in the runtime.

http://en.wikipedia.org/wiki/Run-time_type_information

If it is purely for logging use the macros defined by your compiler (like FILE and LINE ). If you want to do more advanced stuff in the runtime I think making your own "metadata" system might be a better plan ( or looking for an existing one on the net).

Fabio Fracassi
  • 3,791
  • 1
  • 18
  • 17
Joris Mans
  • 6,024
  • 6
  • 42
  • 69
  • RTTI is a part of the official C++ standard (both old and new), and is enabled by default on most compilers. Compilers that don't support RTTI are non conforming. That said, RTTI is sometimes turned of, especially in embedded development – Fabio Fracassi Mar 31 '11 at 12:03
  • 1
    should have used the word "design" instead of "standard" You are correct and I stand corrected. – Joris Mans Mar 31 '11 at 13:20