|
| 1 | +/* |
| 2 | + * Copyright 2020 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import "FIRCLSRecordAdapter.h" |
| 18 | + |
| 19 | +#import "FIRCLSInternalReport.h" |
| 20 | +#import "FIRCLSLogger.h" |
| 21 | +#import "FIRCLSRecordApplication.h" |
| 22 | +#import "FIRCLSRecordBinaryImage.h" |
| 23 | +#import "FIRCLSRecordExecutable.h" |
| 24 | +#import "FIRCLSRecordHost.h" |
| 25 | +#import "FIRCLSRecordIdentity.h" |
| 26 | +#import "FIRCLSRecordKeyValue.h" |
| 27 | +#import "FIRCLSRecordProcessStats.h" |
| 28 | +#import "FIRCLSRecordRegister.h" |
| 29 | +#import "FIRCLSRecordRuntime.h" |
| 30 | +#import "FIRCLSRecordSignal.h" |
| 31 | +#import "FIRCLSRecordStorage.h" |
| 32 | +#import "FIRCLSRecordThread.h" |
| 33 | + |
| 34 | +@interface FIRCLSRecordAdapter () |
| 35 | + |
| 36 | +@property(nonatomic, strong) NSString *folderPath; |
| 37 | + |
| 38 | +@property(nonatomic, strong) FIRCLSRecordSignal *signal; |
| 39 | +@property(nonatomic, strong) NSArray<FIRCLSRecordThread *> *threads; |
| 40 | +@property(nonatomic, strong) FIRCLSRecordProcessStats *processStats; |
| 41 | +@property(nonatomic, strong) FIRCLSRecordStorage *storage; |
| 42 | +@property(nonatomic, strong) NSArray<FIRCLSRecordBinaryImage *> *binaryImages; |
| 43 | +@property(nonatomic, strong) FIRCLSRecordRuntime *runtime; |
| 44 | +@property(nonatomic, strong) FIRCLSRecordIdentity *identity; |
| 45 | +@property(nonatomic, strong) FIRCLSRecordHost *host; |
| 46 | +@property(nonatomic, strong) FIRCLSRecordApplication *application; |
| 47 | +@property(nonatomic, strong) FIRCLSRecordExecutable *executable; |
| 48 | +@property(nonatomic, strong) NSArray<FIRCLSRecordKeyValue *> *keyValues; |
| 49 | + |
| 50 | +@end |
| 51 | + |
| 52 | +@implementation FIRCLSRecordAdapter |
| 53 | + |
| 54 | +- (instancetype)initWithPath:(NSString *)folderPath { |
| 55 | + self = [super init]; |
| 56 | + if (self) { |
| 57 | + _folderPath = folderPath; |
| 58 | + |
| 59 | + [self loadBinaryImagesFile]; |
| 60 | + [self loadMetaDataFile]; |
| 61 | + [self loadSignalFile]; |
| 62 | + [self loadKeyValuesFile]; |
| 63 | + } |
| 64 | + return self; |
| 65 | +} |
| 66 | + |
| 67 | +- (void)loadBinaryImagesFile { |
| 68 | + NSString *path = [self.folderPath stringByAppendingPathComponent:CLSReportBinaryImageFile]; |
| 69 | + self.binaryImages = [FIRCLSRecordBinaryImage |
| 70 | + binaryImagesFromDictionaries:[FIRCLSRecordAdapter dictionariesFromEachLineOfFile:path]]; |
| 71 | +} |
| 72 | + |
| 73 | +- (void)loadMetaDataFile { |
| 74 | + NSString *path = [self.folderPath stringByAppendingPathComponent:CLSReportMetadataFile]; |
| 75 | + NSDictionary *dict = [FIRCLSRecordAdapter combinedDictionariesFromFilePath:path]; |
| 76 | + |
| 77 | + self.identity = [[FIRCLSRecordIdentity alloc] initWithDict:dict[@"identity"]]; |
| 78 | + self.host = [[FIRCLSRecordHost alloc] initWithDict:dict[@"host"]]; |
| 79 | + self.application = [[FIRCLSRecordApplication alloc] initWithDict:dict[@"application"]]; |
| 80 | + self.executable = [[FIRCLSRecordExecutable alloc] initWithDict:dict[@"executable"]]; |
| 81 | +} |
| 82 | + |
| 83 | +- (void)loadSignalFile { |
| 84 | + NSString *path = [self.folderPath stringByAppendingPathComponent:CLSReportSignalFile]; |
| 85 | + NSDictionary *dicts = [FIRCLSRecordAdapter combinedDictionariesFromFilePath:path]; |
| 86 | + |
| 87 | + self.signal = [[FIRCLSRecordSignal alloc] initWithDict:dicts[@"signal"]]; |
| 88 | + self.runtime = [[FIRCLSRecordRuntime alloc] initWithDict:dicts[@"runtime"]]; |
| 89 | + self.processStats = [[FIRCLSRecordProcessStats alloc] initWithDict:dicts[@"process_stats"]]; |
| 90 | + self.storage = [[FIRCLSRecordStorage alloc] initWithDict:dicts[@"storage"]]; |
| 91 | + |
| 92 | + // The thread's objc_selector_name is set with the runtime's info |
| 93 | + self.threads = [FIRCLSRecordThread threadsFromDictionaries:dicts[@"threads"] |
| 94 | + withNames:dicts[@"thread_names"] |
| 95 | + withRuntime:self.runtime]; |
| 96 | +} |
| 97 | + |
| 98 | +- (void)loadKeyValuesFile { |
| 99 | + NSString *path = |
| 100 | + [self.folderPath stringByAppendingPathComponent:CLSReportInternalIncrementalKVFile]; |
| 101 | + self.keyValues = [FIRCLSRecordKeyValue |
| 102 | + keyValuesFromDictionaries:[FIRCLSRecordAdapter dictionariesFromEachLineOfFile:path]]; |
| 103 | +} |
| 104 | + |
| 105 | +/// Return the persisted crash file as a combined dictionary that way lookups can occur with a key |
| 106 | +/// (to avoid ordering dependency) |
| 107 | +/// @param filePath Persisted crash file path |
| 108 | ++ (NSDictionary *)combinedDictionariesFromFilePath:(NSString *)filePath { |
| 109 | + NSMutableDictionary *joinedDict = [[NSMutableDictionary alloc] init]; |
| 110 | + for (NSDictionary *dict in [self dictionariesFromEachLineOfFile:filePath]) { |
| 111 | + [joinedDict addEntriesFromDictionary:dict]; |
| 112 | + } |
| 113 | + return joinedDict; |
| 114 | +} |
| 115 | + |
| 116 | +/// The persisted crash files contains JSON on separate lines. Read each line and return the JSON |
| 117 | +/// data as a dictionary. |
| 118 | +/// @param filePath Persisted crash file path |
| 119 | ++ (NSArray<NSDictionary *> *)dictionariesFromEachLineOfFile:(NSString *)filePath { |
| 120 | + NSString *content = [[NSString alloc] initWithContentsOfFile:filePath |
| 121 | + encoding:NSUTF8StringEncoding |
| 122 | + error:nil]; |
| 123 | + NSArray *lines = |
| 124 | + [content componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]; |
| 125 | + |
| 126 | + NSMutableArray<NSDictionary *> *array = [[NSMutableArray<NSDictionary *> alloc] init]; |
| 127 | + |
| 128 | + int lineNum = 1; |
| 129 | + for (NSString *line in lines) { |
| 130 | + NSError *error; |
| 131 | + NSDictionary *dict = |
| 132 | + [NSJSONSerialization JSONObjectWithData:[line dataUsingEncoding:NSUTF8StringEncoding] |
| 133 | + options:0 |
| 134 | + error:&error]; |
| 135 | + |
| 136 | + if (error) { |
| 137 | + FIRCLSErrorLog(@"Failed to read JSON from file (%@) line (%d) with error: %@", filePath, |
| 138 | + lineNum, error); |
| 139 | + } else { |
| 140 | + [array addObject:dict]; |
| 141 | + } |
| 142 | + |
| 143 | + lineNum++; |
| 144 | + } |
| 145 | + |
| 146 | + return array; |
| 147 | +} |
| 148 | + |
| 149 | +@end |
0 commit comments