简体   繁体   中英

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.

You can't create dropdown menus in regedit, but what you can do is create a new entry called LoggingLevel . This entry is ignored if LoggingStatus is 0. LoggingLevel is a string defining the level.

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:

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() . 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. For example Warning = 1 and ALL = 0. Then you can check if curLevel < type to see if the logger should log.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM