Skip to content

Commit 45c872e

Browse files
Updated crash reporting and other minor changes
1 parent 9716968 commit 45c872e

2 files changed

Lines changed: 48 additions & 21 deletions

File tree

AppCenter/AppCenterSDK+HiDrive.swift

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fileprivate enum AppCenterConstants {
1717

1818
static let appSecretKey = "AppCenterAppSecret"
1919
static let smfLogUploadMaxSize : Int = 5000
20+
static let crashLogFileName = "SMFLogger.log"
2021
}
2122

2223
class AppCenterSDK: NSObject {
@@ -33,8 +34,7 @@ class AppCenterSDK: NSObject {
3334

3435
// MARK: - Private properties
3536

36-
fileprivate static var shared : AppCenterSDK?
37-
fileprivate var configuration : Configuration?
37+
fileprivate static var delegate : AppCenterSDKDelegate?
3838

3939
// MARK: - Public properties
4040

@@ -54,15 +54,17 @@ class AppCenterSDK: NSObject {
5454
return
5555
}
5656

57+
self.delegate = AppCenterSDKDelegate(isLogUploadEnabled: configuration.isLogUploadEnabled)
58+
5759
let services = (configuration.isDistributionEnabled == true) ? [MSCrashes.self, MSDistribute.self] : [MSCrashes.self]
5860

5961
MSAppCenter.start(configuration.appSecret, withServices: services)
60-
MSCrashes.setEnabled(configuration.enableCrashes)
61-
MSCrashes.setDelegate(shared)
62+
MSCrashes.setEnabled(configuration.isCrashReportEnabled)
63+
MSCrashes.setDelegate(self.delegate)
6264
}
6365

6466
/// Returns True, if and only if the Service got started and is enabled.
65-
var isDistributionEnabled: Bool {
67+
static var isDistributionEnabled: Bool {
6668
return MSDistribute.isEnabled()
6769
}
6870

@@ -76,10 +78,29 @@ class AppCenterSDK: NSObject {
7678
/// - Enable Distribution at a later time using the same method
7779
///
7880
/// - Parameter enabled: Enable or Disable Distribtion
79-
func enableDistribution(enabled: Bool = true) {
81+
static func enableDistribution(enabled: Bool = true) {
8082
MSDistribute.setEnabled(enabled)
8183
}
8284

85+
/// Returns True, if and only if Crash Reporting is enabled.
86+
static var isCrashReportingEnabled: Bool {
87+
return MSCrashes.isEnabled()
88+
}
89+
90+
/// Will enable or disable the sending od crash reports to AppCenter
91+
/// While disabling works always.
92+
/// For enabling the Setup (aka. start) - Method should be called before.
93+
///
94+
/// Flow in the App, for Apps that want to dynamically change that State:
95+
/// - Call the Setup Method for with `isDistributionEnabled` set to true.
96+
/// - Disable Distribution using `enableDistribution(enabled: false)`
97+
/// - Enable Distribution at a later time using the same method
98+
///
99+
/// - Parameter enabled: Enable or Disable Distribtion
100+
static func enableCrashReporting(enabled: Bool = true) {
101+
MSCrashes.setEnabled(enabled)
102+
}
103+
83104
/// This will create a `fatalError` to crash the app.
84105
static func performTestCrash() {
85106

@@ -94,15 +115,18 @@ extension AppCenterSDK {
94115
fileprivate var enableDebug : Bool
95116
fileprivate var appSecret : String
96117
fileprivate var isDistributionEnabled : Bool
97-
fileprivate var enableCrashes : Bool
118+
fileprivate var isCrashReportEnabled : Bool
119+
fileprivate var isLogUploadEnabled : Bool
98120

99121
/// Initializes a AppCenterSDK Configuration
100122
///
101123
/// - Parameters:
102124
/// - appSecret: supply this manually if you dont want it in the info.plist
103125
/// - enableDebug: Should start the Services even for Debug, Default is false
104-
/// - isDistributionEnabled: Should start the Distribution Service, Default is false for Debug and Live apps, else true,
105-
init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil, enableCrashes: Bool = true) {
126+
/// - isDistributionEnabled: Should start the Distribution Service, Default is false for Debug and Live apps, else true
127+
/// - isCrashReportEnabled: enables sending of crash reports, Default is true
128+
/// - isLogUploadEnabled: enables attachment of logs to crash reports, Default is true
129+
init(appSecret: String? = nil, enableDebug: Bool = false, isDistributionEnabled: Bool? = nil, isCrashReportEnabled: Bool = true, isLogUploadEnabled: Bool = true) {
106130

107131
let appSecretFromBundle = Bundle.main.object(forInfoDictionaryKey: AppCenterConstants.appSecretKey) as? String
108132

@@ -112,7 +136,8 @@ extension AppCenterSDK {
112136

113137
self.enableDebug = enableDebug
114138
self.appSecret = _appSecret
115-
self.enableCrashes = enableCrashes
139+
self.isCrashReportEnabled = isCrashReportEnabled
140+
self.isLogUploadEnabled = isLogUploadEnabled
116141

117142
#if DEBUG
118143
self.isDistributionEnabled = isDistributionEnabled ?? false
@@ -129,19 +154,26 @@ extension AppCenterSDK {
129154
}
130155
}
131156

132-
extension AppCenterSDK: MSCrashesDelegate {
157+
private class AppCenterSDKDelegate : NSObject, MSCrashesDelegate {
158+
159+
private var isLogUploadEnabled : Bool
160+
161+
init(isLogUploadEnabled : Bool) {
162+
self.isLogUploadEnabled = isLogUploadEnabled
163+
}
133164

134165
func attachments(with crashes: MSCrashes!, for errorReport: MSErrorReport!) -> [MSErrorAttachmentLog]! {
135166
return [
136-
MSErrorAttachmentLog.attachment(withText: self.applicationLog ?? "No Log found", filename: "SMFLogger.log")
167+
MSErrorAttachmentLog.attachment(withText: self.applicationLog, filename: AppCenterConstants.crashLogFileName)
137168
]
138169
}
139170

140-
private var applicationLog: String? {
171+
private var applicationLog: String {
141172
guard
173+
(self.isLogUploadEnabled == true),
142174
let description = Logger.logFilesContent(maxSize: AppCenterConstants.smfLogUploadMaxSize),
143175
(description.isEmpty == false) else {
144-
return nil
176+
return "No Log found"
145177
}
146178

147179
return description

AppCenter/AppCenterSDK+SMFLogger.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ class AppCenterSDK: NSObject {
2828
#endif
2929
}()
3030

31-
// MARK: - Private properties
32-
33-
fileprivate var isInitialized = false
34-
fileprivate var configuration : Configuration?
35-
3631
// MARK: - Public properties
3732

3833
static var wasInitialized : Bool {
@@ -57,7 +52,7 @@ class AppCenterSDK: NSObject {
5752
}
5853

5954
/// Returns True, if and only if the Service got started and is enabled.
60-
var isDistributionEnabled: Bool {
55+
static var isDistributionEnabled: Bool {
6156
return MSDistribute.isEnabled()
6257
}
6358

@@ -71,7 +66,7 @@ class AppCenterSDK: NSObject {
7166
/// - Enable Distribution at a later time using the same method
7267
///
7368
/// - Parameter enabled: Enable or Disable Distribtion
74-
func enableDistribution(enabled: Bool = true) {
69+
static func enableDistribution(enabled: Bool = true) {
7570
MSDistribute.setEnabled(enabled)
7671
}
7772

0 commit comments

Comments
 (0)