views:

3563

answers:

1

Hi all,

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.

+4  A: 

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))
Kailoa Kadano
thank you, this works for me.
MiRAGe
`__PRETTY_FUNCTION__` will give you the class and any category as well as the selector name. It also works with C++ and Obj-C++.If you're planning on using the debugger, print-object by default invokes `-debugDescription`, so you can override that to provide a more debugging-appropriate description than that returned by `-description`.
Jeremy W. Sherman
Awesome, thank you
Yar