C++ 是否可以在GCC中获取正在编译的函数的名称?

C++ 是否可以在GCC中获取正在编译的函数的名称?,c++,gcc,C++,Gcc,对不起,我甚至不知道该怎么问这个问题 我想用正在运行的一些方法或函数生成跟踪日志,但我不想在每个方法中都写入命令名(我真的很懒!) 例如: 我当前的代码: void doSomething() { TRACE("doSomething"); // now do something! system("pause"); } 我想做的是: void doSomething() { TRACE; // do something! system("paus

对不起,我甚至不知道该怎么问这个问题

我想用正在运行的一些方法或函数生成跟踪日志,但我不想在每个方法中都写入命令名(我真的很懒!)

例如:

我当前的代码:

void doSomething() {
    TRACE("doSomething");
    // now do something!
    system("pause");
}
我想做的是:

void doSomething() {
    TRACE;
    // do something!
    system("pause");
}
预期输出(两个程序):


如果你需要我说得更清楚,请告诉我。我会尽量说清楚。

我会从以下内容开始:

#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");
}
我的下一步是将消息更改为格式字符串,并在跟踪上启用
printf()
style 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);
}

如果你想要的话,你也可以使用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;
}
#包括
#定义跟踪LogImpl(\uuuuu文件\uuuuuu、\uuuuuu行\uuuuuuu、\uuuuuuuu漂亮的函数\uuuuuuu)
类LogImpl{
公众:
LogImpl(常量字符*文件、int行、字符*函数){

std::我想你需要
\uuuuuuu func\uuuuu
。底部有一个解释和示例。如果TRACE是一个宏,那么它可以替换文本,包括
\uuuu FILE\uuuuuuu
\uuuuu LINE\uuuuuu
,以便在日志中使用。@TonyD谢谢!这正是我想要的!请将您的评论作为答案发布到日志中。@TonyDd我会接受的!@ViníciusGobboA.deOliveira:另外,因为您使用的是g++,所以您也可以研究一下。@sharth谢谢,这帮了大忙!您可能需要研究
-finstrument函数
命令行标志,以自动插入跟踪代码。
printf
?接受stre的参数更安全、更可扩展我对一个
iostream
@TonyD:这很公平。但是我可能会放弃整个样式,只使用某种类型的日志对象。如果你想写这样的东西,我想Vinícius会发现它很有趣。单独一个日志对象无法帮助捕获函数名(或文件、行…),所以这个“样式”是必要的。我已将此标记为重复,因此现在无法添加其他答案!想法非常简单:
#define TRACE(MSG)do{log_stream@TonyD:对,但随后您的格式变得更加复杂,或者您使用了一种奇怪的语法
TRACE(“金丝雀”)或者您使用了一种奇怪的语法-,除非您习惯了它;-)。
[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
#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;
}