I am tying to generalize calling NSLog and avoid having to comment-out the calls when I am finished debugging.
I have:
#define USE_ADLog 0
#define USE_RPLog 0
#define USE_DLLog 1
void ConsoleADLog(NSString *message, ...);
void ConsoleRPLog(NSString *message, ...);
void ConsoleDLLog(NSString *message, ...);
and, for example:
void ConsoleADLog(NSString *message, ...) {
#if (USE_ADLog)
va_list optionalArgs;
va_start(optionalArgs, message); // after the parm = message
va_end(optionalArgs);
NSLog(message, optionalArgs);
#endif
}
So far, so good ... however, as soon as I call, for example,:
ConsoleDLLog(@"parm1 = %@, parm2 = %@", parm1, parm2);
which call is inside a secondary thread, I bomb. I thought?? that va_start, va_end were thread safe.
... or is the problem %@ ... I know %f works ???
Apparently not!, so how do I make them thread safe ... plain ole
NSLog(@"whatever %@", whateverParm)
wor开发者_如何转开发ks, but not the function above.
Thanks,
this is why many variadic functions include variants which accept va_list
s.
see NSLogv
.
#if DEBUG == 0
#define DebugLog(...)
#elif DEBUG == 1
#define DebugLog(...) NSLog(__VA_ARGS__)
#endif
Then invoke it using DebugLog(@"Uh oh: %@", someArgument);
. If DEBUG
is set to 1
, the preprocessor will emit NSLog(@"Uh oh: %@", someArgument);
. If it's set to 0
, it will not emit anything.
精彩评论