Hariprasad
Hariprasad

Reputation: 1084

How to get Class name and method name while displaying a message in console using 'NSLog' in iPad

I have started a new project on iPad.I would like to know the method name and class name from which a particular message is sent to console while printing the message.Is there any way to print class & method names along with the log statement automatically.Please help and make my debugging easier thanks in advance.

Upvotes: 2

Views: 5270

Answers (4)

anoop4real
anoop4real

Reputation: 7708

The below is the way I used in my app

NSLog(@"%@",NSStringFromClass([self class]));

Also see improved logging section in Apple documentation. Improved logging in Objective-C

-anoop

Upvotes: 2

seymatanoglu
seymatanoglu

Reputation: 151

If you want the classname as an NSString, use code below;

[[myObject class] description]

Upvotes: 1

JeremyP
JeremyP

Reputation: 86651

NSLog(@"%@ %s", [self className], sel_getName(_cmd));

As _cmd starts with an underscore, it's potentially something you might not be able to rely on in the future, but everybody seems to use it for diagnostic logging.

Upvotes: 2

Vladimir
Vladimir

Reputation: 170829

Try:

NSLog(@"%s", __FUNCTION__);
NSLog(@"%s", __PRETTY_FUNCTION__);

P.S. This question may also be useful.

Upvotes: 6

Related Questions