比NSLog酷100倍的日志输出

不是标题党,至少在我看来NSLog前面用来输出时间简直是渣得一逼,还要占一大串没用的字符,渣!

  • 改良后的输出格式: 距离上次输出日志的时差(用于粗略计算方法运行时间), 线程ID(查看是否异步), 方法(精确调试), 方法名(类方法或是实例方法) 行号
  • 实现思路很简单,就不贴完整代码了, 贴个日志根据自己需求定制便是(本人日志不但输出,还以以SQLite保存,方便随时查询)
time: 0.001133, tid:01 0x0000000103fe5265 +[Reachability reachabilityForInternetConnection] + 37
time: 0.000027, tid:01 0x0000000103fe51bb +[Reachability reachabilityWithAddress:] + 27
time: 0.000014, tid:01 0x0000000103fe537b -[Reachability initWithReachabilityRef:] + 27
time: 0.000012, tid:01 0x0000000103fe5265 +[Reachability reachabilityForInternetConnection] + 37
time: 0.000007, tid:01 0x0000000103fe51bb +[Reachability reachabilityWithAddress:] + 27
time: 0.000008, tid:01 0x0000000103fe537b -[Reachability initWithReachabilityRef:] + 27
time: 0.000008, tid:01 0x0000000103fe5cf7 -[Reachability currentReachabilityStatus] + 23
time: 0.000006, tid:01 0x0000000103fe5967 -[Reachability isReachable] + 23
time: 0.000679, tid:01 0x0000000103fe58da -[Reachability isReachableWithFlags:] + 26
time: 0.000030, tid:01 0x0000000103fe5a87 -[Reachability isReachableViaWiFi] + 23

- (void)log: (SCLoggerBlock)block
{
    NSTimeInterval  now = [NSDate nowTimeStamp];
    NSThread        *thread = [NSThread currentThread];
    NSArray         *stacks = [NSThread callStackSymbols];
    
    NSTimeInterval delta = now - _lastTime;
    _lastTime = now;
    
    dispatch_async(_queue, ^{
        block(thread, stacks, delta);
    });
}

void sc_LogFunctionName()
{
    static SCLogger *logger = nil;
    static NSString *logTableName = nil;
    static FMDatabase *logDB = nil;
    
    if (nil == logger)
    {
        logger = [[SCLogger alloc] init];
        
        logTableName = [[NSDate date] dateStringWithFormat:@"YYYY-MM-dd HH_mm_ss"];
        
        NSString *path = upload_log();
        logDB = [FMDatabase databaseWithPath:path];
        [logDB open];
        
        [logDB createTable:logTableName fromTypeList:@{
                                                       @"index":@"integer PRIMARY KEY autoincrement",
                                                       @"deltaTime":kDBColumnTypeFloat,
                                                       @"tid":kDBColumnTypeInteger,
                                                       @"text":kDBColumnTypeText
                                                       }];
        MLog(@"LogDB: %@", path);
        
        UIDevice        *dev = [UIDevice currentDevice];
        NSDictionary    *info = @{
                                  @"name":dev.name,
                                  @"model":dev.model,
                                  @"localizedModel":dev.localizedModel,
                                  @"systemName":dev.systemName,
                                  @"systemVersion":dev.systemVersion,
                                  @"identifierForVendor":dev.identifierForVendor.UUIDString,
                                  };
        
        NSString *device = [info toJsonString];
        [logDB insertDataWithTable:logTableName columnData:@{@"text":device}];
    }
    
    [logger log:^(NSThread *thread, NSArray *stacks, NSTimeInterval deltaTime) {
        NSString *stack = stacks[2];
        NSString *method = [stack substringFromIndex:40];
        NSString *log = [NSString stringWithFormat:@"time: %lf, tid:%02d %@",
                         deltaTime,
                         thread.seqNumber,
                         method];
        printf("%s\n", [log cStringUsingEncoding:NSUTF8StringEncoding]);
        
        [logDB insertDataWithTable:logTableName columnData:@{
                                                             @"deltaTime":@(deltaTime),
                                                             @"tid":@(thread.seqNumber),
                                                             @"text":method,
                                                             }];
     }];
}

推荐阅读更多精彩内容

  • [这是第9篇] 导语: 日志输出不仅仅是NSLog的简单使用,它对定位开发中的问题,收集用户的使用习惯有着很重要的...
    南华coder阅读 1,957评论 0 30
  • (http://www.cnblogs.com/zhangchenliang/p/4546352.html) 1、...
    凌雲木阅读 1,825评论 0 2
  • 作者:知秋来源:重走Java基础之Streams(一) 因为经常逛stackoverflow,最近也在看react...
    极乐君阅读 319评论 0 2
  • 其实,我是不大愿意吃泡面的。 但是,除了泡面最便捷以外,还能有什么?_? 发明泡面的人,真是天才。 我发誓,只吃这...
    里颜阅读 100评论 0 0
  • 在iOS中和运动相关的有三个事件:开始运动、结束运动、取消运动。 监听运动事件对于UI控件有个前提就是监听对象必须...
    FallPine阅读 88评论 0 0