NSLog在iPhone中使用objective-C的方法名

时间:2023-01-15 18:38:26

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, ...) NSLog(@"<%@:%d> %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
    __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__])

For example, when I call NCLog(@"Hello world"); The output will be:

例如,当我调用NCLog(@“Hello world”);的输出将会是:

<ApplicationDelegate:10>Hello world

Now I also want to log out the method name like:

现在我还想注销方法名,如:

<ApplicationDelegate:applicationDidFinishLaunching:10>Hello world

So, this would make our debugging become easier when we can know which method is getting called. I know that we also have XCode debugger but sometimes, I also want to do debugging by logging out.

因此,当我们知道要调用哪个方法时,这将使我们的调试变得更容易。我知道我们也有XCode调试器,但有时我也想通过退出来进行调试。

6 个解决方案

#1


241  

print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C

Swift 3 and above

斯威夫特3及以上

print(#function)

#2


156  

To technically answer your question, you want:

从技术上回答你的问题,你想:

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

Or you could also do:

或者你也可以这样做:

NSLog(@"%s", __PRETTY_FUNCTION__);

#3


72  

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?

苹果有一个技术问答页面:QA1669——我如何向我的日志记录语句中添加上下文信息——比如当前方法或行号?

To assist with logging:

协助记录:

  • The C preprocessor provides a few macros.
  • C预处理器提供了一些宏。
  • Objective-C provides expressions (methods).
    • Pass the implicit argument for the current method's selector: _cmd
    • 传递当前方法的选择器:_cmd的隐式参数
  • objective - c提供了表达式(方法)。传递当前方法的选择器:_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:

要获取当前方法名和当前行号,请使用这两个宏__func__和__LINE__,如下所示:

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

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

另一个例子……我保存在Xcode的代码片段库中的代码片段:

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.

一些日志框架也可以帮助获取当前方法或行号。我不确定,因为我在Java中使用了一个很棒的日志框架(SLF4J + LogBack),但不是Cocoa。

See this question for links to various Cocoa logging frameworks.

有关各种Cocoa日志框架的链接,请参见这个问题。

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:

如果您有一个选择器变量(SEL),您可以用以下两种方式打印它的方法名(“message”):

  • Using Objective-C call to NSStringFromSelector:
    NSLog(@"%@", NSStringFromSelector(selector) );
  • 使用Objective-C调用NSStringFromSelector: NSLog(@“%@”,NSStringFromSelector(selector));
  • Using straight C:
    NSLog(@"%s", selector );
  • 使用直接的C: NSLog(@“%s”,选择器);

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

此信息来自于2013-07-19年的链接的Apple doc页面。该页面最近一次更新是在2011年10月10日。

#4


8  

NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
print(__FUNCTION__) // Swift

#5


0  

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

由于某些原因,iOS允许将_cmd作为一个文本字符传递,甚至没有一个编译警告。谁知道

#6


0  

In Swift 4:

在斯威夫特4:

func test(){

函数测试(){

print(#function)

}

}

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

test() //打印值“test()”

#1


241  

print(__FUNCTION__) // Swift
NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C

Swift 3 and above

斯威夫特3及以上

print(#function)

#2


156  

To technically answer your question, you want:

从技术上回答你的问题,你想:

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

Or you could also do:

或者你也可以这样做:

NSLog(@"%s", __PRETTY_FUNCTION__);

#3


72  

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?

苹果有一个技术问答页面:QA1669——我如何向我的日志记录语句中添加上下文信息——比如当前方法或行号?

To assist with logging:

协助记录:

  • The C preprocessor provides a few macros.
  • C预处理器提供了一些宏。
  • Objective-C provides expressions (methods).
    • Pass the implicit argument for the current method's selector: _cmd
    • 传递当前方法的选择器:_cmd的隐式参数
  • objective - c提供了表达式(方法)。传递当前方法的选择器:_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:

要获取当前方法名和当前行号,请使用这两个宏__func__和__LINE__,如下所示:

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

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

另一个例子……我保存在Xcode的代码片段库中的代码片段:

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.

一些日志框架也可以帮助获取当前方法或行号。我不确定,因为我在Java中使用了一个很棒的日志框架(SLF4J + LogBack),但不是Cocoa。

See this question for links to various Cocoa logging frameworks.

有关各种Cocoa日志框架的链接,请参见这个问题。

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:

如果您有一个选择器变量(SEL),您可以用以下两种方式打印它的方法名(“message”):

  • Using Objective-C call to NSStringFromSelector:
    NSLog(@"%@", NSStringFromSelector(selector) );
  • 使用Objective-C调用NSStringFromSelector: NSLog(@“%@”,NSStringFromSelector(selector));
  • Using straight C:
    NSLog(@"%s", selector );
  • 使用直接的C: NSLog(@“%s”,选择器);

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

此信息来自于2013-07-19年的链接的Apple doc页面。该页面最近一次更新是在2011年10月10日。

#4


8  

NSLog(@"%@", NSStringFromSelector(_cmd)); // Objective-C
print(__FUNCTION__) // Swift

#5


0  

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

由于某些原因,iOS允许将_cmd作为一个文本字符传递,甚至没有一个编译警告。谁知道

#6


0  

In Swift 4:

在斯威夫特4:

func test(){

函数测试(){

print(#function)

}

}

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

test() //打印值“test()”