iOS开发之将NSLog日志展示在app上并将根据不同日志种类设置文字颜色与背景色的日志文件分享到电脑查看

一种代码捕捉NSLog打印的方法是使用宏定义,将NSLog重新定义为一个新的方法,在这个方法里使用全局单例、全局变量数组保存这些打印

比如

#   define NSLog(...) YYLogDebug(@"%s [Line %d] %@", __func__, __LINE__,[NSString stringWithFormat:__VA_ARGS__]);

然后再将其定义为全局方法

#define YYLogDebug(isAsynchronous, lvl, flg, ctx, atag, fnct, frmt, ...) \
        [YYYLog log : isAsynchronous                                     \
             level : lvl                                                \
              flag : flg                                                \
           context : ctx                                                \
              file : __FILE__                                           \
          function : fnct                                               \
              line : __LINE__                                           \
               tag : atag                                               \
            format : (frmt), ## __VA_ARGS__]

在这个方法里在进行 数组addobj操作

将这些数据再写一个tableview展示出来即可在客户端中查看了。

可以将这些打印日志,写到纯文本里来,分享到电脑上来查看,

但通常日志量都很大,排查问题需要查看大量的日志,搜索也不方便。

因此将日志添加富文本标签,增强阅读性,增加背景色,文字颜色的支持,来使排查日志更方便。

有了思路,然后就好办了,选择html的标签将日志文本包装分享出来

- (void)shareHtml{

//组装html头

    NSMutableString *mustr = [[NSMutableString alloc] initWithCapacity:1000];
    [mustr appendString:@"<!DOCTYPE html>\
     <html>\
     <head>\
         <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> \
         <title>log</title> \
         <meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, viewport-fit=cover ,user-scalable=no\" name=\"viewport\"> \
     </head>\
     <body>\
         <div id=\"app\" class=\"layyy\">    "];
    
//循环遍历日志数组,拼接p标签

    for (NSLogModel* model  in   [[self.dataArray reverseObjectEnumerator] allObjects] ) {
        NSString *logL = model.content;
        NSString *content = [NSString stringWithFormat: @"\n触发时间:%@  %@\n",model.timeStr ,logL];
        
        if([content containsString:@"<"]||[content containsString:@">"]){
            content = [content stringByReplacingOccurrencesOfString:@"<" withString:@""];
            content = [content stringByReplacingOccurrencesOfString:@">" withString:@""];
            
        }
        if ([content containsString:@"&param"])/// &para 转译
        {
            content = [content stringByReplacingOccurrencesOfString:@"&param" withString:@"&amp;param"];
        }
        
        if ([content containsString:@"\n"]){/// 换行转译
            content = [content stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
        }
        

//跳转页面一个颜色

        if(
           [content containsString:@"页面已经"]||
           [content containsString:@"页面将被"]||
           [content containsString:@"Title"]){
            
            
            NSString *hh = [NSString stringWithFormat:@"<p style=\"color:red;\ font-size: 20px;\ word-wrap: break-word;\ margin-left: 10px;\  margin-right: 10px;\">%@</p>",content];;
            [mustr appendFormat:@"%@",hh];
            
        }else if([content containsString:@"JSBridge:"]){
            

//桥接一个颜色

            [mustr appendFormat:@"<p style=\"font-size: 20px;\ word-wrap: break-word;\ margin-left: 20px;\  margin-right: 20px;\ background-color :yellow\">%@</p>",content];
        }else if([content containsString:@"--------接口-"]){
            

//接口一个颜色

            NSString *hh = [NSString stringWithFormat:@"<p style=\"color:blue;\ font-size: 20px;\ word-wrap: break-word;\ margin-left: 20px;\  margin-right: 20px;\">%@</p>",content];
            [mustr appendFormat:@"%@",hh];
            
        }else{
            [mustr appendFormat:@"<p style=\"font-size: 20px;\ word-wrap: break-word;\ margin-left: 20px;\  margin-right: 20px;\">%@</p>",content];

        }
                
    }
    
    [mustr appendFormat:@" </div></body></html>"];



//将内容写到文件

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    

    NSString *theFileName = [NSString stringWithFormat:@"%@.html",[DoraemonUtil dateFormatTimeInterval:[[NSDate date] timeIntervalSince1970]]];
 
    
    NSString *filename=[plistPath1 stringByAppendingPathComponent:theFileName];
    NSFileManager* fm = [NSFileManager defaultManager];
    if([fm fileExistsAtPath:filename] == NO){
        [fm createFileAtPath:filename contents:nil attributes:nil];
    }
    NSError *error = nil;
    [mustr writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
        
    id object;
    object = [NSURL fileURLWithPath:filename isDirectory:NO];
    if (!object) {
        return;
    }


//使用系统分享,将文件分享出去

    NSArray *objectsToShare = @[object];
    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

    [self presentViewController:controller animated:YES completion:nil];

//    return;
    


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值