iOS開發之如何關閉Xcode控制檯的所有輸出
阿新 • • 發佈:2018-12-11
一執行工程發現控制檯輸出一大堆內容,怎麼把它關掉呢。
1.原始的列印資料包括xcode列印的,第三方庫的,還有自己寫的
2.通過在pch裡,巨集定義把NSLog替換後發現少了很多自己寫的列印
#define NSLog(...) //NSLog(__VA_ARGS__)
3.[DYMTLInitPlatform] platform initialization successful是怎麼回事
經搜尋得知在Edit Scheme->Options下把GPU Frame Capture 和它下邊的Metal API Validation 關閉即可
4.然後會剩下這些
這些都是第三方庫裡面的輸出,比如:聽雲 高德個推
5使用fishhook替換一些輸出函式(fishhook很簡單就一個.h一個.m使用也很簡單,如果需要可以聯絡我要demo工程)
先替換NSLog
static void (*orig_NSLog)(NSString *format, ...); void my_NSLog(NSString *buts, ...){ } rebind_symbols((struct rebinding[1]){ {"NSLog", my_NSLog, (void *)&orig_NSLog}}, 1); int fd = open(argv[0], O_RDONLY); uint32_t magic_number = 0; read(fd, &magic_number, 4); printf("Mach-O Magic Number: %x \n", magic_number); close(fd);
發現少了一些
6.繼續替換 printf發現沒有變化 ,替換NSLogv就只剩一條了
int my_printf(const char * ss, ...) { return 0; // return orig_printf(ss); } static void (*orig_NSLogv)(NSString *format,va_list args); void my_NSLogv(NSString *format,va_list args){ } rebind_symbols((struct rebinding[1]){ {"NSLogv", my_NSLogv, (void *)&orig_NSLogv}}, 1); // rebind_symbols((struct rebinding[1]){ {"printf", my_printf, (void *)&orig_printf}}, 1);
7.CUICatalog: Invalid asset name supplied: 是因為[UIImage imageNamed:@""]後面的名字是空的,修改一下即可
8.至此所有日誌都沒了
但
還有另一個方法 只需幾行程式碼 上面的步驟都省了
#pragma mark - 使用者方法,將輸出資訊寫入到dr.log檔案中;
// 將列印資訊儲存到Document目錄下的檔案中
- (void)redirectNSlogToDocumentFolder
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"dr.log"];// 注意不是NSData!
NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];
// 先刪除已經存在的檔案
NSFileManager *defaultManager = [NSFileManager defaultManager];
[defaultManager removeItemAtPath:logFilePath error:nil];
// 將log輸入到檔案
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
}