Tencent / QMUI_iOS

QMUI iOS——致力于提高项目 UI 开发效率的解决方案
http://qmuiteam.com/ios
Other
7.08k stars 1.38k forks source link

QMUIConsole 在子线程里输出 log 会 crash #1200

Closed JzwOnly closed 3 years ago

JzwOnly commented 3 years ago

Bug 表现 QMUIConsole 在网络请求中,log 偶尔会出现crash 报错信息:!!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 879 截图 crash 堆栈截图 WechatIMG45

如何重现

  1. ...
  2. ...

预期的表现 正常情况下,应该是什么表现

其他信息

MoLice commented 3 years ago

QMUIConsole 没考虑在子线程里使用的情况,因此出现 crash,下个版本会修复,在此之前你可以将以下代码替换 QMUILog+QMUIConsole.m 里的同名方法临时修复该问题:

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        OverrideImplementation([QMUILogger class], @selector(printLogWithFile:line:func:logItem:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
            return ^(QMUILogger *selfObject, const char *file, int line, const char *func, QMUILogItem *logItem) {

                // call super
                void (*originSelectorIMP)(id, SEL, const char *, int, const char *, QMUILogItem *);
                originSelectorIMP = (void (*)(id, SEL, const char *, int, const char *, QMUILogItem *))originalIMPProvider();
                originSelectorIMP(selfObject, originCMD, file, line, func, logItem);

                if (!QMUICMIActivated || !ShouldPrintQMUIWarnLogToConsole) return;
                if (!logItem.enabled) return;
                if (logItem.level != QMUILogLevelWarn) return;

                void (^block)(void) = ^void(void) {
                    NSString *funcString = [NSString stringWithFormat:@"%s", func];
                    NSString *defaultString = [NSString stringWithFormat:@"%@:%@ | %@", funcString, @(line), logItem];
                    [QMUIConsole logWithLevel:logItem.levelDisplayString name:logItem.name logString:defaultString];
                };
                if (!NSThread.currentThread.isMainThread) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        block();
                    });
                } else {
                    block();
                }

            };
        });
    });
}
MoLice commented 3 years ago

已发布 4.2.3 修复该问题。