开发者

iPhone Dev: How to get string format attributes like NSLog

I'm trying to wrap NSLog function just to add some info every time I log something, but I have a problem.

The NSLog declaration is

void NSLog(NSString *format, ...) __attribute__((format(__NSString__,1,2)))

this allow to have multiple parameters in call as

NSLog(@"first %@ second %@ third %d,string,string,number);

My declaration function is similar

  void LogUtil(id sender, int level, NSString *str, ...) __attribute__((format(__NSString__,3,4)))开发者_Python百科

and the implementation is just

void LogUtil(id sender, int level, NSString *str, ...){
  if(level>=LEVEL){
    NSLog(@"<%@> %@",sender,str);
  }
}

So I'm able to call it right as

NSLog(self, 1, @"first %@ second %@ third %d,string,string,number);

but in this case the attributes won't evaluated.

So, I think I have to do something in my implementation to formatting str with paramters but how??

thaks in advance


There is full description of that in: http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html


Thanks...this is working implementation

    void LogUtil(id sender, int level, NSString *str, ...){

      if(level>=LEVEL){
        va_list args;
        va_start(args,str);
        NSString *format=[[NSString alloc] initWithFormat:str arguments:args];
        va_end(args);  
        NSLog(@"<%@> %@",sender,format);
        [format release];
      }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜