通过重定向NSLog过滤WKWebView [Process] kill() returned unexpected error 1

网上有很多关于解决WKWebView错误日志不停打印 [Process] kill() returned unexpected error 1 的文章,解决方法都是通过

1、Xcode menu 打开: Product > Scheme > Edit Scheme
2、在 Environment Variables 设置 OS_ACTIVITY_MODE = disable

这种做法是干净利落,但是系统其它错误日志也被误杀了,还导致NSLog无效。其实我们可以通过重定向NSLog,过滤掉不需要的信息,然后使用非标准错误流输出日志信息。下面是实现代码:

Swift版本

#if DEBUG
private func hookSTDERR() {
	// 能连接到控制台才允许重定向
    guard isatty(STDERR_FILENO) == 1 else {
        return
    }
    let pipe = Pipe()
    let pipeReadHandle = pipe.fileHandleForReading
    // 将标准错误输出流重定向到新建的文件流
    dup2(pipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)
    
    let _ = NotificationCenter.default.rx
        .notification(FileHandle.readCompletionNotification, object: pipeReadHandle)
        .subscribe(onNext: {
            guard let data = $0.userInfo?[NSFileHandleNotificationDataItem] as? Data else {
                return
            }
    
            /// 过滤wk的错误信息(使用正则过滤,防止过滤掉重要信息)
            if var log = String(data: data, encoding: .utf8) {
                log = log.replacingOccurrences(of: "\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+\\.\\d+[-+]\\d+\\d.+\\[\\d+:\\d+\\]\\s\\[Process\\] kill\\(\\) returned unexpected error 1\\s?", with: "", options: .regularExpression)
                // 使用标准输出流,输出日志信息
                print(log, terminator: "")
            }
            ($0.object as? FileHandle)?.readInBackgroundAndNotify()
        })
    pipeReadHandle.readInBackgroundAndNotify()
}
#endif

OC版本

#if DEBUG
void hookSTDERR() {
	// 能连接到控制台才允许重定向
	if (isatty(STDERR_FILENO) == 0) {
		return;
	}
    NSPipe *pipe = NSPipe.pipe;
    NSFileHandle *pipeReadHandle = pipe.fileHandleForReading;
    // 将标准错误输出流重定向到新建的文件流
    dup2(pipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO);
    [NSNotificationCenter.defaultCenter addObserverForName:NSFileHandleReadCompletionNotification object:pipeReadHandle queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
        NSData *data = note.userInfo[NSFileHandleNotificationDataItem];
        if (![data isKindOfClass:NSData.class]) {
            return;
        }
        
        NSString *log = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        /// 过滤wk的错误信息(使用正则过滤,防止过滤掉重要信息)
        log = [log stringByReplacingOccurrencesOfString:@"\\d+-\\d+-\\d+\\s\\d+:\\d+:\\d+\\.\\d+[-+]\\d+\\d.+\\[\\d+:\\d+\\]\\s\\[Process\\] kill\\(\\) returned unexpected error 1\\s?" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, log.length)];
        // 使用标准输出流,输出日志信息
        printf("%s", log.UTF8String);
        
        [((NSFileHandle *)note.object) readInBackgroundAndNotify];
    }];
    [pipeReadHandle readInBackgroundAndNotify];
}
#endif

最后在AppDelegate里面调用就好了。

#if DEBUG
    hookSTDERR()
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值