-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from mapbox/events-profiling
extend event logging for debugging purposes
- Loading branch information
Showing
6 changed files
with
152 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.