有没有一种方法可以在C ++中不用宏来获取行号? [英] Is there a way to get the line number with out a macro in c++?

查看:47
本文介绍了有没有一种方法可以在C ++中不用宏来获取行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个记录器,我希望记录器还记录调用它的行,函数和文件.我尝试了 #define my_logger(format,...)_log(format,__LINE__,__PRETTY_FUNCTION__,__FILE__,__VA_ARGS __),但是这会给我带来一些问题(我无法轻易重载)它,我不能将其放在命名空间中,而不是最可移植的,等等).

I am writing a logger and I would like the logger to also record which line, function, and file called it. I have tried #define my_logger(format, ...) _log(format, __LINE__, __PRETTY_FUNCTION__, __FILE__, __VA_ARGS__) which works however, this leads to several problems for me (I can't easily overload it, I can't put it in a namespace, not the most portable, etc).

有没有一种方法可以声明一个普通的函数,例如 void my_logger(const char * format,...),并且在函数定义中让它知道从某处调用它的行如果存在这样的宏,是否等同于 __ LINE_FUNCTION_WAS_CALLED_FROM __ ?它至少需要在GCC上运行,并且实际上应该是跨平台的.

Is there a way where I can declare a normal function like void my_logger(const char* format, ...) and in the function definition have it know the line it was called from with something equivilent to __LINE_FUNCTION_WAS_CALLED_FROM__ if such macro existed? At a minimum it needs to work on GCC and really should be cross-platform.

跨平台,我的意思是x86和ARM上的Linux和Windows

By cross-platform, I only mean Linux and Windows on x86 and ARM

推荐答案

不能有 __ LINE_FUNCTION_WAS_CALLED_FROM __ 这样的宏.这是不可能的.

There cannot be such macro as __LINE_FUNCTION_WAS_CALLED_FROM__. There's no way it could work.

从C ++ 20开始,有一种方法可以通过传递默认初始化的 std :: source_location :

Since C++20, there is a way to get the line, function, etc. into a normal function by passing default initialised std::source_location:

void log(std::string_view message,
         std::source_location location = std::source_location::current());

但是,由于可变参数要依靠默认参数,所以实现起来有些棘手:

However, since it relies on a defaulted parameter, variadic arguments are a bit tricky to implement: How to use source_location in a variadic template function?

在C ++ 20之前,宏是您唯一的可移植选项.

Prior to C++20, a macro is your only portable option.

我看到您在宏中调用了名为 _log 的函数.该标识符保留给全局名称空间中的语言实现.您可能应该给该函数起另一个名字,以避免未定义的行为.

I see that you call a function named _log in your macro. That identifier is reserved to the language implementation in the global namespace. You probably should give the function another name to avoid undefined behaviour.

log 也保留在全局名称空间中.无论如何,您都应该在自定义名称空间中声明所有名称(自然是该自定义名称空间除外).

log is also reserved in the global namespace. You should declare all your names in a custom namespace anyway (except that custom namespace naturally).

[宏是] ...不是最便于携带的

[the macro is] ... not the most portable

如果要用 __ func __ 替换 __ PRETTY_FUNCTION __ ,则该宏将可移植到所有符合标准的编译器中-尽管 __ func __ 在实现之间可能有所不同.

If you were to replace __PRETTY_FUNCTION__ with __func__, then the macro would be portable to all standard conforming compilers - although the exact string given by __func__ may differ between implementations.

这篇关于有没有一种方法可以在C ++中不用宏来获取行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆