Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I wrap NSLog in a block that takes a variable number of arguments?

I’m writing an Objective-C library and I’d like it to offer a simple pluggable logging mechanism, so that the library user can turn the logging on and off. I thought an interesting way to do this would be a block property on the library classes:

typedef void (^Logger)(NSString *fmt, ...);
@property(copy) Logger logger;
logger(@"Foo, %@.", self);

But I don’t know how to pass the variable argument list to NSLog:

const Logger SimpleLogger = ^(NSString *fmt, ...) {
    // what goes in here?
};
like image 783
zoul Avatar asked Nov 27 '25 10:11

zoul


1 Answers

Ah, I completely missed NSLogv:

const Logger SimpleLogger = ^(NSString *fmt, ...) {
    va_list arglist;
    va_start(arglist, fmt);
    NSLogv(fmt, arglist);
    va_end(arglist);
};
like image 113
zoul Avatar answered Nov 29 '25 00:11

zoul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!