fix
杨锴
2025-06-16 3fa53409f5132333ce6d83fff796e108ddd62090
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
55
56
57
58
59
60
61
62
63
64
//
//  BaseQueuedDestination.swift
//  XCGLogger: https://github.com/DaveWoodCom/XCGLogger
//
//  Created by Dave Wood on 2017-04-02.
//  Copyright © 2017 Dave Wood, Cerebral Gardens.
//  Some rights reserved: https://github.com/DaveWoodCom/XCGLogger/blob/master/LICENSE.txt
//
 
import Foundation
import Dispatch
 
// MARK: - BaseQueuedDestination
/// A base class destination (with a possible DispatchQueue) that doesn't actually output the log anywhere and is intended to be subclassed
open class BaseQueuedDestination: BaseDestination {
    // MARK: - Properties
    /// The dispatch queue to process the log on
    open var logQueue: DispatchQueue? = nil
 
    // MARK: - Life Cycle
 
    // MARK: - Overridden Methods
    /// Apply filters and formatters to the message before queuing it to be written by the write method.
    ///
    /// - Parameters:
    ///     - logDetails:   The log details.
    ///     - message:      Message ready to be formatted for output.
    ///
    /// - Returns:  Nothing
    ///
    open override func output(logDetails: LogDetails, message: String) {
        let outputClosure = {
            // Create mutable versions of our parameters
            var logDetails = logDetails
            var message = message
 
            // Apply filters, if any indicate we should drop the message, we abort before doing the actual logging
            guard !self.shouldExclude(logDetails: &logDetails, message: &message) else { return }
 
            self.applyFormatters(logDetails: &logDetails, message: &message)
            self.write(message: message)
        }
 
        if let logQueue = logQueue {
            logQueue.async(execute: outputClosure)
        }
        else {
            outputClosure()
        }
    }
 
    // MARK: - Methods that must be overridden in subclasses
    /// Write the log message to the destination.
    ///
    /// - Parameters:
    ///     - message:   Formatted/processed message ready for output.
    ///
    /// - Returns:  Nothing
    ///
    open func write(message: String) {
        // Do something with the message in an overridden version of this method
        precondition(false, "Must override this")
    }
}