From a19f5a4186f8c3ad2d5f79fec09b945b050a8ac4 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Wed, 23 Aug 2023 14:58:44 +0300 Subject: [PATCH] Expose the log level in developer settings --- .../Sources/Application/AppCoordinator.swift | 17 +++++++++-------- ElementX/Sources/Application/AppSettings.swift | 4 ++++ ElementX/Sources/Other/Logging/MXLog.swift | 12 ++---------- .../Sources/Other/Logging/RustTracing.swift | 16 ++++++---------- ElementX/Sources/Other/Pills/PillView.swift | 4 +++- .../DeveloperOptionsScreenModels.swift | 1 + .../View/DeveloperOptionsScreen.swift | 9 +++++++++ NSE/Sources/Other/NSELogger.swift | 2 +- UnitTests/Sources/LoggingTests.swift | 18 +++++++++--------- 9 files changed, 44 insertions(+), 39 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index c95f03ae4..f3982bc1d 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -57,11 +57,16 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, @Consumable private var storedAppRoute: AppRoute? init() { - MXLog.configure() + let appSettings = AppSettings() + MXLog.configure(logLevel: appSettings.logLevel) + + if ProcessInfo.processInfo.environment["RESET_APP_SETTINGS"].map(Bool.init) == true { + AppSettings.reset() + } navigationRootCoordinator = NavigationRootCoordinator() - Self.setupServiceLocator(navigationRootCoordinator: navigationRootCoordinator) + Self.setupServiceLocator(navigationRootCoordinator: navigationRootCoordinator, appSettings: appSettings) ServiceLocator.shared.analytics.startIfEnabled() @@ -191,13 +196,9 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate, // MARK: - Private - private static func setupServiceLocator(navigationRootCoordinator: NavigationRootCoordinator) { - if ProcessInfo.processInfo.environment["RESET_APP_SETTINGS"].map(Bool.init) == true { - AppSettings.reset() - } - + private static func setupServiceLocator(navigationRootCoordinator: NavigationRootCoordinator, appSettings: AppSettings) { ServiceLocator.shared.register(userIndicatorController: UserIndicatorController(rootCoordinator: navigationRootCoordinator)) - ServiceLocator.shared.register(appSettings: AppSettings()) + ServiceLocator.shared.register(appSettings: appSettings) ServiceLocator.shared.register(networkMonitor: NetworkMonitor()) ServiceLocator.shared.register(bugReportService: BugReportService(withBaseURL: ServiceLocator.shared.settings.bugReportServiceBaseURL, sentryURL: ServiceLocator.shared.settings.bugReportSentryURL, diff --git a/ElementX/Sources/Application/AppSettings.swift b/ElementX/Sources/Application/AppSettings.swift index 9b464e275..b6797ef2e 100644 --- a/ElementX/Sources/Application/AppSettings.swift +++ b/ElementX/Sources/Application/AppSettings.swift @@ -29,6 +29,7 @@ final class AppSettings { case enableNotifications case enableInAppNotifications case pusherProfileTag + case logLevel // Feature flags case shouldCollapseRoomStateEvents @@ -196,6 +197,9 @@ final class AppSettings { let permalinkBaseURL: URL = "https://matrix.to" + @UserPreference(key: UserDefaultsKeys.logLevel, defaultValue: TracingConfiguration.LogLevel.info, storageType: .userDefaults(store)) + var logLevel + // MARK: - Maps // maptiler base url diff --git a/ElementX/Sources/Other/Logging/MXLog.swift b/ElementX/Sources/Other/Logging/MXLog.swift index f23039336..1d1a1ab75 100644 --- a/ElementX/Sources/Other/Logging/MXLog.swift +++ b/ElementX/Sources/Other/Logging/MXLog.swift @@ -42,7 +42,7 @@ enum MXLog { private static var target: String! static func configure(target: String? = nil, - logLevel: TracingConfiguration.LogLevel? = nil, + logLevel: TracingConfiguration.LogLevel, redirectToFiles: Bool = Constants.redirectToFiles, maxLogFileCount: UInt = Constants.maxLogFileCount, logFileSizeLimit: UInt = Constants.logFilesSizeLimit) { @@ -58,15 +58,7 @@ enum MXLog { return } - if let logLevel { - setupTracing(configuration: .custom(logLevel: logLevel)) - } else { - #if DEBUG - setupTracing(configuration: .debug) - #else - setupTracing(configuration: .release) - #endif - } + setupTracing(configuration: .custom(logLevel: logLevel)) if let target { self.target = target diff --git a/ElementX/Sources/Other/Logging/RustTracing.swift b/ElementX/Sources/Other/Logging/RustTracing.swift index 08be4e89e..cc6c36166 100644 --- a/ElementX/Sources/Other/Logging/RustTracing.swift +++ b/ElementX/Sources/Other/Logging/RustTracing.swift @@ -21,10 +21,6 @@ import MatrixRustSDK // We can filter by level, crate and even file. See more details here: // https://docs.rs/tracing-subscriber/0.2.7/tracing_subscriber/filter/struct.EnvFilter.html#examples struct TracingConfiguration { - static var release = TracingConfiguration(overrides: [.common: .info]) - - static var debug = TracingConfiguration(overrides: [.common: .info]) - /// Configure tracing with certain overrides in place /// - Parameter overrides: the desired overrides /// - Returns: a custom tracing configuration @@ -43,7 +39,7 @@ struct TracingConfiguration { return TracingConfiguration(overrides: overrides) } - enum LogLevel: String { case error, warn, info, debug, trace } + enum LogLevel: String, Codable, CaseIterable { case error, warn, info, debug, trace } enum Target: String { case common = "" @@ -59,11 +55,11 @@ struct TracingConfiguration { static let targets: OrderedDictionary = [ .common: .info, - .hyper: .warn, - .matrix_sdk_crypto: .debug, - .matrix_sdk_http_client: .debug, - .matrix_sdk_sliding_sync: .debug, - .matrix_sdk_base_sliding_sync: .debug, + .hyper: .info, + .matrix_sdk_crypto: .info, + .matrix_sdk_http_client: .info, + .matrix_sdk_sliding_sync: .info, + .matrix_sdk_base_sliding_sync: .info, .matrix_sdk_ui_timeline: .info ] diff --git a/ElementX/Sources/Other/Pills/PillView.swift b/ElementX/Sources/Other/Pills/PillView.swift index b2f1d997a..51c449168 100644 --- a/ElementX/Sources/Other/Pills/PillView.swift +++ b/ElementX/Sources/Other/Pills/PillView.swift @@ -18,7 +18,9 @@ import SwiftUI struct PillView: View { var body: some View { - Button(action: { MXLog.info("TEXT ATTACHMENT TEST") }) { + Button { + MXLog.info("TEXT ATTACHMENT TEST") + } label: { HStack { Image(asset: Asset.Images.launchLogo) .resizable() diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift index a3255f19d..e5853d1c6 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/DeveloperOptionsScreenModels.swift @@ -43,6 +43,7 @@ enum DeveloperOptionsScreenViewAction { } protocol DeveloperOptionsProtocol: AnyObject { + var logLevel: TracingConfiguration.LogLevel { get set } var shouldCollapseRoomStateEvents: Bool { get set } var userSuggestionsEnabled: Bool { get set } var readReceiptsEnabled: Bool { get set } diff --git a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift index 7ec796678..80d20877d 100644 --- a/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift +++ b/ElementX/Sources/Screens/Settings/DeveloperOptionsScreen/View/DeveloperOptionsScreen.swift @@ -22,6 +22,15 @@ struct DeveloperOptionsScreen: View { var body: some View { Form { + Picker(selection: $context.logLevel) { + ForEach(TracingConfiguration.LogLevel.allCases, id: \.self) { logLevel in + Text(logLevel.rawValue.capitalized) + } + } label: { + Text("Log level") + Text("Requires app reboot") + } + Section("Timeline") { Toggle(isOn: $context.shouldCollapseRoomStateEvents) { Text("Collapse room state events") diff --git a/NSE/Sources/Other/NSELogger.swift b/NSE/Sources/Other/NSELogger.swift index 3b25f6482..d51f263d5 100644 --- a/NSE/Sources/Other/NSELogger.swift +++ b/NSE/Sources/Other/NSELogger.swift @@ -81,7 +81,7 @@ class NSELogger { } isConfigured = true - MXLog.configure(target: "nse") + MXLog.configure(target: "nse", logLevel: .info) } static func logMemory(with tag: String) { diff --git a/UnitTests/Sources/LoggingTests.swift b/UnitTests/Sources/LoggingTests.swift index e714d44e4..83763b19c 100644 --- a/UnitTests/Sources/LoggingTests.swift +++ b/UnitTests/Sources/LoggingTests.swift @@ -36,7 +36,7 @@ class LoggingTests: XCTestCase { let log = UUID().uuidString - MXLog.configure(redirectToFiles: true) + MXLog.configure(logLevel: .info, redirectToFiles: true) MXLog.info(log) guard let logFile = MXLogger.logFiles.first else { @@ -55,7 +55,7 @@ class LoggingTests: XCTestCase { // When launching the app 5 times. let launchCount = 5 for index in 0..