c++ - 当确实需要 ifdef 进行日志记录时

标签 c++

我目前正在处理一个很旧的 C++ 代码。在这段代码中我可以找到:

#ifdef LOG
LOG_DEBUG(bla bla bla);
#endif

在调试头文件中:

#ifdef LOG
LOG_DEBUG(X) ....code....
#else
LOG_DEBUG(X) (void) 0
#endif

现在我的问题是:是否有任何理由在代码的每个语句中重复 ifdef?我想这种策略仅在一种情况下非常值得,即当我需要执行一些艰苦的准备工作只是为了记录一些东西时。

#ifdef LOG
int a = mySuperComplicatedFunction();
....other worth code here with "a"...
LOG_DEBUG(the result here);
#endif

我错过了什么吗?

最佳答案

Now my question is: is there any reason why the ifdef is repeated in every statement in the code?

不,在那些情况下它是完全多余的,你不会遗漏任何东西。特别是,您正确识别了实际需要它的一种情况。

关于c++ - 当确实需要 ifdef 进行日志记录时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51670133/

相关文章:

C++ : TCP Server "bind" function failed (errno 98) if I do not wait enough time between two consecutive app launch

c++ - STL中的 float 比较,BOOST

c++ - TravisCI 中的混合 C 和 C++ 项目

c++ - 如何使用继承自基类的成员函数?

c++ - 多线程服务器中的EnterCriticalSection和大量共享数据

c++ - 重载 operator<< 和 operator+ 导致错误

c++ - 通过 atomic::load() 访问 vector 时发生访问冲突

C++多维数组

c++ - 静态链接可以导致执行一些初始化例程吗?

c++ - 如何在 C++ 中快速将大缓冲区写入二进制文件?