views:

161

answers:

4

I'm trying to debug an iPhone app I'm working on, and the idea of adding fifty NSLog statements to the various source files gives me the willies.

What I'd like to do is write a pair of statements, say

NSString *methodName = [self methodName];
NSLog(@"%@", methodName);

that I can just paste into each method I need to. Is there a way to do this? Is there some Objective-C construct for asking a method for its name? Or am I gonna have to do this the hard way?

+4  A: 

Use: NSLog("%@", NSStringFromSelector(_cmd));

_cmd is a special variable passed to every method just like self which is a reference to the selector that caused the method to be invoked (basically the method's name and signature).

Jason Coco
`NSLog( "%s" , _cmd );`A SEL is a C string.
drawnonward
@drawnonward yes it technically is, but it's still good to use the proper conversion functions.
Dave DeLong
@drawnonward: It is implemented as one, yes, but it defined as an opaque type. Apple is infamous for swapping things like that out from under you, so use it as documented regardless of how it's actually implemented.
Jason Coco
+5  A: 

Try NSLog(@"%s", __func__). This prints out a pretty description, like -[MyView drawRect:].

This also works with functions. It's a compiler feature.

Ken
`__func__` is a C99 language feature. Its cousins `__FUNCTION__` and `__PRETTY_FUNCTION__` are language extensions provided by GCC and possibly other compilers. All three are, I believe, extensions to the C++ language.
Jeremy W. Sherman
Exact dup of http://stackoverflow.com/questions/2687785/any-way-to-ask-a-method-for-its-name/2687873#2687873
Ken
A: 

This is what you want:

NSLog(@"%s", __PRETTY_FUNCTION__);

For more info on related logging stuff, read over the answers to this question: http://stackoverflow.com/questions/969130/nslog-tips-and-tricks

Nick Forge
`__func__` does the same thing (in all current Mac OS X compilers) and is guaranteed to exist by C99. Also, don't use `%@`, as both `__PRETTY_FUNCTION__` and `__func__` are C strings, not NSString objects.
Peter Hosey
Thanks for pointing out the %@ error (I've fixed the mistake now). Does `__func__` return the same thing as `__PRETTY_FUNCTION__` for Obj-C methods, C functions and C++ functions? I thought `__PRETTY_FUNCTION__` gave more info for C functions, like parameter names or something?
Nick Forge
Both identifiers have the same value in Objective-C methods (`-[MyClass selector]`) and C functions (`nameoffunc`) in both GCC 4.2.1 and Clang. I didn't test C++, because I don't know C++..
Peter Hosey
For C++, `__func__` gives just the bare name of C++ member and static member functions. `__PRETTY_FUNCTION__` gives the entire shebang - everything that gets encoded into the mangled name.`void* A::B::C::DoIt(int, int)` yields `__func__ = __FUNCTION__ = DoIt` but `__PRETTY_FUNCTION__ = void* A::B::C::DoIt(int, int)`.
Jeremy W. Sherman
I don't know C++ either, so thanks for clarifying. :-)
Nick Forge
A: 

I use the following macros frequently:

#if DEBUG
#  define LOG(format, args ...) fprintf(stderr, format "\n", ## args)
#  ifdef __cplusplus
#    define ERR(format, args ...) fprintf(stderr, "[%s] (%s:%i): " format "\n", __PRETTY_FUNCTION__, __FILE__, __LINE__, ## args)
#  else
#    define ERR(format, args ...) fprintf(stderr, "[%s] (%s:%i): " format "\n", __func__, __FILE__, __LINE__, ## args)
#  endif
#else
#  define LOG(format, args ...)
#  define ERR(format, args ...)
#endif

If you always wanted the function name you could easily adapt them as required.

sbooth