i want to use DebugLog() to print values instead of NSLog(). I believe DebugLog() is more efficient. But i am not able to use it in my project(doesn't appear in prompt). I believe we have to set something in Build settings for that. Does anyone have any idea about this?
Thanks.
DebugLog() is not a supported method in objetice-c, if you want to implement it yourself, do something like this:
#ifdef DEBUG
#define DebugLog(s, ...) NSLog(s, ##__VA_ARGS__)
#else
#define DebugLog(s, ...)
#endif
Taken from here:
DebugLog Format string is not a string literal
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((#"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define DLog(...)
#endif
To call NSLog only for the debug build:
#ifdef DEBUG
NSLog("Debugging");
//or any other statement
#endif
(This will be called only when the build configuration is 'debug' in your scheme)
Related
My current project does not contain prefix.pch file.I added the following code on Constants.h file
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((#"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif
Then I replaced my NSLog in my viewcontroller like the following:
DLog(#“Replaced”)
But I always got the error like Implicit declaration of function 'DLog' is invalid in C99 on my this specific view controller .
How to resolve this issue?
How can I configure Xcode to show the number of the line that caused a log to be printed?
I'm seeing logs in my debugger, and I'm not sure where they're coming from.
You can use built in macros: __PRETTY_FUNCTION__, __LINE__, __FILE__, and others.
Objective C example:
#define NSLog(__FORMAT__, ...) NSLog((#"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
Swift:
println("assertion failed at \(__FILE__):\(__LINE__)")
gcc macros:
https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
swift example: https://developer.apple.com/swift/blog/?id=15
Where should I place such debugging macros, I want to use it as gloabl.
#define DEBUG_MODE
#ifdef DEBUG_MODE
#define DebugLog( s, ... ) NSLog( #"<%p %#:(%d)> %#", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DebugLog( s, ... )
#endif
Since prefix.pch has been removed in Xcode 6.
Create one Constant.h file only.
import view controllers, macros etc that need to be imported for whole application.
Use this in viewcontroller like this :
#import "Constant.h"
If you want to use macros in a Project, just add them in "Preprocessor Macro -> Debug" under "Build Settings" of your project.
And if you want to use the macros across project, you can create a Constant.h file as suggested by Prince.
I've below code in my project dlog should print the values in console if isConsoleLogActive is YES.
It gives error like Operator 'defined' requires an identifier
#if defined ([Util isConsoleLogActive])// Operator 'defined' requires an identifier in this line
#define DLog(...) NSLog(__VA_ARGS__)
#define DTrace() NSLog(#"%s", __PRETTY_FUNCTION__)
#else
#define DLog(...) /* */
#define DTrace() /* */
#endif
if I use the same code([Util isConsoleLogActive]) in my .m it works perfectly fine. I face this issue only in #define
What could be the issue. Please give me some idea.
The various commands that start with # are preprocessor directives. These get executed before the compilation phase at build time, before your application actually executes. You should use the preprocessor directives to conditionally include different code in your application based on build configuration. The preprocessor, however, is the wrong way to handle conditional execution on a specific platform at runtime; for that, you want your standard "if...else" logic.
If your goal with that statement is to determine if the given selector exists, try respondsToSelector, instead.
Result of
[Util isConsoleLogActive]
is not known at compile-time. So you can not use it with '#if defined'.
I would like to do something similar to #ifdef __linux__, but with the bada SDK. Is there a constant defined by default?
Also, can I detect when I am compiling for the simulator?
You can, by checking the MingW Compiler's keyword, here's an interesting link that would point you in the right spot... so in theory you could have it this way
#ifdef __MINGW32__
/* we're in the simulator target */
#else
/* we're in the native target */
#endif
I use something like :
#ifdef SHP
# define CONFIG_SUPPORT_API_Osp 1 // bada
#endif
I wish there is also a define that tell sdk version or target api too ..
Use :
#ifdef _DEBUG
// Your code
#endif