Skip to content

Commit

Permalink
Merge pull request #22 from mapbox/events-profiling
Browse files Browse the repository at this point in the history
extend event logging for debugging purposes
  • Loading branch information
rclee authored Nov 9, 2017
2 parents afc8f6a + d359ab8 commit ca193a4
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 47 deletions.
10 changes: 10 additions & 0 deletions MapboxMobileEvents/MMEConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ extern NSString * const MMEAPIClientHeaderFieldContentEncodingKey;
extern NSString * const MMEAPIClientHTTPMethodPost;
extern NSString * const MMEErrorDomain;

// Debug types
extern NSString * const MMEDebugEventTypeFlush;
extern NSString * const MMEDebugEventTypePush;
extern NSString * const MMEDebugEventTypePost;
extern NSString * const MMEDebugEventTypeTurnstile;
extern NSString * const MMEDebugEventTypeBackgroundTask;
extern NSString * const MMEDebugEventTypeMetricCollection;
extern NSString * const MMEDebugEventTypeLocationManager;

// Event types
extern NSString * const MMEEventTypeAppUserTurnstile;
extern NSString * const MMEEventTypeMapLoad;
Expand Down Expand Up @@ -60,6 +69,7 @@ extern NSString * const MMEEventTypeNavigationArrive;
extern NSString * const MMEEventTypeNavigationCancel;
extern NSString * const MMEEventTypeNavigationFeedback;
extern NSString * const MMEEventTypeNavigationReroute;
extern NSString * const MMEDebugEventType;

// SDK event source
extern NSString * const MMEEventSource;
Expand Down
9 changes: 9 additions & 0 deletions MapboxMobileEvents/MMEConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
NSString * const MMEAPIClientHTTPMethodPost = @"POST";
NSString * const MMEErrorDomain = @"MMEErrorDomain";

NSString * const MMEDebugEventTypeFlush = @"flush";
NSString * const MMEDebugEventTypePush = @"push";
NSString * const MMEDebugEventTypePost = @"post";
NSString * const MMEDebugEventTypeTurnstile = @"turnstile";
NSString * const MMEDebugEventTypeBackgroundTask = @"backgroundTask";
NSString * const MMEDebugEventTypeMetricCollection = @"metricCollection";
NSString * const MMEDebugEventTypeLocationManager = @"locationManager";

NSString * const MMEEventTypeAppUserTurnstile = @"appUserTurnstile";
NSString * const MMEEventTypeMapLoad = @"map.load";
NSString * const MMEEventTypeMapTap = @"map.click";
Expand Down Expand Up @@ -59,6 +67,7 @@
NSString * const MMEEventTypeNavigationCancel = @"navigation.cancel";
NSString * const MMEEventTypeNavigationFeedback = @"navigation.feedback";
NSString * const MMEEventTypeNavigationReroute = @"navigation.reroute";
NSString * const MMEDebugEventType = @"debug.type";

NSString * const MMEEventSource = @"mapbox";

Expand Down
8 changes: 5 additions & 3 deletions MapboxMobileEvents/MMEEventLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ NS_ASSUME_NONNULL_BEGIN

@interface MMEEventLogger : NSObject

+ (BOOL)isEnabled;
+ (void)setEnabled:(BOOL)enabled;
+ (void)logEvent:(MMEEvent *)event;
@property (nonatomic, getter=isEnabled) BOOL enabled;

+ (instancetype)sharedLogger;

- (void)logEvent:(MMEEvent *)event;

@end

Expand Down
67 changes: 59 additions & 8 deletions MapboxMobileEvents/MMEEventLogger.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
#import "MMEEventLogger.h"

@interface MMEEventLogger()

@property (nonatomic, copy) NSString *dateForDebugLogFile;
@property (nonatomic) NSFileManager *fileManager;
@property (nonatomic) dispatch_queue_t debugLogSerialQueue;

@end

@implementation MMEEventLogger

static BOOL _enabled;
+ (instancetype)sharedLogger {
static MMEEventLogger *_sharedLogger;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
_sharedLogger = [[MMEEventLogger alloc] init];
});

return _sharedLogger;
}

+ (void)logEvent:(MMEEvent *)event {
if (_enabled) {
- (void)logEvent:(MMEEvent *)event {
if (self.isEnabled) {
NSLog(@"%@", [NSString stringWithFormat:@"Mapbox Telemetry event %@", event]);

[self writeEventToLocalDebugLog:event];
}
}

+ (BOOL)isEnabled {
return _enabled;
- (void)writeEventToLocalDebugLog:(MMEEvent *)event {
if (!self.isEnabled) {
return;
}

if (!self.dateForDebugLogFile) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
self.dateForDebugLogFile = [dateFormatter stringFromDate:[NSDate date]];
}

if (!self.debugLogSerialQueue) {
NSString *uniqueID = [[NSProcessInfo processInfo] globallyUniqueString];
NSString *appBundleID = [[NSBundle mainBundle] bundleIdentifier];
self.debugLogSerialQueue = dispatch_queue_create([[NSString stringWithFormat:@"%@.%@.events.debugLog", appBundleID, uniqueID] UTF8String], DISPATCH_QUEUE_SERIAL);
}

dispatch_async(self.debugLogSerialQueue, ^{
if ([NSJSONSerialization isValidJSONObject:event]) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:event options:NSJSONWritingPrettyPrinted error:nil];

NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByAppendingString:@",\n"];

NSString *logFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"telemetry_log-%@.json", self.dateForDebugLogFile]];

NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:logFilePath]) {
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
} else {
[fileManager createFileAtPath:logFilePath contents:[jsonString dataUsingEncoding:NSUTF8StringEncoding] attributes:@{ NSFileProtectionKey: NSFileProtectionCompleteUntilFirstUserAuthentication }];
}
}
});
}

+ (void)setEnabled:(BOOL)enabled {
_enabled = enabled;
}

@end
Loading

0 comments on commit ca193a4

Please sign in to comment.