Xcode compiler errors when using ##__VA_ARGS__ - ios

Here is my customized NSLog.
#define NSLog(fmt, ...) printf("🌳🌳🌳%s,%d\n %s\n\n", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:fmt, ##__VA_ARGS__].UTF8String)
This log is work good on another project, but it occurred 4 error when compiler time in current project. If using __VA_ARGS__, like #define NSLog(...) NSLog(__VA_ARGS__), it's going to compile successful. I think that maybe caused in Building Setting.
It's anyone has any idea to help me to solve this.

Check that the header file declaring this imports Foundation. Check that you don't have NSLog defined somewhere else as well and finally, really not a good idea to redefine NSLog. You're probably better off adopting a logging framework or defining your own like this:
#define MELog(fmt, ...) NSLog("🌳🌳🌳%s,%d\n %s\ ....

Related

Redefinition of variable error

I'm trying to use the other code in my app. But in some lines I have got such error:
Redefinition of "some_variable_name" error
For instance,
struct some_struct {double t0, t1;};
How to fix this issue? This is the one initialization of this struct over the all code. But I got this.
How to fix it?
maybe you're including you're headers multiple times? tough to say without more details.
Try wrapping your code around this
#ifndef SOMETHING_H
#define SOMETHING_H
#endif
These are called Include Guards and prevent the code from being included again and thus the double declarations of identifiers.

Turn logging to lldb console off (Socket IO framework for iOS)

I am using pkyeck / socket.IO-objc framework. It is very good, however I am unable to debug with the verbose logs it produces to the lldb, which makes the console constantly full with descriptions about traffic, which are useless to me at the moment. I couldn't find a way how to turn it OFF. Anybody knows how to do it? I just can't imagine myself commenting out all the lines with NSLog...
Found the solution. It is rather undesired to tinker with the source code of the framework - usually it is a good practice to take it AS-IS but since there wasn't any other way...
In the file SocketIO.m, locate line numbers 32 - 39:
#define DEBUG_LOGS 1
#define DEBUG_CERTIFICATE 1
#if DEBUG_LOGS
#define DEBUGLOG(...) NSLog(__VA_ARGS__)
#else
#define DEBUGLOG(...)
#endif
Naturally, everybody now knows what comes next - change the value of DEBUG_LOGS to 0.
That's it, done. I recommend adding a //TODO: to the line above in order not to forget for the next time, when debugging logs are desired.

Is there a way to tell what line of code generates log output without using breakpoints and stepping in Xcode (iOS)?

I have the following log outputs in Xcode:
2013-05-20 17:23:19.901 MyApp[2408:303] invalid pixel format
2013-05-20 17:23:19.901 MyApp[2408:303] invalid context
The problem is I don't know what line of code is generating these errors. I've tried walking through but it's extremely tedious considering the complexity of this part of my app. Is there a quick and simple way to track down the line of code that is outputting these?
Yes, there is a way to do what you are asking. Simply add the following to your prefix header YourAppName-Prefix.pch.
#define NSLog(fmt, ...) NSLog((#"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
That will override NSLog with a custom NSLog that will print like this:
2013-05-20 21:11:10.407 YourAppName[46526:c07] -[JFDepthView initWithGestureRecognizer:] [Line 81] JFDepthView Initialized!
It should also work for 3rd party libraries you've added to your project.
From "C" documentation, Standard Predefined Macros - The C Preprocessor:
__FILE__ and __LINE__ are useful in generating an error message
NSLog(#"Line: %d", __LINE__);

How to disable NSLog all over the app?

I want to disable NSLog() across all instances in an app. I found some code that does that:
#ifndef DEBUG
#define NSLog //
#endif
But adding this code to each file isn't good idea. How can I make it easier?
Xcode has a precompiled header file ({project-name}-Prefix.pch in the Supporting Files group by default) that is a great place to put code that will be used across every file in the project. For going a step further and improving the log message itself, see Is it true that one should not use NSLog() on production code?.
Add the following lines to your constants file:
#define DEBUGGING NO //or YES
#define NSLog if(DEBUGGING)NSLog
The two following sentences give the same results
#define NSLog
and
#define NSLog //
because all text that begin with // are deleted in a precompiling phase ("//" included)
What I do is put this in the precompiled header file (YourAppName.pch):
#define MyLog if(0); else NSLog
Then I change all NSLog to MyLog everywhere else in the app. It works as if it were NSLog. But when you want to disable NSLog, go back to the .pch file and change the 0 to 1 and presto, all logging stops.
This will also take care of the warnings that arise when using
#define NSLog //
The code:
#ifndef DEBUG
#define NSLog(...)
#endif
Just add the following lines when you do'"t need NSLOG to your constants file
#define NSLog //
and comment it when you need it.

Sequence of execution when loading xib?

I have been programming on iOS for almost six months now, using xibs left and right, but i am still unaware how loading process works...
What i mean, i have view controller and push button to open new modal view controller. From that point onwards how are things done. Is init method first called, then xib created, outlets connected and then nib loaded?
Is there any good article or book that explains this in details?
this maybe not answer your question the way like you want,
but i recommend you to find it out yourself.
How? see the text above.
use XLog() in case of NSLog()
paste the code above in your prefix.pch file
put in every method you want an XLog() statement and see, which methods are called firstly.
XLog() is a better way of NSLog(). In console you can see the Line numbers and the methodnames where the log is called. this should help you out to understand the way of loading nibs.
#define DEBUG 1
//#define RELEASE 1
#ifdef DEBUG
// Debug definitions
#define DEBUG_MODE
#define XLog(fmt, ...) NSLog(#"%s line:%d " fmt, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#ifndef RELEASE
#error DEBUG or RELEASE need to be #defined
#endif
// Release definitions
#define RELEASE_MODE
#define XLog(...)
#endif

Resources