采用NSLog打印超长的字符串,可能出现日志没有打印完全,后面一部分丢失了。
开始以为是和异步一致一起打印时被异步日志线程强行插入,结果找了半天没有找到,说明它根本没有打印出来。
出问题的日志打印代码:NSLog(@"3加密前:%@", data);
,怎么看这个代码没有任何问题,打印(NSLog(@"data.length:%d", data.length);
)出的这个字符串长度是比较长:data.length:7353
。我怀疑是系统这个打印函数是有内存限制,超过它的长度就直接截取扔掉了后面的字符。
分析它最大能支持多长的字符串:加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3130303030393539" }, { "openType" : "03", "ope
,它实际只有1021个包含空格汉字的字符。难道它真的只能打印这么一点字符串吗?
我直接对这个字符串打印:NSLog(@"%@", data);
。结果直接把这7353个字符全部打印出来了。可以看到它不是只打印这1021个汉字和字符这么点内容,应该可以打印很多。
难道是因为拼接的同时进行打印才有这个限制,我测试这种情况:NSLog(@"testdata%@", data);
,结果仍是可以把这7353个字符全部打印出来。可以看出推测不完全对。
然后我试一下再外面拼接字符串,直接对字符串进行打印:
str = [NSString stringWithFormat:@"加%@", data];
NSLog(@"%@", str);
结果和不行,也把这7353个字符全部打印出来。可以看出推测不完全对。
比对他们的不同发现了规律:在打印是拼接字符串,并且里面的常量字符串里有中文,才会出现打印的字符串很短,超过最大的长度字符串后面的部分被扔掉。那么找到的问题的所在,那来研究下这个最大长度是多少呢?
由于发现打印的最大的字符串最大是1021个字符,包含一个汉字,很可能和1021个字符有关,就对字符串进行截取测试。测试发现:在一个中文常量的情况下,和一个1020个字符的字符串拼接没有问题,但是和1021个字符的字符串拼接后打印少了最后一个字符。
总结:
当直接打印一个长字符串,只要不是超级长,一般没有问题。当加入输出信息,并且有常量中文时,和变量字符串拼接时,当他们的字符数和超过1021时,后面的字符串打印不出来(测试的场景是:一个中文字符和和不含有非汉字json字符串。若中文多,他们的总和是否为1021,没有进一步研究)。
开发环境有效的重置NSLog的方法,这种方案我提倡,这个已经原来的mac的控制台打印函数,而是采用的linux操作系统日志函数进行替换了原来的函数了:
#ifndef __OPTIMIZE__
//这里执行的是debug模式下,打印日志,当前行
#define NSLog(...) printf("%s %s 第%d行: %s\n",__TIME__, __PRETTY_FUNCTION__ ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);
#else
//这里执行的是release模式下,不打印日志
#define NSLog(...)
#endif
推荐解决方案:
先把要打印的所有字符串拼接为一个字符串,然后一次打印。
解决方案:
先把要打印的所有字符串拼接为一个字符串,然后一次打印。至于直接重置NSLog:#define NSLog(format, ...) printf("TIME:%s FILE:%s(%d行) FUNCTION:%s \n %s\n\n",__TIME__, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(format), ##__VA_ARGS__] UTF8String])
我不提倡。
测试代码:
NSLog(@"data.length:%d", data.length);
NSString *str = [NSString stringWithFormat:@"%@", [data substringToIndex:1022]];
NSLog(@"加%@", str);
NSLog(@"str.length:%d", str.length);
str = [NSString stringWithFormat:@"%@", [data substringToIndex:1021]];
NSLog(@"加%@", str);
str = [NSString stringWithFormat:@"加%@", [data substringToIndex:1021]];
NSLog(@"%@", str);
NSLog(@"str.length:%d", str.length);
str = [NSString stringWithFormat:@"%@", [data substringToIndex:1020]];
NSLog(@"加%@", str);
str = [NSString stringWithFormat:@"加%@", [data substringToIndex:1020]];
NSLog(@"%@", str);
NSLog(@"str.length:%d", str.length);
// str = [NSString stringWithFormat:@"3加密前:data:%@", data];
NSLog(@"3加密前:%@", data);
打印的日志,大家可以对照进行分析:
2019-08-02 16:11:54.297359+0800 ELock[1447:400407] data.length:7353
2019-08-02 16:11:54.297597+0800 ELock[1447:400407] 加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3130303030393539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3130303030393539" }, { "openType" : "03", "ope
2019-08-02 16:11:54.461804+0800 ELock[1447:400407] str.length:1022
2019-08-02 16:11:54.461982+0800 ELock[1447:400407] 加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3420303038763539" }, { "openType" : "03", "ope
2019-08-02 16:11:54.473584+0800 ELock[1447:400407] 加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3420303038763539" }, { "openType" : "03", "open
2019-08-02 16:11:54.475564+0800 ELock[1447:400407] str.length:1022
2019-08-02 16:11:54.475860+0800 ELock[1447:400407] 加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3420303038763539" }, { "openType" : "03", "ope
2019-08-02 16:11:54.477939+0800 ELock[1447:400407] 加[ { "openType" : "03", "openTime" : "1564651163", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651170", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651176", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651182", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651188", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651194", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651200", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651206", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651212", "uid" : "3420303038763539" }, { "openType" : "03", "openTime" : "1564651218", "uid" : "3420303038763539" }, { "openType" : "03", "ope
2019-08-02 16:11:54.480312+0800 ELock[1447:400407] str.length:1021