Jules
Jules

Reputation: 7776

Obj-C, using ifndef and target parameters to only printf debug lines?

I've been happily using a replacement for NSLog called DLog so that I don't have to worry about debugging being left in a release build.

#ifndef NDEBUG
#   define DLog(FORMAT, ...) printf("%s\n", 
      [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

#else
#   define DLog(...)
#endif

However, I'd like to do some a bit more complicated, I have numerous targets with debug parameters and I'd like to include two parameters to enable me to get debugging information.

I had hoped I could use, something like this...

#ifndef NDEBUG
#   define DLog(FORMAT, ...) printf("%s\n", ...
#else
#ifdef TESTFLIGHT && FREEMIUM
#   define DLog(FORMAT, ...) printf("%s\n", ...
#else
#   define DLog(...)
#endif

But it's not as simple as that.. I'm probably missing something obvious?

I've googled the problem but can't find the AND operator for this purpose and nested IF statements seems a problem too ?

Upvotes: 1

Views: 687

Answers (3)

Ben Zotto
Ben Zotto

Reputation: 71048

When you're using the existence of the definition like this, to combine use this syntax:

#if defined TESTFLIGHT && defined FREEMIUM 

(#ifdef is shorthand for #if defined). Use #elif for "else if" to cascade without nesting.


You can use the boolean operator straight-up like you want if you're defining the constant to be equal to something (e.g. 1) rather than just being "defined". e.g.

#define TESTFLIGHT 1
#define FREEMIUM   1

...

#if TESTFLIGHT && FREEMIUM

Upvotes: 2

fbernardo
fbernardo

Reputation: 10124

You should use "#elif" to make an else if, instead of nested ifs.

And you can can use the #if defined(TESTFLIGHT) && defined(FREEMIUM), as said on the other answers. So :

#ifndef NDEBUG
#   define DLog(FORMAT, ...) printf("%s\n", ...
#elif TESTFLIGHT && FREEMIUM
#   define DLog(FORMAT, ...) printf("%s\n", ...
#else
#   define DLog(...)
#endif

Upvotes: 0

DarkDust
DarkDust

Reputation: 92384

You need to use "normal" #if:

#if defined(TESTFLIGHT) && defined(FREEMIUM

#ifdef FOO is simply short for #if defined(FOO)

You're also missing an #endif. So complete, it's:

#ifndef NDEBUG
#  define DLog(FORMAT, ...) printf("%s\n", ...
#else
#  if defined(TESTFLIGHT) && defined(FREEMIUM)
#    define DLog(FORMAT, ...) printf("%s\n", ...
#  else
#    define DLog(...)
#  endif
#endif

Upvotes: 0

Related Questions