在开发模式为 DEBUG 或 RELEASE 之后切换 on/off 日志记录(例如 zlog)

Switch on/off logging (e.g. zlog) following dev-mode being DEBUG or RELEASE

我正在用 ANSI C 编写一个小型项目。我还打算使用 zlog 等日志系统。但是,我不希望项目在发布时依赖于 zlog,而只在开发时需要它。当我在 DEBUG 和 RELEASE 模式之间切换时,切换 on/off 那些日志语句的正确方法是什么?

例如,我有一个配置文件指定

mode = DEBUG | RELEASE

当 mode = RELEASE 时,我想要我的项目源文件中的所有这些说明,例如

#include "zlog.h"

zlog_init("init_file") 

等消失,因此该项目不需要 link 与 zlog。

当然,可以使用宏包围每个 zlog 用法来实现这一点,但这似乎很尴尬,我想有更优雅的方法来切换 on/off 那些日志记录指令?

我建议添加一个源文件,它定义了您引用的所有 zlog 接口,并由指定它是处于 DEBUG 还是 RELEASE 模式的宏保护。

例如:

#ifdef NDEBUG
/* It's RELEASE mode */
int zlog_init(const char *confpath) { return 0; }
int zlog_reload(const char *confpath) { return 0; }
void zlog_fini(void) {}
/* ... */
#endif

调试时,文件只是空的(由于未定义NDEBUG宏),zlog_*() links到真正的zlog库; RELEASing 时,不要 link 到真正的 zlog,而是 link 到这个虚拟源文件中定义的无操作函数。