Show line number beside Xcode logs - ios

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

Related

How to replace NSLog with DLog with common file?

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?

Define custom NSAssert that includes line, class and general formatting

As the title reads, how could I define a custom NSAssert which would include the line, class, and formatting as per my NSLog below:
#define NSLog(__FORMAT__, ...) NSLog((#"%#: " __FORMAT__), NSStringFromClass([self class]), ##__VA_ARGS__)
The problem is that NSAssert has a BOOL value first before the rest of the arguments are taken under account. I can't seem to find a solution without taking out the arguments and separating them.
Is there a better way to solve this?
Long story short, I'm looking for something like this:
#define DebugAssert(__VA_ARGS__[0], #"%#: %#", NSStringFromClass([self class]), __VA_ARGS__[1])
The NSAssert macro is defined like this:
#define NSAssert(condition, desc, ...) /* the implementation */
So, the condition is already a separate parameter from the format string and the variable argument list. There should be no problem doing something similar to what you did for NSLog:
#define MyAssert(condition, desc, ...) \
NSAssert(condition, (#"%#: " desc), NSStringFromClass([self class]), ##__VA_ARGS__)

Where should I place my global macros?

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.

Using DebugLog() instead of NSLog()

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)

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__);

Resources