是否有可能在GCC中获得正在编译的函数的名称?
Is it possible to get the name of the function being compiled in GCC?
对不起,我甚至不知道该怎么问。
我想生成一些正在运行的方法或函数的跟踪日志,但我不想在每个方法中都写命令名(我真的很懒!)。
的例子:
我的当前代码:
void doSomething() {
TRACE("doSomething");
// now do something!
system("pause");
}
我想做什么:
void doSomething() {
TRACE;
// do something!
system("pause");
}
期望输出(两个程序):
doSomething
Press any key to continue...
如果你需要我说得更清楚,请告诉我。我尽量说得清楚些
我会这样开始:
#define TRACE(message) TRACE_IMPL(__FILE__, __LINE__, __PRETTY_FUNCTION__, message)
void TRACE_IMPL(const char *file, int line, const char *function, const char *message) {
...
}
int main() {
TRACE("help");
}
我的下一步是将message更改为格式字符串,并在Trace上启用printf()
样式va_args。它看起来像:
#include <cstdio>
#include <stdarg.h>
#define TRACE(format, ...) TRACE_IMPL("File: %s Line: %d Function: %s Message: " format "n", __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
void TRACE_IMPL(const char *format, ...) {
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
int main() {
TRACE("help");
TRACE("Canary %d", 2);
}
输出:
[8:18pm][wlynch@watermelon /tmp] ./foo
File: foo.c Line: 14 Function: int main() Message: help
File: foo.c Line: 15 Function: int main() Message: Canary 2
如果你愿意,你也可以使用c++流:
#include <iostream>
#define TRACE LogImpl(__FILE__, __LINE__, __PRETTY_FUNCTION__)
class LogImpl {
public:
LogImpl(const char *file, int line, char *function) {
std::cout << "File: " << file << " Line: " << line << " Function: " << function << " Message: ";
}
~LogImpl() {
std::cout << "n";
}
LogImpl(LogImpl const &) = delete;
LogImpl & operator=(LogImpl const &) = delete;
template <typename T>
LogImpl & operator<<(T const & obj) {
std::cout << obj;
return *this;
}
};
int main() {
TRACE << "help";
TRACE << "Canary " << 2;
}
相关文章:
- 跨模板化函数编译的静态变量
- 无法使用 LoadObject() 函数编译 UE4 Actor。
- 为什么在使用转换构造函数编译代码时需要 const 复制构造函数?
- 函数编译,即使它不接受整数
- 使用C 模板函数编译时间递归
- 安卓 JNI - 'raw'函数编译失败
- C++就地析构函数编译警告
- 对 TR1 使用 boost 时提升数学特殊函数编译错误
- 显式复制构造函数编译错误
- 使用SSE内部函数编译一个简单的c++程序
- 函数编译时错误
- 模板函数编译错误
- C++:使用类型名作为基的模板类调用函数编译时错误
- 将 MATLAB 函数编译成可以在 linux 终端上运行的东西,w.out MATLAB
- boost::绑定不要使用成员模板函数编译
- C++函数编译错误
- 加速模板函数编译
- 如何在C++中使用辅助函数编译以下flex文件
- 如何让g++使用move构造函数编译c++11代码
- 同时将一组函数编译为.LIB和.DLL