iOS 简单日志系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define YostarDebugLogLevel(level, fmt, ...) \
[YostarDebugLog logLevel:level file:__FILE__ function:__PRETTY_FUNCTION__ line:__LINE__ format:(fmt), ##__VA_ARGS__]

#define YostarDebugLog(fmt, ...) \
YostarDebugLogLevel(YostarDebugLogLevelInfo, (fmt), ##__VA_ARGS__)

#define YostarDebugWarningLog(fmt, ...) \
YostarDebugLogLevel(YostarDebugLogLevelWarning, (fmt), ##__VA_ARGS__)

#define YostarDebugErrorLog(fmt, ...) \
YostarDebugLogLevel(YostarDebugLogLevelError, (fmt), ##__VA_ARGS__)

typedef NS_ENUM(NSUInteger, YostarDebugLogLevel) {
YostarDebugLogLevelInfo = 1,
YostarDebugLogLevelWarning,
YostarDebugLogLevelError
};

@interface YostarDebugLog : NSObject

+ (BOOL)isDebugLogEnabled;

+ (void)enableDebugLog:(BOOL)enableLog;

+ (void)logLevel:(NSInteger)level file:(const char *)file function:(const char *)function line:(NSUInteger)line format:(NSString *)format, ...;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
static BOOL _enableLog;
+ (void)initialize{
_enableLog = NO;
}

+ (BOOL)isDebugLogEnabled{
return _enableLog;
}

+ (void)enableDebugLog:(BOOL)enableLog{
_enableLog = enableLog;
}

static id sharedInstance = nil;
+ (instancetype)sharedInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}

+ (void)logLevel:(NSInteger)level file:(const char *)file function:(const char *)function line:(NSUInteger)line format:(NSString *)format, ...{
@try {
//参数链表指针
va_list args;
//遍历开始
va_start(args, format);
NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
[self.sharedInstance logMessage:message level:level file:file function:function line:line];
//结束遍历
va_end(args);
} @catch (NSException *exception) {
NSLog(@"⚠️WARN::%@", exception);
} @finally {

}
}

- (void)logMessage:(NSString *)message level:(NSInteger)level file:(const char *)file function:(const char *)function line:(NSUInteger)line{
NSString *logMessage = [NSString stringWithFormat:@"[YostarLog][%@][line:%lu]: %s %s %@", [self descriptionForLevel:level], (unsigned long)line, function, "", message];
if (_enableLog) {
NSLog(@"%@", logMessage);
}
}

- (NSString *)descriptionForLevel:(YostarDebugLogLevel)level{
NSString *desc = nil;
switch (level) {
case YostarDebugLogLevelInfo:
desc = @"INFO";
break;
case YostarDebugLogLevelWarning:
desc = @"⚠️WARN";
break;
case YostarDebugLogLevelError:
desc = @"❌ERROR";
break;
default:
desc = @"UNKNOW";
break;
}
return desc;
}
-------------本文结束感谢您的阅读-------------
最近的文章

iOS timer定时器正确使用方式

1. 初始化,添加定时器前先移除1234[self.timer invalidate];self.timer = nil;self.timer = [NSTimer scheduledTimerWithTimeInterval:2.f target:self selector:@ …

继续阅读
更早的文章

iOS IAP安全性问题汇总

1. 常用的攻击方式 劫持apple server攻击 重复验证攻击 跨app攻击 换价格攻击 歧义攻击 中间人攻击 2. 讲解攻击方式及处理 劫持apple server攻击 通过dns污染,让客户端通过假的apple_server进行verify,从而认为自己支付成功。这个主要针对客户 …

继续阅读