0

I have some code like this:

#define FORCE_DEBUG_MODE [[[NSUserDefaults standardUserDefaults] valueForKey:@"forceDebugMode"] isEqualToString:@"1"]

#if defined DEBUG_MODE || defined FORCE_DEBUG_MODE
#define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s),  ##__VA_ARGS__] )
#else
#define DLog( s, ... ) 
#endif

I read from some article outside to use the preprocess marcos to define the DEBUG_MODE, however I would like to override the mode if I set a NSUserDefaults value to something.

These code got no errors. But seems that no matter what the FORCE_DEBUG_MODE is, defined FORCE_DEBUG_MODE equal to true. What I want is something like FORCE_DEBUG_MODE == 1

Please tell me if I can do this, and How?

2 Answers 2

1

you defined FORCE_DEBUG_MODE right above your check to see if it is defined. To check if it is true then you must just do || FORCE_DEBUG_MODE however that will as well be always true as It is only seeing if there is some value under the macro - this is all done in the preprocessor meaning none of this code will be ran or reran at runtime - these defines will define what code is actually compiled, not what code is ran

Sign up to request clarification or add additional context in comments.

3 Comments

I have a secret settings page that I want to enable the log there. Anyway to do this?
Yea, so check at runtime if some preference setting is set or however you want to check that, and then at runtime decide if it should log. Note that that means you would do if (FORCE_DEBUG_MODE) in code so that it would do runtime checks to see if it should log rather than actually changing the code that is compiled
I am using this way and things go just fine. Thank you~
1

#if defined just checks whether the value has been defined, not what value it is. You can just use #if to check if a value is defined to a value other than zero.

This will still not work in your case though. The preprocessor runs before the compiler, so you cannot use it to switch in code at runtime. You need to perform the check for the defaults value at runtime.

1 Comment

alright, quite clear. Why I want to override is my company publish the app using enterprise program to our client. We would like to monitor the Log sometimes by their side to find out some unexpected behavior. But we don't want the log access by another.(No matter who have a mac with Xcode can do this) What can I do for this situation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.