A better NSLog
By AkH, 1 year, 5 months ago, modified Aug. 3, 2009
When debugging, I often found that NSLog is really boring, here is an attempt to make it nicer:
NSLog(@"mycar: %@",myCar);
NSLog(@" I'M HERE ");
NSLog doesn't tell you where the call was made (which file or which method), so you end with a lot of:
2009-03-11 22:30:53.789 ObjCTest[1565:10b] mycar: Porsche
2009-03-11 22:30:53.791 ObjCTest[1565:10b] I'M HERE
A better NSLog could tell us:
2009-03-11 22:32:25.823 ObjCTest[1581:10b] Car.m:32 -[Car init] I'M HERE AND I KNOW WHERE I AM
Now we have the sourcefile, the linenumber and the method of the call !!
Another repeating action when debugging is just to look for the value of something (call the description method of an object)
NSLog(myCar);
NSLog(@"%@",myCar);
That will produce:
2009-03-11 22:38:22.707 ObjCTest[1625:10b] Porsche
Not really sexy, no filename, no name for that value ... a better answer could be:
2009-03-11 22:38:22.708 ObjCTest[1625:10b] Car.m:35 -[Car init] [myCar description] = Porsche
To enable this "better" NSLog, in the Source folder of your Xcode you will find a file named YOURAPPNAME_Prefix.pch add the following code
#include <libgen.h>
#define ALog(format, ...) NSLog([NSString stringWithFormat:@"%s:%d %s %@",basename(__FILE__),__LINE__,__FUNCTION__,format], ##__VA_ARGS__)
#define ALogv(var) NSLog([NSString stringWithFormat:@"%s:%d %s [" # var " description] = %@",basename(__FILE__),__LINE__,__FUNCTION__, var] )
A much better solution is to associated it with a DEBUG define, and put '-DDEBUG=1' it in "Other C Flags" in Xcode Debug configuration, explained here, it will remove all calls to ALog when buit for release.
#ifdef DEBUG
#include <libgen.h>
#define ALog(format, ...) NSLog([NSString stringWithFormat:@"%s:%d %s %@",basename(__FILE__),__LINE__,__FUNCTION__,format], ##__VA_ARGS__)
#define ALogv(var) NSLog([NSString stringWithFormat:@"%s:%d %s [" # var " description] = %@",basename(__FILE__),__LINE__,__FUNCTION__, var] )
#else
#define ALog(...) /* */
#define ALogv(var) /* */
#endif
Then call ALog rather than NSLog and ALogv for the variable name and his value:
ALog(@"mycar: %@", myCar);
ALogv(myCar);
2009-03-11 22:38:22.706 ObjCTest[1625:10b] Car.m:33 -[Car init] mycar: Porsche
2009-03-11 22:38:22.708 ObjCTest[1625:10b] Car.m:35 -[Car init] [myCar description] = Porsche
Feel free to change the name of this functions.
Bye Bye NSLog.
EDIT: here is the update version I'm using
2009-07-31 12:22:46.070 WheelOfFate[30351:20b] [WheelBrowserViewController.m:26 initWithStyle:] myWheelStorage=<WheelStorage: 0xd134a0>
What is this strange method ? In fact this is just [TheFileWhereTheMethodeWasCalled:TheLineNumberTheMethodWasCalled TheMethod], it sounds strange but it takes less space on debug console, warning sometimes where you define multiple class in same file it could fool you.
Why DLog instead of ALog ? Just run xcode and type dl <Echap> you will see that there is no completion for DL letters ...
What is this new ALogm method (Log Method) ? Sometimes you just need to display "the application has gone here" without any others informations.
#ifdef DEBUG
#include <libgen.h>
#define DLogm() NSLog([NSString stringWithFormat:@"[%s:%d %@]",basename(__FILE__),__LINE__,NSStringFromSelector(_cmd)])
#define DLog(format, ...) NSLog([NSString stringWithFormat:@"[%s:%d %@] %@",basename(__FILE__),__LINE__,NSStringFromSelector(_cmd),format], ##__VA_ARGS__)
#define DLogv(var) NSLog([NSString stringWithFormat:@"[%s:%d %@] "# var "=%@",basename(__FILE__),__LINE__,NSStringFromSelector(_cmd), var] )
#else
#define DLog(...) /* */
#define DLogv(var) /* */
#define DLogm() /* */
#endif


Comments
Another repeating action when debugging is just to look for the value of something (call the description method of an object)
First thought: it should be possible to assign a single agent for all users, instead of for example having to put a screensaver agent for every user account separately.
I loved the editorial.It is very interesting.Thank you for the information.
I am always searching online for articles that can help me get further ahead. Thanks a million!
Thanks for this! I’ve been looking all over the internet for it.
Great thanks for sharing this article post.
To enable this "better" NSLog, in the Source folder of your Xcode you will find a file named YOURAPPNAME_Prefix.pch add the following code
A: I have the perfect son.
B: Does he smoke?
A: No, he doesn't.
B: Does he drink whiskey?
A: No, he doesn't.
B: Does he ever come home late?
A: No, he doesn't.
B: I guess you really do have the perfect son. How old is he?
A: He will be six months old next Wednesday.
<a href="http://www.youngerfashion.com/">prada handbags</a>
<a href="http://www.youngerfashion.com/">nike air max 90</a>
<a href="http://www.youngerfashion.com/">adidas tracksuit</a>
<a href="http://www.youngerfashion.com/">marc jacobs handbags</a>
<a href="http://www.youngerfashion.com/">snow boots</a>
<a href="http://www.youngerfashion.com/">prada handbags</a>
<a href="http://www.youngerfashion.com/">nike air max 90</a>
<a href="http://www.youngerfashion.com/">adidas tracksuit</a>
<a href="http://www.youngerfashion.com/">marc jacobs handbags</a>
<a href="http://www.youngerfashion.com/">snow boots</a>
<a href="http://www.youngerfashion.com/">prada handbags</a>
<a href="http://www.youngerfashion.com/">nike air max 90</a>
<a href="http://www.youngerfashion.com/">adidas tracksuit</a>
<a href="http://www.youngerfashion.com/">marc jacobs handbags</a>
<a href="http://www.youngerfashion.com/">snow boots</a>
<a href="http://www.youngerfashion.com/">prada handbags</a>
<a href="http://www.youngerfashion.com/">nike air max 90</a>
<a href="http://www.youngerfashion.com/">adidas tracksuit</a>
<a href="http://www.youngerfashion.com/">marc jacobs handbags</a>
<a href="http://www.youngerfashion.com/">snow boots</a>
<a href="http://www.youngerfashion.com/">prada handbags</a>
<a href="http://www.youngerfashion.com/">nike air max 90</a>
<a href="http://www.youngerfashion.com/">adidas tracksuit</a>
<a href="http://www.youngerfashion.com/">marc jacobs handbags</a>
<a href="http://www.youngerfashion.com/">snow boots</a>
O(∩_∩)O~..O(∩_∩)O~
I enjoyed reading it. I need to read more on this topic...I admiring time and effort you put in your blog, because it is
I have glanced at numerous of your entries and I was itching to know if you wanted to swap web site links? I own a blog site about funeral poetry, and I am
often looking to swap links with blogs on very similar themes! I look forward to hearing back from you in the near future.
nice, good
<a href=" http://www.luxurygathering.com/product.asp?classid=281">designer fendi handbags</a> for every day use are not restricted to the traditional role of serving as a carrier for money or other necessities. In lieu, they symbolize women's sense of fashion and social status as <a href=" http://www.luxurygathering.com/Watches/">designer watches</a> did.
With the speedy development of modern society, females become fashion-conscious. They always require to add something stylish <a href=" http://www.luxurygathering.com/Clothing/">Replica Clothing</a> and <a href=" http://www.luxurygathering.com/Handbags/">Replica Handbags</a> to accentuate their impression to others.
vibram five fingers http://www.vibramwebsite.com
MBT shoes http://www.mbtorg.com
UGG boots http://www.theseugg.com
Rosetta Stone http://www.software-rosettastone.com
Christian Louboutin http://www.retail-christianlouboutin.com
best nike air shoes store online , buy cheap nike max shoes
<a href="http://www.christianlouboutinonsale.de/">Christian louboutin</a>
<a href="http://www.christianlouboutinonsale.de/">christian louboutin schuhe</a>
<a href="http://www.christianlouboutinonsale.de/">louboutin schuhe</a>
<a href="http://www.christianlouboutinonsale.de/">schuhe christian louboutin</a>
<a href="http://www.christianlouboutinonsale.de/">Christian louboutin</a>|
Thanks for such a great post and the review, I am totally impressed! Keep stuff like this coming.
Obliteration is precisely ugg boots cardy sale what nasa intends for the spacecraft. The reason is that Galileo may still harbor some ugg classic cardy boots signs of life on Earth: microorganisms that have survived since its launch from the Kennedy Space uggs bailey button uk Center, in Florida, in 1989. If the orbiter were left to circle Jupiter after running out of bailey button uggs chocolate propellant (barring an intervention, this would likely happen within a year), it might eventually crash into Europa, one of Jupiter’s large moons. In 1996, Galileo conducted the first of eight close flybys of Europa, producing breathtaking sale bailey button ugg boots pictures of its surface, which suggested that the moon has an immense ocean hidden ugg usa bailey button beneath its frozen crust. These images have led to vociferous scientific debate about the prospects for uggs boots life there; as a result, nasa officials decided that it was necessary to avoid the possibility of seeding Europa with alien cheap argyle uggs life-forms. And so the craft has been programmed to commit suicide, guaranteeing a fiery, spectacular end to one of the most ambitious, tortured, and revelatory missions in the history of uggs classic mini space exploration. http://www.uggcardyuk.co.uk/UGG-Classic-Tall-Boots.html XY