歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 調試日志信息清晰化

iOS 調試日志信息清晰化

日期:2017/3/1 10:00:55   编辑:Linux編程

介紹

Objective-C和C語言一樣,提供了一些標准宏,描述了當前文件,所在源碼文件的行數,以及函數信息。而Objective-C本身,也提供了相關的類類型。都可以應用在調試和錯誤處理日志當中。

預處理器在C/C++/Objective-C語言中提供的宏

* __func__%s 當前函數簽名
* __LINE__ %d 在源代碼文件中當前所在行數

* __FILE__ %s 當前源代碼文件全路徑

* __PRETTY_FUNCTION__ %s 像 __func__,但是包含了C++代碼中的隱形類型信息。


在Objective-C使用的一些日志信息

* NSStringFromSelector(_cmd) %@ 當前selector名稱
* NSStringFromClass([self class]) %@ 當前對象名
* [[NSString stringWithUTF8String:**FILE**] lastPathComponent] %@ 源代碼文件名
* [NSThread callStackSymbols] %@ 當前stack的可讀字符串數組。僅在調度時使用。

**例子代碼:**

  • #import <foundation /Foundation.h>
  • @interface TestObj : NSObject
  • - (void) fun:(NSString *)input;
  • @end
  • @implementation TestObj
  • - (void) fun:(NSString *)input {
  1. NSLog(@"%s:%d:%s:%s", __func__, __LINE__, __FILE__,__PRETTY_FUNCTION__);
  2. NSLog(@"%@",NSStringFromSelector(_cmd));
  3. NSLog(@"%@",NSStringFromClass([self class]));
  4. NSLog(@"%@",[[NSString stringWithUTF8String:__FILE__] lastPathComponent]);
  5. NSLog(@"%@",[NSThread callStackSymbols]);
  6. NSLog(@"%@",input);}
  7. @end
  8. int main (int argc, const char * argv[]){
  9. @autoreleasepool {
  10. TestObj *to = [[TestObj alloc]init];
  11. [to fun:@"call"];
  12. [to release];
  13. }
  14. return 0;
  15. }
Copyright © Linux教程網 All Rights Reserved