2

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.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Hariprasad
  • 1,094
  • 2
  • 12
  • 30

4 Answers4

6

Try:

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

P.S. This question may also be useful.

Community
  • 1
  • 1
Vladimir
  • 170,431
  • 36
  • 387
  • 313
2

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

anoop4real
  • 7,598
  • 4
  • 53
  • 56
2
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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • but i dont want to include any formate in NSLog statement, except the actual information to be displayed – Hariprasad Feb 14 '11 at 09:24
  • @Hariprasad: I'm not sure I understand. The format specifiers I put in are necessary. Obviously, you can do two NSLogs, one for each. – JeremyP Feb 14 '11 at 09:27
  • Suggest `NSLog(@"%@ %s", [self class], sel_getName(_cmd));` as `className` message does not compute – Cerniuk Feb 19 '17 at 17:41
1

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

[[myObject class] description]

seymatanoglu
  • 151
  • 1
  • 9