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
Related
I am wondering whether it is possible for a VSCode extension to have an unindent rule that removes all indentation when there is a regex match. I can specify indentation rules in a language-configuration.json file but decreaseIndentPattern only decreases indentation by 1. (Also, I wouldn't want all the other decreaseIndentPatterns to drop all their indentations.)
Example
Normally what you get is this
program main
integer :: a
#ifdef DEBUG
integer :: b
#endif
end program main
which is not compilable code for Fortran so you have to manually indent the preprocessor directives (lines starting with #).
Ideally, what I would want is when typing the # character in VSCode, to drop all indentation for that line.
program main
integer :: a
#ifdef DEBUG ! the # character should drop all indentation only for this line
integer :: b
#endif
end program main
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.
I would like to index every #ifdef and #else blocks in my C/C++ source files with my CDT indexer 8.0.2 on Eclipse indigo on Ubuntu.
Adding symbols wouldn't work since it would not cover #else blocks
Is there any way that I can force Eclipse CDT to simply ignore the #ifdef and #else blocks? like other indexers do ( like emacs cscope )
I've been searching for days and didn't find any good solution.
I have a really big amount of code and would better not edit it to remove #ifdefs even temporary.
The short answer is no. The CDT pre-processor doesn't work that way. It follows the correct semantics, meaning it only parses one branch of a #ifdef. Emacs may be going into each branch but its not doing an analysis that's as correct or complete as CDT.
Your only option is to set up multiple build configurations that are configured to parse each branch. Then you can switch build configurations when you want to work in the different branches. This is far from ideal though because it can be time consuming to set up and it will trigger a re-index every time you switch. (Also you have to configure it to do the re-index, go to Window > Preferences > C/C++ > Indexer and select "Use active build configuration")
Note that the CDT parser will parse inside of inactive #else branches just to find declarations to show in the outline view.
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
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).