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/Controllers/FIRCLSAnalyticsManager.h"
|
|
|
16 |
|
|
|
17 |
#import "Crashlytics/Crashlytics/Components/FIRCLSUserLogging.h"
|
|
|
18 |
#import "Crashlytics/Crashlytics/Helpers/FIRCLSInternalLogging.h"
|
|
|
19 |
|
|
|
20 |
#import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
|
|
|
21 |
#import "Interop/Analytics/Public/FIRAnalyticsInteropListener.h"
|
|
|
22 |
|
|
|
23 |
static NSString *FIRCLSFirebaseAnalyticsEventLogFormat = @"$A$:%@";
|
|
|
24 |
|
|
|
25 |
// Origin for events and user properties generated by Crashlytics.
|
|
|
26 |
static NSString *const kFIREventOriginCrash = @"clx";
|
|
|
27 |
|
|
|
28 |
// App exception event name.
|
|
|
29 |
static NSString *const kFIREventAppException = @"_ae";
|
|
|
30 |
|
|
|
31 |
// Timestamp key for the event payload.
|
|
|
32 |
static NSString *const kFIRParameterTimestamp = @"timestamp";
|
|
|
33 |
|
|
|
34 |
// Fatal key for the event payload.
|
|
|
35 |
static NSString *const kFIRParameterFatal = @"fatal";
|
|
|
36 |
|
|
|
37 |
FOUNDATION_STATIC_INLINE NSNumber *timeIntervalInMillis(NSTimeInterval timeInterval) {
|
|
|
38 |
return @(llrint(timeInterval * 1000.0));
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
@interface FIRCLSAnalyticsManager () <FIRAnalyticsInteropListener>
|
|
|
42 |
|
|
|
43 |
@property(nonatomic, strong) id<FIRAnalyticsInterop> analytics;
|
|
|
44 |
|
|
|
45 |
@property(nonatomic, assign) BOOL registeredAnalyticsEventListener;
|
|
|
46 |
|
|
|
47 |
@end
|
|
|
48 |
|
|
|
49 |
@implementation FIRCLSAnalyticsManager
|
|
|
50 |
|
|
|
51 |
- (instancetype)initWithAnalytics:(nullable id<FIRAnalyticsInterop>)analytics {
|
|
|
52 |
self = [super init];
|
|
|
53 |
if (!self) {
|
|
|
54 |
return nil;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
_analytics = analytics;
|
|
|
58 |
|
|
|
59 |
return self;
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
- (void)registerAnalyticsListener {
|
|
|
63 |
if (self.registeredAnalyticsEventListener) {
|
|
|
64 |
return;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if (self.analytics == nil) {
|
|
|
68 |
FIRCLSDeveloperLog(@"Crashlytics:Crash:Reports:Event",
|
|
|
69 |
"Firebase Analytics SDK not detected. Crash-free statistics and "
|
|
|
70 |
"breadcrumbs will not be reported");
|
|
|
71 |
return;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
[self.analytics registerAnalyticsListener:self withOrigin:kFIREventOriginCrash];
|
|
|
75 |
|
|
|
76 |
FIRCLSDeveloperLog(@"Crashlytics:Crash:Reports:Event",
|
|
|
77 |
"Registered Firebase Analytics event listener to receive breadcrumb logs");
|
|
|
78 |
|
|
|
79 |
self.registeredAnalyticsEventListener = YES;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
- (void)messageTriggered:(NSString *)name parameters:(NSDictionary *)parameters {
|
|
|
83 |
NSDictionary *event = @{
|
|
|
84 |
@"name" : name,
|
|
|
85 |
@"parameters" : parameters,
|
|
|
86 |
};
|
|
|
87 |
NSString *json = FIRCLSFIRAEventDictionaryToJSON(event);
|
|
|
88 |
if (json != nil) {
|
|
|
89 |
FIRCLSLog(FIRCLSFirebaseAnalyticsEventLogFormat, json);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
+ (void)logCrashWithTimeStamp:(NSTimeInterval)crashTimeStamp
|
|
|
94 |
toAnalytics:(id<FIRAnalyticsInterop>)analytics {
|
|
|
95 |
if (analytics == nil) {
|
|
|
96 |
return;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
FIRCLSDeveloperLog(@"Crashlytics:Crash:Reports:Event",
|
|
|
100 |
"Sending app_exception event to Firebase Analytics for crash-free statistics");
|
|
|
101 |
|
|
|
102 |
NSDictionary *params = @{
|
|
|
103 |
kFIRParameterTimestamp : timeIntervalInMillis(crashTimeStamp),
|
|
|
104 |
kFIRParameterFatal : @(INT64_C(1))
|
|
|
105 |
};
|
|
|
106 |
|
|
|
107 |
[analytics logEventWithOrigin:kFIREventOriginCrash name:kFIREventAppException parameters:params];
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
NSString *FIRCLSFIRAEventDictionaryToJSON(NSDictionary *eventAsDictionary) {
|
|
|
111 |
NSError *error = nil;
|
|
|
112 |
|
|
|
113 |
if (eventAsDictionary == nil) {
|
|
|
114 |
return nil;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
if (![NSJSONSerialization isValidJSONObject:eventAsDictionary]) {
|
|
|
118 |
FIRCLSSDKLog("Firebase Analytics event is not valid JSON");
|
|
|
119 |
return nil;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:eventAsDictionary
|
|
|
123 |
options:0
|
|
|
124 |
error:&error];
|
|
|
125 |
|
|
|
126 |
if (error == nil) {
|
|
|
127 |
NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
128 |
return json;
|
|
|
129 |
} else {
|
|
|
130 |
FIRCLSSDKLog("Unable to convert Firebase Analytics event to json");
|
|
|
131 |
return nil;
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
@end
|