diff --git a/Riot/Managers/Logging/MatrixSDKLogger.swift b/Riot/Managers/Logging/MatrixSDKLogger.swift new file mode 100644 index 000000000..3012f7bb3 --- /dev/null +++ b/Riot/Managers/Logging/MatrixSDKLogger.swift @@ -0,0 +1,39 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +/** + A logger for logging to MXLog. + For use with UILog. + */ +class MatrixSDKLogger: LoggerProtocol { + static func verbose(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + MXLog.verbose(message(), file, function, line: line, context: context) + } + static func debug(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + MXLog.debug(message(), file, function, line: line, context: context) + } + static func info(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + MXLog.info(message(), file, function, line: line, context: context) + } + static func warning(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + MXLog.warning(message(), file, function, line: line, context: context) + } + static func error(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + MXLog.error(message(), file, function, line: line, context: context) + } +} diff --git a/Riot/Modules/Application/AppCoordinator.swift b/Riot/Modules/Application/AppCoordinator.swift index 12cc4e3f3..bb9b653c2 100755 --- a/Riot/Modules/Application/AppCoordinator.swift +++ b/Riot/Modules/Application/AppCoordinator.swift @@ -75,6 +75,7 @@ final class AppCoordinator: NSObject, AppCoordinatorType { // MARK: - Public methods func start() { + self.setupLogger() self.setupTheme() if BuildSettings.enableSideMenu { @@ -100,6 +101,9 @@ final class AppCoordinator: NSObject, AppCoordinatorType { } // MARK: - Private methods + private func setupLogger() { + UILog.configure(logger: MatrixSDKLogger.self) + } private func setupTheme() { ThemeService.shared().themeId = RiotSettings.shared.userInterfaceTheme diff --git a/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift b/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift index f6a81f6d1..1612da1c2 100644 --- a/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift +++ b/RiotSwiftUI/Modules/Common/Avatar/ViewModel/AvatarViewModel.swift @@ -47,8 +47,7 @@ class AvatarViewModel: InjectableObject, ObservableObject { avatarService.avatarImage(mxContentUri: mxContentUri, avatarSize: avatarSize) .sink { completion in guard case let .failure(error) = completion else { return } -// MXLog.error("[AvatarService] Failed to retrieve avatar: \(error)") - // TODO: Report non-fatal error when we have Sentry or similar. + UILog.error("[AvatarService] Failed to retrieve avatar: \(error)") } receiveValue: { image in self.viewState = .avatar(image) } diff --git a/RiotSwiftUI/Modules/Common/Logging/LoggerProtocol.swift b/RiotSwiftUI/Modules/Common/Logging/LoggerProtocol.swift new file mode 100644 index 000000000..8930f07c7 --- /dev/null +++ b/RiotSwiftUI/Modules/Common/Logging/LoggerProtocol.swift @@ -0,0 +1,29 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +/** + A logger protocol that enables confirming types + to be used with UILog. + */ +protocol LoggerProtocol { + static func verbose(_ message: @autoclosure () -> Any, _ file: String, _ function: String, line: Int, context: Any?) + static func debug(_ message: @autoclosure () -> Any, _ file: String, _ function: String, line: Int, context: Any?) + static func info(_ message: @autoclosure () -> Any, _ file: String, _ function: String, line: Int, context: Any?) + static func warning(_ message: @autoclosure () -> Any, _ file: String, _ function: String, line: Int, context: Any?) + static func error(_ message: @autoclosure () -> Any, _ file: String, _ function: String, line: Int, context: Any?) +} diff --git a/RiotSwiftUI/Modules/Common/Logging/PrintLogger.swift b/RiotSwiftUI/Modules/Common/Logging/PrintLogger.swift new file mode 100644 index 000000000..faf90b540 --- /dev/null +++ b/RiotSwiftUI/Modules/Common/Logging/PrintLogger.swift @@ -0,0 +1,39 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation + +/** + A logger for logging to `print`. + For use with UILog. + */ +class PrintLogger: LoggerProtocol { + static func verbose(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + print(message()) + } + static func debug(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + print(message()) + } + static func info(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + print(message()) + } + static func warning(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + print(message()) + } + static func error(_ message: @autoclosure () -> Any, _ file: String = #file, _ function: String = #function, line: Int = #line, context: Any? = nil) { + print(message()) + } +} diff --git a/RiotSwiftUI/Modules/Common/Logging/UILog.swift b/RiotSwiftUI/Modules/Common/Logging/UILog.swift new file mode 100644 index 000000000..9874be30c --- /dev/null +++ b/RiotSwiftUI/Modules/Common/Logging/UILog.swift @@ -0,0 +1,73 @@ +// +// Copyright 2021 New Vector Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation +/* + A logger for use in different application targets that can be configured + at runtime with a suitable logger. + */ +class UILog: LoggerProtocol { + + static var _logger: LoggerProtocol.Type? + static func configure(logger: LoggerProtocol.Type) { + _logger = logger + } + + static func verbose( + _ message: @autoclosure () -> Any, + _ file: String = #file, + _ function: String = #function, + line: Int = #line, + context: Any? = nil) { + _logger?.verbose(message(), file, function, line: line, context: context) + } + + static func debug( + _ message: @autoclosure () -> Any, + _ file: String = #file, + _ function: String = #function, + line: Int = #line, + context: Any? = nil) { + _logger?.debug(message(), file, function, line: line, context: context) + } + + static func info( + _ message: @autoclosure () -> Any, + _ file: String = #file, + _ function: String = #function, + line: Int = #line, + context: Any? = nil) { + _logger?.info(message(), file, function, line: line, context: context) + } + + static func warning( + _ message: @autoclosure () -> Any, + _ file: String = #file, + _ function: String = #function, + line: Int = #line, + context: Any? = nil) { + _logger?.warning(message(), file, function, line: line, context: context) + } + + static func error( + _ message: @autoclosure () -> Any, + _ file: String = #file, + _ function: String = #function, + line: Int = #line, + context: Any? = nil) { + _logger?.error(message(), file, function, line: line, context: context) + } +} diff --git a/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/Service/MatrixSDK/TemplateUserProfileService.swift b/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/Service/MatrixSDK/TemplateUserProfileService.swift index bf08fba09..d42fd39a1 100644 --- a/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/Service/MatrixSDK/TemplateUserProfileService.swift +++ b/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/Service/MatrixSDK/TemplateUserProfileService.swift @@ -64,11 +64,9 @@ class TemplateUserProfileService: TemplateUserProfileServiceProtocol { else { return } self.presenceSubject.send(TemplateUserProfilePresence(mxPresence: self.session.myUser.presence)) } -// TODO: Add log back when abstract logger added to RiotSwiftUI -// if reference == nil { -// -// MXLog.error("[TemplateUserProfileService] Did not recieve a lisenter reference.") -// } + if reference == nil { + UILog.error("[TemplateUserProfileService] Did not recieve a lisenter reference.") + } return reference } } diff --git a/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/ViewModel/TemplateUserProfileViewModel.swift b/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/ViewModel/TemplateUserProfileViewModel.swift index 45a70d253..cf625e152 100644 --- a/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/ViewModel/TemplateUserProfileViewModel.swift +++ b/RiotSwiftUI/Modules/Template/SimpleUserProfileExample/ViewModel/TemplateUserProfileViewModel.swift @@ -75,8 +75,7 @@ class TemplateUserProfileViewModel: ObservableObject, TemplateUserProfileViewMod case .updatePresence(let presence): state.presence = presence } -// TODO: Uncomment when we have an abstract logger for RiotSwiftUI -// MXLog.debug("[TemplateUserProfileViewModel] reducer with action \(action) produced state: \(state)") + UILog.debug("[TemplateUserProfileViewModel] reducer with action \(action) produced state: \(state)") } private func done() { diff --git a/RiotSwiftUI/RiotSwiftUIApp.swift b/RiotSwiftUI/RiotSwiftUIApp.swift index 478d6f5d4..cd767602d 100644 --- a/RiotSwiftUI/RiotSwiftUIApp.swift +++ b/RiotSwiftUI/RiotSwiftUIApp.swift @@ -20,7 +20,10 @@ import SwiftUI */ @available(iOS 14.0, *) @main -struct testApp: App { +struct RiotSwiftUIApp: App { + init() { + UILog.configure(logger: PrintLogger.self) + } var body: some Scene { WindowGroup { Text("app")