Wie erhalte ich einen formatierten NSString aus format und va_list?
Ich entwickle eine statische Bibliothek, die an andere Entwickler verteilt wird, die möglicherweise Debug-Anweisungen benötigen. Ich habe also mehrere Protokollierungsebenen.
Um ein ständiges Erscheinen von @ zu vermeid
if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){
NSLog(@"log this");
}
Ich habe eine Reihe von Wrappern für Protokollierungsfunktionen erstellt. Eine vereinfachte Version sieht so aus:
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
Aber jetzt möchte ich in myLog auf die vollständig formatierte Zeichenfolge zugreifen, um etwas anderes zu tun.
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);
}
}