Debug

2023-11-26  本文已影响0人  LucXion

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

日志

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

解决方案:

  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");
上一篇 下一篇

猜你喜欢

热点阅读