需要一种在编译时删除和添加日志功能的巧妙方法 [英] Need a clever way of removing and adding log functions at compile time

查看:82
本文介绍了需要一种在编译时删除和添加日志功能的巧妙方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我一直在尝试寻找一种从C ++程序中删除和添加日志记录函数调用的巧妙方法.日志函数调用一个"logger"类,但我希望删除这些日志级别,而使维护人员的头痛最少.

我们已经有正常的文件#undef WARN等程序,但我正在尝试对此进行改进.基本上,尝试编写如下代码:

Hi,

I have been trying to find a clever way of removing and adding logging function calls from a C++ programme. The log functions call into a "logger" class, but I would like to remove the different log levels with the least amount of headache for the maintainers.

We already have the normal file #undef WARN, etc. procedure, but I am trying to improve this. Basically, trying to write code such that:

MODULE_LOG ("MyModule", ALARM);

int MyClass::Function (int param)
{
   ALARM ("Print");
   WARN ("No Print");
}



理想情况下,它将编译为:



This would ideally compile into just:

int MyClass::Function (int param)
{
   logger::alarm_print_function ("Print");
}



因此,"WARN"定义已被删除.

如果维护者想要查看WARN消息,则他们可以更改代码,例如:



Hence, the "WARN" definition has been removed.

If the maintainer wanted to see the WARN messages, then they could just change the code such that:

MODULE_LOG ("MyModule", ALARM, WARN);

>

理想情况下,它将编译为:



This would ideally compile into just:

int MyClass::Function (int param)
{
   logger::alarm_print_function ("Print");
   logger::warn_print_function ("No Print");
}



这样,维护者可以只更新一个模块.如前所述,这可以通过常规的WARN的#undef/#define等来完成.

我想知道是否有更聪明的方法?


谢谢.



This way the maintainer can just update a single module. As said, this can be done through the normal #undef/#define of WARN, etc.

I was wondering if there was a more clever way of doing this?


Thanks.

推荐答案

您可以使用以下宏:
You could use macros such as:
#define ALARM(x) logger::alarm_print_function(x)
#define WARN(x)


这将做你想要的.然后只需更改宏定义即可根据需要生成不同的代码.

但是,有时最好保留所有代码的启用状态,并使用运行时标志生成消息,例如:


which will do what you want. Then just change the macro definitions to generate different code as required.

Sometimes however, it is better to leave all the code enabled and use runtime flags to generate messages such as:

static bool ALARM = false;
static bool WARNING = false;
void MyClass::Function (int param)
{
    if (ALARM)
        logger::alarm_print_function ("Print");
    if (WARNING)
        logger::warn_print_function ("No Print");
}


并添加另一个函数以在运行时将相关变量设置为true.


and add another function to set the relevant variables to true at run time.


这篇关于需要一种在编译时删除和添加日志功能的巧妙方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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