Detecting if iOS app is run in debugger

前端 未结 8 1868
北恋
北恋 2020-11-27 13:45

I set up my application to either send debugging output to console or a log file. Now, I\'d like to decide with in the code whether

  • it is run in the debugger
8条回答
  •  广开言路
    2020-11-27 14:03

    I usually go for a much more simple solution; is the binary compiled with optimizations?

    A debug build is not optimized, and logs are nice. A release build should have optimizations and not as many logs. You can check for this with the __OPTIMIZE__ symbol.

    For logging I use this setup for logg-functions:

    #ifdef __OPTIMIZE__ 
      #define CWLog(...)
      #define CWLogDebug(...)
      #define CWLogInfo(...)
    #else
      #define CWLog(...) NSLog(__VA_ARGS__)
      #define CWLogDebug( s, ... ) NSLog( @"DEBUG <%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
      #ifndef LOG_INFO
        #define CWLogInfo(...)
      #else
        #define CWLogInfo( s, ... ) NSLog( @"INFO <%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
      #endif
    #endif
    #define CWLogWarning( s, ... ) NSLog( @"WARNING <%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    #define CWLogError( s, ... ) NSLog( @"ERROR <%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
    

提交回复
热议问题