Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2021 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#import "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRCrashlyticsReport.h"
16
 
17
#import "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
18
#import "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
19
#import "Crashlytics/Crashlytics/Helpers/FIRCLSLogger.h"
20
#import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
21
 
22
@interface FIRCrashlyticsReport () {
23
  NSString *_reportID;
24
  NSDate *_dateCreated;
25
  BOOL _hasCrash;
26
 
27
  FIRCLSUserLoggingABStorage _logStorage;
28
  const char *_activeLogPath;
29
 
30
  uint32_t _internalKVCounter;
31
  FIRCLSUserLoggingKVStorage _internalKVStorage;
32
 
33
  uint32_t _userKVCounter;
34
  FIRCLSUserLoggingKVStorage _userKVStorage;
35
}
36
 
37
@property(nonatomic, strong) FIRCLSInternalReport *internalReport;
38
 
39
@end
40
 
41
@implementation FIRCrashlyticsReport
42
 
43
- (instancetype)initWithInternalReport:(FIRCLSInternalReport *)internalReport {
44
  self = [super init];
45
  if (!self) {
46
    return nil;
47
  }
48
 
49
  _internalReport = internalReport;
50
  _reportID = [[internalReport identifier] copy];
51
  _dateCreated = [[internalReport dateCreated] copy];
52
  _hasCrash = [internalReport isCrash];
53
 
54
  _logStorage.maxSize = _firclsContext.readonly->logging.logStorage.maxSize;
55
  _logStorage.maxEntries = _firclsContext.readonly->logging.logStorage.maxEntries;
56
  _logStorage.restrictBySize = _firclsContext.readonly->logging.logStorage.restrictBySize;
57
  _logStorage.entryCount = _firclsContext.readonly->logging.logStorage.entryCount;
58
  _logStorage.aPath = [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportLogAFile
59
                                                        inInternalReport:internalReport];
60
  _logStorage.bPath = [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportLogBFile
61
                                                        inInternalReport:internalReport];
62
 
63
  _activeLogPath = _logStorage.aPath;
64
 
65
  // TODO: correct kv accounting
66
  // The internal report will have non-zero compacted and incremental keys. The right thing to do
67
  // is count them, so we can kick off compactions/pruning at the right times. By
68
  // setting this value to zero, we're allowing more entries to be made than there really
69
  // should be. Not the end of the world, but we should do better eventually.
70
  _internalKVCounter = 0;
71
  _userKVCounter = 0;
72
 
73
  _userKVStorage.maxCount = _firclsContext.readonly->logging.userKVStorage.maxCount;
74
  _userKVStorage.maxIncrementalCount =
75
      _firclsContext.readonly->logging.userKVStorage.maxIncrementalCount;
76
  _userKVStorage.compactedPath =
77
      [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportUserCompactedKVFile
78
                                        inInternalReport:internalReport];
79
  _userKVStorage.incrementalPath =
80
      [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportUserIncrementalKVFile
81
                                        inInternalReport:internalReport];
82
 
83
  _internalKVStorage.maxCount = _firclsContext.readonly->logging.internalKVStorage.maxCount;
84
  _internalKVStorage.maxIncrementalCount =
85
      _firclsContext.readonly->logging.internalKVStorage.maxIncrementalCount;
86
  _internalKVStorage.compactedPath =
87
      [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportInternalCompactedKVFile
88
                                        inInternalReport:internalReport];
89
  _internalKVStorage.incrementalPath =
90
      [FIRCrashlyticsReport filesystemPathForContentFile:FIRCLSReportInternalIncrementalKVFile
91
                                        inInternalReport:internalReport];
92
 
93
  return self;
94
}
95
 
96
+ (const char *)filesystemPathForContentFile:(NSString *)contentFile
97
                            inInternalReport:(FIRCLSInternalReport *)internalReport {
98
  if (!internalReport) {
99
    return nil;
100
  }
101
 
102
  // We need to be defensive because strdup will crash
103
  // if given a nil.
104
  NSString *objCString = [internalReport pathForContentFile:contentFile];
105
  const char *fileSystemString = [objCString fileSystemRepresentation];
106
  if (!objCString || !fileSystemString) {
107
    return nil;
108
  }
109
 
110
  // Paths need to be duplicated because fileSystemRepresentation returns C strings
111
  // that are freed outside of this context.
112
  return strdup(fileSystemString);
113
}
114
 
115
- (BOOL)checkContextForMethod:(NSString *)methodName {
116
  if (!FIRCLSContextIsInitialized()) {
117
    FIRCLSErrorLog(@"%@ failed for FIRCrashlyticsReport because Crashlytics context isn't "
118
                   @"initialized.",
119
                   methodName);
120
    return false;
121
  }
122
  return true;
123
}
124
 
125
#pragma mark - API: Getters
126
 
127
- (NSString *)reportID {
128
  return _reportID;
129
}
130
 
131
- (NSDate *)dateCreated {
132
  return _dateCreated;
133
}
134
 
135
- (BOOL)hasCrash {
136
  return _hasCrash;
137
}
138
 
139
#pragma mark - API: Logging
140
 
141
- (void)log:(NSString *)msg {
142
  if (![self checkContextForMethod:@"log:"]) {
143
    return;
144
  }
145
 
146
  FIRCLSLogToStorage(&_logStorage, &_activeLogPath, @"%@", msg);
147
}
148
 
149
- (void)logWithFormat:(NSString *)format, ... {
150
  if (![self checkContextForMethod:@"logWithFormat:"]) {
151
    return;
152
  }
153
 
154
  va_list args;
155
  va_start(args, format);
156
  [self logWithFormat:format arguments:args];
157
  va_end(args);
158
}
159
 
160
- (void)logWithFormat:(NSString *)format arguments:(va_list)args {
161
  if (![self checkContextForMethod:@"logWithFormat:arguments:"]) {
162
    return;
163
  }
164
 
165
  [self log:[[NSString alloc] initWithFormat:format arguments:args]];
166
}
167
 
168
#pragma mark - API: setUserID
169
 
170
- (void)setUserID:(nullable NSString *)userID {
171
  if (![self checkContextForMethod:@"setUserID:"]) {
172
    return;
173
  }
174
 
175
  FIRCLSUserLoggingRecordKeyValue(FIRCLSUserIdentifierKey, userID, &_internalKVStorage,
176
                                  &_internalKVCounter);
177
}
178
 
179
#pragma mark - API: setCustomValue
180
 
181
- (void)setCustomValue:(nullable id)value forKey:(NSString *)key {
182
  if (![self checkContextForMethod:@"setCustomValue:forKey:"]) {
183
    return;
184
  }
185
 
186
  FIRCLSUserLoggingRecordKeyValue(key, value, &_userKVStorage, &_userKVCounter);
187
}
188
 
189
- (void)setCustomKeysAndValues:(NSDictionary *)keysAndValues {
190
  if (![self checkContextForMethod:@"setCustomKeysAndValues:"]) {
191
    return;
192
  }
193
 
194
  FIRCLSUserLoggingRecordKeysAndValues(keysAndValues, &_userKVStorage, &_userKVCounter);
195
}
196
 
197
@end