如何绕过<<像"#ifndef DEBUG"一样进行呼叫C ++中的宏? [英] How to bypass a << calling as if "#ifndef DEBUG" macro in c++?
问题描述
我为自己编写了一个日志记录库,它接受两种形式的调用.
I coded a little logging-lib for myself, and it accepts two forms calling.
一个喜欢普通的函数调用,另一个喜欢std :: ostream<<操作员输出. 然后我分别为每个日志级别定义了一些宏,如下所示:
The one likes a normal function calling, the other likes a std::ostream << operator output. And then I defined a few of Macros respectively for every log-levels as following:
#ifdef DEBUG
#define LOG_DEBUG( strLogBody ) appendLog( leon_log::LogLevel_e::ellDebug, std::string( __func__ ) + "()," + ( strLogBody ) )
#define LOG_INFOR( strLogBody ) appendLog( leon_log::LogLevel_e::ellInfor, std::string( __func__ ) + "()," + ( strLogBody ) )
#define log_debug ( Logger_t( LogLevel_e::ellDebug ) << __func__ << "()," )
#define log_infor ( Logger_t( LogLevel_e::ellInfor ) << __func__ << "()," )
//...more for other log-levels
#else
#define LOG_DEBUG( strLogBody )
#define LOG_INFOR( strLogBody ) appendLog( leon_log::LogLevel_e::ellInfor, ( strLogBody ) )
#define log_debug ( Logger_t( LogLevel_e::ellDebug ) )
#define log_infor ( Logger_t( LogLevel_e::ellInfor ) )
//...more for other log-levels
#endif
当在客户代码空间中定义了"DEBUG"宏时,两者均形成用于DEBUGGING目的的产品目标代码. 如果未定义"DEBUG"宏,则前一种形式(如函数调用)不会产生任何二进制代码来加速我的应用程序(如我所愿),而后者形式仍然会产生代码.
When there is a "DEBUG" macro defined in the client-code space, both forms product objective codes for DEBUGGING purpose. When there is no "DEBUG" macro defined, the former form(likes a function call) do not products any binary codes to speed up my app( as I want), while the latter form products codes anyway.
有没有办法,我可以绕过那些<<"调用与那些普通函数调用一样吗?
Is there a way, I can bypass those "<<" callings as same as doing with those normal function callings?
到目前为止,我正在使用一种类似于给定的@Botje的解决方案.所不同的只是,我的是Logger_t的朋友函数,而Botje的是成员函数. 跟随的是我的:
So far, I'm using a solution that similars to the one @Botje given. The difference just is that mines is a friend-func of the Logger_t, while Botje's is a member-func. Follows is mine:
template <typename T>
inline Logger_t& operator<<( Logger_t& lgr, const T& body ) {
if ( lgr.m_LogLevel >= g_ellLogLevel )
dynamic_cast<std::ostringstream&>( lgr ) << body;
return lgr;
};
但是我想GCC仍然会产生调用二进制代码的函数,即使这些都是"no-op"调用.我不知道如何拆卸目标编,所以无法确认.
But I guess that GCC still products the function calling binary-codes, even though those are all of "no-op" calls. I don't know how to dis-assemble my target prog, so I can't confirm it.
谢谢!请原谅我难看的英语!
Thanks! Pls forgive my ugly English!
推荐答案
为什么在非调试版本中不将operator<<
设置为非操作:
Why not make operator<<
a no-op in non-debug builds:
#ifndef DEBUG
struct Logger_t {
template <class T>
Logger_t& operator <<(const T& o) { return *this; }
};
#endif
编译器应该能够将整个log_debug << ... << ...
链减少为零.
The compiler should be able to reduce an entire log_debug << ... << ...
chain to nothing.
如果要避免在<<以及为Logger_t
和
If you want to avoid function calls in the << chain as well, define operator bool
for Logger_t
and
#define log_debug false && Logger_t{}
这篇关于如何绕过<<像"#ifndef DEBUG"一样进行呼叫C ++中的宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!