1

I was googling a method to hide my NSLog statements from my release version and found this thread

Where in a comment is mentioned...

defaults write my.bundle.identifier SandboxModeFlag -bool YES

This sounds great, it doesn't rely on me forgetting to remove my own flag saying I'm in debug mode.

Can anyone tell me how to detect SandModeFlag in code?

Community
  • 1
  • 1
Jules
  • 7,568
  • 14
  • 102
  • 186

2 Answers2

1

Personally I would have two build targets: debug & release. The code would not use NSLog directly but would instead use a macro such DGBTRACE(xxx) and that would map to NSLog in the debug build, and to nothing in the release build.

Example:

#if defined(DEBUG)
#define DBGTRACE(x, ...) NSLog(@"DBG %s(%d): %@", __FILE__, __LINE__, [NSString stringWithFormat:x, ## __VA_ARGS__]);
#else
#define DBGTRACE(x, ...)
#endif

void func(void)
{
  int val1 = 27;
  NSString *val2 = @"james";
  DBGTRACE(@"val1=%d, val2=%@", val1, val2);
}
jarmod
  • 71,565
  • 16
  • 115
  • 122
  • I've never created a macro in xcode and you provide some further details of how I'd use this and determine debug and release ? – Jules Feb 01 '11 at 14:56
  • Have updated my answer to give an example of how you might do it. This method also prints out the current source filename and line of code, which is generally valuable information. Of course you need to have DEBUG defined in debug builds, and undefined in release builds. – jarmod Feb 01 '11 at 20:48
1

That command line sets NSUserDefaults, so it could be read with:

[[[NSUserDefaults] standardUserDefaults] boolForKey:@"SandboxModeFlag"]

Here's a link to the Apple documentation.

Edit

To elaborate, that gives you the ability to turn the logging on and off without using a different build. Of course, checking this at runtime is an overhead that may be undesirable.

If using a different build is acceptable, then macros are the way forward. Here's a post describing your situation, with some nice examples.

paulbailey
  • 5,328
  • 22
  • 35
  • That does answer the question, but I don't want to have to read a the `NSUserDefaults` file each time or in each screen, i'll give you a little time to respond. – Jules Feb 01 '11 at 15:47
  • I've added a link to a post describing how to use macros in your situation. – paulbailey Feb 01 '11 at 15:59
  • Is this what you mean ? define DebugLog(s, …) NSLog((@”%s %s:%d ” s), func, basename(FILE), LINE, ## VA_ARGS); – Jules Feb 01 '11 at 16:08
  • No, where is that from? If you include the code as described in the blog post, you can literally just replace NSLog with DLog where you want logging only in the debug build, and ALog where you always want it to log. – paulbailey Feb 01 '11 at 16:12