Adopt consolidated logging mechanism (#4370)

* Adopted the new MXLog and replaced NSLog throughout the application - vector-im/element-ios/issues/4351
* Replaced NSLog() and print() usages with MXLog.debug()
* Added swiftlint rules for NSLog(), print(), println() and os_log()
* Escape paths used to run script build phases for swiftlint and swiftgen
This commit is contained in:
Stefan Ceriu
2021-06-03 11:30:07 +03:00
committed by GitHub
parent f70b6df6c9
commit 43f6c44c78
94 changed files with 689 additions and 630 deletions

View File

@@ -77,11 +77,11 @@ class NotificationService: UNNotificationServiceExtension {
// setup logs
setupLogger()
NSLog(" ")
NSLog(" ")
NSLog("################################################################################")
NSLog("[NotificationService] Instance: \(self), thread: \(Thread.current)")
NSLog("[NotificationService] Payload came: \(userInfo)")
MXLog.debug(" ")
MXLog.debug(" ")
MXLog.debug("################################################################################")
MXLog.debug("[NotificationService] Instance: \(self), thread: \(Thread.current)")
MXLog.debug("[NotificationService] Payload came: \(userInfo)")
// log memory at the beginning of the process
logMemory()
@@ -91,7 +91,7 @@ class NotificationService: UNNotificationServiceExtension {
// check if this is a Matrix notification
guard let roomId = userInfo["room_id"] as? String, let eventId = userInfo["event_id"] as? String else {
// it's not a Matrix notification, do not change the content
NSLog("[NotificationService] didReceiveRequest: This is not a Matrix notification.")
MXLog.debug("[NotificationService] didReceiveRequest: This is not a Matrix notification.")
contentHandler(request.content)
return
}
@@ -124,30 +124,37 @@ class NotificationService: UNNotificationServiceExtension {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
NSLog("[NotificationService] serviceExtensionTimeWillExpire")
MXLog.debug("[NotificationService] serviceExtensionTimeWillExpire")
// No-op here. If the process is killed by the OS due to time limit, it will also show the notification with the original content.
}
deinit {
NSLog("[NotificationService] deinit for \(self)");
MXLog.debug("[NotificationService] deinit for \(self)");
self.logMemory()
NSLog(" ")
MXLog.debug(" ")
}
// MARK: - Private
private func logMemory() {
NSLog("[NotificationService] Memory: footprint: \(MXMemory.formattedMemoryFootprint()) - available: \(MXMemory.formattedMemoryAvailable())")
MXLog.debug("[NotificationService] Memory: footprint: \(MXMemory.formattedMemoryFootprint()) - available: \(MXMemory.formattedMemoryAvailable())")
}
private func setupLogger() {
if !NotificationService.isLoggerInitialized {
let configuration = MXLogConfiguration()
configuration.logLevel = .verbose
configuration.maxLogFilesCount = 100
configuration.logFilesSizeLimit = 10 * 1024 * 1024; // 10MB
configuration.subLogName = "nse"
if isatty(STDERR_FILENO) == 0 {
MXLogger.setSubLogName("nse")
let sizeLimit: UInt = 10 * 1024 * 1024; // 10MB
MXLogger.redirectNSLog(toFiles: true, numberOfFiles: 100, sizeLimit: sizeLimit)
configuration.redirectLogsToFiles = true
}
MXLog.configure(configuration)
NotificationService.isLoggerInitialized = true
}
}
@@ -157,15 +164,15 @@ class NotificationService: UNNotificationServiceExtension {
self.userAccount = MXKAccountManager.shared()?.activeAccounts.first
if let userAccount = userAccount {
if NotificationService.backgroundSyncService == nil {
NSLog("[NotificationService] setup: MXBackgroundSyncService init: BEFORE")
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: BEFORE")
self.logMemory()
NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials)
NSLog("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
self.logMemory()
}
completion()
} else {
NSLog("[NotificationService] setup: No active accounts")
MXLog.debug("[NotificationService] setup: No active accounts")
fallbackToBestAttemptContent(forEventId: eventId)
}
}
@@ -176,7 +183,7 @@ class NotificationService: UNNotificationServiceExtension {
/// - roomId: Room identifier to fetch display name
private func preprocessPayload(forEventId eventId: String, roomId: String) {
if localAuthenticationService.isProtectionSet {
NSLog("[NotificationService] preprocessPayload: Do not preprocess because app protection is set")
MXLog.debug("[NotificationService] preprocessPayload: Do not preprocess because app protection is set")
return
}
guard let roomSummary = NotificationService.backgroundSyncService.roomSummary(forRoomId: roomId) else { return }
@@ -189,17 +196,17 @@ class NotificationService: UNNotificationServiceExtension {
}
private func fetchEvent(withEventId eventId: String, roomId: String, allowSync: Bool = true) {
NSLog("[NotificationService] fetchEvent")
MXLog.debug("[NotificationService] fetchEvent")
NotificationService.backgroundSyncService.event(withEventId: eventId,
inRoom: roomId,
completion: { (response) in
switch response {
case .success(let event):
NSLog("[NotificationService] fetchEvent: Event fetched successfully")
MXLog.debug("[NotificationService] fetchEvent: Event fetched successfully")
self.processEvent(event)
case .failure(let error):
NSLog("[NotificationService] fetchEvent: error: \(error)")
MXLog.debug("[NotificationService] fetchEvent: error: \(error)")
self.fallbackToBestAttemptContent(forEventId: eventId)
}
})
@@ -207,7 +214,7 @@ class NotificationService: UNNotificationServiceExtension {
private func processEvent(_ event: MXEvent) {
if let receiveDate = receiveDates[event.eventId] {
NSLog("[NotificationService] processEvent: notification receive delay: \(receiveDate.timeIntervalSince1970*MSEC_PER_SEC - TimeInterval(event.originServerTs)) ms")
MXLog.debug("[NotificationService] processEvent: notification receive delay: \(receiveDate.timeIntervalSince1970*MSEC_PER_SEC - TimeInterval(event.originServerTs)) ms")
}
guard let content = bestAttemptContents[event.eventId], let userAccount = userAccount else {
@@ -241,23 +248,23 @@ class NotificationService: UNNotificationServiceExtension {
// When it completes, it'll continue with the bestAttemptContent.
return
} else {
NSLog("[NotificationService] processEvent: Calling content handler for: \(String(describing: event.eventId)), isUnwanted: \(isUnwantedNotification)")
MXLog.debug("[NotificationService] processEvent: Calling content handler for: \(String(describing: event.eventId)), isUnwanted: \(isUnwantedNotification)")
self.contentHandlers[event.eventId]?(content)
// clear maps
self.contentHandlers.removeValue(forKey: event.eventId)
self.bestAttemptContents.removeValue(forKey: event.eventId)
// We are done for this push
NSLog("--------------------------------------------------------------------------------")
MXLog.debug("--------------------------------------------------------------------------------")
}
}
}
private func fallbackToBestAttemptContent(forEventId eventId: String) {
NSLog("[NotificationService] fallbackToBestAttemptContent: method called.")
MXLog.debug("[NotificationService] fallbackToBestAttemptContent: method called.")
guard let content = bestAttemptContents[eventId] else {
NSLog("[NotificationService] fallbackToBestAttemptContent: Best attempt content is missing.")
MXLog.debug("[NotificationService] fallbackToBestAttemptContent: Best attempt content is missing.")
return
}
@@ -269,12 +276,12 @@ class NotificationService: UNNotificationServiceExtension {
receiveDates.removeValue(forKey: eventId)
// We are done for this push
NSLog("--------------------------------------------------------------------------------")
MXLog.debug("--------------------------------------------------------------------------------")
}
private func notificationContent(forEvent event: MXEvent, forAccount account: MXKAccount, onComplete: @escaping (UNNotificationContent?) -> Void) {
guard let content = event.content, content.count > 0 else {
NSLog("[NotificationService] notificationContentForEvent: empty event content")
MXLog.debug("[NotificationService] notificationContentForEvent: empty event content")
onComplete(nil)
return
}
@@ -283,7 +290,7 @@ class NotificationService: UNNotificationServiceExtension {
let isRoomMentionsOnly = NotificationService.backgroundSyncService.isRoomMentionsOnly(roomId)
let roomSummary = NotificationService.backgroundSyncService.roomSummary(forRoomId: roomId)
NSLog("[NotificationService] notificationContentForEvent: Attempt to fetch the room state")
MXLog.debug("[NotificationService] notificationContentForEvent: Attempt to fetch the room state")
self.context(ofEvent: event, inRoom: roomId, completion: { (response) in
switch response {
@@ -317,7 +324,7 @@ class NotificationService: UNNotificationServiceExtension {
(callInviteContent.lifetime - event.age) > UInt(NSE.Constants.timeNeededToSendVoIPPushes * MSEC_PER_SEC) {
self.sendVoipPush(forEvent: event)
} else {
NSLog("[NotificationService] notificationContent: Do not attempt to send a VoIP push, there is not enough time to process it.")
MXLog.debug("[NotificationService] notificationContent: Do not attempt to send a VoIP push, there is not enough time to process it.")
}
case .roomMessage, .roomEncrypted:
if isRoomMentionsOnly {
@@ -339,7 +346,7 @@ class NotificationService: UNNotificationServiceExtension {
if !isHighlighted {
// Ignore this notif.
NSLog("[NotificationService] notificationContentForEvent: Ignore non highlighted notif in mentions only room")
MXLog.debug("[NotificationService] notificationContentForEvent: Ignore non highlighted notif in mentions only room")
onComplete(nil)
return
}
@@ -422,13 +429,13 @@ class NotificationService: UNNotificationServiceExtension {
}
if self.localAuthenticationService.isProtectionSet {
NSLog("[NotificationService] notificationContentForEvent: Resetting title and body because app protection is set")
MXLog.debug("[NotificationService] notificationContentForEvent: Resetting title and body because app protection is set")
notificationBody = NSString.localizedUserNotificationString(forKey: "MESSAGE_PROTECTED", arguments: [])
notificationTitle = nil
}
guard notificationBody != nil else {
NSLog("[NotificationService] notificationContentForEvent: notificationBody is nil")
MXLog.debug("[NotificationService] notificationContentForEvent: notificationBody is nil")
onComplete(nil)
return
}
@@ -441,10 +448,10 @@ class NotificationService: UNNotificationServiceExtension {
pushRule: pushRule,
additionalInfo: additionalUserInfo)
NSLog("[NotificationService] notificationContentForEvent: Calling onComplete.")
MXLog.debug("[NotificationService] notificationContentForEvent: Calling onComplete.")
onComplete(notificationContent)
case .failure(let error):
NSLog("[NotificationService] notificationContentForEvent: error: \(error)")
MXLog.debug("[NotificationService] notificationContentForEvent: error: \(error)")
onComplete(nil)
}
})
@@ -556,7 +563,7 @@ class NotificationService: UNNotificationServiceExtension {
}
}
NSLog("Sound name: \(String(describing: soundName))")
MXLog.debug("Sound name: \(String(describing: soundName))")
return soundName
}
@@ -590,7 +597,7 @@ class NotificationService: UNNotificationServiceExtension {
if #available(iOS 13.0, *) {
if event.isEncrypted {
guard let clearEvent = event.clear else {
NSLog("[NotificationService] sendVoipPush: Do not send a VoIP push for undecrypted event, it'll cause a crash.")
MXLog.debug("[NotificationService] sendVoipPush: Do not send a VoIP push for undecrypted event, it'll cause a crash.")
return
}
@@ -617,14 +624,14 @@ class NotificationService: UNNotificationServiceExtension {
sender: event.sender,
timeout: NSE.Constants.voipPushRequestTimeout,
success: { [weak self] (rejected) in
NSLog("[NotificationService] sendVoipPush succeeded, rejected tokens: \(rejected)")
MXLog.debug("[NotificationService] sendVoipPush succeeded, rejected tokens: \(rejected)")
guard let self = self else { return }
self.ongoingVoIPPushRequests.removeValue(forKey: event.eventId)
self.fallbackToBestAttemptContent(forEventId: event.eventId)
}) { [weak self] (error) in
NSLog("[NotificationService] sendVoipPush failed with error: \(error)")
MXLog.debug("[NotificationService] sendVoipPush failed with error: \(error)")
guard let self = self else { return }
self.ongoingVoIPPushRequests.removeValue(forKey: event.eventId)