Defining a macro in PCH file giving warnings - iphone

I have following macro in my PCH file.
#ifdef DEBUG
#define MYLOG(...) NSLog(__VA_ARGS__)
#else
#define MYLOG(...) MYSERVERLOG(MYObject.enableLogging, __VA_ARGS__)
#endif
#define MYSERVERLOG(iLog, ...) iLog ? NSLog(__VA_ARGS__) : TRUE
Now, no matter if I put DEBUG=0 or DEBUG=1, it always go in first clause. But if I use "if" instead of "ifdef" for DEBUG in PCH then it works fine but then I get warnings on all my MYLOG statements saying "Expressing results unused".
How can I get rid of this situation?

I am guessing you only get the warning if DEBUG=0. The problem is that, after running through the preprocessor, the compiler sees code like this:
...
MYObject.enableLogging ? NSLog(#"your log",...) : TRUE;
...
The compiler is fine with letting you ignore the results of a function, assuming that the function did whatever you wanted and the result isn't useful. However, when you go through the trouble of calculating a value in your code with the ternary operator, it expects that you want the result of that calculation. If you know that you will never use the result of this macro, you can cast it to void to ignore it:
#define MYSERVERLOG(iLog, ...) ((void)(iLog ? NSLog(__VA_ARGS__) : TRUE))
If you might use the results of the macro, then you will have to do the cast every time you use the macro and ignore the result, or live with the warnings. In this case, you should still put parentheses around your macro to avoid possible problems.
#define MYSERVERLOG(iLog, ...) (iLog ? NSLog(__VA_ARGS__) : TRUE)

if you do a
#define DEBUG 0
OR if you do a
#define DEBUG 1
the "DEBUG" macro is defined so...
#ifdef DEBUG
will always be true since DEBUG is defined.
If you want to get into the 2nd clause, you will have to make sure the macro is not defined AT ALL (i.e. Delete the macro or comment it out).

Related

Eclipse breakpoint conditional skipping

I read several posts on this but I could not fix my problem which is that eclipse skips a conditional I defined on a variable.
if(currentContent=="content") {
return true;
}
Can you please advice what I should do for the conditional to trigger the breakpoint when the value of my string variable is equal "content".
You can't rely on == with Strings. Use .equals().
nitind's point about using .equals is important, but I think a bigger issue is that the breakpoint condition is evaluated BEFORE the line executes, not after. I get the feeling you want to hit the breakpoint when the resulting value of "currentContent" matches your expectations AFTER the assignment, not before.
In this case, I suggest you move your breakpoint to the executable line that comes after this line.

In Swift, how to "print()" like "p" command in lldb

"p variable" command prints all the elements within the variable in lldb. In Swift, just regular print() statement does not do this. A hack would be to loop through the backtrace of the variable and print the elements. I'm curious if there is an easier way to simulate the in-depth printing mechanism like the "p" command I'm not aware of.

where to find the previous definition of #define value

I have made a mistake to change the .h file for some reason. And I do the #define CPU_ONLY 1 in the file, but I forgot its name.
Now I come across this problem when I make caffe test in terminal
./include/caffe/util/device_alternate.hpp:4:9: warning: 'CPU_ONLY' macro
redefined [-Wmacro-redefined]
#define CPU_ONLY
^
<command line>:6:9: note: previous definition is here
#define CPU_ONLY 1
^
1 warning generated.
How can I find the .h file I had changed, and I have searched in finder on MAC OS yet.

How to Restrict NSLog in Distribution and Release Mode -iPhone

I want to Enable the NSLog only in Debug Mode. I need to restrict NSLog in Distribution and Release mode.
I am using the code below,
#ifdef DEBUG
# define NSLog(...) NSLog(__VA_ARGS__);
#else
# define NSLog(...)
#endif
But it's not working. anyone please explain me as brief as possible. Where should i use the code in every NSLog statement or Every class or only in .PCH class..
Thanks for your consideration and Effort
Write this to your .pch file
#ifndef DLog
#ifdef DEBUG
#define DLog(_format_, ...) NSLog(_format_, ## __VA_ARGS__)
#else
#define DLog(_format_, ...)
#endif
#endif
After this use DLog instead of NSLog to meet with your goal
Put this into your .pch file
#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif

Regular expression for turning NSLogs on and off?

I've got a large Xcode project which has a few dozen NSLogs, some commented out.
The find/replace in Xcode has a regular expression option, so how could I make it comment out all active NSLogs, in a way which another find/replace can turn them back on again. So, the initial code is:
//NSLog(#"one");
NSLog(#"two");
becomes, after regex find/replace:
//NSLog(#"one");
//**NSLog(#"two");
which which turned back on becomes:
//NSLog(#"one");
NSLog(#"two");
There's a much better solution. Just put this in your prefix.pch file:
//disable logging when not in debug
#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif
That will disable NSLogs when your app is not in debug mode. That way you don't have to actually remove them from the code, but they'll have no performance impact.
In case you're wondering how it works, it redefines the NSLog() function as blank when the DEBUG macro is not defined. By default DEBUG is defined only for debug projects. You can change it to be toggled on and off in a different way if you prefer by just replacing the first line with #if SOME_OTHER_CONDITION.
A better solution for this would be to use a macro such as DLog which you could control using a debug (or your own compiler flag). Check this link or this