|
15 | 15 | */
|
16 | 16 |
|
17 | 17 | #import "GDLLogStorage.h"
|
| 18 | +#import "GDLLogStorage_Private.h" |
| 19 | + |
| 20 | +#import <GoogleDataLogger/GDLLogPrioritizer.h> |
| 21 | + |
| 22 | +#import "GDLConsoleLogger.h" |
| 23 | +#import "GDLLogEvent_Private.h" |
| 24 | +#import "GDLRegistrar_Private.h" |
| 25 | +#import "GDLUploader.h" |
| 26 | + |
| 27 | +/** Creates and/or returns a singleton NSString that is the shared logging path. |
| 28 | + * |
| 29 | + * @return The SDK logging path. |
| 30 | + */ |
| 31 | +static NSString *GDLStoragePath() { |
| 32 | + static NSString *archivePath; |
| 33 | + static dispatch_once_t onceToken; |
| 34 | + dispatch_once(&onceToken, ^{ |
| 35 | + NSString *cachePath = |
| 36 | + NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; |
| 37 | + archivePath = [NSString stringWithFormat:@"%@/google-sdks-logs", cachePath]; |
| 38 | + }); |
| 39 | + return archivePath; |
| 40 | +} |
18 | 41 |
|
19 | 42 | @implementation GDLLogStorage
|
20 | 43 |
|
| 44 | ++ (instancetype)sharedInstance { |
| 45 | + static GDLLogStorage *sharedStorage; |
| 46 | + static dispatch_once_t onceToken; |
| 47 | + dispatch_once(&onceToken, ^{ |
| 48 | + sharedStorage = [[GDLLogStorage alloc] init]; |
| 49 | + }); |
| 50 | + return sharedStorage; |
| 51 | +} |
| 52 | + |
| 53 | +- (instancetype)init { |
| 54 | + self = [super init]; |
| 55 | + if (self) { |
| 56 | + _storageQueue = dispatch_queue_create("com.google.GDLLogStorage", DISPATCH_QUEUE_SERIAL); |
| 57 | + _logHashToLogFile = [[NSMutableDictionary alloc] init]; |
| 58 | + _logTargetToLogFileSet = [[NSMutableDictionary alloc] init]; |
| 59 | + } |
| 60 | + return self; |
| 61 | +} |
| 62 | + |
| 63 | +- (void)storeLog:(GDLLogEvent *)log { |
| 64 | + [self createLogDirectoryIfNotExists]; |
| 65 | + |
| 66 | + // This is done to ensure that log is deallocated at the end of the ensuing block. |
| 67 | + __block GDLLogEvent *shortLivedLog = log; |
| 68 | + __weak GDLLogEvent *weakShortLivedLog = log; |
| 69 | + log = nil; |
| 70 | + |
| 71 | + dispatch_async(_storageQueue, ^{ |
| 72 | + // Check that a backend implementation is available for this logTarget. |
| 73 | + NSInteger logTarget = shortLivedLog.logTarget; |
| 74 | + |
| 75 | + // Check that a log prioritizer is available for this logTarget. |
| 76 | + id<GDLLogPrioritizer> logPrioritizer = |
| 77 | + [GDLRegistrar sharedInstance].logTargetToPrioritizer[@(logTarget)]; |
| 78 | + NSAssert(logPrioritizer, @"There's no scorer registered for the given logTarget."); |
| 79 | + |
| 80 | + // Write the extension bytes to disk, get a filename. |
| 81 | + NSAssert(shortLivedLog.extensionBytes, @"The log should have been serialized to bytes"); |
| 82 | + NSAssert(shortLivedLog.extension == nil, @"The original log proto should be removed"); |
| 83 | + NSURL *logFile = |
| 84 | + [self saveLogProtoToDisk:shortLivedLog.extensionBytes logHash:shortLivedLog.hash]; |
| 85 | + |
| 86 | + // Add log to tracking collections. |
| 87 | + [self addLogToTrackingCollections:shortLivedLog logFile:logFile]; |
| 88 | + |
| 89 | + // Check the QoS, if it's high priority, notify the log target that it has a high priority log. |
| 90 | + if (shortLivedLog.qosTier == GDLLogQoSFast) { |
| 91 | + NSSet<NSURL *> *allLogsForLogTarget = self.logTargetToLogFileSet[@(logTarget)]; |
| 92 | + [[GDLUploader sharedInstance] forceUploadLogs:allLogsForLogTarget target:logTarget]; |
| 93 | + } |
| 94 | + |
| 95 | + // Have the prioritizer prioritize the log, enforcing that they do not retain it. |
| 96 | + @autoreleasepool { |
| 97 | + [logPrioritizer prioritizeLog:shortLivedLog]; |
| 98 | + shortLivedLog = nil; |
| 99 | + } |
| 100 | + if (weakShortLivedLog) { |
| 101 | + GDLLogError(GDLMCELogEventWasIllegallyRetained, @"%@", |
| 102 | + @"A LogEvent should not be retained outside of storage."); |
| 103 | + }; |
| 104 | + }); |
| 105 | +} |
| 106 | + |
| 107 | +- (void)removeLog:(NSNumber *)logHash logTarget:(NSNumber *)logTarget { |
| 108 | + dispatch_async(_storageQueue, ^{ |
| 109 | + NSURL *logFile = self.logHashToLogFile[logHash]; |
| 110 | + |
| 111 | + // Remove from disk, first and foremost. |
| 112 | + NSError *error; |
| 113 | + [[NSFileManager defaultManager] removeItemAtURL:logFile error:&error]; |
| 114 | + NSAssert(error == nil, @"There was an error removing a logFile: %@", error); |
| 115 | + |
| 116 | + // Remove from the tracking collections. |
| 117 | + [self.logHashToLogFile removeObjectForKey:logHash]; |
| 118 | + NSMutableSet<NSURL *> *logFiles = self.logTargetToLogFileSet[logTarget]; |
| 119 | + NSAssert(logFiles, @"There wasn't a logSet for this logTarget."); |
| 120 | + [logFiles removeObject:logFile]; |
| 121 | + // It's fine to not remove the set if it's empty. |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +#pragma mark - Private helper methods |
| 126 | + |
| 127 | +/** Creates the log directory if it does not exist. */ |
| 128 | +- (void)createLogDirectoryIfNotExists { |
| 129 | + NSError *error; |
| 130 | + BOOL result = [[NSFileManager defaultManager] createDirectoryAtPath:GDLStoragePath() |
| 131 | + withIntermediateDirectories:YES |
| 132 | + attributes:0 |
| 133 | + error:&error]; |
| 134 | + if (!result || error) { |
| 135 | + GDLLogError(GDLMCEDirectoryCreationError, @"Error creating the directory: %@", error); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** Saves the log's extensionBytes to a file using NSData mechanisms. |
| 140 | + * |
| 141 | + * @note This method should only be called from a method within a block on _storageQueue to maintain |
| 142 | + * thread safety. |
| 143 | + * |
| 144 | + * @param logProtoBytes The extensionBytes of the log, presumably proto bytes. |
| 145 | + * @param logHash The hash value of the log. |
| 146 | + * @return The filename |
| 147 | + */ |
| 148 | +- (NSURL *)saveLogProtoToDisk:(NSData *)logProtoBytes logHash:(NSUInteger)logHash { |
| 149 | + NSString *storagePath = GDLStoragePath(); |
| 150 | + NSString *logFile = [NSString stringWithFormat:@"log-%lu", (unsigned long)logHash]; |
| 151 | + NSURL *logFilePath = [NSURL fileURLWithPath:[storagePath stringByAppendingPathComponent:logFile]]; |
| 152 | + |
| 153 | + BOOL writingSuccess = [logProtoBytes writeToURL:logFilePath atomically:YES]; |
| 154 | + if (!writingSuccess) { |
| 155 | + GDLLogError(GDLMCEFileWriteError, @"A log file could not be written: %@", logFilePath); |
| 156 | + } |
| 157 | + |
| 158 | + return logFilePath; |
| 159 | +} |
| 160 | + |
| 161 | +/** Adds the log to internal collections in order to help track the log. |
| 162 | + * |
| 163 | + * @note This method should only be called from a method within a block on _storageQueue to maintain |
| 164 | + * thread safety. |
| 165 | + * |
| 166 | + * @param log The log to track. |
| 167 | + * @param logFile The file the log has been saved to. |
| 168 | + */ |
| 169 | +- (void)addLogToTrackingCollections:(GDLLogEvent *)log logFile:(NSURL *)logFile { |
| 170 | + NSInteger logTarget = log.logTarget; |
| 171 | + self.logHashToLogFile[@(log.hash)] = logFile; |
| 172 | + NSMutableSet<NSURL *> *logs = self.logTargetToLogFileSet[@(logTarget)]; |
| 173 | + if (logs) { |
| 174 | + [logs addObject:logFile]; |
| 175 | + } else { |
| 176 | + NSMutableSet<NSURL *> *logSet = [NSMutableSet setWithObject:logFile]; |
| 177 | + self.logTargetToLogFileSet[@(logTarget)] = logSet; |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +#pragma mark - NSSecureCoding |
| 182 | + |
| 183 | ++ (BOOL)supportsSecureCoding { |
| 184 | + return YES; |
| 185 | +} |
| 186 | + |
| 187 | +- (instancetype)initWithCoder:(NSCoder *)aDecoder { |
| 188 | + // TODO |
| 189 | + return [self.class sharedInstance]; |
| 190 | +} |
| 191 | + |
| 192 | +- (void)encodeWithCoder:(NSCoder *)aCoder { |
| 193 | + // TODO |
| 194 | +} |
| 195 | + |
21 | 196 | @end
|
0 commit comments