一种代码捕捉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:@"¶m"])/// ¶ 转译
{
content = [content stringByReplacingOccurrencesOfString:@"¶m" withString:@"&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;
}