美文网首页
比NSLog酷100倍的日志输出

比NSLog酷100倍的日志输出

作者: sma11case | 来源:发表于2016-08-31 14:18 被阅读64次

    不是标题党,至少在我看来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,
                                                                 }];
         }];
    }
    

    相关文章

      网友评论

          本文标题:比NSLog酷100倍的日志输出

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