Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tracing configuration to the rust side. #3668

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/element-hq/matrix-rust-components-swift",
"state" : {
"revision" : "3047d2b193a437b53998fa0133c46f198ee57126",
"version" : "24.12.20"
"revision" : "cc9cd80aa6954a7945d82c29182f81c5219f635e",
"version" : "25.1.10-2"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg
}

if oldVersion < Version(1, 6, 7) {
RustTracing.deleteLogFiles()
Tracing.deleteLogFiles()
MXLog.info("Migrating to v1.6.7, log files have been wiped")
}
}
Expand Down
4 changes: 2 additions & 2 deletions ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

// Common settings between app and NSE
protocol CommonSettingsProtocol {
var logLevel: TracingConfiguration.LogLevel { get }
var logLevel: LogLevel { get }
var enableOnlySignedDeviceIsolationMode: Bool { get }
var hideTimelineMedia: Bool { get }
var eventCacheEnabled: Bool { get }
Expand Down Expand Up @@ -294,7 +294,7 @@ final class AppSettings {

// MARK: - Shared

@UserPreference(key: UserDefaultsKeys.logLevel, defaultValue: TracingConfiguration.LogLevel.info, storageType: .userDefaults(store))
@UserPreference(key: UserDefaultsKeys.logLevel, defaultValue: LogLevel.info, storageType: .userDefaults(store))
var logLevel

/// Configuration to enable only signed device isolation mode for crypto. In this mode only devices signed by their owner will be considered in e2ee rooms.
Expand Down
339 changes: 214 additions & 125 deletions ElementX/Sources/Mocks/Generated/SDKGeneratedMocks.swift

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion ElementX/Sources/Mocks/PhotoLibraryManagerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extension PhotoLibraryManagerMock {
var authorizationDenied = false
}

// swiftlint:disable:next cyclomatic_complexity
convenience init(_ configuration: Configuration) {
self.init()

Expand Down
2 changes: 1 addition & 1 deletion ElementX/Sources/Other/Logging/ExtensionLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ enum ExtensionLogger {
return "\(formattedStr) MB"
}

static func configure(currentTarget: String, logLevel: TracingConfiguration.LogLevel) {
static func configure(currentTarget: String, logLevel: LogLevel) {
guard !isConfigured else {
return
}
Expand Down
43 changes: 43 additions & 0 deletions ElementX/Sources/Other/Logging/LogLevel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
//

import Foundation
import MatrixRustSDK

enum LogLevel: String, Codable, Hashable {
case error, warn, info, debug, trace

var title: String {
switch self {
case .error:
return "Error"
case .warn:
return "Warning"
case .info:
return "Info"
case .debug:
return "Debug"
case .trace:
return "Trace"
}
}

var rustLogLevel: MatrixRustSDK.LogLevel {
switch self {
case .error:
.error
case .warn:
.warn
case .info:
.info
case .debug:
.debug
case .trace:
.trace
}
}
}
8 changes: 4 additions & 4 deletions ElementX/Sources/Other/Logging/MXLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ enum MXLog {

static func configure(currentTarget: String,
filePrefix: String?,
logLevel: TracingConfiguration.LogLevel) {
logLevel: LogLevel) {
guard !didConfigureOnce else { return }

RustTracing.setup(configuration: .init(logLevel: logLevel, currentTarget: currentTarget, filePrefix: filePrefix))
Tracing.setup(logLevel: logLevel, currentTarget: currentTarget, filePrefix: filePrefix)

self.currentTarget = currentTarget

Expand Down Expand Up @@ -135,7 +135,7 @@ enum MXLog {
rootSpan.enter()
}

return Span(file: file, line: UInt32(line), level: level, target: currentTarget, name: name)
return Span(file: file, line: UInt32(line), level: level.rustLogLevel, target: currentTarget, name: name)
}

// periphery:ignore:parameters function,column,context
Expand All @@ -154,6 +154,6 @@ enum MXLog {
rootSpan.enter()
}

logEvent(file: (file as NSString).lastPathComponent, line: UInt32(line), level: level, target: currentTarget, message: "\(message)")
logEvent(file: (file as NSString).lastPathComponent, line: UInt32(line), level: level.rustLogLevel, target: currentTarget, message: "\(message)")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation
import MatrixRustSDK

enum RustTracing {
enum Tracing {
/// The base filename used for log files. This may be suffixed by the target
/// name and other log management metadata during rotation.
static let filePrefix = "console"
Expand All @@ -21,27 +21,29 @@ enum RustTracing {
}
}

private(set) static var currentTracingConfiguration: TracingConfiguration?
static func setup(configuration: TracingConfiguration) {
currentTracingConfiguration = configuration
static let fileExtension = "log"

static func setup(logLevel: LogLevel, currentTarget: String, filePrefix: String?) {
let fileName = if let filePrefix {
"\(Tracing.filePrefix)-\(filePrefix)"
} else {
Tracing.filePrefix
}

// Keep a minimum of 1 week of log files. In reality it will be longer
// as the app is unlikely to be running continuously.
let maxFiles: UInt64 = 24 * 7

// Log everything on integration tests to check whether
// the logs contain any sensitive data. See `UserFlowTests.swift`
let filter = if ProcessInfo.isRunningIntegrationTests {
TracingConfiguration(logLevel: .trace, currentTarget: "integrationtests", filePrefix: nil).filter
} else {
configuration.filter
}
// the logs contain any sensitive data. See `integration-tests.yml`
let level: LogLevel = ProcessInfo.isRunningIntegrationTests ? .trace : logLevel

setupTracing(config: .init(filter: filter,
setupTracing(config: .init(logLevel: level.rustLogLevel,
extraTargets: [currentTarget],
writeToStdoutOrSystem: true,
writeToFiles: .init(path: logsDirectory.path(percentEncoded: false),
filePrefix: configuration.fileName,
fileSuffix: configuration.fileExtension,
filePrefix: fileName,
fileSuffix: fileExtension,
maxFiles: maxFiles)))
}

Expand Down
140 changes: 0 additions & 140 deletions ElementX/Sources/Other/Logging/TracingConfiguration.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LogViewerScreenViewModel: LogViewerScreenViewModelType, LogViewerScreenVie
}

init() {
super.init(initialViewState: LogViewerScreenViewState(urls: RustTracing.logFiles))
super.init(initialViewState: LogViewerScreenViewState(urls: Tracing.logFiles))
}

// MARK: - Public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ enum DeveloperOptionsScreenViewAction {
}

protocol DeveloperOptionsProtocol: AnyObject {
var logLevel: TracingConfiguration.LogLevel { get set }
var logLevel: LogLevel { get set }
var slidingSyncDiscovery: AppSettings.SlidingSyncDiscovery { get set }
var publicSearchEnabled: Bool { get set }
var hideUnreadMessagesBadge: Bool { get set }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ struct DeveloperOptionsScreen: View {
}

private struct LogLevelConfigurationView: View {
@Binding var logLevel: TracingConfiguration.LogLevel
@Binding var logLevel: LogLevel

var body: some View {
Picker(selection: $logLevel) {
Expand All @@ -166,7 +166,7 @@ private struct LogLevelConfigurationView: View {
}

/// Allows the picker to work with associated values
private var logLevels: [TracingConfiguration.LogLevel] {
private var logLevels: [LogLevel] {
[.error, .warn, .info, .debug, .trace]
}
}
Expand Down
5 changes: 2 additions & 3 deletions ElementX/Sources/Services/BugReport/BugReportService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BugReportService: NSObject, BugReportServiceProtocol {
}

if bugReport.includeLogs {
let logAttachments = await zipFiles(RustTracing.logFiles)
let logAttachments = await zipFiles(Tracing.logFiles)
for url in logAttachments.files {
params.append(MultipartFormData(key: "compressed-log", type: .file(url: url)))
}
Expand Down Expand Up @@ -163,8 +163,7 @@ class BugReportService: NSObject, BugReportServiceProtocol {
MultipartFormData(key: "fallback_language", type: .text(value: Bundle.app.developmentLocalization ?? "null")),
MultipartFormData(key: "local_time", type: .text(value: localTime)),
MultipartFormData(key: "utc_time", type: .text(value: utcTime)),
MultipartFormData(key: "base_bundle_identifier", type: .text(value: InfoPlistReader.main.baseBundleIdentifier)),
MultipartFormData(key: "rust_tracing_filter", type: .text(value: RustTracing.currentTracingConfiguration?.filter ?? "null"))
stefanceriu marked this conversation as resolved.
Show resolved Hide resolved
MultipartFormData(key: "base_bundle_identifier", type: .text(value: InfoPlistReader.main.baseBundleIdentifier))
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,12 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
message: String,
html: String?,
intentionalMentions: IntentionalMentions) async {
// We're waiting on an API for including mentions: https://github.com/matrix-org/matrix-rust-sdk/issues/4302
MXLog.info("Editing timeline item caption: \(eventOrTransactionID) in \(roomID)")

// When formattedCaption is nil, caption will be parsed as markdown and generate the HTML for us.
let newContent = createCaptionEdit(caption: message, formattedCaption: html.map { .init(format: .html, body: $0) })
let newContent = createCaptionEdit(caption: message,
formattedCaption: html.map { .init(format: .html, body: $0) },
mentions: intentionalMentions.toRustMentions())
switch await activeTimeline.edit(eventOrTransactionID, newContent: newContent) {
case .success:
MXLog.info("Finished editing caption")
Expand All @@ -270,7 +271,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {

func removeCaption(_ eventOrTransactionID: EventOrTransactionId) async {
// Set a `nil` caption to remove it from the event.
let newContent = createCaptionEdit(caption: nil, formattedCaption: nil)
let newContent = createCaptionEdit(caption: nil, formattedCaption: nil, mentions: nil)
switch await activeTimeline.edit(eventOrTransactionID, newContent: newContent) {
case .success:
MXLog.info("Finished removing caption.")
Expand Down
Loading
Loading