13
13
#import " FBControlCoreLogger.h"
14
14
#import " FBControlCoreError.h"
15
15
16
- #if defined(__apple_build_version__)
17
-
18
- #import < CoreServices/CoreServices.h>
19
- #include < sys/stat.h>
20
-
21
- @interface FBCrashLogNotifier_FSEvents : NSObject
22
-
23
- @property (nonatomic , copy , readonly ) NSArray <NSString *> *directories;
24
- @property (nonatomic , strong , readonly ) FBCrashLogStore *store;
25
- @property (nonatomic , strong , readonly ) id <FBControlCoreLogger> logger;
26
- @property (nonatomic , strong , readonly ) dispatch_queue_t queue;
27
- @property (nonatomic , assign , readwrite ) FSEventStreamRef eventStream;
28
-
29
- @end
30
-
31
- typedef NS_ENUM (NSUInteger , FBCrashLogNotifierFileEvent) {
32
- FBCrashLogNotifierFileEventUnknown = 0 ,
33
- FBCrashLogNotifierFileEventAdded = 1 ,
34
- FBCrashLogNotifierFileEventRemoved = 2 ,
35
- };
36
-
37
- static FBCrashLogNotifierFileEvent GetEventType (FSEventStreamEventFlags flag, NSString *filePath) {
38
- if (flag & kFSEventStreamEventFlagItemRemoved ) {
39
- return FBCrashLogNotifierFileEventRemoved;
40
- } else if (flag & kFSEventStreamEventFlagItemCreated ) {
41
- return FBCrashLogNotifierFileEventAdded;
42
- } else if (flag & kFSEventStreamEventFlagItemRenamed ) {
43
- struct stat buffer;
44
- int value = stat (filePath.UTF8String , &buffer);
45
- return value == 0 ? FBCrashLogNotifierFileEventAdded : FBCrashLogNotifierFileEventRemoved;
46
- }
47
- return FBCrashLogNotifierFileEventUnknown;
48
- }
49
-
50
- static void EventStreamCallback (
51
- ConstFSEventStreamRef streamRef,
52
- FBCrashLogNotifier_FSEvents *notifier,
53
- size_t numEvents,
54
- NSArray <NSString *> *eventPaths,
55
- const FSEventStreamEventFlags *eventFlags,
56
- const FSEventStreamEventId *eventIds
57
- ){
58
- for (size_t index = 0 ; index < numEvents; index ++) {
59
- NSString *path = eventPaths[index ];
60
- FSEventStreamEventFlags flag = eventFlags[index ];
61
- switch (GetEventType (flag, path)) {
62
- case FBCrashLogNotifierFileEventAdded:
63
- [notifier.store ingestCrashLogAtPath: path];
64
- continue ;
65
- case FBCrashLogNotifierFileEventRemoved:
66
- [notifier.store removeCrashLogAtPath: path];
67
- continue ;
68
- default :
69
- continue ;
70
- }
71
- }
72
- }
73
-
74
- @implementation FBCrashLogNotifier_FSEvents
75
-
76
- - (instancetype )initWithDirectories : (NSArray <NSString *> *)directories store : (FBCrashLogStore *)store logger : (id <FBControlCoreLogger>)logger
77
- {
78
- self = [super init ];
79
- if (!self) {
80
- return nil ;
81
- }
82
-
83
- _directories = directories;
84
- _store = store;
85
- _logger = logger;
86
- _queue = dispatch_queue_create (" com.facebook.fbcontrolcore.crash_logs.fsevents" , DISPATCH_QUEUE_SERIAL);
87
-
88
- return self;
89
- }
90
-
91
- - (BOOL )startListening : (BOOL )onlyNew
92
- {
93
- if (self.eventStream ) {
94
- return YES ;
95
- }
96
-
97
- FSEventStreamContext context = {
98
- .version = 0 ,
99
- .info = (void *) CFBridgingRetain (self),
100
- .retain = CFRetain ,
101
- .release = CFRelease ,
102
- .copyDescription = NULL ,
103
- };
104
-
105
- NSMutableArray <NSString *> *pathsToWatch = NSMutableArray .array ;
106
- for (NSString *reportPath in self.directories ) {
107
- if ([[NSFileManager defaultManager ] fileExistsAtPath: reportPath]) {
108
- [pathsToWatch addObject: reportPath];
109
- }
110
- }
111
-
112
- FSEventStreamRef eventStream = FSEventStreamCreate (
113
- NULL , // Allocator
114
- (FSEventStreamCallback) EventStreamCallback, // Callback
115
- &context, // Context
116
- CFBridgingRetain (pathsToWatch), // Paths to watch
117
- onlyNew ? kFSEventStreamEventIdSinceNow : 0 , // Since When
118
- 0 , // Latency
119
- kFSEventStreamCreateFlagUseCFTypes | kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer
120
- );
121
- FSEventStreamSetDispatchQueue (eventStream, self.queue );
122
- Boolean started = FSEventStreamStart (eventStream);
123
- if (!started) {
124
- NSLog (@" FS Event Stream could not be started" );
125
- return NO ;
126
- }
127
-
128
- self.eventStream = eventStream;
129
- return YES ;
130
- }
131
-
132
- @end
133
-
134
- #endif
135
-
136
16
@interface FBCrashLogNotifier ()
137
17
138
- #if defined(__apple_build_version__)
139
- @property (nonatomic , strong , readonly ) FBCrashLogNotifier_FSEvents *fsEvents;
140
- #else
141
18
@property (nonatomic , copy , readwrite ) NSDate *sinceDate;
142
- #endif
143
19
144
20
@end
145
21
@@ -155,12 +31,7 @@ - (instancetype)initWithLogger:(id<FBControlCoreLogger>)logger
155
31
}
156
32
157
33
_store = [FBCrashLogStore storeForDirectories: FBCrashLogInfo.diagnosticReportsPaths logger: logger];
158
-
159
- #if defined(__apple_build_version__)
160
- _fsEvents = [[FBCrashLogNotifier_FSEvents alloc ] initWithDirectories: FBCrashLogInfo.diagnosticReportsPaths store: _store logger: logger];
161
- #else
162
34
_sinceDate = NSDate .date ;
163
- #endif
164
35
165
36
return self;
166
37
}
@@ -179,12 +50,8 @@ + (instancetype)sharedInstance
179
50
180
51
- (BOOL )startListening : (BOOL )onlyNew
181
52
{
182
- #if defined(__apple_build_version__)
183
- return [self .fsEvents startListening: onlyNew];
184
- #else
185
- self.sinceDate = NSDate .date ;
53
+ self.sinceDate = onlyNew ? NSDate .date : [NSDate distantPast ];
186
54
return YES ;
187
- #endif
188
55
}
189
56
190
57
- (FBFuture<FBCrashLogInfo *> *)nextCrashLogForPredicate : (NSPredicate *)predicate
@@ -195,9 +62,6 @@ - (BOOL)startListening:(BOOL)onlyNew
195
62
failFuture ];
196
63
}
197
64
198
- #if defined(__apple_build_version__)
199
- return [FBCrashLogNotifier.sharedInstance.fsEvents.store nextCrashLogForMatchingPredicate: predicate];
200
- #else
201
65
dispatch_queue_t queue = dispatch_queue_create (" com.facebook.fbcontrolcore.crashlogfetch" , DISPATCH_QUEUE_SERIAL);
202
66
return [FBFuture
203
67
onQueue: queue resolveUntil: ^{
@@ -210,9 +74,9 @@ - (BOOL)startListening:(BOOL)onlyNew
210
74
describeFormat: @" Crash Log Info for %@ could not be obtained" , predicate]
211
75
failFuture ];
212
76
}
77
+ [self .store ingestCrashLogAtPath: crashInfo.crashPath];
213
78
return [FBFuture futureWithResult: crashInfo];
214
79
}];
215
- #endif
216
80
}
217
81
218
82
@end
0 commit comments