xcode的NSLog打印中文和打印不全问题处理
2022-01-05 本文已影响0人
越来越胖了
1. 打印不全,使用如下宏,打印时,调用CRLog
即可
#ifdef DEBUG
//# define CRLog(fmt, ...) NSLog((@"[文件名:%s]\n" "[函数名:%s]\n" "[行号:%d] \n" fmt), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__);
# define CRLog(fmt, ...) NSLog((@"[函数名:%s]" "[行号:%d] \n" fmt), __FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
# define CRLog(...)
#endif
/**
* 完美解决Xcode NSLog打印不全的宏
*/
#ifdef DEBUG
#define NSLog(FORMAT, ...) {\
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];\
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];\
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];\
[dateFormatter setTimeZone:timeZone];\
[dateFormatter setDateFormat:@"HH:mm:ss.SSSSSSZ"];\
NSString *str = [dateFormatter stringFromDate:[NSDate date]];\
fprintf(stderr,"--TIME:%s【FILE:%s--LINE:%d】FUNCTION:%s\n%s\n",[str UTF8String],[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__,__PRETTY_FUNCTION__,[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);\
}
#else
# define NSLog(...);
#endif
2. 打印中文时,显示的是Unicode,把Unicode转中文的方法是在工程中导入如下两个类(+load方法中,不需要引用)
image.png//
// LYLUnicode.h
//
// blog : http://blog.csdn.net/biggercoffee
// github : https://github.com/biggercoffee/LYLUnicode
//
// Created by Mango on 2017/3/31.
// Copyright © 2017年 coffee. All rights reserved.
//
#import <Foundation/Foundation.h>
//
// LYLUnicode.m
//
// blog : http://blog.csdn.net/biggercoffee
// github : https://github.com/allencelee/LYLUnicode
//
// Created by Mango on 2017/3/31.
// Copyright © 2017年 coffee. All rights reserved.
//
#import "LYLUnicode.h"
#import <objc/runtime.h>
static inline void LYL_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
if (class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@implementation NSString (LYLUnicode)
- (NSString *)stringByReplaceUnicode {
NSMutableString *convertedString = [self mutableCopy];
[convertedString replaceOccurrencesOfString:@"\\U"
withString:@"\\u"
options:0
range:NSMakeRange(0, convertedString.length)];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
return convertedString;
}
@end
@implementation NSArray (LYLUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
LYL_swizzleSelector(class, @selector(description), @selector(LYL_description));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(LYL_descriptionWithLocale:));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(LYL_descriptionWithLocale:indent:));
});
}
/**
* 我觉得
* 可以把以下的方法放到一个NSObject的category中
* 然后在需要的类中进行swizzle
* 但是又觉得这样太粗暴了。。。。
*/
- (NSString *)LYL_description {
return [[self LYL_description] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale {
return [[self LYL_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self LYL_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementation NSDictionary (LYLUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
LYL_swizzleSelector(class, @selector(description), @selector(LYL_description));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(LYL_descriptionWithLocale:));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(LYL_descriptionWithLocale:indent:));
});
}
- (NSString *)LYL_description {
return [[self LYL_description] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale {
return [[self LYL_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self LYL_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementation NSSet (LYLUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
LYL_swizzleSelector(class, @selector(description), @selector(LYL_description));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(LYL_descriptionWithLocale:));
LYL_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(LYL_descriptionWithLocale:indent:));
});
}
- (NSString *)LYL_description {
return [[self LYL_description] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale {
return [[self LYL_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)LYL_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self LYL_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end