【发布时间】:2019-07-26 20:24:15
【问题描述】:
当开发人员在 C 和 C++ 中添加调用函数 log_message 的代码时,我需要跟踪某个日志级别的每条日志消息。
这是为了项目中的日志消息管理,避免太多开发者在源代码中添加太多不遵循相同约定的消息。
例如:
file1.c:
log_message("Module:Failed to create the file %s\n", file_name);
file2.c:
log_message("Failed to create the XXXX file %s\n", file_name);
MESSAGE_1000 在另一个文件 message.h 中定义,该文件由我们的消息数据库生成。 消息.h:
const char* MESSAGE_1000 = “Module: Failed to create the file %s.\n”
文件预编译后:
file1.c 变为:
#include "message.h"
...
log_message(MESSAGE_1000,file_name)
file2.c 变为:
#include "message.h"
...
log_message(MESSAGE_1000,file_name)
当他们需要向日志消息数据库添加一些新消息时。
他们可以使用一个可以被钩子识别的固定名称的函数。
例如:
file1.c:
log_message_new("My new message:%s", message)
编译期间:
message.h 更新为:
#defined MESSAGE_1001 "My new message:%s", message
在我们的消息数据库中,添加了一个新条目。
MESSAGE_1001="My new message:%s"
为什么会有消息数据库?它们适用于非开发人员。他们可以查看客户可能看到的所有日志消息。
这里我需要一个钩子来捕获和修改源代码。
你有什么想法吗?
【问题讨论】:
-
C 和 C++ 的答案肯定会有所不同。请选择一个。
-
请展示您想要的假设示例。我不明白你的问题。 “原始字符串而不是字符串变量” 是什么意思?它始终是一个变量,无论是文字变量还是字符串变量。
-
请把你描述的代码写成minimal reproducible example(“他们习惯于调用log_message函数”)并显示出来。
-
什么是“字符串变量”?它与“原始字符串”有何不同?
-
在 C++ 中我会想到 std::string,在 C 中“字符串变量”是一个非常不清楚的表达式。也许决定这是关于哪种语言实际上会有所帮助。
标签: c++ c gcc logging compilation