2

I wanted to override the existing NSLog mechanism.

NSLog should have different modes.NSLOG(, " Description"); can be numbers from 1, 2 or 3 to indicate whether the loglevel is CRITICAL, MAJOR or MINOR.

If the loglevel = 1, I want Overridden NSLOG along with function name and line number.

Why Printing i=5 is not getting Printed in below sample code? How to fix this?

#define NSLog(loglevel, ...) \
 if(loglevel == 1) \
   NSLog( @"%s [Line %d] %s", __PRETTY_FUNCTION__, __LINE__, 
       ##__VA_ARGS__);


 - (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
       // Override point for customization after application launch.
        NSLog(1,"Hello how r u");
        int i=5;
        NSLog(1,"Printing i=%d", i);

        return YES;
  }

OUTPUT

[AppDelegate application:didFinishLaunchingWithOptions:] [Line 32] Hello how r u 2017-04-06 18:15:17.303 LogLevel[3620:160181] -[AppDelegate application:didFinishLaunchingWithOptions:] [Line 34] Printing i=%d

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • 1
    If you want to use specific logging levels you can use `os_log`: http://stackoverflow.com/q/39754199/558933 – Robotic Cat Apr 06 '17 at 13:00
  • 2
    More details here: http://doing-it-wrong.mikeweller.com/2012/07/youre-doing-it-wrong-1-nslogdebug-ios.html – VitorMM Apr 06 '17 at 13:01
  • 1
    I would suggest adding your own function that uses `NSLog` instead of defining over it, specially if you are modifying the parameters it takes. – EmilioPelaez Apr 06 '17 at 14:19

0 Answers0