-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
extend event logging for debugging purposes #22
Changes from 1 commit
3498613
0e342b9
c180872
f14c3bc
1f2f1f9
d359ab8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ NS_ASSUME_NONNULL_BEGIN | |
+ (void)setEnabled:(BOOL)enabled; | ||
+ (void)logEvent:(MMEEvent *)event; | ||
|
||
- (void)writeEventToLocalDebugLog:(MMEEvent *)event; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we could remove this in the header file and make it an implementation detail. |
||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
#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; | ||
|
@@ -18,4 +26,44 @@ + (void)setEnabled:(BOOL)enabled { | |
_enabled = enabled; | ||
} | ||
|
||
- (void)writeEventToLocalDebugLog:(MMEEvent *)event { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can go ahead and combine this new method and the old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely like this approach better. Initially I had similar thoughts with combining the two methods. My only concern was potentially having too many logs written locally and that a majority of the debug logs will have the same It seems like we'll still need to have other keys for at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's go ahead and default to writing "too much" for now as a first pass. We can eventually introduce the notion of log levels to filter what actually gets printed and written. |
||
if (!_enabled) { | ||
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 }]; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could probably make
enabled
a property.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot that this class has so far only used class methods to invoke logging (e.g.
[MMEEventLogger logEvent:debugEvent]
). This PR introduces the notion of using an instance ofMMEEventLogger
inMMEEventsManager
.I think we should change the methods to make them instance based and add a singleton interface so that we can use a
[MMEEventLogger sharedLogger]
instance everywhere we want log messages to go to the same file with the same date. We can still create 1-off instances for other kinds of logging we may eventually want to do in different files.