Skip to content

Commit

Permalink
Move tracing configuration to the rust side.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanceriu committed Jan 10, 2025
1 parent fc719d9 commit c6e6be7
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 244 deletions.
36 changes: 16 additions & 20 deletions ElementX.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

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
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"))
MultipartFormData(key: "base_bundle_identifier", type: .text(value: InfoPlistReader.main.baseBundleIdentifier))
]
}

Expand Down
Loading

0 comments on commit c6e6be7

Please sign in to comment.