вівторок, 4 жовтня 2011 р.

Logging in iOS projects

A standard NSLog function is a quite inconvenient way of doing logging - mainly for these reasons:

a) we are not able to easily turn it off in Release builds
b) NSLog does not show the function name / line number information before the output log message.

A good drop in replacement, which solves these problems is this set of macros, which were stolen by someone, who then shared it with me.  Original author of the macros I don't know, but I guess the link to this post removes some guilt from me.  And also the credit should probably go to this SO answer

So here is the (extended compared to original) set of macros we use in our projects:


#ifdef DEBUG  
 #     define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);  
 #else  
 #     define DLog(...)  
 #endif  
 // ALog always displays output regardless of the DEBUG setting  
 #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);  
 //Convenience macros, to output just one value of certain type:  
 #define VLog(v) DLog(@#v @"=%@",v)  
 #define fLog(v) DLog(@#v @"=%f",v)  
 #define dLog(v) DLog(@#v @"=%d",v)  
 //Convenience macro to output CGRect value in a human readable format:  
 #define CGRectLog(v) DLog(@#v @"=(%f,%f,%f,%f)",v.origin.x,v.origin.y,v.size.width,v.size.height)  
 #define CGSizeLog(v) DLog(@#v @"=(%f,%f)",v.width,v.height)  
 #define CGPointLog(v) DLog(@#v @"=(%f,%f)",v.x,v.y)