Macros for Xcode

These are some of the macros I use with Xcode:

CMLog: I use this macro to replace NSLog:

#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);

When you use this macro, it outputs text to the console, including the class and method from where it was called. So, if you call this macro from the class MyAppDelegate and the method applicationDidFinishLaunching,

CMLog(@"My iPhone is an %@, v %@", [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion]);

you get this in the console:

2009-01-05 10:06:28.957 MyApp15173:20b] -[MyAppDelegate applicationDidFinishLaunching:]:My iPhone is an iPhone Simulator, v 2.2

MARK: I use this macro to just output the name of class and method it was called from. Useful to know if a method was called.

#define MARK	CMLog(@"%s", __PRETTY_FUNCTION__);

START_TIMER and END_TIMER: These are for benchmarking a method:

#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);

Just put a START_TIMER at the beginning of the block to benchmark, and END_TIMER and the end, and you’ll get the timing.

- (NSData *)loadDataFromURL:(NSString *)dataURL
{
  START_TIMER;
  NSData *data = [self doSomeStuff:dataURL];
  END_TIMER(@"loadDataFromURL");
  return data;
}

Will output:

2009-01-05 10:31:37.943 MyApp[15283:20b] -[MyAppDelegate loadDataFromURL:]:loadDataFromURL Time = 3.636021

Now wrap all these declarations in your precompiled header, using a conditional flag. This flag will be set to 1 for Debug, and 0 for release, so your app will not fill the console with output.

#if DEBUG==1
#define CMLog(format, ...) NSLog(@"%s:%@", __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);
#define MARK	CMLog(@"%s", __PRETTY_FUNCTION__);
#define START_TIMER NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
#define END_TIMER(msg) 	NSTimeInterval stop = [NSDate timeIntervalSinceReferenceDate]; CMLog([NSString stringWithFormat:@"%@ Time = %f", msg, stop-start]);
#else
#define CMLog(format, ...)
#define MARK
#define START_TIMER
#define END_TIMER(msg)
#endif

Just add this to your Debug target setting

OTHER_CFLAGS = -DDEBUG=1

and this to your Release target setting:

OTHER_CFLAGS = -DDEBUG=0

8 réflexions au sujet de “Macros for Xcode”

  1. __PRETTY_FUNCTION__ is a magic variable defined by GCC. At compile time, it is replaced by the method definition.

    So, if you have a method:

    – (void) myMethod:(NSString *)myString param:(NSString *)myParam

    __PRETTY_FUNCTION__ will be implicitly declared by GCC, as if, immediately following the opening brace of each method the declaration:
    static const char __PRETTY_FUNCTION__[] = « myMethod:param: »;

    was added.

    Stephan

  2. #define CMLog(format, …) NSLog(@ »%s:%@ », __PRETTY_FUNCTION__,[NSString stringWithFormat:format, ## __VA_ARGS__]);

    can be written as

    #define CMLog(format, …) NSLog(@ »%s: » format, __PRETTY_FUNCTION__, ## __VA_ARGS__]);

Laisser un commentaire