[英]C++ and writing enum values into Windows registry
I am writing a function as follows: 我正在写一个函数如下:
bool abc::GetLoggingStatus() {
//true or false is returned
int value;
if (regKey->HasValue("LoggingStatus")) {
regKey->QueryValue("LoggingStatus", &value);
if (value == 1)
return true; //no logging possible
else
return false;
}
regKey->SetValue("LoggingStatus", 1);
return true;
}
Logging level is defined as: 记录级别定义为:
typedef enum {
Entry,
Exit,
Debug,
Warning,
Notification,
Error
} TYPE;
What I need if I select 1 the levels for logging must be shown namely debug,error ... In regedit and if 0 nothing should be shown and logging be disabled. 如果我选择1记录级别,我需要显示的是调试,错误...在regedit中,如果0则不显示任何内容并禁用日志记录。
You can't create dropdown menus in regedit, but what you can do is create a new entry called LoggingLevel
. 您无法在regedit中创建下拉菜单,但您可以创建一个名为
LoggingLevel
的新条目。 This entry is ignored if LoggingStatus
is 0. LoggingLevel
is a string defining the level. 如果
LoggingStatus
为0,则忽略此条目LoggingLevel
是定义级别的字符串。
If you want to convert this string back to an enum, the easiest way is to create a map from string to your Enum type: 如果要将此字符串转换回枚举,最简单的方法是创建从字符串到Enum类型的映射:
std::map<std::string, TYPE> typeMap;
typeMap["Warning"] = Warning;
...
In your code you query the logging level: 在您的代码中,您查询日志记录级别:
char* level;
regKey->QueryValue("LoggingLevel", level);
TYPE theLevel = typeMap[level];
Of course you need to do appropriate error checking. 当然,您需要进行适当的错误检查。
edit 编辑
You should add two function to get the log settings, shouldLog()
and getLevel()
. 您应该添加两个函数来获取日志设置,
shouldLog()
和getLevel()
。 The log function would then look like: 日志功能如下所示:
void log(Logger* logger, TYPE type, string sClassName, string sMethodName, string sMessage = "") {
if (!logger || !abc::shouldLog()) {
return;
}
TYPE curLevel = abc::getLevel();
bool shouldLog = false;
if (type == Warning && (curLevel == All || curLevel == Warning) ...) {
shouldLog = true;
}
if (shouldLog) {logger->WriteEntry(sClassName, sMethodName); }
}
If you want to avoid complicated if-structures, you could also try and map the enums to a value and compare that. 如果你想避免复杂的if结构,你也可以尝试将枚举映射到一个值并进行比较。 For example Warning = 1 and ALL = 0. Then you can check if
curLevel < type
to see if the logger should log. 例如,Warning = 1且ALL = 0.然后,您可以检查
curLevel < type
是否记录,以查看记录器是否应该记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.