Je développe une bibliothèque statique qui sera distribuée à d'autres développeurs, qui pourraient avoir besoin d'instructions de débogage. J'ai donc plusieurs niveaux de journalisation.Comment obtenir un format NSString formaté et va_list?
Afin d'éviter l'aspect constant de
if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){
NSLog(@"log this");
}
J'ai créé un ensemble d'enveloppes de fonction d'enregistrement. Une version simplifiée ressemble à ceci:
void myLog(int logLevel, NSString *format, va_list args){
if((loggingLevelCurrentlySet >= logLevel)){
NSLogv(format, args);
}
}
void myLogLevel1(NSString *format, ...){
va_list args;
va_start(args, format);
myLog(1, format, args);
va_end(args);
}
void myLogLevel2(NSString *format, ...){
va_list args;
va_start(args, format);
myLog(2, format, args);
va_end(args);
}
etc.
Mais maintenant, je veux, à partir de mylog, l'accès à la chaîne entièrement formated faire quelque chose d'autre avec.
void myLog(int logLevel, NSString *format, va_list args){
NSString *fullString = [NSString stringWithFormat:format, args]; //crashes when args is anything but an empty list
CFStringRef cfsr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, format, args); //also crashes
//want to use the string here
if((loggingLevelCurrentlySet >= logLevel)){
NSLogv(format, args);
}
}
Merci. Quelle est la raison pour laquelle la méthode de classe de raccourci ne fonctionne pas? – executor21
Dans (Objective-) C, il est possible de capturer une liste d'arguments de longueur variable dans un tel 'va_list', cependant vous ne pourrez plus jamais l'utiliser en appelant un autre objet. Cela ne marchera tout simplement pas, très ennuyeux. En utilisant la macro cependant, le préprocesseur la gérera correctement et il compilera comme si vous tapiez juste 'myLog (1, format, arg1, arg2, arg3);' cela fonctionnera. C'est encore plus rapide, car il n'y a pas de code supplémentaire à exécuter au moment de l'exécution. – Joost