Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get class and runtime information?

For debugging purposes, I'd like to display as much class information as I can, and possibly runtime information (which thread does the class/function run in, etc) into the console.

Is there an easy way to do this with a function, variable or even (external) framework?

P.S: I'm using Cocoa Touch.

like image 696
Jake Avatar asked Apr 21 '09 09:04

Jake


1 Answers

in a class, if you overload the -(NSString *)description method, you can easily log class info with NSLog(@"%@", some_object);

here's a fictitious example:

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@, %@, %d", 
                                      [super description], 
                                      class.object_ivar, 
                                      class.int_ivar];
}

You can use standard C macros to get things like name, file, line number etc... use the NSThread classes to get information about what thread the method is being called on.

I posted this one to twitter. http://twitter.com/kailoa/status/1349928820 Feel free to follow me if you are interested in more tidbits like this. I try to put them up regularly.

#define METHOD_LOG (NSLog(@"%@ %s\n%@", NSStringFromSelector(_cmd), __FILE__, self))
like image 112
amattn Avatar answered Sep 19 '22 10:09

amattn