NSLog the method name with Objective-C in iPhone

前端 未结 6 2076
自闭症患者
自闭症患者 2020-12-07 07:09

Currently, we are defining ourselves an extended log mechanism to print out the class name and the source line number of the log.

#define NCLog(s, ...) NSLo         


        
相关标签:
6条回答
  • 2020-12-07 07:28

    In Swift 4:

    func test(){

    print(#function)
    

    }

    test() //print the value "test()"

    0 讨论(0)
  • 2020-12-07 07:31

    tl;dr

    NSLog( @"ERROR %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );
    

    Details

    Apple has a Technical Q&A page: QA1669 - How can I add context information - such as the current method or line number - to my logging statements?

    To assist with logging:

    • The C preprocessor provides a few macros.
    • Objective-C provides expressions (methods).
      • Pass the implicit argument for the current method's selector: _cmd

    As other answers indicated, to merely get the current method's name, call:

    NSStringFromSelector(_cmd)
    

    To get the current method name and current line number, use these two macros __func__ and __LINE__ as seen here:

    NSLog(@"%s:%d someObject=%@", __func__, __LINE__, someObject);
    

    Another example… Snippets of code I keep in Xcode's Code Snippet Library:

    NSLog( @"ERROR %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );
    

    …and TRACE instead of ERROR…

    NSLog( @"TRACE %@ METHOD %s:%d ", @"DescriptionGoesHere", __func__, __LINE__ );
    

    …and a longer one using a soft-coded description passing a value ([rows count])…

    NSLog( @"TRACE %@ METHOD %s:%d.", [NSString stringWithFormat:@"'Table of Contents.txt' file's count of Linefeed-delimited rows: %u.", [rows count]] , __func__, __LINE__ );
    

    Preprocessor macros for logging

    Note the use of a pair of underscore characters around both sides of the macro.

    | Macro                | Format   | Description
      __func__               %s         Current function signature
      __LINE__               %d         Current line number
      __FILE__               %s         Full path to source file
      __PRETTY_FUNCTION__    %s         Like __func__, but includes verbose
                                        type information in C++ code. 
    

    Expressions for logging

    | Expression                       | Format   | Description
      NSStringFromSelector(_cmd)         %@         Name of the current selector
      NSStringFromClass([self class])    %@         Current object's class name
      [[NSString                         %@         Source code file name
        stringWithUTF8String:__FILE__]   
        lastPathComponent] 
      [NSThread callStackSymbols]        %@         NSArray of stack trace
    

    Logging Frameworks

    Some logging frameworks may help with getting current method or line number as well. I'm not sure, as I've used a great logging framework in Java (SLF4J + LogBack) but not Cocoa.

    See this question for links to various Cocoa logging frameworks.

    Name of Selector

    If you have a Selector variable (a SEL), you can print its method name ("message") in either of two ways as described by this Codec blog post:

    • Using Objective-C call to NSStringFromSelector:
      NSLog(@"%@", NSStringFromSelector(selector) );
    • Using straight C:
      NSLog(@"%s", selector );

    This information drawn from the linked Apple doc page as of 2013-07-19. That page had been last updated 2011-10-04.

    0 讨论(0)
  • 2020-12-07 07:31

    It's actually just as simple as:

    printf(_cmd);
    

    For some reason iOS allows _cmd to be passed as a literal char with not even a compile warning. Who knows

    0 讨论(0)
  • 2020-12-07 07:32

    To technically answer your question, you want:

    NSLog(@"<%@:%@:%d>", NSStringFromClass([self class]), NSStringFromSelector(_cmd), __LINE__);
    

    Or you could also do:

    NSLog(@"%s", __PRETTY_FUNCTION__);
    
    0 讨论(0)
  • 2020-12-07 07:40
    NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
    print(__FUNCTION__) // Swift
    
    0 讨论(0)
  • 2020-12-07 07:46
    print(__FUNCTION__) // Swift
    NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
    

    Swift 3 and above

    print(#function)
    
    0 讨论(0)
提交回复
热议问题