-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdout.m
executable file
·90 lines (74 loc) · 2.65 KB
/
dout.m
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import "dout.h"
#import <sys/sysctl.h>
void RFDebugger(NSString *format, ...) {
if (format) {
va_list args;
va_start(args, format);
NSLogv(format, args);
va_end(args);
}
@try {
@throw [NSException exceptionWithName:@"pause" reason:@"debug" userInfo:nil];
}
@catch (__unused NSException *exception) { }
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
void _dout_log_config(void) {
#ifdef DEBUG
NSLog(@"DEBUG is %@", DEBUG? @"ON":@"OFF");
#else
NSLog(@"DEBUG was not defined.");
#endif
NSLog(@"RFDEBUG is %@", RFDEBUG? @"ON":@"OFF");
NSLog(@"RFDebugLevel = %d", RFDebugLevel);
NSLog(@"DOUT_FALG_TRACE is %@", DOUT_FALG_TRACE? @"ON":@"OFF");
NSLog(@"DOUT_ASSERT_AT_ERROR is %@", DOUT_ASSERT_AT_ERROR? @"ON":@"OFF");
NSLog(@"DOUT_ASSERT_AT_WANRNING is %@", DOUT_ASSERT_AT_WANRNING? @"ON":@"OFF");
NSLog(@"DOUT_TREAT_ERROR_AS_EXCEPTION is %@", DOUT_TREAT_ERROR_AS_EXCEPTION? @"ON":@"OFF");
NSLog(@"DOUT_TREAT_WANRNING_AS_EXCEPTION is %@", DOUT_TREAT_WANRNING_AS_EXCEPTION? @"ON":@"OFF");
}
#pragma clang diagnostic pop
static bool DoutIsBeingDebugged(void) {
struct kinfo_proc info;
info.kp_proc.p_flag = 0;
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
size_t size = sizeof(info);
sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
return ((info.kp_proc.p_flag & P_TRACED) != 0);
}
void DoutLogString(NSString *string) {
static BOOL usingPrintf = NO;
static NSDateFormatter *dateFormatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
usingPrintf = DoutIsBeingDebugged();
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH':'mm':'ss'.'SSS"];
});
if (!usingPrintf) {
NSLog(@"%@", string);
return;
}
NSString *timeString = [dateFormatter stringFromDate:[NSDate date]];
NSString *threadFlag = @"";
if (![NSThread isMainThread]) {
threadFlag = [NSString stringWithFormat:@"(%@)", DoutCurrentThreadOrQueueName()];
}
printf("%s\n", [[NSString stringWithFormat:@"%@%@: %@", timeString, threadFlag, string] UTF8String]);
}
NSString *DoutCurrentThreadOrQueueName(void) {
NSString *threadName = [NSThread currentThread].name;
if (threadName.length) {
return threadName;
}
NSString *queueName = @(dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));
if (queueName.length) {
return queueName;
}
return [NSString stringWithFormat:@"%p", (void *)[NSThread currentThread]];
}