C语言 在 iPhone 中用 Objective-C 记录方法名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2770307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:22:35  来源:igfitidea点击:

NSLog the method name with Objective-C in iPhone

objective-cciphonedebugging

提问by vodkhang

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 调试器,但有时,我也想通过注销来进行调试。

回答by drawnonward

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

Swift 3 and above

Swift 3 及以上

print(#function)

回答by Dave DeLong

To technically answer your question, you want:

要从技术上回答您的问题,您需要:

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

Or you could also do:

或者你也可以这样做:

NSLog(@"%s", __PRETTY_FUNCTION__);

回答by Basil Bourque

tl;dr

tl;博士

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?

Apple 有一个技术问答页面:QA1669 - 如何将上下文信息(例如当前方法或行号)添加到我的日志记录语句中?

To assist with logging:

协助记录:

  • The C preprocessor provides a few macros.
  • Objective-C provides expressions(methods).
    • Pass the implicit argumentfor the current method's selector: _cmd
  • C 预处理器提供了一些
  • Objective-C 提供了表达式(方法)。
    • 传递当前方法选择器的隐式参数_cmd

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

正如其他答案所示,仅获取当前方法的名称,请调用:

NSStringFromSelector(_cmd)

To get the current method name andcurrent 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…

...和 ​​TRACE 而不是 ERROR ...

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

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

……还有一个使用传递值 ( [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 underscorecharacters 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 questionfor 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 Codecblog post:

如果您有一个 Selector 变量(一个SEL),您可以按照此编解码器博客文章中所述的两种方式之一打印其方法名称(“消息”):

  • Using Objective-C call to NSStringFromSelector:
    NSLog(@"%@", NSStringFromSelector(selector) );
  • Using straight C:
    NSLog(@"%s", selector );
  • 使用 Objective-C 调用NSStringFromSelector
    NSLog(@"%@", NSStringFromSelector(selector) );
  • 使用直 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.

此信息取自 2013 年 7 月 19 日的链接 Apple 文档页面。该页面上次更新时间为 2011-10-04。

回答by Huynh Inc

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

回答by Albert Renshaw

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 作为文字字符传递,甚至没有编译警告。谁知道

回答by Ankit garg

In Swift 4:

在 Swift 4 中:

func test(){

功能测试(){

print(#function)

}

}

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

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