iOS中控制NSLog輸出時機詳解
-(void)saveDEBUGlog{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy_MM_dd_HH_mm_ss"];
NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"testLog_%@.log",currentDateStr];
NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
// 先刪除已經(jīng)存在的文件
NSFileManager *defaultManager = [NSFileManager defaultManager];
[defaultManager removeItemAtPath:logFilePath error:nil];
// 將log輸入到文件
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}
這個方法主要是調(diào)用 freopen 這個方法來寫入, 其中 stdout 和 stderr 囊括了 iOS 大部分的異常輸出。
2. 根據(jù) Bool 值控制 log 輸出
用戶在使用 app 遇到各種各樣的問題,當自己以及測試團隊不好定位原因的時候,能將用戶把關鍵點的 log 發(fā)送過來是最好的分析方法了。但是如何將 app 運行過程中的 log 截取一部分保存呢?像開關一樣能夠控制 log 的讀寫呢?通過閱讀 MQTTLog 源碼發(fā)現(xiàn)獲得的靈感。
首先對 NSLog 進行下宏替換,項目中統(tǒng)一使用 SLOG 來進行輸出
#define SLOG(fmt, ...) if (reportLoggerIsOpen) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)從宏定義可以看出,reportLoggerIsOpen 是控制 Log 輸出的開關,對于reportLoggerIsOpen的定義,在 h 文件中先 extern 聲明這個變量,然后再 m 文件中去實現(xiàn)。
.h 文件
#import <Foundation/Foundation.h>
extern BOOL reportLoggerIsOpen;
.m 文件
BOOL reportLoggerIsOpen = NO;
+ (void)setLogOpen:(BOOL)open {
reportLoggerIsOpen = open;
}
通過 setLogOpen 這個方法,就能夠收放自如的控制日志寫入了。比如你需要抓取登錄模塊的日志,那么就在登錄前傳入 true,登錄完畢后,傳入 false,即可只保留登錄模塊的日志了。
相關文章
Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用
這篇文章主要介紹了Objective-C的緩存框架EGOCache在iOS App開發(fā)中的使用,重點講解了EGOCache對緩存過期時間的檢測及處理,需要的朋友可以參考下2016-05-05
iOS頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞(三種)
本文主要介紹了iOS頁面跳轉(zhuǎn)的三種方法及數(shù)據(jù)傳遞的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03
iOS使用 CABasicAnimation 實現(xiàn)簡單的跑馬燈(無cpu暴漲)
本篇文章主要介紹了iOS使用 CABasicAnimation 實現(xiàn)簡單的跑馬燈(無cpu暴漲),具有一定的參考價值,有興趣的可以了解一下。2017-01-01

