views:

48

answers:

2

Hi all, 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)))

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

+1  A: 

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

Guy Ephraim
wonderful!!! it works great...I post my new code if can help others
Achille
@Achile, don't forget to accept @Guy's answer. Just click the "v" near his answer.
Michael Kessler
thanks Michael, I'm newbie here
Achille
+1  A: 

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];
      }

    }
Achille
I suggest you to put the entire function code inside the "if" statement `if(level>=LEVEL)`...
Michael Kessler
sorry..it's so..I wrong to write it here.I'll edit
Achille