forked from pilotmoon/Scroll-Reverser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TapLogger.m
171 lines (139 loc) · 3.91 KB
/
TapLogger.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// This file is part of Scroll Reverser <https://pilotmoon.com/scrollreverser/>
// Licensed under Apache License v2.0 <http://www.apache.org/licenses/LICENSE-2.0>
#import "TapLogger.h"
@interface TapLogger ()
@property NSMutableDictionary *params;
@property NSMutableDictionary *keyCounts;
@property NSMutableOrderedSet *keyOrder;
@property NSUInteger serial;
@end
@implementation TapLogger
- (id)init
{
self=[super init];
if (self) {
self.params=[NSMutableDictionary dictionary];
self.keyCounts=[NSMutableDictionary dictionary];
self.keyOrder=[NSMutableOrderedSet orderedSet];
}
return self;
}
- (void)logObject:(NSObject *)obj forCountedKey:(NSString *)key
{
if (obj&&key) {
NSNumber *count=@([self.keyCounts[key] unsignedIntegerValue]+1);
self.keyCounts[key]=count;
NSString *adjustedKey=[NSString stringWithFormat:@"%@%@", key, count];
[self.keyOrder addObject:adjustedKey];
self.params[adjustedKey]=obj;
}
}
- (void)logObject:(NSObject *)obj forKey:(NSString *)key
{
if (obj&&key) {
[self.keyOrder addObject:key];
self.params[key]=obj;
}
}
- (void)logBool:(BOOL)val forKey:(NSString *)key
{
[self logObject:val?@"yes":@"no" forKey:key];
}
- (void)logIfYes:(BOOL)val forKey:(NSString *)key
{
if (val) {
[self logBool:val forKey:key];
}
}
- (void)logSignedInteger:(NSInteger)val forKey:(NSString *)key
{
[self logObject:@(val) forKey:key];
}
- (void)logUnsignedInteger:(NSUInteger)val forKey:(NSString *)key
{
[self logObject:@(val) forKey:key];
}
- (void)logDouble:(double)val forKey:(NSString *)key
{
[self logObject:@(val) forKey:key];
}
- (void)logNanoseconds:(uint64_t)ns forKey:(NSString *)key
{
[self logObject:@(ns/1000000) forKey:key];
}
- (void)logCount:(id)obj forKey:(NSString *)key
{
[self logObject:@([obj count]) forKey:key];
}
- (void)logEventType:(CGEventType)type forKey:(NSString *)key
{
NSString *str=@"unknown";
switch ((NSUInteger)type) {
case NSEventTypeGesture:
str=@"gesture";
break;
case NSScrollWheel:
str=@"scroll";
break;
case kCGEventTapDisabledByTimeout:
str=@"timeout";
break;
default:
break;
}
[self logObject:str forKey:key];
}
- (void)logSource:(ScrollEventSource)source forKey:(NSString *)key
{
NSString *str=@"unknown";
switch (source) {
case ScrollEventSourceMouse:
str=@"mouse";
break;
case ScrollEventSourceTrackpad:
str=@"trackpad";
break;
case ScrollEventSourceTablet:
str=@"tablet";
break;
default:
break;
}
[self logObject:str forKey:key];
}
- (void)logPhase:(ScrollPhase)phase forKey:(NSString *)key
{
NSString *str=@"unknown";
switch (phase) {
case ScrollPhaseStart:
str=@"start";
break;
case ScrollPhaseNormal:
str=@"normal";
break;
case ScrollPhaseMomentum:
str=@"momentum";
break;
case ScrollPhaseEnd:
str=@"end";
break;
default:
break;
}
[self logObject:str forKey:key];
}
- (void)logParams
{
NSMutableString *const paramString=[[NSString stringWithFormat:@"%@ ", @(self.serial++)] mutableCopy];
void (^p)(NSString *, NSObject *) = ^(NSString *label, NSObject *obj) {
[paramString appendFormat:@"[%@ %@]", label, obj];
};
for (NSString *key in [self.keyOrder reverseObjectEnumerator]) {
p(key, self.params[key]);
}
[self logMessage:paramString];
[self.params removeAllObjects];
[self.keyCounts removeAllObjects];
[self.keyOrder removeAllObjects];
}
@end