2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import "SFAnalytics+Internal.h"
27 #import "SFAnalyticsDefines.h"
28 #import "SFAnalyticsActivityTracker+Internal.h"
29 #import "SFAnalyticsSampler+Internal.h"
30 #import "SFAnalyticsMultiSampler+Internal.h"
31 #import "SFAnalyticsSQLiteStore.h"
32 #import "NSDate+SFAnalytics.h"
33 #import "utilities/debugging.h"
34 #import <utilities/SecFileLocations.h>
35 #import <objc/runtime.h>
37 #import <CoreFoundation/CFPriv.h>
38 #include <os/transaction_private.h>
39 #include <os/variant_private.h>
41 #import <utilities/SecCoreAnalytics.h>
43 // SFAnalyticsDefines constants
44 NSString* const SFAnalyticsTableSuccessCount = @"success_count";
45 NSString* const SFAnalyticsTableHardFailures = @"hard_failures";
46 NSString* const SFAnalyticsTableSoftFailures = @"soft_failures";
47 NSString* const SFAnalyticsTableSamples = @"samples";
48 NSString* const SFAnalyticsTableNotes = @"notes";
50 NSString* const SFAnalyticsColumnSuccessCount = @"success_count";
51 NSString* const SFAnalyticsColumnHardFailureCount = @"hard_failure_count";
52 NSString* const SFAnalyticsColumnSoftFailureCount = @"soft_failure_count";
53 NSString* const SFAnalyticsColumnSampleValue = @"value";
54 NSString* const SFAnalyticsColumnSampleName = @"name";
56 NSString* const SFAnalyticsEventTime = @"eventTime";
57 NSString* const SFAnalyticsEventType = @"eventType";
58 NSString* const SFAnalyticsEventTypeErrorEvent = @"errorEvent";
59 NSString* const SFAnalyticsEventErrorDestription = @"errorDescription";
60 NSString* const SFAnalyticsEventClassKey = @"eventClass";
62 NSString* const SFAnalyticsAttributeErrorUnderlyingChain = @"errorChain";
63 NSString* const SFAnalyticsAttributeErrorDomain = @"errorDomain";
64 NSString* const SFAnalyticsAttributeErrorCode = @"errorCode";
66 NSString* const SFAnalyticsAttributeLastUploadTime = @"lastUploadTime";
68 NSString* const SFAnalyticsUserDefaultsSuite = @"com.apple.security.analytics";
70 char* const SFAnalyticsFireSamplersNotification = "com.apple.security.sfanalytics.samplers";
72 NSString* const SFAnalyticsTopicCloudServices = @"CloudServicesTopic";
73 NSString* const SFAnalyticsTopicKeySync = @"KeySyncTopic";
74 NSString* const SFAnalyticsTopicTrust = @"TrustTopic";
75 NSString* const SFAnalyticsTopicTransparency = @"TransparencyTopic";
77 NSString* const SFAnalyticsTableSchema = @"CREATE TABLE IF NOT EXISTS hard_failures (\n"
78 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
82 @"DROP TRIGGER IF EXISTS maintain_ring_buffer_hard_failures;\n"
83 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_hard_failures_v2 AFTER INSERT ON hard_failures\n"
85 @"DELETE FROM hard_failures WHERE id <= NEW.id - 1000;\n"
87 @"CREATE TABLE IF NOT EXISTS soft_failures (\n"
88 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
92 @"DROP TRIGGER IF EXISTS maintain_ring_buffer_soft_failures;\n"
93 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_soft_failures_v2 AFTER INSERT ON soft_failures\n"
95 @"DELETE FROM soft_failures WHERE id <= NEW.id - 1000;\n"
97 @"CREATE TABLE IF NOT EXISTS notes (\n"
98 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
102 @"DROP TRIGGER IF EXISTS maintain_ring_buffer_notes;\n"
103 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_notes_v2 AFTER INSERT ON notes\n"
105 @"DELETE FROM notes WHERE id <= NEW.id - 1000;\n"
107 @"CREATE TABLE IF NOT EXISTS samples (\n"
108 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
113 @"DROP TRIGGER IF EXISTS maintain_ring_buffer_samples;\n"
114 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer_samples_v2 AFTER INSERT ON samples\n"
116 @"DELETE FROM samples WHERE id <= NEW.id - 1000;\n"
118 @"CREATE TABLE IF NOT EXISTS success_count (\n"
119 @"event_type STRING PRIMARY KEY,\n"
120 @"success_count INTEGER,\n"
121 @"hard_failure_count INTEGER,\n"
122 @"soft_failure_count INTEGER\n"
124 @"DROP TABLE IF EXISTS all_events;\n";
126 NSUInteger const SFAnalyticsMaxEventsToReport = 1000;
128 NSString* const SFAnalyticsErrorDomain = @"com.apple.security.sfanalytics";
131 NSString* const SFAnalyticsEventBuild = @"build";
132 NSString* const SFAnalyticsEventProduct = @"product";
133 NSString* const SFAnalyticsEventInternal = @"internal";
134 const NSTimeInterval SFAnalyticsSamplerIntervalOncePerReport = -1.0;
136 @interface SFAnalytics ()
137 @property (nonatomic) SFAnalyticsSQLiteStore* database;
138 @property (nonatomic) dispatch_queue_t queue;
141 @implementation SFAnalytics {
142 SFAnalyticsSQLiteStore* _database;
143 dispatch_queue_t _queue;
144 NSMutableDictionary<NSString*, SFAnalyticsSampler*>* _samplers;
145 NSMutableDictionary<NSString*, SFAnalyticsMultiSampler*>* _multisamplers;
146 unsigned int _disableLogging:1;
149 + (instancetype)logger
151 if (self == [SFAnalytics class]) {
152 secerror("attempt to instatiate abstract class SFAnalytics");
156 SFAnalytics* logger = nil;
157 @synchronized(self) {
158 logger = objc_getAssociatedObject(self, "SFAnalyticsInstance");
160 logger = [[self alloc] init];
161 objc_setAssociatedObject(self, "SFAnalyticsInstance", logger, OBJC_ASSOCIATION_RETAIN);
165 [logger database]; // For unit testing so there's always a database. DB shouldn't be nilled in production though
169 + (NSString*)databasePath
174 + (NSString *)defaultAnalyticsDatabasePath:(NSString *)basename
176 WithPathInKeychainDirectory(CFSTR("Analytics"), ^(const char *path) {
178 mode_t permissions = 0775;
180 mode_t permissions = 0700;
181 #endif // TARGET_OS_IPHONE
182 int ret = mkpath_np(path, permissions);
183 if (!(ret == 0 || ret == EEXIST)) {
184 secerror("could not create path: %s (%s)", path, strerror(ret));
186 chmod(path, permissions);
188 NSString *path = [NSString stringWithFormat:@"Analytics/%@.db", basename];
189 return [(__bridge_transfer NSURL*)SecCopyURLForFileInKeychainDirectory((__bridge CFStringRef)path) path];
192 + (NSInteger)fuzzyDaysSinceDate:(NSDate*)date
194 // Sentinel: it didn't happen at all
199 // Sentinel: it happened but we don't know when because the date doesn't make sense
200 // Magic number represents January 1, 2017.
201 if ([date compare:[NSDate dateWithTimeIntervalSince1970:1483228800]] == NSOrderedAscending) {
205 NSInteger secondsPerDay = 60 * 60 * 24;
207 NSTimeInterval timeIntervalSinceDate = [[NSDate date] timeIntervalSinceDate:date];
208 if (timeIntervalSinceDate < secondsPerDay) {
211 else if (timeIntervalSinceDate < (secondsPerDay * 7)) {
214 else if (timeIntervalSinceDate < (secondsPerDay * 30)) {
217 else if (timeIntervalSinceDate < (secondsPerDay * 365)) {
225 // Instantiate lazily so unit tests can have clean databases each
226 - (SFAnalyticsSQLiteStore*)database
229 _database = [SFAnalyticsSQLiteStore storeWithPath:self.class.databasePath schema:SFAnalyticsTableSchema];
231 seccritical("Did not get a database! (Client %@)", NSStringFromClass([self class]));
239 [_samplers removeAllObjects];
240 [_multisamplers removeAllObjects];
242 __weak __typeof(self) weakSelf = self;
243 dispatch_sync(_queue, ^{
244 __strong __typeof(self) strongSelf = weakSelf;
246 [strongSelf.database close];
247 strongSelf->_database = nil;
252 - (void)setDateProperty:(NSDate*)date forKey:(NSString*)key
254 __weak __typeof(self) weakSelf = self;
255 dispatch_sync(_queue, ^{
256 __strong __typeof(self) strongSelf = weakSelf;
258 [strongSelf.database setDateProperty:date forKey:key];
263 - (NSDate*)datePropertyForKey:(NSString*)key
265 __block NSDate* result = nil;
266 __weak __typeof(self) weakSelf = self;
267 dispatch_sync(_queue, ^{
268 __strong __typeof(self) strongSelf = weakSelf;
270 result = [strongSelf.database datePropertyForKey:key];
277 - (void)incrementIntegerPropertyForKey:(NSString*)key
279 __weak __typeof(self) weakSelf = self;
280 dispatch_sync(_queue, ^{
281 __strong __typeof(self) strongSelf = weakSelf;
282 if (strongSelf == nil) {
285 NSInteger integer = [[strongSelf.database propertyForKey:key] integerValue];
286 [strongSelf.database setProperty:[NSString stringWithFormat:@"%ld", (long)integer + 1] forKey:key];
290 - (void)setNumberProperty:(NSNumber* _Nullable)number forKey:(NSString*)key
292 __weak __typeof(self) weakSelf = self;
293 dispatch_sync(_queue, ^{
294 __strong __typeof(self) strongSelf = weakSelf;
296 [strongSelf.database setProperty:[number stringValue] forKey:key];
301 - (NSNumber* _Nullable)numberPropertyForKey:(NSString*)key
303 __block NSNumber* result = nil;
304 __weak __typeof(self) weakSelf = self;
305 dispatch_sync(_queue, ^{
306 __strong __typeof(self) strongSelf = weakSelf;
308 NSString *property = [strongSelf.database propertyForKey:key];
310 result = [NSNumber numberWithInteger:[property integerValue]];
318 + (void)addOSVersionToEvent:(NSMutableDictionary*)eventDict {
319 static dispatch_once_t onceToken;
320 static NSString *build = NULL;
321 static NSString *product = NULL;
322 static BOOL internal = NO;
323 dispatch_once(&onceToken, ^{
324 NSDictionary *version = CFBridgingRelease(_CFCopySystemVersionDictionary());
327 build = version[(__bridge NSString *)_kCFSystemVersionBuildVersionKey];
328 product = version[(__bridge NSString *)_kCFSystemVersionProductNameKey];
329 internal = os_variant_has_internal_diagnostics("com.apple.security");
332 eventDict[SFAnalyticsEventBuild] = build;
335 eventDict[SFAnalyticsEventProduct] = product;
338 eventDict[SFAnalyticsEventInternal] = @YES;
344 if (self = [super init]) {
345 _queue = dispatch_queue_create("SFAnalytics data access queue", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
346 _samplers = [NSMutableDictionary<NSString*, SFAnalyticsSampler*> new];
347 _multisamplers = [NSMutableDictionary<NSString*, SFAnalyticsMultiSampler*> new];
348 [self database]; // for side effect of instantiating DB object. Used for testing.
354 - (NSDictionary *)coreAnalyticsKeyFilter:(NSDictionary<NSString *, id> *)info
356 NSMutableDictionary *filtered = [NSMutableDictionary dictionary];
357 [info enumerateKeysAndObjectsUsingBlock:^(NSString *_Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
358 filtered[[key stringByReplacingOccurrencesOfString:@"-" withString:@"_"]] = obj;
363 // Daily CoreAnalytics metrics
364 // Call this once per say if you want to have the once per day sampler collect their data and submit it
366 - (void)dailyCoreAnalyticsMetrics:(NSString *)eventName
368 NSMutableDictionary<NSString*, NSNumber*> *dailyMetrics = [NSMutableDictionary dictionary];
369 __block NSDictionary<NSString*, SFAnalyticsMultiSampler*>* multisamplers;
370 __block NSDictionary<NSString*, SFAnalyticsSampler*>* samplers;
372 dispatch_sync(_queue, ^{
373 multisamplers = [self->_multisamplers copy];
374 samplers = [self->_samplers copy];
377 [multisamplers enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, SFAnalyticsMultiSampler * _Nonnull obj, BOOL * _Nonnull stop) {
378 if (obj.oncePerReport == FALSE) {
381 NSDictionary<NSString*, NSNumber*> *samples = [obj sampleNow];
382 if (samples == nil) {
385 [dailyMetrics addEntriesFromDictionary:samples];
388 [samplers enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, SFAnalyticsSampler * _Nonnull obj, BOOL * _Nonnull stop) {
389 if (obj.oncePerReport == FALSE) {
392 dailyMetrics[key] = [obj sampleNow];
395 [SecCoreAnalytics sendEvent:eventName event:[self coreAnalyticsKeyFilter:dailyMetrics]];
398 // MARK: Event logging
400 - (void)logSuccessForEventNamed:(NSString*)eventName timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
402 [self logEventNamed:eventName class:SFAnalyticsEventClassSuccess attributes:nil timestampBucket:timestampBucket];
405 - (void)logSuccessForEventNamed:(NSString*)eventName
407 [self logSuccessForEventNamed:eventName timestampBucket:SFAnalyticsTimestampBucketSecond];
410 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
412 [self logEventNamed:eventName class:SFAnalyticsEventClassHardFailure attributes:attributes timestampBucket:timestampBucket];
415 - (void)logHardFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes
417 [self logHardFailureForEventNamed:eventName withAttributes:attributes timestampBucket:SFAnalyticsTimestampBucketSecond];
420 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
422 [self logEventNamed:eventName class:SFAnalyticsEventClassSoftFailure attributes:attributes timestampBucket:timestampBucket];
425 - (void)logSoftFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes
427 [self logSoftFailureForEventNamed:eventName withAttributes:attributes timestampBucket:SFAnalyticsTimestampBucketSecond];
430 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
432 [self logResultForEvent:eventName hardFailure:hardFailure result:eventResultError withAttributes:nil timestampBucket:SFAnalyticsTimestampBucketSecond];
435 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError
437 [self logResultForEvent:eventName hardFailure:hardFailure result:eventResultError timestampBucket:SFAnalyticsTimestampBucketSecond];
440 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError withAttributes:(NSDictionary*)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
442 if(!eventResultError) {
443 [self logSuccessForEventNamed:eventName];
445 // Make an Attributes dictionary
446 NSMutableDictionary* eventAttributes = nil;
448 eventAttributes = [attributes mutableCopy];
450 eventAttributes = [NSMutableDictionary dictionary];
453 /* if we have underlying errors, capture the chain below the top-most error */
454 NSError *underlyingError = eventResultError.userInfo[NSUnderlyingErrorKey];
455 if ([underlyingError isKindOfClass:[NSError class]]) {
456 NSMutableString *chain = [NSMutableString string];
459 [chain appendFormat:@"%@-%ld:", underlyingError.domain, (long)underlyingError.code];
460 underlyingError = underlyingError.userInfo[NSUnderlyingErrorKey];
461 } while (count++ < 5 && [underlyingError isKindOfClass:[NSError class]]);
463 eventAttributes[SFAnalyticsAttributeErrorUnderlyingChain] = chain;
466 eventAttributes[SFAnalyticsAttributeErrorDomain] = eventResultError.domain;
467 eventAttributes[SFAnalyticsAttributeErrorCode] = @(eventResultError.code);
470 [self logHardFailureForEventNamed:eventName withAttributes:eventAttributes];
472 [self logSoftFailureForEventNamed:eventName withAttributes:eventAttributes];
477 - (void)logResultForEvent:(NSString*)eventName hardFailure:(bool)hardFailure result:(NSError*)eventResultError withAttributes:(NSDictionary*)attributes
479 [self logResultForEvent:eventName hardFailure:hardFailure result:eventResultError withAttributes:attributes timestampBucket:SFAnalyticsTimestampBucketSecond];
482 - (void)noteEventNamed:(NSString*)eventName timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
484 [self logEventNamed:eventName class:SFAnalyticsEventClassNote attributes:nil timestampBucket:timestampBucket];
487 - (void)noteEventNamed:(NSString*)eventName
489 [self noteEventNamed:eventName timestampBucket:SFAnalyticsTimestampBucketSecond];
492 - (void)logEventNamed:(NSString*)eventName class:(SFAnalyticsEventClass)class attributes:(NSDictionary*)attributes timestampBucket:(SFAnalyticsTimestampBucket)timestampBucket
495 secerror("SFAnalytics: attempt to log an event with no name");
499 __weak __typeof(self) weakSelf = self;
500 dispatch_sync(_queue, ^{
501 __strong __typeof(self) strongSelf = weakSelf;
502 if (!strongSelf || strongSelf->_disableLogging) {
506 [strongSelf.database begin];
508 NSDictionary* eventDict = [self eventDictForEventName:eventName withAttributes:attributes eventClass:class timestampBucket:timestampBucket];
510 if (class == SFAnalyticsEventClassHardFailure) {
511 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableHardFailures timestampBucket:timestampBucket];
512 [strongSelf.database incrementHardFailureCountForEventType:eventName];
514 else if (class == SFAnalyticsEventClassSoftFailure) {
515 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableSoftFailures timestampBucket:timestampBucket];
516 [strongSelf.database incrementSoftFailureCountForEventType:eventName];
518 else if (class == SFAnalyticsEventClassNote) {
519 [strongSelf.database addEventDict:eventDict toTable:SFAnalyticsTableNotes timestampBucket:timestampBucket];
520 [strongSelf.database incrementSuccessCountForEventType:eventName];
522 else if (class == SFAnalyticsEventClassSuccess) {
523 [strongSelf.database incrementSuccessCountForEventType:eventName];
526 [strongSelf.database end];
530 - (void)logEventNamed:(NSString*)eventName class:(SFAnalyticsEventClass)class attributes:(NSDictionary*)attributes
532 [self logEventNamed:eventName class:class attributes:attributes timestampBucket:SFAnalyticsTimestampBucketSecond];
535 - (NSDictionary*) eventDictForEventName:(NSString*)eventName withAttributes:(NSDictionary*)attributes eventClass:(SFAnalyticsEventClass)eventClass timestampBucket:(NSTimeInterval)timestampBucket
537 NSMutableDictionary* eventDict = attributes ? attributes.mutableCopy : [NSMutableDictionary dictionary];
538 eventDict[SFAnalyticsEventType] = eventName;
540 NSTimeInterval timestamp = [[NSDate date] timeIntervalSince1970WithBucket:timestampBucket];
542 // our backend wants timestamps in milliseconds
543 eventDict[SFAnalyticsEventTime] = @(timestamp * 1000);
544 eventDict[SFAnalyticsEventClassKey] = @(eventClass);
545 [SFAnalytics addOSVersionToEvent:eventDict];
552 - (SFAnalyticsSampler*)addMetricSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSNumber *(^)(void))block
555 secerror("SFAnalytics: cannot add sampler without name");
558 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
559 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
563 secerror("SFAnalytics: cannot add sampler without block");
567 __block SFAnalyticsSampler* sampler = nil;
569 __weak __typeof(self) weakSelf = self;
570 dispatch_sync(_queue, ^{
571 __strong __typeof(self) strongSelf = weakSelf;
572 if (strongSelf->_samplers[samplerName]) {
573 secerror("SFAnalytics: sampler \"%@\" already exists", samplerName);
575 sampler = [[SFAnalyticsSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
576 strongSelf->_samplers[samplerName] = sampler; // If sampler did not init because of bad data this 'removes' it from the dict, so a noop
583 - (SFAnalyticsMultiSampler*)AddMultiSamplerForName:(NSString *)samplerName withTimeInterval:(NSTimeInterval)timeInterval block:(NSDictionary<NSString *,NSNumber *> *(^)(void))block
586 secerror("SFAnalytics: cannot add sampler without name");
589 if (timeInterval < 1.0f && timeInterval != SFAnalyticsSamplerIntervalOncePerReport) {
590 secerror("SFAnalytics: cannot add sampler with interval %f", timeInterval);
594 secerror("SFAnalytics: cannot add sampler without block");
598 __block SFAnalyticsMultiSampler* sampler = nil;
599 __weak __typeof(self) weakSelf = self;
600 dispatch_sync(_queue, ^{
601 __strong __typeof(self) strongSelf = weakSelf;
602 if (strongSelf->_multisamplers[samplerName]) {
603 secerror("SFAnalytics: multisampler \"%@\" already exists", samplerName);
605 sampler = [[SFAnalyticsMultiSampler alloc] initWithName:samplerName interval:timeInterval block:block clientClass:[self class]];
606 strongSelf->_multisamplers[samplerName] = sampler;
614 - (SFAnalyticsSampler*)existingMetricSamplerForName:(NSString *)samplerName
616 __block SFAnalyticsSampler* sampler = nil;
618 __weak __typeof(self) weakSelf = self;
619 dispatch_sync(_queue, ^{
620 __strong __typeof(self) strongSelf = weakSelf;
622 sampler = strongSelf->_samplers[samplerName];
628 - (SFAnalyticsMultiSampler*)existingMultiSamplerForName:(NSString *)samplerName
630 __block SFAnalyticsMultiSampler* sampler = nil;
632 __weak __typeof(self) weakSelf = self;
633 dispatch_sync(_queue, ^{
634 __strong __typeof(self) strongSelf = weakSelf;
636 sampler = strongSelf->_multisamplers[samplerName];
642 - (void)removeMetricSamplerForName:(NSString *)samplerName
645 secerror("Attempt to remove sampler without specifying samplerName");
649 __weak __typeof(self) weakSelf = self;
650 dispatch_async(_queue, ^{
651 os_transaction_t transaction = os_transaction_create("com.apple.security.sfanalytics.samplerGC");
652 __strong __typeof(self) strongSelf = weakSelf;
654 [strongSelf->_samplers[samplerName] pauseSampling]; // when dealloced it would also stop, but we're not sure when that is so let's stop it right away
655 [strongSelf->_samplers removeObjectForKey:samplerName];
662 - (void)removeMultiSamplerForName:(NSString *)samplerName
665 secerror("Attempt to remove multisampler without specifying samplerName");
669 __weak __typeof(self) weakSelf = self;
670 dispatch_async(_queue, ^{
671 os_transaction_t transaction = os_transaction_create("com.apple.security.sfanalytics.samplerGC");
672 __strong __typeof(self) strongSelf = weakSelf;
674 [strongSelf->_multisamplers[samplerName] pauseSampling]; // when dealloced it would also stop, but we're not sure when that is so let's stop it right away
675 [strongSelf->_multisamplers removeObjectForKey:samplerName];
682 - (SFAnalyticsActivityTracker*)logSystemMetricsForActivityNamed:(NSString *)eventName withAction:(void (^)(void))action
684 if (![eventName isKindOfClass:[NSString class]]) {
685 secerror("Cannot log system metrics without name");
688 SFAnalyticsActivityTracker* tracker = [[SFAnalyticsActivityTracker alloc] initWithName:eventName clientClass:[self class]];
690 [tracker performAction:action];
695 - (SFAnalyticsActivityTracker*)startLogSystemMetricsForActivityNamed:(NSString *)eventName
697 if (![eventName isKindOfClass:[NSString class]]) {
698 secerror("Cannot log system metrics without name");
701 SFAnalyticsActivityTracker* tracker = [[SFAnalyticsActivityTracker alloc] initWithName:eventName clientClass:[self class]];
706 - (void)logMetric:(NSNumber *)metric withName:(NSString *)metricName
708 [self logMetric:metric withName:metricName oncePerReport:NO];
711 - (void)logMetric:(NSNumber*)metric withName:(NSString*)metricName oncePerReport:(BOOL)once
713 if (![metric isKindOfClass:[NSNumber class]] || ![metricName isKindOfClass:[NSString class]]) {
714 secerror("SFAnalytics: Need a valid result and name to log result");
718 __weak __typeof(self) weakSelf = self;
719 dispatch_async(_queue, ^{
720 os_transaction_t transaction = os_transaction_create("com.apple.security.sfanalytics.samplerGC");
721 __strong __typeof(self) strongSelf = weakSelf;
722 if (strongSelf && !strongSelf->_disableLogging) {
724 [strongSelf.database removeAllSamplesForName:metricName];
726 [strongSelf.database addSample:metric forName:metricName];