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 "CKKSLogger.h"
29 #import "CKKSViewManager.h"
30 #import "keychain/ckks/CKKSKeychainView.h"
31 #include <utilities/SecFileLocations.h>
32 #import <Security/SFSQLite.h>
35 NSString* const CKKSLoggerTableSuccessCount = @"success_count";
36 NSString* const CKKSLoggerColumnEventType = @"event_type";
37 NSString* const CKKSLoggerColumnSuccessCount = @"success_count";
38 NSString* const CKKSLoggerColumnFailureCount = @"failure_count";
40 NSString* const CKKSLoggerTableFailures = @"failures";
41 NSString* const CKKSLoggerColumnData = @"data";
43 NSString* const CKKSLoggerUploadDate = @"upload_date";
44 NSString* const CKKSLoggerLastClassASync = @"last_class_a_sync";
45 NSString* const CKKSLoggerLastClassCSync = @"last_class_c_sync";
47 NSString* const CKKSLoggerDaysSinceLastSyncClassA = @"lastSyncClassA";
48 NSString* const CKKSLoggerDaysSinceLastSyncClassC = @"lastSyncClassC";
50 NSString* const CKKSLoggerSplunkTopic = @"topic";
51 NSString* const CKKSLoggerSplunkEventTime = @"eventTime";
52 NSString* const CKKSLoggerSplunkPostTime = @"postTime";
53 NSString* const CKKSLoggerSplunkEvents = @"events";
54 NSString* const CKKSLoggerSplunkEventType = @"eventType";
55 NSString* const CKKSLoggerMetricsBase = @"metricsBase";
57 NSString* const CKKSLoggerValueSuccess = @"success";
59 #define CKKS_SPLUNK_DEV 0
62 #define SECONDS_BETWEEN_UPLOADS 10
64 // three days = 60 seconds times 60 minutes * 72 hours
65 #define SECONDS_BETWEEN_UPLOADS (60 * 60 * 72)
68 NSString* const CKKSLoggingTableSchema = @"CREATE TABLE IF NOT EXISTS failures (\n"
69 @"id INTEGER PRIMARY KEY AUTOINCREMENT,\n"
72 @"CREATE TRIGGER IF NOT EXISTS maintain_ring_buffer AFTER INSERT ON failures\n"
74 @"DELETE FROM failures WHERE id != NEW.id AND id % 999 = NEW.id % 999;\n"
76 @"CREATE TABLE IF NOT EXISTS success_count (\n"
77 @"event_type STRING PRIMARY KEY,\n"
78 @"success_count INTEGER,\n"
79 @"failure_count INTEGER\n"
82 static NSString* CKKSLoggingTablePath()
84 return [(__bridge_transfer NSURL*)SecCopyURLForFileInKeychainDirectory((__bridge CFStringRef)@"ckks_analytics_v1.db") path];
87 @interface CKKSLoggerSQLiteStore : SFSQLite
89 + (instancetype)sharedStore;
91 @property (readonly, strong) NSArray* failureRecords;
92 @property (readwrite, strong) NSDate* uploadDate;
94 - (void)incrementSuccessCountForEventType:(NSString*)eventType;
95 - (void)incrementFailureCountForEventType:(NSString*)eventType;
96 - (NSInteger)successCountForEventType:(NSString*)eventType;
97 - (NSInteger)failureCountForEventType:(NSString*)eventType;
98 - (void)addFailureRecord:(NSDictionary*)valueDict;
101 - (NSDictionary*)summaryCounts;
105 @implementation CKKSLogger {
106 NSURL* _splunkUploadURL;
107 NSString* _splunkTopicName;
108 NSURL* _splunkBagURL;
109 dispatch_queue_t _queue;
110 NSInteger _secondsBetweenUploads;
111 NSDictionary* _metricsBase; // data the server provides and wants us to send back
112 NSArray* _blacklistedFields;
113 NSArray* _blacklistedEvents;
115 unsigned int _allowInsecureSplunkCert:1;
116 unsigned int _disableLogging:1;
117 unsigned int _disableUploads:1;
118 unsigned int _ignoreServersMessagesTellingUsToGoAway:1;
121 @synthesize splunkUploadURL = _splunkUploadURL;
122 @synthesize splunkBagURL = _splunkBagURL;
123 @synthesize splunkTopicName = _splunkTopicName;
124 @synthesize splunkLoggingQueue = _queue;
126 + (instancetype)logger
128 #if TARGET_OS_SIMULATOR
131 static CKKSLogger* __sharedLogger;
132 static dispatch_once_t onceToken;
133 dispatch_once(&onceToken, ^{
134 __sharedLogger = [[CKKSLogger alloc] init];
137 return __sharedLogger;
142 if (self = [super init]) {
143 _queue = dispatch_queue_create("com.apple.security.ckks.logging", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
144 _secondsBetweenUploads = SECONDS_BETWEEN_UPLOADS;
146 NSDictionary* systemDefaultValues = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle bundleWithPath:@"/System/Library/Frameworks/Security.framework"] pathForResource:@"CKKSLogging" ofType:@"plist"]];
147 _splunkTopicName = systemDefaultValues[@"splunk_topic"];
148 _splunkUploadURL = [NSURL URLWithString:systemDefaultValues[@"splunk_uploadURL"]];
149 _splunkBagURL = [NSURL URLWithString:systemDefaultValues[@"splunk_bagURL"]];
150 _allowInsecureSplunkCert = [[systemDefaultValues valueForKey:@"splunk_allowInsecureCertificate"] boolValue];
151 NSString* splunkEndpoint = systemDefaultValues[@"splunk_endpointDomain"];
153 NSUserDefaults* defaults = [[NSUserDefaults alloc] initWithSuiteName:SecCKKSUserDefaultsSuite];
154 NSString* userDefaultsSplunkTopic = [defaults stringForKey:@"splunk_topic"];
155 if (userDefaultsSplunkTopic) {
156 _splunkTopicName = userDefaultsSplunkTopic;
159 NSURL* userDefaultsSplunkUploadURL = [NSURL URLWithString:[defaults stringForKey:@"splunk_uploadURL"]];
160 if (userDefaultsSplunkUploadURL) {
161 _splunkUploadURL = userDefaultsSplunkUploadURL;
164 NSURL* userDefaultsSplunkBagURL = [NSURL URLWithString:[defaults stringForKey:@"splunk_bagURL"]];
165 if (userDefaultsSplunkUploadURL) {
166 _splunkBagURL = userDefaultsSplunkBagURL;
169 BOOL userDefaultsAllowInsecureSplunkCert = [defaults boolForKey:@"splunk_allowInsecureCertificate"];
170 _allowInsecureSplunkCert |= userDefaultsAllowInsecureSplunkCert;
172 NSString* userDefaultsSplunkEndpoint = [defaults stringForKey:@"splunk_endpointDomain"];
173 if (userDefaultsSplunkEndpoint) {
174 splunkEndpoint = userDefaultsSplunkEndpoint;
178 _ignoreServersMessagesTellingUsToGoAway = YES;
180 if (!_splunkUploadURL && splunkEndpoint) {
181 NSString* urlString = [NSString stringWithFormat:@"https://%@/report/2/%@", splunkEndpoint, _splunkTopicName];
182 _splunkUploadURL = [NSURL URLWithString:urlString];
185 (void)splunkEndpoint;
192 - (void)setLastSuccessfulClassASyncDate:(NSDate*)lastSuccessfulClassASyncDate
194 dispatch_sync(_queue, ^{
195 [[CKKSLoggerSQLiteStore sharedStore] setDateProperty:lastSuccessfulClassASyncDate forKey:CKKSLoggerLastClassASync];
199 - (NSDate*)lastSuccessfulClassASyncDate
201 __block NSDate* result = nil;
202 dispatch_sync(_queue, ^{
203 result = [self _onQueueLastSuccessfulClassASyncDate];
209 - (NSDate*)_onQueueLastSuccessfulClassASyncDate
211 dispatch_assert_queue(_queue);
212 return [[CKKSLoggerSQLiteStore sharedStore] datePropertyForKey:CKKSLoggerLastClassASync] ?: [NSDate distantPast];
215 - (void)setLastSuccessfulClassCSyncDate:(NSDate*)lastSuccessfulClassCSyncDate
217 dispatch_sync(_queue, ^{
218 [[CKKSLoggerSQLiteStore sharedStore] setDateProperty:lastSuccessfulClassCSyncDate forKey:CKKSLoggerLastClassCSync];
222 - (NSDate*)lastSuccessfulClassCSyncDate
224 __block NSDate* result = nil;
225 dispatch_sync(_queue, ^{
226 result = [self _onQueueLastSuccessfulClassCSyncDate];
232 - (NSDate*)_onQueueLastSuccessfulClassCSyncDate
234 dispatch_assert_queue(_queue);
235 return [[CKKSLoggerSQLiteStore sharedStore] datePropertyForKey:CKKSLoggerLastClassCSync] ?: [NSDate distantPast];
238 - (void)logSuccessForEventNamed:(NSString*)eventName
240 [self logEventNamed:eventName value:nil isSuccess:YES];
243 - (void)logFailureForEventNamed:(NSString*)eventName withAttributes:(NSDictionary*)attributes
245 [self logEventNamed:eventName value:attributes isSuccess:NO];
248 - (void)logEventNamed:(NSString*)eventName value:(NSDictionary*)valueDict isSuccess:(BOOL)isSuccess
250 __weak __typeof(self) weakSelf = self;
251 dispatch_async(_queue, ^{
253 __strong __typeof(self) strongSelf = weakSelf;
258 if (strongSelf->_disableLogging || [strongSelf->_blacklistedEvents containsObject:eventName]) {
262 CKKSLoggerSQLiteStore* store = [CKKSLoggerSQLiteStore sharedStore];
264 [store incrementSuccessCountForEventType:eventName];
267 [store incrementFailureCountForEventType:eventName];
268 NSMutableDictionary* eventDict = valueDict.mutableCopy;
269 eventDict[CKKSLoggerSplunkTopic] = strongSelf->_splunkTopicName;
270 eventDict[CKKSLoggerSplunkEventType] = eventName;
271 eventDict[CKKSLoggerSplunkEventTime] = @([[NSDate date] timeIntervalSince1970] * 1000);
272 eventDict[CKKSLoggerMetricsBase] = strongSelf->_metricsBase ?: [NSDictionary dictionary];
274 for (NSString* blacklistedField in strongSelf->_blacklistedFields) {
275 [eventDict removeObjectForKey:blacklistedField];
278 [store addFailureRecord:eventDict];
281 NSDate* uploadDate = store.uploadDate;
282 NSDate* nowDate = [NSDate date];
284 if ([nowDate compare:uploadDate] == NSOrderedDescending) {
285 [self _onQueueUploadDataWithError:nil];
289 store.uploadDate = [nowDate dateByAddingTimeInterval:strongSelf->_secondsBetweenUploads];
294 // this method is kind of evil for the fact that it has side-effects in pulling other things besides the metricsURL from the server, and as such should NOT be memoized.
295 // TODO redo this, probably to return a dictionary.
296 - (NSURL*)splunkUploadURL
298 dispatch_assert_queue(_queue);
300 if (_splunkUploadURL) {
301 return _splunkUploadURL;
304 __weak __typeof(self) weakSelf = self;
305 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
307 __block NSError* error = nil;
308 NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
309 NSURLSession* storeBagSession = [NSURLSession sessionWithConfiguration:defaultConfiguration
313 NSURL* requestEndpoint = _splunkBagURL;
314 __block NSURL* result = nil;
315 NSURLSessionDataTask* storeBagTask = [storeBagSession dataTaskWithURL:requestEndpoint completionHandler:^(NSData * _Nullable data,
316 NSURLResponse * _Nullable __unused response,
317 NSError * _Nullable responseError) {
319 __strong __typeof(self) strongSelf = weakSelf;
324 if (data && !responseError) {
325 NSData *responseData = data; // shut up compiler
326 NSDictionary* responseDict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
327 if([responseDict isKindOfClass:NSDictionary.class] && !error) {
328 if (!self->_ignoreServersMessagesTellingUsToGoAway) {
329 strongSelf->_disableLogging = [[responseDict valueForKey:@"disabled"] boolValue];
330 if (strongSelf->_disableLogging || [[responseDict valueForKey:@"sendDisabled"] boolValue]) {
331 // then don't upload anything right now
332 secerror("not returning a splunk URL because uploads are disabled");
333 dispatch_semaphore_signal(sem);
337 NSUInteger millisecondsBetweenUploads = [[responseDict valueForKey:@"postFrequency"] unsignedIntegerValue] / 1000;
338 if (millisecondsBetweenUploads > 0) {
339 strongSelf->_secondsBetweenUploads = millisecondsBetweenUploads;
342 strongSelf->_blacklistedEvents = responseDict[@"blacklistedEvents"];
343 strongSelf->_blacklistedFields = responseDict[@"blacklistedFields"];
346 strongSelf->_metricsBase = responseDict[@"metricsBase"];
348 NSString* metricsEndpoint = responseDict[@"metricsUrl"];
349 if([metricsEndpoint isKindOfClass:NSString.class]) {
351 NSString* endpoint = [metricsEndpoint stringByAppendingFormat:@"/2/%@", strongSelf->_splunkTopicName];
352 secnotice("ckks", "got metrics endpoint: %@", endpoint);
353 NSURL* endpointURL = [NSURL URLWithString:endpoint];
354 if([endpointURL.scheme isEqualToString:@"https"]) {
355 result = endpointURL;
361 error = responseError;
364 secnotice("ckks", "Unable to fetch splunk endpoint at URL: %@ -- error: %@", requestEndpoint, error.description);
367 secnotice("ckks", "Malformed iTunes config payload!");
370 dispatch_semaphore_signal(sem);
373 [storeBagTask resume];
374 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
379 - (BOOL)forceUploadWithError:(NSError**)error
381 __block BOOL result = NO;
382 dispatch_sync(_queue, ^{
383 result = [self _onQueueUploadDataWithError:error];
388 - (BOOL)_onQueueUploadDataWithError:(NSError**)error
390 dispatch_assert_queue(_queue);
392 NSData* json = [self _onQueueGetLoggingJSONWithError:error];
393 if (json && [self _onQueuePostJSON:json error:error]) {
394 secinfo("ckks", "uploading sync health data: %@", json);
396 CKKSLoggerSQLiteStore* store = [CKKSLoggerSQLiteStore sharedStore];
397 [store clearAllData];
398 store.uploadDate = [NSDate dateWithTimeIntervalSinceNow:_secondsBetweenUploads];
406 - (BOOL)_onQueuePostJSON:(NSData*)json error:(NSError**)error
408 dispatch_assert_queue(_queue);
411 * Create the NSURLSession
412 * We use the ephemeral session config because we don't need cookies or cache
414 NSURLSessionConfiguration *defaultConfiguration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
415 NSURLSession* postSession = [NSURLSession sessionWithConfiguration:defaultConfiguration
422 NSURL* postEndpoint = self.splunkUploadURL;
424 secerror("failed to get a splunk upload endpoint - not uploading");
428 NSMutableURLRequest* postRequest = [[NSMutableURLRequest alloc] init];
429 postRequest.URL = postEndpoint;
430 postRequest.HTTPMethod = @"POST";
431 postRequest.HTTPBody = json;
434 * Create the upload task.
436 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
437 __block BOOL uploadSuccess = NO;
438 NSURLSessionDataTask* uploadTask = [postSession dataTaskWithRequest:postRequest
439 completionHandler:^(NSData * _Nullable __unused data, NSURLResponse * _Nullable response, NSError * _Nullable requestError) {
441 secerror("Error in uploading the events to splunk: %@", requestError);
443 else if (![response isKindOfClass:NSHTTPURLResponse.class]){
444 Class class = response.class;
445 secerror("Received the wrong kind of response: %@", NSStringFromClass(class));
448 NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
449 if(httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
452 secnotice("ckks", "Splunk upload success");
455 secnotice("ckks", "Splunk upload unexpected status to URL: %@ -- status: %d", postEndpoint, (int)(httpResponse.statusCode));
458 dispatch_semaphore_signal(sem);
461 secnotice("ckks", "Splunk upload start");
463 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
464 return uploadSuccess;
467 #define SECOND_PER_DAY (60 * 60 * 24)
469 - (NSInteger)fuzzyDaysSinceDate:(NSDate*)date
471 NSTimeInterval timeIntervalSinceDate = [[NSDate date] timeIntervalSinceDate:date];
472 if (timeIntervalSinceDate < SECOND_PER_DAY) {
475 else if (timeIntervalSinceDate < (SECOND_PER_DAY * 7)) {
478 else if (timeIntervalSinceDate < (SECOND_PER_DAY * 30)) {
481 else if (timeIntervalSinceDate < (SECOND_PER_DAY * 365)) {
489 - (NSData*)getLoggingJSONWithError:(NSError**)error
491 __block NSData* json = nil;
492 dispatch_sync(_queue, ^{
493 json = [self _onQueueGetLoggingJSONWithError:error];
499 - (NSData*)_onQueueGetLoggingJSONWithError:(NSError**)error
501 dispatch_assert_queue(_queue);
503 CKKSLoggerSQLiteStore* store = [CKKSLoggerSQLiteStore sharedStore];
504 NSArray* failureRecords = [store failureRecords];
506 NSDictionary* successCounts = [store summaryCounts];
507 NSInteger totalSuccessCount = 0;
508 NSInteger totalFailureCount = 0;
509 for (NSDictionary* perEventTypeSuccessCounts in successCounts.objectEnumerator) {
510 totalSuccessCount += [perEventTypeSuccessCounts[CKKSLoggerColumnSuccessCount] integerValue];
511 totalFailureCount += [perEventTypeSuccessCounts[CKKSLoggerColumnFailureCount] integerValue];
514 NSDate* now = [NSDate date];
516 NSMutableDictionary* healthSummaryEvent = [[NSMutableDictionary alloc] init];
517 healthSummaryEvent[CKKSLoggerSplunkTopic] = _splunkTopicName ?: [NSNull null];
518 healthSummaryEvent[CKKSLoggerSplunkEventTime] = @([now timeIntervalSince1970] * 1000);
519 healthSummaryEvent[CKKSLoggerSplunkEventType] = @"manifestHealthSummary";
520 healthSummaryEvent[CKKSLoggerColumnSuccessCount] = @(totalSuccessCount);
521 healthSummaryEvent[CKKSLoggerColumnFailureCount] = @(totalFailureCount);
522 healthSummaryEvent[CKKSLoggerMetricsBase] = _metricsBase ?: [NSDictionary dictionary];
524 for (NSString* viewName in [CKKSViewManager viewList]) {
525 CKKSKeychainView* view = [CKKSViewManager findOrCreateView:viewName];
526 [healthSummaryEvent setValue:@([self fuzzyDaysSinceDate:[self _onQueueLastSuccessfulClassASyncDate]]) forKey:[NSString stringWithFormat:@"%@-%@", view.zoneName, CKKSLoggerDaysSinceLastSyncClassA]];
527 [healthSummaryEvent setValue:@([self fuzzyDaysSinceDate:[self _onQueueLastSuccessfulClassCSyncDate]]) forKey:[NSString stringWithFormat:@"%@-%@", view.zoneName, CKKSLoggerDaysSinceLastSyncClassC]];
530 NSMutableArray* splunkRecords = failureRecords.mutableCopy;
531 [splunkRecords addObject:healthSummaryEvent];
533 NSDictionary* jsonDict = @{CKKSLoggerSplunkPostTime : @([now timeIntervalSince1970] * 1000), @"events" : splunkRecords};
535 return [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:error];
538 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
539 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler {
540 assert(completionHandler);
542 secnotice("ckks", "Splunk upload challenge");
543 NSURLCredential *cred = nil;
544 SecTrustResultType result = kSecTrustResultInvalid;
546 if ([challenge previousFailureCount] > 0) {
547 // Previous failures occurred, bail
548 completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
550 } else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
552 * Evaluate trust for the certificate
555 SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
557 OSStatus status = SecTrustEvaluate(serverTrust, &result);
558 if (status == errSecSuccess && (result == kSecTrustResultProceed || result == kSecTrustResultUnspecified)) {
560 * All is well, accept the credentials
563 cred = [NSURLCredential credentialForTrust:serverTrust];
564 completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
565 } else if (_allowInsecureSplunkCert) {
566 secnotice("ckks", "Force Accepting Splunk Credential");
568 cred = [NSURLCredential credentialForTrust:serverTrust];
569 completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
572 * An error occurred in evaluating trust, bail
574 completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
578 * Just perform the default handling
580 completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
585 - (BOOL)ignoreServerDisablingMessages
587 return _ignoreServersMessagesTellingUsToGoAway;
590 - (void)setIgnoreServerDisablingMessages:(BOOL)ignoreServer
592 _ignoreServersMessagesTellingUsToGoAway = ignoreServer ? YES : NO;
597 @implementation CKKSLoggerSQLiteStore
599 + (instancetype)sharedStore
601 static CKKSLoggerSQLiteStore* store = nil;
602 static dispatch_once_t onceToken;
603 dispatch_once(&onceToken, ^{
604 store = [[self alloc] initWithPath:CKKSLoggingTablePath() schema:CKKSLoggingTableSchema];
616 - (NSInteger)successCountForEventType:(NSString*)eventType
618 return [[[[self select:@[CKKSLoggerColumnSuccessCount] from:CKKSLoggerTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:CKKSLoggerColumnSuccessCount] integerValue];
621 - (void)incrementSuccessCountForEventType:(NSString*)eventType
624 NSInteger successCount = [self successCountForEventType:eventType];
625 NSInteger failureCount = [self failureCountForEventType:eventType];
626 [self insertOrReplaceInto:CKKSLoggerTableSuccessCount values:@{CKKSLoggerColumnEventType : eventType, CKKSLoggerColumnSuccessCount : @(successCount + 1), CKKSLoggerColumnFailureCount : @(failureCount)}];
628 secerror("incrementSuccessCountForEventType exception: %@", ue);
632 - (NSInteger)failureCountForEventType:(NSString*)eventType
634 return [[[[self select:@[CKKSLoggerColumnFailureCount] from:CKKSLoggerTableSuccessCount where:@"event_type = ?" bindings:@[eventType]] firstObject] valueForKey:CKKSLoggerColumnFailureCount] integerValue];
637 - (void)incrementFailureCountForEventType:(NSString*)eventType
640 NSInteger successCount = [self successCountForEventType:eventType];
641 NSInteger failureCount = [self failureCountForEventType:eventType];
642 [self insertOrReplaceInto:CKKSLoggerTableSuccessCount values:@{CKKSLoggerColumnEventType : eventType, CKKSLoggerColumnSuccessCount : @(successCount), CKKSLoggerColumnFailureCount : @(failureCount + 1)}];
644 secerror("incrementFailureCountForEventType exception: %@", ue);
648 - (NSDictionary*)summaryCounts
650 NSMutableDictionary* successCountsDict = [NSMutableDictionary dictionary];
651 NSArray* rows = [self selectAllFrom:CKKSLoggerTableSuccessCount where:nil bindings:nil];
652 for (NSDictionary* rowDict in rows) {
653 successCountsDict[rowDict[CKKSLoggerColumnEventType]] = @{CKKSLoggerColumnSuccessCount : rowDict[CKKSLoggerColumnSuccessCount], CKKSLoggerColumnFailureCount : rowDict[CKKSLoggerColumnFailureCount]};
656 return successCountsDict;
659 - (NSArray*)failureRecords
661 NSArray* recordBlobs = [self select:@[CKKSLoggerColumnData] from:CKKSLoggerTableFailures];
663 NSMutableArray* failureRecords = [[NSMutableArray alloc] init];
664 for (NSDictionary* row in recordBlobs) {
665 NSDictionary* deserializedRecord = [NSPropertyListSerialization propertyListWithData:row[CKKSLoggerColumnData] options:0 format:nil error:nil];
666 [failureRecords addObject:deserializedRecord];
669 return failureRecords;
672 - (void)addFailureRecord:(NSDictionary*)valueDict
675 NSError* error = nil;
676 NSData* serializedRecord = [NSPropertyListSerialization dataWithPropertyList:valueDict format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error];
677 if(!error && serializedRecord) {
678 [self insertOrReplaceInto:CKKSLoggerTableFailures values:@{CKKSLoggerColumnData : serializedRecord}];
680 if(error && !serializedRecord) {
681 secerror("Couldn't serialize failure record: %@", error);
684 secerror("addFailureRecord exception: %@", ue);
688 - (NSDate*)uploadDate
690 return [self datePropertyForKey:CKKSLoggerUploadDate];
693 - (void)setUploadDate:(NSDate*)uploadDate
695 [self setDateProperty:uploadDate forKey:CKKSLoggerUploadDate];
700 [self deleteFrom:CKKSLoggerTableSuccessCount where:@"event_type like ?" bindings:@[@"%"]];
701 [self deleteFrom:CKKSLoggerTableFailures where:@"id >= 0" bindings:nil];