Debug

作者: LucXion | 来源:发表于2023-11-26 11:24 被阅读0次

首先需要获得更多的信息帮助我们分析问题。

  • 可见的: 屏幕上视图的渲染层次。
  • 不可见的:变量的状态、哪一行代码正在执行。
日志

常见问题:输出的信息缺少筛选,大量冗余信息造成滚动目盲,获取有效信息的成本昂贵。

解决方案:

  1. 给语句划分等级,在系统配置中控制输出语句的等级
  2. 语句元素:源码位置、执行线程、执行时间、接口信息(参数、返回值)、数据对象数量、剩余内存大小
性能提升

原则上是从痛点入手,优先处理那些能按倍率

  1. 对 I/O 操作敏感,包括一些音视频、图片的编解码操作。
  2. 注意线程问题:锁、优先级反转
  3. 编码过程中注意
  • 更专业的使用库,特别是没有专门做过性能优化的子库。
  • 合理的选择对象类型,尽量使用更轻量级的对象,同时避免复杂的类型转换。
  • 删除浮点运算,尽量不使用除法而是用位运算代替。
  • 循环中产生的大量对象要及时释放,比如自动释放池。遇到递归要考虑还可以对算法做优化。
项目中加入调试信息打印:

pod 'CocoaLumberjack'

SDK初始化

#import "AppDelegate.h"
// 创建一个 DDLog 对象
static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // 设置日志输出到控制台
    [DDLog addLogger:[DDTTYLogger sharedInstance]];

    // 设置日志输出到控制台和文本视图
    [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:ddLogLevel];
    [DDLog addLogger:[DDASLLogger sharedInstance] withLevel:ddLogLevel];
    [DDLog addLogger:[DDOSLogger sharedInstance] withLevel:ddLogLevel];

    // 将日志输出到文本视图
//    [[DDTTYLogger sharedInstance] setLogFormatter:[[DDLogMessage alloc] init].logFormatter];
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    [[DDTTYLogger sharedInstance] setForegroundColor:[UIColor blackColor] backgroundColor:nil forFlag:DDLogFlagInfo];
    
    return YES;
}

创建中间对象

static const DDLogLevel ddLogLevel = DDLogLevelDebug;
// 创建一个自定义的 DDAbstractLogger 子类来将日志输出到文本视图
@interface TextViewLogger : DDAbstractLogger <DDLogger>

@property (nonatomic, weak) UITextView *textView;

@end
@implementation TextViewLogger

- (instancetype)initWithTextView:(UITextView *)textView {
    self = [super init];
    if (self) {
        _textView = textView;
    }
    return self;
}

- (void)logMessage:(DDLogMessage *)logMessage {
    NSString *logMsg = logMessage->_message;
    if (_textView) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self->_textView setText:[NSString stringWithFormat:@"%@\n%@", self->_textView.text, logMsg]];
        });
    }
}

@end

使用

//
    // 创建一个文本视图来显示日志信息
    UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    textView.editable = NO;
    textView.scrollEnabled = YES;
    textView.showsVerticalScrollIndicator = YES;
    textView.font = [UIFont systemFontOfSize:14];
    textView.textColor = [UIColor blackColor];
    [self.view addSubview:textView];
//    
    TextViewLogger *textViewLogger = [[TextViewLogger alloc] initWithTextView:textView];
    [DDLog addLogger:textViewLogger];

//    DDLogVerbose(@"Verbose log message");
//    DDLogDebug(@"Debug log message");
//    DDLogInfo(@"Info log message");
//    DDLogWarn(@"Warn log message");
//    DDLogError(@"Error log message");

相关文章

网友评论

      本文标题:Debug

      本文链接:https://www.haomeiwen.com/subject/bmgqwdtx.html