自定義的打印語句NSLog在控制台輸出不完整的完美解決


之前定義日志輸出時用的下面的方法

1 #ifdef DEBUG // 調試狀態, 打開LOG功能
2 #define CXTLog(...) NSLog(__VA_ARGS__)
3 #else // 發布狀態, 關閉LOG功能
4 #define CXTLog(...)
5 #endif

 

感覺很完美,但是最近升級xcode 9以后發現控制台總是輸出不完整,打印接口數據總是打印出一部分,很是郁悶,

直到發現了下面的方法:

1 #ifdef DEBUG // 調試狀態, 打開LOG功能
2 
3 #define CXTLog( s, ... ) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(s), ##__VA_ARGS__] UTF8String] )
4 
5 #else// 發布狀態, 關閉LOG功能
6 #define CXTLog( s, ... )
7 #endif

 

用這個方法解決了控制台輸出不完整的問題,整個人就好了!

 

升級版:

#ifdef DEBUG

#define LYLog(FORMAT, ...) {\
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];\
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];\
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];\
[dateFormatter setTimeZone:timeZone];\
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSSSSZ"];\
NSString *str = [dateFormatter stringFromDate:[NSDate date]];\
fprintf(stderr,"TIME:%s【FILE:%s--LINE:%d】FUNCTION:%s\n%s\n",[str UTF8String],[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__,__PRETTY_FUNCTION__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);\
}

#else
 # define LYLog(...);
#endif

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM