• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments?


  • Subject: Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments?
  • From: Ken Thomases <email@hidden>
  • Date: Wed, 18 Apr 2012 01:55:52 -0500

On Apr 17, 2012, at 6:18 PM, Wim Lewis wrote:

> On 17 Apr 2012, at 4:01 PM, Laurent Daudelin wrote:
>
>> You're probably right, Christiaan, as it doesn't work any better.
>>
>> So, how would a macro like this be written?
>
> Macros work by being textually substituted into the place where they're referenced, so presumably you want something like this:
>
> #define FileLog(format, ...) [NSString stringWithFormat:@"\n  %s [Line %d] \n       %@", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:(format), ##__VA_ARGS__]]
>
> Then FileLog() expands to an expression whose type is (NSString *), and you can use it in places where you might use other expressions (like function calls), eg:
>
>  [logger logMessage:FileLog("foo %d", foovar) atLevel:LOG_DEBUG];
>
> Alternately, if you actually do want the macro to expand into a statement, then the do{ ... }while(0) trick is the customary way to avoid unexpected results.
>
> My usual approach for this particular case is to write a function-like macro like this:
>
> #define FileLog(format, ...)  FileLogAtLocation(__PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:(format), ##__VA_ARGS__])
>
> and have FileLogAtLocation compose the final logged string. Or even to make the FileLogAtLocation() function variadic.

Laurent suggested he just wanted to log something as normal, so it may be simpler to use something like:

#define FileLog(format, ...) NSLog(@"%s:%d: " format, __PRETTY_FUNCTION__, __LINE__, ##__VAR_ARGS__)

Then he can use it as a drop-in replacement for NSLog which prepends the function and line number.  The format has to be a string literal (either a C-style string literal or an Objective-C string literal will work) to work with this, but that's a good idea, anyway.

Regards,
Ken


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Macro to output an NSString containing function name, line number plus a string with a variable number of arguments? (From: Laurent Daudelin <email@hidden>)
 >Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments? (From: Marco S Hyman <email@hidden>)
 >Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments? (From: Christiaan Hofman <email@hidden>)
 >Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments? (From: Laurent Daudelin <email@hidden>)
 >Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments? (From: Wim Lewis <email@hidden>)

  • Prev by Date: Have not Agvtool
  • Next by Date: Re: Would you expect to see a warning about things that aren't supported in the deployment target OS?
  • Previous by thread: Re: Macro to output an NSString containing function name, line number plus a string with a variable number of arguments?
  • Next by thread: Have not Agvtool
  • Index(es):
    • Date
    • Thread