Qt/C++ 中的反射
我刚刚开始使用 Qt,发现 QMetaObject 在我看来非常好/方便。
现在我想知道是否可以使用 Qt/标准 C++ 或任何其他 C++ 库获取当前函数名称、调用函数名称或当前堆栈等信息。
我需要这个用于调试/记录目的。
例如,在 C# 中,您可以调用 MethodBase.GetCurrentMethod() 来获取当前方法。这样的事情真的很方便。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个宏PRETTY_FUNCTION,它返回一个格式良好的函数名称。
它在 GCC 和其他一些编译器中可用。
There is a macro PRETTY_FUNCTION that returns a nice formatted function name.
It is available in GCC and maybe some other compilers.
不要认为你可以在 C++ 中做到这一点。我认为您想要做的是创建一个使用 预定义宏:
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:
没有独立于平台的方法可以做到这一点。遗憾的是(或者幸运的是,这取决于您的观点)标准 C/C++ 不包含此类内省功能。
下面和其他地方讨论了获取堆栈跟踪的各种方法。
如何在 C 中获取堆栈跟踪?
您可能需要调试符号信息来获取您可能不希望安装在客户系统上的任何有意义的堆栈跟踪。
如果您确实想在没有安装符号信息的情况下了解 Windows 上日志条目的堆栈,那么您可以编写一个小型迷你转储文件 (MiniDumpWriteDump)。可以将其保存在不带符号的客户系统上,然后使用该版本的 .pdb 符号在开发人员的系统上进行分析。这还将显示局部变量值,并且您可以选择包含所有过程数据(这使得小型转储文件变得巨大)。更多信息见下文和互联网上的其他地方。
http://blogs.msdn.com/b/joshpoley/archive/2008/05/19/prolific-usage-of-minidumpwritedump-automating-crash-dump-analysis-part-0.aspx< /a>
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
如果您绝对想要执行此类操作(并且您的编译器支持它),您可以在编译器中启用 RTTI。但在C++标准的最初设计中(98之前)并没有在运行时定义任何“元”数据。
http://en.wikipedia.org/wiki/Run-time_type_information
如果纯粹是对于日志记录,请使用编译器定义的宏(例如 FILE 和 LINE )。如果您想在运行时做更高级的事情,我认为制作自己的“元数据”系统可能是一个更好的计划(或者在网上寻找现有的系统)。
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).