nslog一些官方文档中的知识备忘

来源:互联网 发布:linux编辑shell脚本 编辑:程序博客网 时间:2024/04/30 11:08

官方文档学习的关于NSLOG的知识:

主要是预编译宏定义


1.

__PRETTY_FUNCTION__  

可以获取到当前的类和方法,例如:


- (void)pressButton:(id)sender


{


    NSLog( @"calling: %s", __PRETTY_FUNCTION__ );


    ....

这个东西是用来输出被调用的函数的名字的,这在存在很多代理方法时分析代码调用情况是非常有用的,可以让你非常好的理解代码执行的顺序。

上面这行会再控制台输出:calling: -[MyObjectClassName pressButton:]


Here, the pre-defined compile time variable __PRETTY_FUNCTION__ (a C style string) is used to print the name of the function being called. This technique is especially useful when you are analyzing code using lots of delegates and you would like to understand the sequence in which calls are being made to your delegate's methods.

The above will print the following in the console:

calling: -[MyObjectClassName pressButton:]


延伸:

Table 1  Preprocessor macros and for logging in C/C++/Objective-C.

Macro

Format Specifier

Description

__func__

%s

Current function signature.

__LINE__

%d

Current line number in the source code file.

__FILE__

%s

Full path to the source code file.

__PRETTY_FUNCTION__

%s

Like __func__, but includes verbose type information in C++ code.

Table 2  Expressions for logging in Objective-C.

Expression

Format Specifier

Description

NSStringFromSelector(_cmd)

%@

Name of the current selector.

NSStringFromClass([self class])

%@

Name of the current object's class.

[[NSString stringWithUTF8String:__FILE__] lastPathComponent]

%@

Name of the source code file.

[NSThread callStackSymbols]

%@

NSArray of the current stack trace as programmer-readable strings. For debugging only, do not present it to end users or use to do any logic in your program


2.nslog  中使用类似于回朔/回顾方法执行顺序的东东。崩溃的时候比较有用,尤其是维护其他人的应用时。


NSLog(@"%@", [NSThread callStackSymbols]);


下面有一个使用这个方法的例子:

2014-04-30 18:44:30.075 AVCustomEdit[52779:60b] (


 0  AVCustomEdit      0x0000efa6 -[APLSimpleEditor buildCompositionObjectsForPlayback:] + 278


 1  AVCustomEdit      0x0000686e -[APLViewController viewDidAppear:] + 590


 2  UIKit             0x007a4099 -[UIViewController _setViewAppearState:isAnimating:] + 526


 3  UIKit             0x007a4617 -[UIViewController __viewDidAppear:] + 146


 4  UIKit             0x007a49aa -[UIViewController _executeAfterAppearanceBlock] + 63


 5  UIKit             0x0069f0d0 ___afterCACommitHandler_block_invoke_2 + 33


 6  UIKit             0x0069f055 _applyBlockToCFArrayCopiedToStack + 403


 7  UIKit             0x0069ee9a _afterCACommitHandler + 568


 8  CoreFoundation    0x029db2bf __CFRunLoopDoObservers + 399


 9  CoreFoundation    0x029b9254 __CFRunLoopRun + 1076


 10 CoreFoundation    0x029b89d3 CFRunLoopRunSpecific + 467


 11 CoreFoundation    0x029b87eb CFRunLoopRunInMode + 123


 12 GraphicsServices  0x0318b5ee GSEventRunModal + 192


 13 GraphicsServices  0x0318b42b GSEventRun + 104


 14 UIKit             0x00681f9b UIApplicationMain + 1225


 15 AVCustomEdit      0x000026bd main + 141


 16 libdyld.dylib     0x0269e701 start + 1


)






3。























0 0
原创粉丝点击