如何从计划中删除日志调试语句 [英] How to remove log debugging statements from a program

查看:103
本文介绍了如何从计划中删除日志调试语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的boost ::登录作为我的C ++程序的记录器。
在开发过程中我经常使用这种方式,例如:

I am using boost::log as a logger for my C++ program. During development I often use it this way, for example:

#define LOG(severity) BOOST_LOG_SEV(boost::logger::get(), (severity))
#define LOG_ERR LOG(Severity::error)
#define LOG_INFO LOG(Severity::info)
#define LOG_DEBUG LOG(Severity::debug)

其中, BOOST_LOG_SEV 提供的设备的boost ::登录,而 LOG LOG_ERROR LOG_INFO LOG_DEBUG 是由我定义的快捷键。

where BOOST_LOG_SEV is the facility provided by boost::log, while LOG, LOG_ERROR, LOG_INFO, LOG_DEBUG are shortcuts defined by me.

在短, BOOST_LOG_SEV 动态比较用传递给宏本身的严重程度的当前调试严重性来决定是否发射输出或不

In short, BOOST_LOG_SEV dynamically compares the current debugging severity with the severity passed to the macro itself to decide whether to emit the output or not.

这是用上面的宏用于调试的程序的一个例子:

This is an example of a program which use the above macros for debugging purposes:

// set at compile time
#define MAX_LOG_SEVERITY Severity::debug

int main() {
   // Print all the messages with a
   // Severity <= MAX_LOG_SEVERITY defined before compiling
   boost::log::set_severity(boost::logger::get(), MAX_LOG_SEVERITY); // set_severity() is fictitious just to give you an idea

   // bool err = ...
   if (err)
      LOG_ERR << "An error occurred";
   else 
      LOG_INFO << "Okay;
   LOG_DEBUG << "main() called";
}

现在,释放程序时对生产环境,调试与严重性::调试级别的消息并不真正意义。我可以通过简单地减少 MAX_LOG_SEVERITY 严重性::信息从输出隐藏起来,但问题是,通话通过 LOG_DEBUG 之前,将不会从可执行code被删除。这对效率和物体的大小不好的影响。

Now, when releasing the program for a production environment, debugging messages with a Severity::debug level do not really make sense. I could hide them from the output by simply decreasing MAX_LOG_SEVERITY to Severity::info, but the problem is that the calls made by LOG_DEBUG will not be removed from the executable code. This has a bad impact on both efficiency and object size.

在code是完全日志记录语句,我很想preserve简单的使用运营商的LT;&LT;()

The code is full of logging statements and I'd really like to preserve the simple use of operator<<().

无需接触这些语句本身,是有 LOG_DEBUG 没有更好的宏定义/把戏,将使pre-处理器或编译器(在其优化) 跳过或当 MAX_LOG_SEVERITY 设置为严重性::调试删除调试语句常数?

Without touching those statements themselves, is there any better macro definition/trick for LOG_DEBUG that would make the pre-processor or the compiler (during its optimizations) "skip" or "remove" the debugging statements when MAX_LOG_SEVERITY is set to the Severity::debug constant ?

推荐答案

虽然我不能做出任何保证,这样的事情可能会奏效。这取决于你的优化器,你是否拥有在参数运营商的LT副作用;&LT;

While I can't make any guarantees, something like this might work. It depends on what your optimizer does and whether or not you have side effects in the parameters to operator<<.

#ifdef NO_LOG_DEBUG

static class DevNull
{
} dev_null;

template <typename T>
DevNull & operator<<(DevNull & dest, T)
{
    return dest;
}

#define LOG_DEBUG dev_null

#else

#define LOG_DEBUG LOG(Severity::debug)

#endif

这篇关于如何从计划中删除日志调试语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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