From e2fc0a5b51d57a5f85d97e5ccf98f4bcad0f043a Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Fri, 28 Jun 2024 18:31:14 +1000 Subject: [PATCH] Further fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Fixed an issue where the default log level was set too low • Fixed an issue where sending the app to the background without any pending jobs could result in the database and network not being suspended correctly • Updated to latest libSession (don't try to recover paths, just rebuild) --- LibSession-Util | 2 +- Scripts/LintLocalizableStrings.swift | 2 +- Session.xcodeproj/project.pbxproj | 4 ++-- .../Call Management/SessionCallManager.swift | 2 +- Session/Meta/AppDelegate.swift | 6 +++--- Session/Shared/ScreenLockUI.swift | 18 +++++++++--------- .../Utilities/ProfileManager.swift | 2 +- .../NotificationServiceExtension.swift | 1 + SessionShareExtension/ShareNavController.swift | 1 + SessionUtilitiesKit/General/Logging.swift | 14 +++++++++----- SessionUtilitiesKit/JobRunner/JobRunner.swift | 15 +++++++++------ .../LibSession/LibSession.swift | 2 +- _SharedTestUtilities/MockGeneralCache.swift | 4 ++++ _SharedTestUtilities/MockJobRunner.swift | 4 ++-- 14 files changed, 45 insertions(+), 32 deletions(-) diff --git a/LibSession-Util b/LibSession-Util index c9a98dbb2..3ad592079 160000 --- a/LibSession-Util +++ b/LibSession-Util @@ -1 +1 @@ -Subproject commit c9a98dbb2e2ef45aa85075576247d61355269e1c +Subproject commit 3ad5920794d6c84d7ebd44936a23627f574fdc73 diff --git a/Scripts/LintLocalizableStrings.swift b/Scripts/LintLocalizableStrings.swift index 0416b0ac8..dcf5db687 100755 --- a/Scripts/LintLocalizableStrings.swift +++ b/Scripts/LintLocalizableStrings.swift @@ -38,7 +38,7 @@ extension ProjectState { .contains("print(", caseSensitive: false), .contains("SNLog(", caseSensitive: false), .contains("Log.setup(", caseSensitive: false), - .contains("Log.trace(", caseSensitive: false), + .contains("Log.verbose(", caseSensitive: false), .contains("Log.debug(", caseSensitive: false), .contains("Log.info(", caseSensitive: false), .contains("Log.warn(", caseSensitive: false), diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 0234064e8..66d1f1c90 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -8042,7 +8042,7 @@ CLANG_WARN__ARC_BRIDGE_CAST_NONARC = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; - CURRENT_PROJECT_VERSION = 454; + CURRENT_PROJECT_VERSION = 458; ENABLE_BITCODE = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; @@ -8120,7 +8120,7 @@ CLANG_WARN__ARC_BRIDGE_CAST_NONARC = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "iPhone Distribution"; - CURRENT_PROJECT_VERSION = 454; + CURRENT_PROJECT_VERSION = 458; ENABLE_BITCODE = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_NO_COMMON_BLOCKS = YES; diff --git a/Session/Calls/Call Management/SessionCallManager.swift b/Session/Calls/Call Management/SessionCallManager.swift index b7df2f0d6..a8690fb6e 100644 --- a/Session/Calls/Call Management/SessionCallManager.swift +++ b/Session/Calls/Call Management/SessionCallManager.swift @@ -196,7 +196,7 @@ public final class SessionCallManager: NSObject, CallManagerProtocol { let dependencies: Dependencies = Dependencies() // Stop all jobs except for message sending and when completed suspend the database - JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend, using: dependencies) { + JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend, using: dependencies) { _ in LibSession.suspendNetworkAccess() Storage.suspendDatabaseAccess() Log.flush() diff --git a/Session/Meta/AppDelegate.swift b/Session/Meta/AppDelegate.swift index 8dbe48be2..26e0eea1b 100644 --- a/Session/Meta/AppDelegate.swift +++ b/Session/Meta/AppDelegate.swift @@ -51,7 +51,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD AppSetup.setupEnvironment( appSpecificBlock: { - Log.setup(with: Logger(primaryPrefix: "Session")) + Log.setup(with: Logger(primaryPrefix: "Session", level: .info)) Log.info("[AppDelegate] Setting up environment.") // Setup LibSession @@ -210,8 +210,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD let dependencies: Dependencies = Dependencies() // Stop all jobs except for message sending and when completed suspend the database - JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend, using: dependencies) { - if !self.hasCallOngoing() && Singleton.hasAppContext && Singleton.appContext.isInBackground { + JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend, using: dependencies) { neededBackgroundProcessing in + if !self.hasCallOngoing() && (!neededBackgroundProcessing || Singleton.hasAppContext && Singleton.appContext.isInBackground) { LibSession.suspendNetworkAccess() Storage.suspendDatabaseAccess() Log.info("[AppDelegate] completed network and database shutdowns.") diff --git a/Session/Shared/ScreenLockUI.swift b/Session/Shared/ScreenLockUI.swift index 38d6c37cc..d3c3e5f2c 100644 --- a/Session/Shared/ScreenLockUI.swift +++ b/Session/Shared/ScreenLockUI.swift @@ -88,17 +88,17 @@ class ScreenLockUI { private var desiredUIState: ScreenLockViewController.State { if isScreenLockLocked { if appIsInactiveOrBackground { - Log.trace("desiredUIState: screen protection 1.") + Log.verbose("desiredUIState: screen protection 1.") return .protection } - Log.trace("desiredUIState: screen lock 2.") + Log.verbose("desiredUIState: screen lock 2.") return (isShowingScreenLockUI ? .protection : .lock) } if !self.appIsInactiveOrBackground { // App is inactive or background. - Log.trace("desiredUIState: none 3."); + Log.verbose("desiredUIState: none 3."); return .none; } @@ -106,7 +106,7 @@ class ScreenLockUI { return .none; } - Log.trace("desiredUIState: screen protection 4.") + Log.verbose("desiredUIState: screen protection 4.") return .protection; } @@ -181,17 +181,17 @@ class ScreenLockUI { // // We don't need to try to lock the screen lock; // It will be initialized by `setupWithRootWindow`. - Log.trace("tryToActivateScreenLockUponBecomingActive NO 0") + Log.verbose("tryToActivateScreenLockUponBecomingActive NO 0") return } guard Storage.shared[.isScreenLockEnabled] else { // Screen lock is not enabled. - Log.trace("tryToActivateScreenLockUponBecomingActive NO 1") + Log.verbose("tryToActivateScreenLockUponBecomingActive NO 1") return; } guard !isScreenLockLocked else { // Screen lock is already activated. - Log.trace("tryToActivateScreenLockUponBecomingActive NO 2") + Log.verbose("tryToActivateScreenLockUponBecomingActive NO 2") return; } @@ -211,7 +211,7 @@ class ScreenLockUI { } let desiredUIState: ScreenLockViewController.State = self.desiredUIState - Log.trace("ensureUI: \(desiredUIState)") + Log.verbose("ensureUI: \(desiredUIState)") // Show the "iOS auth UI to unlock" if necessary. if desiredUIState == .lock && !didLastUnlockAttemptFail { @@ -368,7 +368,7 @@ class ScreenLockUI { // // We don't need to try to lock the screen lock; // It will be initialized by `setupWithRootWindow`. - Log.trace("clockDidChange 0") + Log.verbose("clockDidChange 0") return; } diff --git a/SessionMessagingKit/Utilities/ProfileManager.swift b/SessionMessagingKit/Utilities/ProfileManager.swift index 191bcc385..103468cf4 100644 --- a/SessionMessagingKit/Utilities/ProfileManager.swift +++ b/SessionMessagingKit/Utilities/ProfileManager.swift @@ -210,7 +210,7 @@ public struct ProfileManager { let filePath: String = ProfileManager.profileAvatarFilepath(filename: fileName) var backgroundTask: OWSBackgroundTask? = OWSBackgroundTask(label: funcName) - Log.trace("downloading profile avatar: \(profile.id)") + Log.verbose("downloading profile avatar: \(profile.id)") currentAvatarDownloads.mutate { $0.insert(profile.id) } LibSession diff --git a/SessionNotificationServiceExtension/NotificationServiceExtension.swift b/SessionNotificationServiceExtension/NotificationServiceExtension.swift index 4dace37f6..6be8921e5 100644 --- a/SessionNotificationServiceExtension/NotificationServiceExtension.swift +++ b/SessionNotificationServiceExtension/NotificationServiceExtension.swift @@ -295,6 +295,7 @@ public final class NotificationServiceExtension: UNNotificationServiceExtension appSpecificBlock: { Log.setup(with: Logger( primaryPrefix: "NotificationServiceExtension", // stringlint:disable + level: .info, customDirectory: "\(OWSFileSystem.appSharedDataDirectoryPath())/Logs/NotificationExtension", // stringlint:disable forceNSLog: true )) diff --git a/SessionShareExtension/ShareNavController.swift b/SessionShareExtension/ShareNavController.swift index 388cb961e..722017cb9 100644 --- a/SessionShareExtension/ShareNavController.swift +++ b/SessionShareExtension/ShareNavController.swift @@ -54,6 +54,7 @@ final class ShareNavController: UINavigationController, ShareViewDelegate { appSpecificBlock: { Log.setup(with: Logger( primaryPrefix: "SessionShareExtension", // stringlint:disable + level: .info, customDirectory: "\(OWSFileSystem.appSharedDataDirectoryPath())/Logs/ShareExtension" // stringlint:disable )) diff --git a/SessionUtilitiesKit/General/Logging.swift b/SessionUtilitiesKit/General/Logging.swift index aec96fd3f..a3ffde0dd 100644 --- a/SessionUtilitiesKit/General/Logging.swift +++ b/SessionUtilitiesKit/General/Logging.swift @@ -10,8 +10,8 @@ import SignalCoreKit public enum Log { fileprivate typealias LogInfo = (level: Log.Level, message: String, withPrefixes: Bool, silenceForTests: Bool) - public enum Level { - case trace + public enum Level: Comparable { + case verbose case debug case info case warn @@ -116,12 +116,12 @@ public enum Log { } } - public static func trace( + public static func verbose( _ message: String, withPrefixes: Bool = true, silenceForTests: Bool = false ) { - custom(.trace, message, withPrefixes: withPrefixes, silenceForTests: silenceForTests) + custom(.verbose, message, withPrefixes: withPrefixes, silenceForTests: silenceForTests) } public static func debug( @@ -184,6 +184,7 @@ public enum Log { public class Logger { private let isRunningTests: Bool = (ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil) private let primaryPrefix: String + private var level: Log.Level private let forceNSLog: Bool fileprivate let fileLogger: DDFileLogger fileprivate let isSuspended: Atomic = Atomic(true) @@ -191,10 +192,12 @@ public class Logger { public init( primaryPrefix: String, + level: Log.Level, customDirectory: String? = nil, forceNSLog: Bool = false ) { self.primaryPrefix = primaryPrefix + self.level = level self.forceNSLog = forceNSLog switch customDirectory { @@ -341,6 +344,7 @@ public class Logger { silenceForTests: Bool ) { guard !silenceForTests || !isRunningTests else { return } + guard level >= self.level else { return } // Sort out the prefixes let logPrefix: String = { @@ -367,7 +371,7 @@ public class Logger { switch level { case .off: return - case .trace: OWSLogger.verbose(logMessage) + case .verbose: OWSLogger.verbose(logMessage) case .debug: OWSLogger.debug(logMessage) case .info: OWSLogger.info(logMessage) case .warn: OWSLogger.warn(logMessage) diff --git a/SessionUtilitiesKit/JobRunner/JobRunner.swift b/SessionUtilitiesKit/JobRunner/JobRunner.swift index 7bc83c3c6..049dd310c 100644 --- a/SessionUtilitiesKit/JobRunner/JobRunner.swift +++ b/SessionUtilitiesKit/JobRunner/JobRunner.swift @@ -20,7 +20,10 @@ public protocol JobRunnerType { func appDidFinishLaunching(using dependencies: Dependencies) func appDidBecomeActive(using dependencies: Dependencies) func startNonBlockingQueues(using dependencies: Dependencies) - func stopAndClearPendingJobs(exceptForVariant: Job.Variant?, using dependencies: Dependencies, onComplete: (() -> ())?) + + /// Stops and clears any pending jobs except for the specified variant, the `onComplete` closure will be called once complete providing a flag indicating whether any additionak + /// processing was needed before the closure was called (if not then the closure will be called synchronously) + func stopAndClearPendingJobs(exceptForVariant: Job.Variant?, using dependencies: Dependencies, onComplete: ((Bool) -> ())?) // MARK: - Job Scheduling @@ -577,7 +580,7 @@ public final class JobRunner: JobRunnerType { public func stopAndClearPendingJobs( exceptForVariant: Job.Variant?, using dependencies: Dependencies, - onComplete: (() -> ())? + onComplete: ((Bool) -> ())? ) { // Inform the JobRunner that it can't start any queues (this is to prevent queues from // rescheduling themselves while in the background, when the app restarts or becomes active @@ -602,7 +605,7 @@ public final class JobRunner: JobRunnerType { let queue: JobQueue = queues.wrappedValue[exceptForVariant], queue.isRunning.wrappedValue == true else { - onComplete?() + onComplete?(false) return } @@ -619,7 +622,7 @@ public final class JobRunner: JobRunnerType { } guard state != .success else { return } - onComplete?() + onComplete?(true) queue?.onQueueDrained = oldQueueDrained queue?.stopAndClearPendingJobs() } @@ -629,7 +632,7 @@ public final class JobRunner: JobRunnerType { queue.onQueueDrained = { [weak self, weak queue] in oldQueueDrained?() queue?.onQueueDrained = oldQueueDrained - onComplete?() + onComplete?(true) self?.shutdownBackgroundTask.mutate { $0 = nil } } @@ -1887,7 +1890,7 @@ public extension JobRunner { static func stopAndClearPendingJobs( exceptForVariant: Job.Variant? = nil, using dependencies: Dependencies, - onComplete: (() -> ())? = nil + onComplete: ((Bool) -> ())? = nil ) { instance.stopAndClearPendingJobs(exceptForVariant: exceptForVariant, using: dependencies, onComplete: onComplete) } diff --git a/SessionUtilitiesKit/LibSession/LibSession.swift b/SessionUtilitiesKit/LibSession/LibSession.swift index c73c84450..e3b753cb8 100644 --- a/SessionUtilitiesKit/LibSession/LibSession.swift +++ b/SessionUtilitiesKit/LibSession/LibSession.swift @@ -83,7 +83,7 @@ extension LibSession { fileprivate extension Log.Level { init(_ level: LOG_LEVEL) { switch level { - case LOG_LEVEL_TRACE: self = .trace + case LOG_LEVEL_TRACE: self = .verbose case LOG_LEVEL_DEBUG: self = .debug case LOG_LEVEL_INFO: self = .info case LOG_LEVEL_WARN: self = .warn diff --git a/_SharedTestUtilities/MockGeneralCache.swift b/_SharedTestUtilities/MockGeneralCache.swift index 554c325cd..708083fad 100644 --- a/_SharedTestUtilities/MockGeneralCache.swift +++ b/_SharedTestUtilities/MockGeneralCache.swift @@ -13,4 +13,8 @@ class MockGeneralCache: Mock, GeneralCacheType { get { return (accept() as? [Int64] ?? []) } set { accept(args: [newValue]) } } + + func clearCachedUserPublicKey() { + accept() + } } diff --git a/_SharedTestUtilities/MockJobRunner.swift b/_SharedTestUtilities/MockJobRunner.swift index 445566157..a13a3d81f 100644 --- a/_SharedTestUtilities/MockJobRunner.swift +++ b/_SharedTestUtilities/MockJobRunner.swift @@ -32,9 +32,9 @@ class MockJobRunner: Mock, JobRunnerType { func appDidBecomeActive(using dependencies: Dependencies) {} func startNonBlockingQueues(using dependencies: Dependencies) {} - func stopAndClearPendingJobs(exceptForVariant: Job.Variant?, onComplete: (() -> ())?) { + func stopAndClearPendingJobs(exceptForVariant: Job.Variant?, using dependencies: Dependencies, onComplete: ((Bool) -> ())?) { accept(args: [exceptForVariant, onComplete]) - onComplete?() + onComplete?(false) } // MARK: - Job Scheduling