summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRustRuntime/RustLogging.swift
blob: 6a817bdc951b91a540eb8be09d1a64c4c58da46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
//  RustLogging.swift
//  MullvadRustRuntime
//
//  Created by Emīls on 2026-01-09.
//  Copyright © 2026 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadLogging

/// Global logger instance for Rust logs
private let rustLogger = Logger(label: "Rust")

/// C-compatible logging callback function.
/// This must be a global function (not a closure) to be passed as a C function pointer.
private func rustLogCallback(level: UInt8, messagePtr: UnsafePointer<CChar>?) {
    guard let messagePtr else { return }
    let message = String(cString: messagePtr)

    let level: Logger.Level =
        switch level {
        case 1:
            .error
        case 2:
            .warning
        case 3:
            .info
        case 4:
            .debug
        case 5:
            .trace
        default:
            .debug
        }

    rustLogger
        .log(
            level: level,
            "\(message.trimmingCharacters(in: .whitespacesAndNewlines))"
        )
}

/// Initializes the Rust logging system to forward logs to Swift's Logger.
///
/// This function should be called once early in the application lifecycle,
/// before any Rust code that uses logging is invoked.
public enum RustLogging {
    /// Initialize Rust logging to forward to Swift Logger.
    /// Safe to call multiple times - only the first call has effect.
    public static func initialize() {
        init_rust_logging(rustLogCallback)
    }
}